Skip to content

Take applicationId from package.json #848

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

Merged
merged 4 commits into from
May 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions test-app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ def applyPluginGradleConfigurations = { ->
}
}

def getAppIdentifier = { packageJsonMap ->
def appIdentifier = "";
if (packageJsonMap && packageJsonMap.nativescript) {
appIdentifier = packageJsonMap.nativescript.id;
if (!(appIdentifier instanceof String)) {
appIdentifier = appIdentifier.android;
}
}

return appIdentifier;
}

def setAppIdentifier = { ->
println "\t + setting applicationId";
File packageJsonFile = new File("$rootDir/../../package.json");

if (packageJsonFile.exists()) {
def content = packageJsonFile.getText("UTF-8");
def jsonSlurper = new JsonSlurper();
def packageJsonMap = jsonSlurper.parseText(content);
def appIdentifier = getAppIdentifier(packageJsonMap);

if (appIdentifier) {
project.ext.nsApplicationIdentifier = appIdentifier;
android.defaultConfig.applicationId = appIdentifier;
}
}
}


android {
compileSdkVersion computeCompileSdkVersion()
buildToolsVersion computeBuildToolsVersion()
Expand Down Expand Up @@ -169,6 +199,7 @@ android {
}
}

setAppIdentifier()
applyAppGradleConfiguration()
applyPluginGradleConfigurations()
}
Expand Down Expand Up @@ -307,7 +338,7 @@ tasks.whenTaskAdded({ org.gradle.api.DefaultTask currentTask ->
currentTask.finalizedBy(ensureMetadataOutDir)
ensureMetadataOutDir.finalizedBy(buildMetadata)
}
if (currentTask =~ /assemble.+Debug/ || currentTask =~ /assemble.+Release/) {
if (currentTask =~ /assemble.*Debug/ || currentTask =~ /assemble.*Release/) {
currentTask.finalizedBy("validateAppIdMatch")
}
})
Expand Down Expand Up @@ -520,23 +551,21 @@ copyTypings.onlyIf { generateTypescriptDefinitions.didWork }
generateTypescriptDefinitions.finalizedBy(copyTypings)

task validateAppIdMatch {
doLast {
def packageJsonFile = new File("$USER_PROJECT_ROOT/$PACKAGE_JSON")
def lineSeparator = System.getProperty("line.separator")

if (packageJsonFile.exists() && !project.hasProperty("release")) {
String content = packageJsonFile.getText("UTF-8")
def jsonSlurper = new JsonSlurper()
def packageJsonMap = jsonSlurper.parseText(content)

if (packageJsonMap.nativescript.id != android.defaultConfig.applicationId) {
def errorMessage = "${lineSeparator}WARNING: The Application identifier is different from the one inside $PACKAGE_JSON file.$lineSeparator" +
"NativeScript CLI might not work properly.$lineSeparator" +
"Update the application identifier in $PACKAGE_JSON and app.gradle so that they match."
logger.error(errorMessage)
}
}
}
doLast {
def lineSeparator = System.getProperty("line.separator")

if (project.hasProperty("nsApplicationIdentifier") && !project.hasProperty("release")) {
if(project.nsApplicationIdentifier != android.defaultConfig.applicationId) {
def errorMessage = "${lineSeparator}WARNING: The Application identifier is different from the one inside \"package.json\" file.$lineSeparator" +
"NativeScript CLI might not work properly.$lineSeparator" +
"Remove applicationId from app.gradle and update the \"nativescript.id\" in package.json.$lineSeparator" +
"Actual: ${android.defaultConfig.applicationId}$lineSeparator" +
"Expected(from \"package.json\"): ${project.nsApplicationIdentifier}$lineSeparator";

logger.error(errorMessage)
}
}
}
}

////////////////////////////////////////////////////////////////////////////////////
Expand Down