Skip to content

Commit

Permalink
feat!: API 34 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Oct 21, 2023
1 parent 96218ce commit 9a0d6b7
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 78 deletions.
4 changes: 2 additions & 2 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ android {
buildToolsVersion cordovaConfig.BUILD_TOOLS_VERSION

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

// For the Android Cordova Lib, we allow changing the minSdkVersion, but it is at the users own risk
Expand Down
8 changes: 4 additions & 4 deletions framework/cdv-gradle-config-defaults.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"MIN_SDK_VERSION": 24,
"SDK_VERSION": 33,
"SDK_VERSION": 34,
"COMPILE_SDK_VERSION": null,
"GRADLE_VERSION": "8.0.2",
"MIN_BUILD_TOOLS_VERSION": "33.0.2",
"AGP_VERSION": "8.0.0",
"GRADLE_VERSION": "8.4",
"MIN_BUILD_TOOLS_VERSION": "34.0.0",
"AGP_VERSION": "8.2.0-rc01",
"KOTLIN_VERSION": "1.7.21",
"ANDROIDX_APP_COMPAT_VERSION": "1.6.1",
"ANDROIDX_WEBKIT_VERSION": "1.6.0",
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
8 changes: 6 additions & 2 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 Expand Up @@ -248,8 +252,8 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

if (cdvReleaseSigningPropertiesFile) {
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

0 comments on commit 9a0d6b7

Please sign in to comment.