Achin Achin -4 years ago 129
Android Question

Configure gradle task issue with default Configs

I have succesfuly write my gradle.propreties file using gradle script , i will share my code ,

build.gradle:
afterEvaluate {
processDebugGoogleServices.dependsOn switchToDebug
processReleaseGoogleServices.dependsOn switchToRelease
}
String base = "URL"
def slurper = new JsonSlurper()

def app_data = slurper.parseText(base.toURL().text)

def FileWriter mFile = new FileWriter(".........../gradle.properties")


now i am facing one issue, the key and value pairs which i write in the property file uses in default configuration
for exp - applicationId , versionCode , versionname etc. While writing my gradle.property file ,the default configuration by android parrally executes with my
tasks , due to which system is unable to find applicationId, versionCode , versionname etc.

build.gradle:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "${app_id}"
minSdkVersion 15
targetSdkVersion 23
versionCode java.lang.Integer.parseInt("${version_code}")
versionName "${version_name}"

}


Error:(51, 0) Could not get unknown property 'app_id' for ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.ProductFlavor.
<a href="openFile:.........../app/build.gradle">Open File</a>

Answer Source

To solve this Problem, just create a file other.gradle in your root directory. Put your script in that file, just like

task abc {
String base = "URL"
def slurper = new JsonSlurper()

def app_data = slurper.parseText(base.toURL().text)

def FileWriter mFile = new FileWriter(".........../gradle.properties")
}

Now you can execute gradle command with -b option. for example

gradle -b other.gradle abc

Now run your project normally you were doing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download