Skip to content

Commit

Permalink
Quick fix use SDK versions from AndroidManifest
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J. Brody committed Jan 18, 2019
1 parent 7a98708 commit 314b4ef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
44 changes: 38 additions & 6 deletions bin/templates/cordova/lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,29 @@ class ProjectBuilder {
};
}

extractRealProjectNameFromManifest () {
getAndroidManifestInfo () {
var manifestPath = path.join(this.root, 'app', 'src', 'main', 'AndroidManifest.xml');
var manifestData = fs.readFileSync(manifestPath, 'utf8');
var m = /<manifest[\s\S]*?package\s*=\s*"(.*?)"/i.exec(manifestData);
if (!m) {
throw new CordovaError('Could not find package name in ' + manifestPath);
}

var packageName = m[1];
var lastDotIndex = packageName.lastIndexOf('.');
return packageName.substring(lastDotIndex + 1);
const m2 = /<uses-sdk[\s\S]*?android:minSdkVersion\s*=\s*"(.*?)"/i.exec(manifestData);
if (!m2) {
throw new CordovaError('Could not android:minSdkVersion in ' + manifestPath);
}

const m3 = /<uses-sdk[\s\S]*?android:targetSdkVersion\s*=\s*"(.*?)"/i.exec(manifestData);
if (!m2) {
throw new CordovaError('Could not android:targetSdkVersion in ' + manifestPath);
}

return {
packageName: m[1],
minSdkVersion: m2[1],
targetSdkVersion: m3[1]
};
}

// Makes the project buildable, minus the gradle wrapper.
Expand Down Expand Up @@ -128,7 +140,13 @@ class ProjectBuilder {
checkAndCopy(subProjects[i], this.root);
}
}
var name = this.extractRealProjectNameFromManifest();

const androidManifestInfo = this.getAndroidManifestInfo();

const packageName = androidManifestInfo.packageName;
const lastDotIndex = packageName.lastIndexOf('.');
const name = packageName.substring(lastDotIndex + 1);

// Remove the proj.id/name- prefix from projects: https://issues.apache.org/jira/browse/CB-9149
var settingsGradlePaths = subProjects.map(function (p) {
var realDir = p.replace(/[/\\]/g, ':');
Expand All @@ -144,7 +162,21 @@ class ProjectBuilder {
'// GENERATED FILE - DO NOT EDIT\n' +
'include ":"\n' + settingsGradlePaths.join(''));

// Update dependencies within build.gradle.
// Update SDK versions within root build.gradle - quick fix:
let rootGradle = fs.readFileSync(path.join(this.root, 'build.gradle'), 'utf8');
rootGradle = rootGradle.replace(
/(defaultMinSdkVersion)\s*=\s*\d+\s*\/\//,
'$1=' + androidManifestInfo.minSdkVersion + ' //');
rootGradle = rootGradle.replace(
/(defaultTargetSdkVersion)\s*=\s*\d+\s*\/\//,
'$1=' + androidManifestInfo.targetSdkVersion + ' //');
rootGradle = rootGradle.replace(
/(defaultCompileSdkVersion)\s*=\s*\d+\s*\/\//,
'$1=' + androidManifestInfo.targetSdkVersion + ' //');
// and store it (in the root grade):
fs.writeFileSync(path.join(this.root, 'build.gradle'), rootGradle);

// Update dependencies within app/build.gradle.
var buildGradle = fs.readFileSync(path.join(this.root, 'app', 'build.gradle'), 'utf8');
var depsList = '';
var root = this.root;
Expand Down
10 changes: 10 additions & 0 deletions bin/templates/project/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ if (hasBuildExtras2) {
apply from: '../build-extras.gradle'
}

// Needed for quick fix below:
def customCompileSdkVersion = cdvCompileSdkVersion;

// Set property defaults after extension .gradle files.
if (ext.cdvCompileSdkVersion == null) {
ext.cdvCompileSdkVersion = privateHelpers.getProjectTarget()
Expand All @@ -124,6 +127,10 @@ ext.cdvVersionCodeForceAbiDigit = cdvVersionCodeForceAbiDigit == null ? false :
ext.cdvMinSdkVersion = cdvMinSdkVersion == null ? defaultMinSdkVersion : Integer.parseInt('' + cdvMinSdkVersion)
ext.cdvVersionCode = cdvVersionCode == null ? null : Integer.parseInt('' + cdvVersionCode)

// Quick fix:
def appTargetSdkVersion =
(customCompileSdkVersion == null) ? defaultTargetSdkVersion : Integer.parseInt('' + cdvCompileSdkVersion)

def computeBuildTargetName(debugBuild) {
def ret = 'assemble'
if (cdvBuildMultipleApks && cdvBuildArch) {
Expand Down Expand Up @@ -170,6 +177,9 @@ android {
if (cdvMinSdkVersion != null) {
minSdkVersion cdvMinSdkVersion
}

// Quick fix:
targetSdkVersion appTargetSdkVersion
}

lintOptions {
Expand Down

0 comments on commit 314b4ef

Please sign in to comment.