erisu commented on a change in pull request #1212:
URL: https://github.com/apache/cordova-android/pull/1212#discussion_r664045272
##########
File path: bin/templates/cordova/lib/prepare.js
##########
@@ -92,6 +74,76 @@ module.exports.prepare = function (cordovaProject, options) {
});
};
+/** @param {PlatformApi} project */
+function updateUserProjectGradleConfig (project) {
+ // Generate project gradle config
+ const projectGradleConfig = {
+
...require('cordova-android/framework/cdv-gradle-config-defaults.json'),
+ ...getUserGradleConfig(project._config)
+ };
+
+ // Write out changes
+ const projectGradleConfigPath = path.join(project.root,
'cdv-gradle-config.json');
+ fs.writeJSONSync(projectGradleConfigPath, projectGradleConfig, { spaces: 2
});
+}
+
+function getUserGradleConfig (configXml) {
+ const configXmlToGradleMapping = [
+ { 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-buildToolsVersion', gradleKey:
'BUILD_TOOLS_VERSION', type: String },
+ { xmlKey: 'GradleVersion', gradleKey: 'GRADLE_VERSION', type: String },
+ { xmlKey: 'AndroidGradlePluginVersion', gradleKey: 'AGP_VERSION',
type: String },
+ { xmlKey: 'GradlePluginKotlinVersion', gradleKey: 'KOTLIN_VERSION',
type: String },
+ { xmlKey: 'AndroidXAppCompatVersion', gradleKey:
'ANDROIDX_APP_COMPAT_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesVersion', gradleKey:
'GRADLE_PLUGIN_GOOGLE_SERVICES_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesEnabled', gradleKey:
'IS_GRADLE_PLUGIN_GOOGLE_SERVICES_ENABLED', type: Boolean },
+ { xmlKey: 'GradlePluginKotlinEnabled', gradleKey:
'IS_GRADLE_PLUGIN_KOTLIN_ENABLED', type: Boolean }
+ ];
+
+ return configXmlToGradleMapping.reduce((config, mapping) => {
+ const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
+ const typecastValue = castValueToType(rawValue, mapping.type);
+
+ if (typecastValue !== undefined) {
+ config[mapping.gradleKey] = typecastValue;
+ }
+ return config;
+ }, {});
Review comment:
This code is causing the defaults to not be set.
This is probally because of how `cordova-common` returns an empty string
when it does not find the preference with `getPreference`. Personally I think
in common it should return undefined.
I think we should check first of the value is not an empty string.
```suggestion
return configXmlToGradleMapping.reduce((config, mapping) => {
const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
if (rawValue !== '') {
const typecastValue = castValueToType(rawValue, mapping.type);
if (typecastValue !== undefined) {
config[mapping.gradleKey] = typecastValue;
}
}
return config;
}, {});
```
##########
File path: bin/templates/cordova/lib/prepare.js
##########
@@ -92,6 +74,76 @@ module.exports.prepare = function (cordovaProject, options) {
});
};
+/** @param {PlatformApi} project */
+function updateUserProjectGradleConfig (project) {
+ // Generate project gradle config
+ const projectGradleConfig = {
+
...require('cordova-android/framework/cdv-gradle-config-defaults.json'),
+ ...getUserGradleConfig(project._config)
+ };
+
+ // Write out changes
+ const projectGradleConfigPath = path.join(project.root,
'cdv-gradle-config.json');
+ fs.writeJSONSync(projectGradleConfigPath, projectGradleConfig, { spaces: 2
});
+}
+
+function getUserGradleConfig (configXml) {
+ const configXmlToGradleMapping = [
+ { 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-buildToolsVersion', gradleKey:
'BUILD_TOOLS_VERSION', type: String },
+ { xmlKey: 'GradleVersion', gradleKey: 'GRADLE_VERSION', type: String },
+ { xmlKey: 'AndroidGradlePluginVersion', gradleKey: 'AGP_VERSION',
type: String },
+ { xmlKey: 'GradlePluginKotlinVersion', gradleKey: 'KOTLIN_VERSION',
type: String },
+ { xmlKey: 'AndroidXAppCompatVersion', gradleKey:
'ANDROIDX_APP_COMPAT_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesVersion', gradleKey:
'GRADLE_PLUGIN_GOOGLE_SERVICES_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesEnabled', gradleKey:
'IS_GRADLE_PLUGIN_GOOGLE_SERVICES_ENABLED', type: Boolean },
+ { xmlKey: 'GradlePluginKotlinEnabled', gradleKey:
'IS_GRADLE_PLUGIN_KOTLIN_ENABLED', type: Boolean }
+ ];
+
+ return configXmlToGradleMapping.reduce((config, mapping) => {
+ const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
+ const typecastValue = castValueToType(rawValue, mapping.type);
+
+ if (typecastValue !== undefined) {
+ config[mapping.gradleKey] = typecastValue;
+ }
+ return config;
+ }, {});
Review comment:
This code is causing the defaults to not be set for the items that are
missing in `config.xml`.
This is probally because of how `cordova-common` returns an empty string
when it does not find the preference with `getPreference`. Personally I think
in common it should return undefined.
I think we should check first of the value is not an empty string.
```suggestion
return configXmlToGradleMapping.reduce((config, mapping) => {
const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
if (rawValue !== '') {
const typecastValue = castValueToType(rawValue, mapping.type);
if (typecastValue !== undefined) {
config[mapping.gradleKey] = typecastValue;
}
}
return config;
}, {});
```
##########
File path: bin/templates/cordova/lib/prepare.js
##########
@@ -92,6 +74,76 @@ module.exports.prepare = function (cordovaProject, options) {
});
};
+/** @param {PlatformApi} project */
+function updateUserProjectGradleConfig (project) {
+ // Generate project gradle config
+ const projectGradleConfig = {
+
...require('cordova-android/framework/cdv-gradle-config-defaults.json'),
+ ...getUserGradleConfig(project._config)
+ };
+
+ // Write out changes
+ const projectGradleConfigPath = path.join(project.root,
'cdv-gradle-config.json');
+ fs.writeJSONSync(projectGradleConfigPath, projectGradleConfig, { spaces: 2
});
+}
+
+function getUserGradleConfig (configXml) {
+ const configXmlToGradleMapping = [
+ { 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-buildToolsVersion', gradleKey:
'BUILD_TOOLS_VERSION', type: String },
+ { xmlKey: 'GradleVersion', gradleKey: 'GRADLE_VERSION', type: String },
+ { xmlKey: 'AndroidGradlePluginVersion', gradleKey: 'AGP_VERSION',
type: String },
+ { xmlKey: 'GradlePluginKotlinVersion', gradleKey: 'KOTLIN_VERSION',
type: String },
+ { xmlKey: 'AndroidXAppCompatVersion', gradleKey:
'ANDROIDX_APP_COMPAT_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesVersion', gradleKey:
'GRADLE_PLUGIN_GOOGLE_SERVICES_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesEnabled', gradleKey:
'IS_GRADLE_PLUGIN_GOOGLE_SERVICES_ENABLED', type: Boolean },
+ { xmlKey: 'GradlePluginKotlinEnabled', gradleKey:
'IS_GRADLE_PLUGIN_KOTLIN_ENABLED', type: Boolean }
+ ];
+
+ return configXmlToGradleMapping.reduce((config, mapping) => {
+ const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
+ const typecastValue = castValueToType(rawValue, mapping.type);
+
+ if (typecastValue !== undefined) {
+ config[mapping.gradleKey] = typecastValue;
+ }
+ return config;
+ }, {});
Review comment:
This code is causing the defaults to not be set for the items that are
missing in `config.xml`.
This is probally because of how `cordova-common` returns an empty string
when it does not find the preference with `getPreference`. Personally, I think
common it should be returning undefined for items are not founded. But, that is
a different, future, task.
In this case, I think we should first check if the `rawValue` is not an
empty string.
```suggestion
return configXmlToGradleMapping.reduce((config, mapping) => {
const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
if (rawValue !== '') {
const typecastValue = castValueToType(rawValue, mapping.type);
if (typecastValue !== undefined) {
config[mapping.gradleKey] = typecastValue;
}
}
return config;
}, {});
```
##########
File path: bin/templates/cordova/lib/prepare.js
##########
@@ -92,6 +74,76 @@ module.exports.prepare = function (cordovaProject, options) {
});
};
+/** @param {PlatformApi} project */
+function updateUserProjectGradleConfig (project) {
+ // Generate project gradle config
+ const projectGradleConfig = {
+
...require('cordova-android/framework/cdv-gradle-config-defaults.json'),
+ ...getUserGradleConfig(project._config)
+ };
+
+ // Write out changes
+ const projectGradleConfigPath = path.join(project.root,
'cdv-gradle-config.json');
+ fs.writeJSONSync(projectGradleConfigPath, projectGradleConfig, { spaces: 2
});
+}
+
+function getUserGradleConfig (configXml) {
+ const configXmlToGradleMapping = [
+ { 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-buildToolsVersion', gradleKey:
'BUILD_TOOLS_VERSION', type: String },
+ { xmlKey: 'GradleVersion', gradleKey: 'GRADLE_VERSION', type: String },
+ { xmlKey: 'AndroidGradlePluginVersion', gradleKey: 'AGP_VERSION',
type: String },
+ { xmlKey: 'GradlePluginKotlinVersion', gradleKey: 'KOTLIN_VERSION',
type: String },
+ { xmlKey: 'AndroidXAppCompatVersion', gradleKey:
'ANDROIDX_APP_COMPAT_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesVersion', gradleKey:
'GRADLE_PLUGIN_GOOGLE_SERVICES_VERSION', type: String },
+ { xmlKey: 'GradlePluginGoogleServicesEnabled', gradleKey:
'IS_GRADLE_PLUGIN_GOOGLE_SERVICES_ENABLED', type: Boolean },
+ { xmlKey: 'GradlePluginKotlinEnabled', gradleKey:
'IS_GRADLE_PLUGIN_KOTLIN_ENABLED', type: Boolean }
+ ];
+
+ return configXmlToGradleMapping.reduce((config, mapping) => {
+ const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
+ const typecastValue = castValueToType(rawValue, mapping.type);
+
+ if (typecastValue !== undefined) {
+ config[mapping.gradleKey] = typecastValue;
+ }
+ return config;
+ }, {});
Review comment:
This code is causing the defaults to not be set for the items that are
missing in `config.xml`.
This is probally because of how `cordova-common` returns an empty string
when it does not find the preference with `getPreference`. Personally, I think
common it should be returning undefined for items are not founded. But, that is
a different, future, task.
In this case, I think we should first check if the `rawValue` is not an
empty string.
```suggestion
return configXmlToGradleMapping.reduce((config, mapping) => {
const rawValue = configXml.getPreference(mapping.xmlKey, 'android');
if (rawValue. trim() !== '') {
const typecastValue = castValueToType(rawValue, mapping.type);
if (typecastValue !== undefined) {
config[mapping.gradleKey] = typecastValue;
}
}
return config;
}, {});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]