Skip to content

Commit

Permalink
feat: support custom compileSdk setting (#1431)
Browse files Browse the repository at this point in the history
* feat: support custom compileSdk setting
* chore: apply suggestions from code review
* chore: apply cdv-gradle-config-defaults.json suggestion
* fix: set compile sdk when null
* fix: move compileSdk null check to gradle
* fix: compile sdk requirement warning & display in gradle per subproject

Co-authored-by: Norman Breau <norman@nbsolutions.ca>
  • Loading branch information
erisu and breautek committed May 18, 2022
1 parent cb494ff commit 4744bfe
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 5 deletions.
4 changes: 3 additions & 1 deletion framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ buildscript {
// Android Gradle Plugin (AGP) Build Tools
classpath "com.android.tools.build:gradle:${cordovaConfig.AGP_VERSION}"
}

cdvHelpers.verifyCordovaConfigForBuild()
}

allprojects {
Expand All @@ -42,7 +44,7 @@ allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion cordovaConfig.SDK_VERSION
compileSdkVersion cordovaConfig.COMPILE_SDK_VERSION
buildToolsVersion cordovaConfig.BUILD_TOOLS_VERSION

compileOptions {
Expand Down
1 change: 1 addition & 0 deletions framework/cdv-gradle-config-defaults.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"MIN_SDK_VERSION": 22,
"SDK_VERSION": 32,
"COMPILE_SDK_VERSION": null,
"GRADLE_VERSION": "7.4.2",
"MIN_BUILD_TOOLS_VERSION": "32.0.0",
"AGP_VERSION": "7.1.0",
Expand Down
15 changes: 15 additions & 0 deletions framework/cordova.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def doApplyCordovaConfigCustomization() {
if (project.hasProperty('cdvSdkVersion')) {
cordovaConfig.SDK_VERSION = Integer.parseInt('' + cdvSdkVersion)
}
if (project.hasProperty('cdvCompileSdkVersion')) {
cordovaConfig.COMPILE_SDK_VERSION = Integer.parseInt('' + cdvCompileSdkVersion)
}
if (project.hasProperty('cdvMaxSdkVersion')) {
cordovaConfig.MAX_SDK_VERSION = Integer.parseInt('' + cdvMaxSdkVersion)
}
Expand Down Expand Up @@ -190,6 +193,12 @@ def doApplyCordovaConfigCustomization() {
}
}

def doVerifyCordovaConfigForBuild() {
if (cordovaConfig.COMPILE_SDK_VERSION < cordovaConfig.SDK_VERSION) {
println "The \"compileSdkVersion\" (${cordovaConfig.COMPILE_SDK_VERSION}) should be greater than or equal to the the \"targetSdkVersion\" (${cordovaConfig.SDK_VERSION})."
}
}

// Properties exported here are visible to all plugins.
ext {
def defaultsFilePath = './cdv-gradle-config-defaults.json'
Expand All @@ -210,6 +219,10 @@ ext {
def jsonFile = new File(targetConfigFilePath)
cordovaConfig = new groovy.json.JsonSlurper().parseText(jsonFile.text)

if (cordovaConfig.COMPILE_SDK_VERSION == null) {
cordovaConfig.COMPILE_SDK_VERSION = cordovaConfig.SDK_VERSION
}

// Apply Gradle Properties
doApplyCordovaConfigCustomization()

Expand All @@ -227,6 +240,8 @@ ext {
cdvHelpers.getConfigXml = { doGetConfigXml() }
// Returns the value for the desired <preference>. Added in 4.1.0.
cdvHelpers.getConfigPreference = { name, defaultValue -> doGetConfigPreference(name, defaultValue) }
// Display warnings if any cordova config is not proper for build.
cdvHelpers.verifyCordovaConfigForBuild = { doVerifyCordovaConfigForBuild() }
}

buildscript {
Expand Down
2 changes: 1 addition & 1 deletion lib/builders/plugin-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
}

android {
compileSdkVersion cordovaConfig.SDK_VERSION
compileSdkVersion cordovaConfig.COMPILE_SDK_VERSION
buildToolsVersion cordovaConfig.BUILD_TOOLS_VERSION

compileOptions {
Expand Down
42 changes: 42 additions & 0 deletions lib/check_reqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ module.exports.get_target = function (projectRoot) {
return `android-${Math.max(userTargetSdkVersion, SDK_VERSION)}`;
};

/**
* @param {string} projectRoot
* @returns {string} The android target in format "android-${target}"
*/
module.exports.get_compile = function (projectRoot) {
const userTargetSdkVersion = getUserTargetSdkVersion(projectRoot) || SDK_VERSION;
const userCompileSdkVersion = getUserCompileSdkVersion(projectRoot) || userTargetSdkVersion;

module.exports.isCompileSdkValid(userCompileSdkVersion, userTargetSdkVersion);

return userCompileSdkVersion;
};

module.exports.isCompileSdkValid = (compileSdk, targetSdk) => {
targetSdk = (targetSdk || SDK_VERSION);
compileSdk = (compileSdk || targetSdk);
const isValid = compileSdk >= targetSdk;

if (!isValid) {
events.emit('warn', `The "android-compileSdkVersion" (${compileSdk}) should be greater than or equal to the "android-targetSdkVersion" (${targetSdk}).`);
}

return isValid;
};

/**
* @param {string} projectRoot
* @returns {number} target sdk or 0 if undefined
Expand All @@ -61,6 +86,23 @@ function getUserTargetSdkVersion (projectRoot) {
return isNaN(targetSdkVersion) ? 0 : targetSdkVersion;
}

/**
* @param {string} projectRoot
* @returns {number} target sdk or 0 if undefined
*/
function getUserCompileSdkVersion (projectRoot) {
// If the repo config.xml file exists, find the desired compileSdkVersion.
// We need to use the cordova project's config.xml here, since the platform
// project's config.xml does not yet have the user's preferences when this
// function is called during `Api.createPlatform`.
const configFile = path.join(projectRoot, '../../config.xml');
if (!fs.existsSync(configFile)) return 0;

const configParser = new ConfigParser(configFile);
const compileSdkVersion = parseInt(configParser.getPreference('android-compileSdkVersion', 'android'), 10);
return isNaN(compileSdkVersion) ? 0 : compileSdkVersion;
}

module.exports.get_gradle_wrapper = function () {
let androidStudioPath;
let i = 0;
Expand Down
4 changes: 3 additions & 1 deletion lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ exports.create = function (project_path, config, options, events) {

const safe_activity_name = config.android_activityName() || options.activityName || 'MainActivity';
const target_api = check_reqs.get_target(project_path);
const compile_api = check_reqs.get_compile(project_path);

// Make the package conform to Java package types
return exports.validatePackageName(package_name)
Expand All @@ -220,7 +221,8 @@ exports.create = function (project_path, config, options, events) {
events.emit('log', '\tPackage: ' + package_name);
events.emit('log', '\tName: ' + project_name);
events.emit('log', '\tActivity: ' + safe_activity_name);
events.emit('log', '\tAndroid target: ' + target_api);
events.emit('log', '\tAndroid Target SDK: ' + target_api);
events.emit('log', '\tAndroid Compile SDK: ' + compile_api);

events.emit('verbose', 'Copying android template project to ' + project_path);

Expand Down
9 changes: 9 additions & 0 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ function updateUserProjectGradleConfig (project) {
...getUserGradleConfig(project._config)
};

// Check if compile sdk is valid.
// The returned result is iggnored and since we do not need and will not throw an error.
// Only using the valid check call for display the warning when target is greater then compiled.
checkReqs.isCompileSdkValid(
projectGradleConfig.COMPILE_SDK_VERSION,
projectGradleConfig.SDK_VERSION
);

// Write out changes
const projectGradleConfigPath = path.join(project.root, 'cdv-gradle-config.json');
fs.writeJSONSync(projectGradleConfigPath, projectGradleConfig, { spaces: 2 });
Expand All @@ -92,6 +100,7 @@ function getUserGradleConfig (configXml) {
{ xmlKey: 'android-minSdkVersion', gradleKey: 'MIN_SDK_VERSION', type: Number },
{ xmlKey: 'android-maxSdkVersion', gradleKey: 'MAX_SDK_VERSION', type: Number },
{ xmlKey: 'android-targetSdkVersion', gradleKey: 'SDK_VERSION', type: Number },
{ xmlKey: 'android-compileSdkVersion', gradleKey: 'COMPILE_SDK_VERSION', type: Number },
{ xmlKey: 'android-buildToolsVersion', gradleKey: 'BUILD_TOOLS_VERSION', type: String },
{ xmlKey: 'GradleVersion', gradleKey: 'GRADLE_VERSION', type: String },
{ xmlKey: 'AndroidGradlePluginVersion', gradleKey: 'AGP_VERSION', type: String },
Expand Down
2 changes: 1 addition & 1 deletion templates/project/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ android {
maxSdkVersion cordovaConfig.MAX_SDK_VERSION
}
targetSdkVersion cordovaConfig.SDK_VERSION
compileSdkVersion cordovaConfig.SDK_VERSION
compileSdkVersion cordovaConfig.COMPILE_SDK_VERSION
}

lintOptions {
Expand Down
2 changes: 2 additions & 0 deletions templates/project/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ buildscript {
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

cdvHelpers.verifyCordovaConfigForBuild()
}

allprojects {
Expand Down
2 changes: 1 addition & 1 deletion test/androidx/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ apply plugin: 'com.android.application'
apply from: '../../../framework/cordova.gradle'

android {
compileSdkVersion cordovaConfig.SDK_VERSION
compileSdkVersion cordovaConfig.COMPILE_SDK_VERSION
buildToolsVersion cordovaConfig.BUILD_TOOLS_VERSION

defaultConfig {
Expand Down

0 comments on commit 4744bfe

Please sign in to comment.