Skip to content

RNMT-6402 Update Android Tooling #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions framework/cdv-gradle-config-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"MIN_SDK_VERSION": 28,
"SDK_VERSION": 34,
"COMPILE_SDK_VERSION": null,
"GRADLE_VERSION": "8.3",
"GRADLE_VERSION": "8.5",
"MIN_BUILD_TOOLS_VERSION": "34.0.0",
"AGP_VERSION": "8.1.1",
"KOTLIN_VERSION": "1.9.10",
"AGP_VERSION": "8.2.0-rc03",
"KOTLIN_VERSION": "1.9.21",
"ANDROIDX_APP_COMPAT_VERSION": "1.6.1",
"ANDROIDX_WEBKIT_VERSION": "1.7.0",
"ANDROIDX_WEBKIT_VERSION": "1.8.0",
"ANDROIDX_CORE_SPLASHSCREEN_VERSION": "1.0.1",
"GRADLE_PLUGIN_GOOGLE_SERVICES_VERSION": "4.3.15",
"GRADLE_PLUGIN_GOOGLE_SERVICES_VERSION": "4.4.0",
"IS_GRADLE_PLUGIN_GOOGLE_SERVICES_ENABLED": false,
"IS_GRADLE_PLUGIN_KOTLIN_ENABLED": true,
"PACKAGE_NAMESPACE": "io.cordova.helloCordova"
Expand Down
40 changes: 15 additions & 25 deletions lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ class ProjectBuilder {
}

getArgs (cmd, opts) {
let args = [
'-b', path.join(this.root, 'build.gradle')
];
let args = [];
if (opts.extraArgs) {
args = args.concat(opts.extraArgs);
}
Expand Down Expand Up @@ -116,16 +114,17 @@ class ProjectBuilder {
return args;
}

/*
* This returns a promise
*/
runGradleWrapper (gradle_cmd) {
const gradlePath = path.join(this.root, 'gradlew');
const wrapperGradle = path.join(this.root, 'wrapper.gradle');
if (fs.existsSync(gradlePath)) {
// Literally do nothing, for some reason this works, while !fs.existsSync didn't on Windows
/**
* Installs/updates the gradle wrapper
* @param {string} gradleVersion The gradle version to install. Ignored if CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL environment variable is defined
* @returns {Promise<void>}
*/
async installGradleWrapper (gradleVersion) {
if (process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL) {
events.emit('verbose', `Overriding Gradle Version via CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL (${process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL})`);
await execa('gradle', ['-p', this.root, 'wrapper', '--gradle-distribution-url', process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL], { stdio: 'inherit' });
} else {
return execa(gradle_cmd, ['-p', this.root, 'wrapper', '-b', wrapperGradle], { stdio: 'inherit' });
await execa('gradle', ['-p', this.root, 'wrapper', '--gradle-version', gradleVersion], { stdio: 'inherit' });
}
}

Expand Down Expand Up @@ -275,23 +274,14 @@ class ProjectBuilder {

prepEnv (opts) {
const self = this;
const config = this._getCordovaConfig();
return check_reqs.check_gradle()
.then(function (gradlePath) {
return self.runGradleWrapper(gradlePath);
.then(function () {
events.emit('verbose', `Using Gradle: ${config.GRADLE_VERSION}`);
return self.installGradleWrapper(config.GRADLE_VERSION);
}).then(function () {
return self.prepBuildFiles();
}).then(() => {
const config = this._getCordovaConfig();
// update/set the distributionUrl in the gradle-wrapper.properties
const gradleWrapperPropertiesPath = path.join(self.root, 'gradle/wrapper/gradle-wrapper.properties');
const gradleWrapperProperties = createEditor(gradleWrapperPropertiesPath);
const distributionUrl = process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL || `https://services.gradle.org/distributions/gradle-${config.GRADLE_VERSION}-all.zip`;
gradleWrapperProperties.set('distributionUrl', distributionUrl);
gradleWrapperProperties.save();

events.emit('verbose', `Gradle Distribution URL: ${distributionUrl}`);
})
.then(() => {
const signingPropertiesPath = path.join(self.root, `${opts.buildType}${SIGNING_PROPERTIES}`);

if (fs.existsSync(signingPropertiesPath)) fs.removeSync(signingPropertiesPath);
Expand Down
17 changes: 5 additions & 12 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,13 @@ function prepBuildFiles (projectPath) {
buildModule.getBuilder(projectPath).prepBuildFiles();
}

function copyBuildRules (projectPath, isLegacy) {
function copyBuildRules (projectPath) {
const srcDir = path.join(ROOT, 'templates', 'project');

if (isLegacy) {
// The project's build.gradle is identical to the earlier build.gradle, so it should still work
fs.copySync(path.join(srcDir, 'legacy', 'build.gradle'), path.join(projectPath, 'legacy', 'build.gradle'));
fs.copySync(path.join(srcDir, 'wrapper.gradle'), path.join(projectPath, 'wrapper.gradle'));
} else {
fs.copySync(path.join(srcDir, 'build.gradle'), path.join(projectPath, 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'build.gradle'), path.join(projectPath, 'app', 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'repositories.gradle'), path.join(projectPath, 'app', 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'repositories.gradle'), path.join(projectPath, 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'wrapper.gradle'), path.join(projectPath, 'wrapper.gradle'));
}
fs.copySync(path.join(srcDir, 'build.gradle'), path.join(projectPath, 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'build.gradle'), path.join(projectPath, 'app', 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'repositories.gradle'), path.join(projectPath, 'app', 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'repositories.gradle'), path.join(projectPath, 'repositories.gradle'));
}

function copyScripts (projectPath) {
Expand Down
23 changes: 14 additions & 9 deletions spec/unit/builders/ProjectBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,24 @@ describe('ProjectBuilder', () => {
});
});

describe('runGradleWrapper', () => {
it('should run the provided gradle command if a gradle wrapper does not already exist', () => {
spyOn(fs, 'existsSync').and.returnValue(false);
builder.runGradleWrapper('/my/sweet/gradle');
expect(execaSpy).toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
describe('installGradleWrapper', () => {
beforeEach(() => {
execaSpy.and.resolveTo();
});

it('should do nothing if a gradle wrapper exists in the project directory', () => {
spyOn(fs, 'existsSync').and.returnValue(true);
builder.runGradleWrapper('/my/sweet/gradle');
expect(execaSpy).not.toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
it('should run gradle wrapper 8.3', async () => {
await builder.installGradleWrapper('8.3');
expect(execaSpy).toHaveBeenCalledWith('gradle', ['-p', '/root', 'wrapper', '--gradle-version', '8.3'], jasmine.any(Object));
});

it('CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL should override gradle version', async () => {
process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL = 'https://dist.local';
await builder.installGradleWrapper('8.3');
delete process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL;
expect(execaSpy).toHaveBeenCalledWith('gradle', ['-p', '/root', 'wrapper', '--gradle-distribution-url', 'https://dist.local'], jasmine.any(Object));
});
});

describe('build', () => {
beforeEach(() => {
spyOn(builder, 'getArgs');
Expand Down
4 changes: 4 additions & 0 deletions templates/project/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ task cdvPrintProps {
android {
namespace cordovaConfig.PACKAGE_NAMESPACE

buildFeatures {
buildConfig true
}

defaultConfig {
versionCode cdvVersionCode ?: new BigInteger("" + privateHelpers.extractIntFromManifest("versionCode"))
applicationId cordovaConfig.PACKAGE_NAMESPACE
Expand Down
1 change: 0 additions & 1 deletion templates/project/wrapper.gradle

This file was deleted.

22 changes: 0 additions & 22 deletions test/androidx/wrapper.gradle

This file was deleted.

12 changes: 11 additions & 1 deletion test/run_java_unit_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ class AndroidTestRunner {
);
}

_getGradleVersion () {
const config = JSON.parse(
fs.readFileSync(path.resolve(this.projectDir, '../../framework/cdv-gradle-config-defaults.json'), {
encoding: 'utf-8'
})
);

return config.GRADLE_VERSION;
}

_createProjectBuilder () {
return new ProjectBuilder(this.projectDir).runGradleWrapper('gradle');
return new ProjectBuilder(this.projectDir).installGradleWrapper(this._getGradleVersion());
}

run () {
Expand Down