Skip to content
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

Use custom Gradle properties to read minSdkVersion value from config.xml #655

Merged
merged 9 commits into from
Feb 13, 2019
27 changes: 17 additions & 10 deletions bin/templates/cordova/lib/config/GradlePropertiesParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ class GradlePropertiesParser {
this.gradleFilePath = path.join(platformDir, 'gradle.properties');
}

configure () {
configure (userConfigs) {
events.emit('verbose', '[Gradle Properties] Preparing Configuration');

this._initializeEditor();
this._configureDefaults();

events.emit('verbose', '[Gradle Properties] Appending default configuration properties');
this._configureProperties(this._defaults);

events.emit('verbose', '[Gradle Properties] Appending custom configuration properties');
this._configureProperties(userConfigs);

this._save();
}

Expand All @@ -69,18 +75,19 @@ class GradlePropertiesParser {
}

/**
* Validate that defaults are set and set the missing defaults.
* Validate that defaults or user configuration properties are set and
* set the missing items.
*/
_configureDefaults () {
// Loop though Cordova default properties and set only if missing.
Object.keys(this._defaults).forEach(key => {
_configureProperties (properties) {
// Iterate though the properties and set only if missing.
Object.keys(properties).forEach(key => {
let value = this.gradleFile.get(key);

if (!value) {
events.emit('verbose', `[Gradle Properties] Appended missing default: ${key}=${this._defaults[key]}`);
this.gradleFile.set(key, this._defaults[key]);
} else if (value !== this._defaults[key]) {
events.emit('info', `[Gradle Properties] Detected Gradle property "${key}" with the value of "${value}", Cordova's recommended value is "${this._defaults[key]}"`);
events.emit('verbose', `[Gradle Properties] Appending configuration item: ${key}=${properties[key]}`);
this.gradleFile.set(key, properties[key]);
} else if (value !== properties[key]) {
events.emit('info', `[Gradle Properties] Detected Gradle property "${key}" with the value of "${value}", Cordova's recommended value is "${properties[key]}"`);
}
});
}
Expand Down
8 changes: 7 additions & 1 deletion bin/templates/cordova/lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ module.exports.prepare = function (cordovaProject, options) {

this._config = updateConfigFilesFrom(cordovaProject.projectConfig, munger, this.locations);

// Get the min SDK version from config.xml
const minSdkVersion = this._config.getPreference('android-minSdkVersion', 'android');

let gradlePropertiesUserConfig = {};
if (minSdkVersion) gradlePropertiesUserConfig.cdvMinSdkVersion = minSdkVersion;

let gradlePropertiesParser = new GradlePropertiesParser(this.locations.root);
gradlePropertiesParser.configure();
gradlePropertiesParser.configure(gradlePropertiesUserConfig);

// Update own www dir with project's www assets and plugins' assets and js-files
return Q.when(updateWww(cordovaProject, this.locations)).then(function () {
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/config/GradlePropertiesParser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Gradle Builder', () => {
});
});

describe('_configureDefaults method', () => {
describe('_configureProperties method', () => {
let parser;
let emitSpy;

Expand All @@ -99,11 +99,11 @@ describe('Gradle Builder', () => {
get: getSpy
};

parser._configureDefaults();
parser._configureProperties(parser._defaults);

expect(getSpy).toHaveBeenCalled();
expect(setSpy).toHaveBeenCalled();
expect(emitSpy.calls.argsFor(0)[1]).toContain('Appended missing default');
expect(emitSpy.calls.argsFor(0)[1]).toContain('Appending configuration item');
});

it('should not detect missing defaults and not call set.', () => {
Expand All @@ -115,7 +115,7 @@ describe('Gradle Builder', () => {
get: getSpy
};

parser._configureDefaults();
parser._configureProperties(parser._defaults);

expect(getSpy).toHaveBeenCalled();
expect(setSpy).not.toHaveBeenCalled();
Expand All @@ -130,7 +130,7 @@ describe('Gradle Builder', () => {
get: getSpy
};

parser._configureDefaults();
parser._configureProperties(parser._defaults);

expect(getSpy).toHaveBeenCalled();
expect(setSpy).not.toHaveBeenCalled();
Expand Down