Reputation: 1233
This is nothing new to me, I have been doing this for months before Google released Gradle plugin for Android studio version 2.2.2 and it breaks the file lookup functionality.
Use case:
I am running gradle build within the Android studio as well as from the Jenkins. I need some build information to be passed to the build and then want it to be used by the code. These values include system information, build variant, build date and IP addresses for the associated build flavors.
Before I switched to Gradle 2.2.2 from 2.1.3
android.applicationVariants.all { variant ->
variant.mergeResources.doLast {
File propsFile = file("${buildDir}/intermediates/assets/${variant.dirName}/config.properties")
println("Filtering resource file: " + propsFile)
String content = propsFile.getText('UTF-8')
println("File contents: " + content)
content = content.replaceAll(/@build.date@/, buildDate)
.replaceAll(/@build.mode@/, buildMode)
.replaceAll(/@serverUri@/, serverUrl)
propsFile.write(content, 'UTF-8')
}
}
The above code looks up for the config.properties file in intermediate/assets/${variant.dirName}
path. The file is then processed to replace the tokens marked with @..@ with the variables passed to replaceAll method.
Now my problem is:
The code was supposed to work with the newer Gradle plugin version 2.2.2. However, I observed that the newer build does not generate the asset folder in intermediate directory and so the above code fails to lookup the file and throws following error:
Execution failed for task ':app:mergeDebugResources'.
> /home/pawan/git/MyApplication/app/build/intermediates/assets/debug/config.properties (No such file or directory)
It is apparent that the newer version has changed the way build operation works. Now question is, how should I go ahead from this point? How to achieve the intended functionality? Any workaround or fixes?
Upvotes: 1
Views: 3037
Reputation: 659
May be Build Environment will help for you. You can define environment variables in gradle.properties
and assign it a default value. In you build.gradle
file, just such environment variables. In you jenkins, use gradlew
to assign your need value to variables, and the default value will be replaced.
For example, in your gradle.properties
:
versCode=100
In your build.gradle
:
defaultConfig {
// Other configs
versionCode versCode as Integer
}
In your jenkens build script :
./gradlew -PversCode=200 build
Upvotes: 1