How to share variables between .yaml and .gradle files? - android

I am developing Android library with CI(Continuous Integration) support in git server. The configuration use a .yaml or .yml file. The build system that I use is the default one from Android Studio, which is gradle, so I also use .gradle configuration files here. My question is, how can I share variables between those files? Because I need to upgrade some variables that actually the same from each file, like ANDROID_API_LEVEL, ANDROID_BUILD_TOOLS_VERSION, etc. It will be inconvenience if I should edit every file to upgrade version. Is there any best practice that I can use?
For example, here is my yaml file looks like
language: android
sudo: false
dist: precise
env:
global:
- ANDROID_API_LEVEL=27
- ANDROID_EMULATOR_API_LEVEL=21
- ANDROID_BUILD_TOOLS_VERSION=27.0.3
- ANDROID_ABI=armeabi-v7a
...
And here is my project-level gradle file looks like
buildscript {
ext {
kotlinVersion = '1.2.31'
ankoVersion = "0.10.2"
compileSdkVersion = 27
buildToolsVersion = "27.0.3"
minSdkVersion = 14
targetSdkVersion = 27
androidSupportVersion = "27.1.1"
}
...
Let's say I want to share the value of a variable ANDROID_BUILD_TOOLS_VERSION in yaml file with buildToolsVersion in gradle file.

You can use SnakeYAML for reading and writing properties from YAML files.
Take a look at this answer, they talk about the same problem.
Update answer with example
The implementation is exactly the same since both, Android Studio and Spring (in this case), use Gradle as the main executive buildtool. The link i shared showed the basic principle on how to access YAML properties in your build.gradle and was intended as a how-to-start.
This will synchronise selected env.global properties of your build.gradle file with your YAML file. Trivial properties like language, sudo and dist stay untouched by this action. The main file to maintain in this case would be the build.gradle file since the YAML file will be maintained by Gradle.
I wrote a short snippet on how to attain this:
File: cfg.yaml
language: android
sudo: false
dist: precise
env:
global:
- ANDROID_API_LEVEL=27
- ANDROID_EMULATOR_API_LEVEL=21
- ANDROID_BUILD_TOOLS_VERSION=27.0.3
- ANDROID_ABI=armeabi-v7a
File: build.gradle
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.yaml', name: 'snakeyaml', version: '1.21'
}
}
ext {
buildToolsVersion = '27.202'
}
task updateYAMLFile() {
def configurationYAML = "cfg.yaml"
inputs.files buildscript.sourceFile
outputs.files configurationYAML
DumperOptions options = new DumperOptions()
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
options.setPrettyFlow(true)
Yaml yaml = new Yaml(options)
ext {
updateGlobalProperties = { cfg, name, version ->
def index = (int) cfg.env.global.findIndexValues { it ==~ /$name.*/ }[0]
cfg.env.global.set(index, name + '=' + version)
}
}
doLast {
def cfg = yaml.load(new File(configurationYAML).newInputStream())
updateGlobalProperties(cfg, 'ANDROID_BUILD_TOOLS_VERSION', buildToolsVersion)
FileWriter writer = new FileWriter(configurationYAML)
yaml.dump(cfg, writer)
}
}

Related

Android AAR depending on AAR fails with javadoc generation

I have an android gradle project structure that looks like this
module1-aar
module2-aar
testapp-apk
Key facts
module2-aar depends on module1-aar
testapp-apk depends on module2-aar
JDK11
Gradle 7.4.2
Android gradle plugin 7.1.3
Without javadocs, gpg, signing, or publishing, everything builds just fine. App runs, everything is great.
When i started adding in tasks to generate javadocs, that's when everything went haywire. module1-aar will build and generate javadocs with no problem. module2-aar however always fails during the javadoc task.
Task is below. Most of it was borrowed from here How to generate javadoc for android library when it has dependencies which are also aar libraries?
project.task("javadoc", type: Javadoc) {
afterEvaluate {
configurations.all
.each {item ->
item.setCanBeResolved(true)
}
classpath += configurations.api
classpath += configurations.implementation
// Wait after evaluation to add the android classpath
// to avoid "buildToolsVersion is not specified" error
classpath += files(android.getBootClasspath())
// Process AAR dependencies
def aarDependencies = classpath.filter { it.name.endsWith('.aar') }
classpath -= aarDependencies
//fails here when an AAR depends on an AAR
aarDependencies.each { aar ->
// Extract classes.jar from the AAR dependency, and add it to the javadoc classpath
def outputPath = "$buildDir/tmp/aarJar/${aar.name.replace('.aar', '.jar')}"
classpath += files(outputPath)
// Use a task so the actual extraction only happens before the javadoc task is run
dependsOn task(name: "extract ${aar.name}").doLast {
extractEntry(aar, 'classes.jar', outputPath)
}
}
}
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += project.files(android.getBootClasspath())
classpath += configurations.implementation
classpath += fileTree(dir: project.buildDir.absolutePath + "/tmp/aarsToJars/")
classpath += files(project.buildDir.absolutePath + "/intermediates/compile_r_class_jar/release/R.jar")
classpath += files(project.buildDir.absolutePath + "/generated/source/buildConfig/release/release")
classpath += files(project.buildDir.absolutePath + "/generated/source/r/buildConfig/release/release")
destinationDir = file( project.buildDir.absolutePath + "/outputs/javadoc/")
failOnError true
options.charSet 'UTF-8'
options.docEncoding 'UTF-8'
options.encoding 'UTF-8'
options.addBooleanOption 'Xdoclint:none', true
exclude '**/BuildConfig.java'
exclude '**/R.java'
exclude '**/doc-files/*'
}
// Utility method to extract only one entry in a zip file
private def extractEntry(archive, entryPath, outputPath) {
if (!archive.exists()) {
throw new GradleException("archive $archive not found")
}
def zip = new java.util.zip.ZipFile(archive)
zip.entries().each {
if (it.name == entryPath) {
def path = new File(outputPath)
if (!path.exists()) {
path.getParentFile().mkdirs()
// Surely there's a simpler is->os utility except
// the one in java.nio.Files? Ah well...
def buf = new byte[1024]
def is = zip.getInputStream(it)
def os = new FileOutputStream(path)
def len
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len)
}
os.close()
}
}
}
zip.close()
}
//wires in the javadoc task to the normal build
tasks.named("build") { finalizedBy("generateJavadocJar") }
The error message i'm getting is the following
* What went wrong:
A problem occurred configuring project ':module2-aar'.
> Could not resolve all files for configuration ':module2-aar:implementation'.
> Could not resolve project :module1-aar.
Required by:
project :module2-aar
> Cannot choose between the following variants of project :module1-aar:
- debugRuntimeElements
- releaseRuntimeElements
All of them match the consumer attributes:
- Variant 'debugRuntimeElements' capability com.github.test:module1-aar:6.1.11-SNAPSHOT:
- Unmatched attributes:
- Provides com.android.build.api.attributes.AgpVersionAttr '7.1.3' but the consumer didn't ask for it
- Provides com.android.build.api.attributes.BuildTypeAttr 'debug' but the consumer didn't ask for it
- Provides com.android.build.gradle.internal.attributes.VariantAttr 'debug' but the consumer didn't ask for it
- Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
- Variant 'releaseRuntimeElements' capability com.github.test:module1-aar:6.1.11-SNAPSHOT:
- Unmatched attributes:
- Provides com.android.build.api.attributes.AgpVersionAttr '7.1.3' but the consumer didn't ask for it
- Provides com.android.build.api.attributes.BuildTypeAttr 'release' but the consumer didn't ask for it
- Provides com.android.build.gradle.internal.attributes.VariantAttr 'release' but the consumer didn't ask for it
- Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
I've been playing around with the gradle task a bit and it seems that the error message is generated anytime i attempt to iterate over the classpath of the module2-aar.
I have tried a number of other suggestions, like changing module2-aar's dependency declaration from
api project(':module2-aar')
to
api project(path:':module2-aar')
However that doesn't do anything
I also tried this:
api project(path: ':module1-aar', configuration: 'default')
While the above resolves the reported issue, it causes a compile issue whereby module2-aar doesn't appear to have module1-aar in the classpath during compile...and it seems to compile before module1-aar.
Unfortunately, the documentation for what configuration means when referencing an android project is a bit thin, or perhaps I'm looking in the wrong place. I'm not sure what other valid values are available.
Anyhow, I'm not sure what's wrong here other than I've spent way too much time on this.
I going to publish my solution to the problem of using "aar" files in javadoc. In the course of trying to solve the problem, I too, had been getting the same error that spy was referring to. That actually error means it can differentiate whether it should be using release or debug libraries. It seemed to me to be too futile to try and correct that issue, so instead, I took a different approach to solving what I think is essentially the same problem.
In my case, I have a project that contains multiple subprojects, and when I produce my javadoc documentation I wanted to produce a merged javadoc document, that consisted of just some of the subprojects (not all of the subprojects). As far as I know, this is not a capability, built into Android Studio. The current version of Android Studio(2021.2.1) seems to have problems producing javadoc documentation for android library modules. There are two issues:
1.) the javadoc classpath doesn't have the android bootclasses added. You get errors for referencing any android SDK method such as "Context", "Typeface", etc.
2.) the javadoc classpath doesn't get any AndroidX libraries added to it. Many of the AndroidX libraries are "aar" files. Android Studio(2021.2.1) does not handle aar files correctly when using javadoc.
My environment is similar to spy, except that I'm using android gradle plugin 7.2.0. I've created a custom javadoc task in the "app" module's build.gradle.kts script. My "app" module is an android application module. The code needs to be place in any module that contains either the plugin "com.android.application" or "com.android.library". Some of the modules that I produce the merged javadoc for are java libraries, and that's okay.
// create a custom configuration
val javadocDeps = configurations.create("javadocDeps")
// add javadoc dependencies that you need.
dependencies {
javadocDeps(project(":OsgiFramework"))
// note: I'm using a libs version catalog for the dependencies
// you can add hardwired dependencies if you prefer
javadocDeps (libs.androidx.appcompat)
javadocDeps (libs.androidx.fragment)
javadocDeps (libs.androidx.navigation.fragment)
javadocDeps (libs.androidx.navigation.ui)
javadocDeps (libs.androidx.constraint.layout)
}
// register the createCoreJavadoc task
// in my case, "gradlew app:createCoreJavadoc" creates the merged javadoc
tasks {
register<Javadoc>("createCoreJavadoc") {
setFailOnError(true)
val docDir: File = File(project.projectDir.parentFile.parentFile, "Doc/Core")
println("javadoc destination dir: " + docDir.absolutePath)
// set the location where the documentation is produced in
setDestinationDir(docDir)
// select the projects to produce merged javadoc for
var sourcepaths: FileCollection = project(":CoreInterfaces").files("src/main/java")
sourcepaths =
sourcepaths.plus(project(":CoreInternalInterfaces").files("src/main/java"))
sourcepaths = sourcepaths.plus(project(":CoreAndroidInterfaces").files("src/main/java"))
sourcepaths =
sourcepaths.plus(project(":CoreAndroidInternalInterfaces").files("src/main/java"))
sourcepaths = sourcepaths.plus(project(":OsgiInterface").files("src/main/java"))
sourcepaths =
sourcepaths.plus(project(":InstallPlanInterfaces_1_0_0").files("src/main/java"))
setSource(sourcepaths.asFileTree)
// fix the problem with the missing android bootclasses
android.bootClasspath.forEach{
classpath += fileTree(it)
}
// create a temporary directory for storing the "classes.jar" file contained in the *.aar files
val tmpDir:File = File(project.buildDir, "\\tmpAar\\")
if (tmpDir.exists()) tmpDir.delete()
tmpDir.mkdirs()
// add the javadoc dependencies
javadocDeps.forEach {
// I've got a custom class that allows me to treat jar or zip files and a file system
// you could replace this using spy's zip file extraction method
val zipFileSystem: com.phinneyridge.ZipFileSystem = ZipFileSystem(it.absolutePath,null)
if (it.name.endsWith(".aar")) {
// extract the classes.jar file from the aar file to the tmpDir
// renaming it to name of the aar file, but change the extension to jar
val tmpFile:File = File(tmpDir, it.name.replace(".aar", ".jar"))
zipFileSystem.extractEntry("classes.jar", tmpFile)
} else {
// for jar files, we just add it to the path
classpath += fileTree(it)
}
}
// now add the tmpDir files to the javadoc classpath
classpath += fileTree(tmpDir)
// for diagnosis purposes, we'll print the classpath.
// Notice that you have a lot more path entries then you have javadocDeps
println("classpath: " + classpath.asPath)
}
}
I understand that you have a project structure like this: app -> module2 -> module1.
Where -> means the dependency stream. So I deployed a project with that same dependency flow, but using gradle 7.0.2 (because that's what I'm currently using), and had no problem generating javadoc for module2 and module1
Basically it boils down to implementing this in every gradle of every module: https://stackoverflow.com/a/73096187/9902249
I hope it works for you.

How to change the default output folder for generated apk files?

When I use the gradle task to create an android .apk file it outputs it the the build\javafxports\android folder of the project (both the regular and unaligned files). I couldn't find a setting to change the output folder.
When I export a jar in eclipse I can specify a destination folder. How can I do that with apk files too?
Here is my build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.1.1'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
maven {
url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
}
}
mainClassName = 'com.gluonapplication.GluonApplication'
dependencies {
compile 'com.gluonhq:charm:4.0.1'
}
jfxmobile {
downConfig {
version = '3.0.0'
plugins 'display', 'lifecycle', 'statusbar', 'storage'
}
android {
compileSdkVersion = 24
manifest = 'src/android/AndroidManifest.xml'
androidSdk = 'C:/Users/Mark/AppData/Local/Android/sdk'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses = [
'com.gluonhq.**.*',
'javax.annotations.**.*',
'javax.inject.**.*',
'javax.json.**.*',
'org.glassfish.json.**.*'
]
}
}
The jfxmobile plugin allows changing the path where the apk will be created.
Use installDirectory:
jfxmobile {
downConfig {
version = '3.0.0'
plugins 'display', 'lifecycle', 'statusbar', 'storage'
}
android {
installDirectory = file('/full/path/of/custom/folder')
manifest = 'src/android/AndroidManifest.xml'
}
}
Be aware that the folder should exist before running android task. Currently the plugin manages that for the default installation folder (removing it, and the apk, if exists and creating it again on every run). So you have to do it yourself, otherwise the task will skip it.
EDIT
The list of global variables that are intended to be modified if necessary are here, but the full list of variables currently included in the plugin can be found in the plugin source code.
Variables like installDirectory are used internally by the plugin and they are initialized with a default value, perform some actions like deleting the previous directory and creating it again (so Gradle performs the task). In case of overriding, these actions won't be executed, so you should take care of that yourself (or create a task for that).
This works for the standard android plugin to change the directory of the generated APKs:
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = file("/some/dir/" + variant.name + "/" + archivesBaseName + ".apk")
}
}
}

Android Studio Gradle DSL method not found: 'android()' -- Error(17,0)

I am attempting to run my project in Android Studio but the error appears below:
I have followed many sources just to get this to run and have wound up here, but do not know what else to do.
How can I configure this project to run?
build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.1"
}
settings.gradle:
include ':app'
local.properties:
sdk.dir=C\:\\Users\\KJA\\AppData\\Local\\Android\\sdk
gradle.propertes:
# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
I went ahead and downloaded the project from the link you provided: http://javapapers.com/android/android-chat-bubble/
Since this is an old tutorial, you simply need to upgrade the software, gradle, the android build tools and plugin.
Make sure you have the latest Gradle and Android Studio:
https://www.gradle.org/
http://tools.android.com/tech-docs/new-build-system/version-compatibility
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
allprojects {
repositories {
jcenter()
}
}
app/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName '1.0'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
}
Then run gradle:
gradle installDebug
In your top level build.gradle you seem to have the code
android {
compileSdkVersion 19
buildToolsVersion "19.1"
}
You can't have this code at the top level build.gradle because the android build plugin isn't loaded just yet. You can define the compile version and build tools version in the app level build.gradle.
For some unknown reason, Android Studio incorrectly adds the android()
method in the top-level build.gradle file.
Just delete the method and it works for me.
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
}
I have tried to manage this issue via below steps :
Delete android { ... } block in top level root gradle file
Look into
compileSdkVersion 22
buildToolsVersion "22.0.0"
lines of code in app/gradle file here only one of the version persent in below dropdown should be present else it would give provide option to downloaad the same.
I got this same error when I was trying to import an Eclipse NDK project into Android Studio. It turns out, for NDK support in Android Studio, you need to use a new gradle and android plugin (and gradle version 2.5+ for that matter). This plugin, requires changes in the module's build.gradle file. Specifically the "android{...}" object should be inside "model{...}" object like this:
apply plugin: 'com.android.model.application'
model {
android {
....
}
}
So if you have updated your gradle configuration to use the new gradle plugin, and the new android plugin, but didn't change the module's build.gradle syntax, you could get "Gradle DSL method not found: 'android()'" error.
I prepared a patch file here that has some further explanations in the comments:
https://gist.github.com/shumoapp/91d815de6e01f5921d1f
These are the changes I had to do after importing the native-audio ndk project into Android Studio.
Correcting gradle settings is quite difficult. If you don't know much about Gradle it requires you to learn alot. Instead you can do the following:
1) Start a new project in a new folder. Choose the same settings with your project with gradle problem but keep it simple: Choose an empty main activity.
2) Delete all the files in ...\NewProjectName\app\src\main folder
3) Copy all the files in ...\ProjectWithGradleProblem\app\src\main folder to ...\NewProjectName\app\src\main folder.
4) If you are using the Test project (\ProjectWithGradleProblem\app\src\AndroidTest) you can do the same for that too.
this method works fine if your Gradle installation is healthy. If you just installed Android studio and did not modify it, the Gradle installation should be fine.
Actually i tried many combinations nothing worked
but when i modified my application gradle file with following
buildTypes {
release {
minifyEnabled false
}
}
By removing the Line
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
it worked Normally :)) cheers
Another solution if you have installed android-studio-bundle-143.2915827-windows
and gradle2.14
You can verify in
C:\Program Files\Android\Android Studio\gradle if you have gradle-2.14.
Then you must go to
C:\Users\\AndroidStudioProjects\android_app\
And in this build.gradle you put this code:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Then, go to C:\Users\Raul\AndroidStudioProjects\android_app\Desnutricion_infantil\app
And in this build.gradle you put:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '24.0.0'
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
}
You must check your sdk version and the buildTools.
compileSdkVersion 23
buildToolsVersion '24.0.0'
Save all changes and restart AndroidStudio and all should be fine !
Just delete these lines from the root build.gradle
android {
compileSdkVersion 19
buildToolsVersion '19.1' }
Now trying and compile again. It should work.
Change to root build.gradle file
to
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
What worked for me was to import the project with "File -> New -> Project from Version Control" then choose your online source (GitHub for example). This way all the .gradle files were created in the import.
I also meet that problems,and just delete bottom code:
DELETE THESE LINES:
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
}
it worked。
This error has occurred when i was importing a project from Github. This happens as the project was of older version. Just try to delete these methods below from your root gradle.
android {
compileSdkVersion 19
buildToolsVersion "19.1"
}
then try again.
I hope it works.

How do I use a library with my Android project using Gradle?

I'm only just starting with Android development so I'm sure this is incredibly basic and a dumb question, but I'm getting the following error when I try and include a library to use in my code with Gradle:
Gradle 'MyApp' project refresh failed: You are using an old, unsupported version of Gradle. Please use version 1.6 or greater.
The library I'm trying to use is ION. All I've tried so far is adding the following lines into the build.gradle file.
dependencies {
compile 'com.koushikdutta.ion:ion:1.1.5'
}
Which I guessed at based on this section of Koush's GitHub account, but what else do I need to do? Still download the jar and put it somewhere? I have looked at other questions/searched for guides on the basics of Gradle, but bizarrely I can't find a solid, definitive answer.
And how do I upgrade Gradle? Can it be done from within Android studio? I'm using Android Studio 0.2.13 on Windows 7 64bit:
SOLUTION: - Make sure you're putting the dependencies in the correct build.gradle file! Doh! It should be in the one just above your src folder, not the root!
Make sure that your buildscript contains the classpath with "gradle:0.5.+". Afterwards, navigate to the root directory of your project (one directory level above the build.gradle file) and (in the command line) run "./gradlew clean"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 18
}
sourceSets {
main {
manifest.srcFile 'MyApp\src\main\AndroidManifest.xml'
}
}
}
dependencies {
compile "com.koushikdutta.ion:ion:1.1.5"
}

How to replace strings resources with Android Gradle

I made a new app with gradle in Android Studio, and now I need to make about 10 versions with different package names and values in resources. I made custom flavors as in example and want to replace some strings in this custom flavors with custom values. I found example like this:
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: ['version': '2.2'])
But i don't know where to put it. As i understand i need to put it into separate task, but how to make this task called by IDE?
Also i need to replace few variables inside Java classes and Content Provider's auth, maybe i need to do this by copy files into flavor1 folder and let gradle to merge it, but it seems like wrong solution to store many copies of files with difference in one line... Maybe i need to user some other solution for all this?
Here is build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':JazzyListView')
compile project(':ABS')
compile project(':Volley')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
versionCode 5
versionName "3.0"
minSdkVersion 8
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
}
}
productFlavors {
flavor1 {
packageName "com.example.flavor1"
}
flavor2 {
packageName "com.example.flavor2"
}
}
}
I had a similar problem. I wanted to add the Jenkins build number to the strings that get merged from strings.xml. Here's my solution as of Android Gradle plugin 0.12.+.
// Insert the build number into strings.xml
android.applicationVariants.all{ variant ->
variant.mergeResources.doLast{
ext.env = System.getenv()
def buildNumber = env.BUILD_NUMBER
if (buildNumber != null) {
File valuesFile = file("${buildDir}/intermediates/res/${variant.dirName}/values/values.xml")
println("Replacing revision number in " + valuesFile)
println("Build number = " + buildNumber)
String content = valuesFile.getText('UTF-8')
content = content.replaceAll(/devBuild/, buildNumber)
valuesFile.write(content, 'UTF-8')
}
}
}
You might want to hook into a different Gradle task depending on what you want to do. Take a look at the tasks that are part of the Android build to figure that out.
http://tools.android.com/tech-docs/new-build-system/user-guide
UPDATE: At some point, the Android Gradle plugin changed the way to iterate through application variants keyword from each to all. My answer has been updated to reflect the change, but try switching to each if this code doesn't print anything to the console.
I was trying to get similar functionality as Maven resource filtering.
This is what I came up with. My solution could use some changes to be more robust (i.e. pulling from a properties file, etc).
My example just shows how to replace a single value, which is all that I needed. The variables follow the ${some.property} convention. This solution also works with product flavors that have their own resource files.
import org.apache.tools.ant.filters.*
...
android.applicationVariants.all{ variant ->
// Perform resource filtering
variant.mergeResources.doLast {
filterResources(variant)
}
}
def filterResources(buildVariant) {
//Setup temp directory to filter the resources
File resFiltered = file("${buildDir}/res/all/filtered/${buildVariant.dirName}")
if(resFiltered.exists()){
resFiltered.delete()
}
//Copy and filter the resources.
copy {
from(buildVariant.processResources.resDir) {
include '**/*.xml'
//Could be improved upon to pull from a properties file, etc.
ant.properties['app.version'] = project.version
filter(ExpandProperties, project: ant.project)
}
from(buildVariant.processResources.resDir) {
exclude '**/*.xml'
}
into resFiltered
}
//Delete all the original resource files
file(buildVariant.processResources.resDir).deleteDir()
//Replace with the filtered ones.
resFiltered.renameTo(file(buildVariant.processResources.resDir))
//Delete the original 'filtered' directory
file( "${buildDir}/res/all/filtered").deleteDir()
}
Example in strings.xml
...
<string name="app_version">${app.version}</string>
...
These links may be helpful:
Using Gradle for building Android applications
Gradle Plugin User Guide
And my filter definition using regex to replace something:
from('res/') {
include '**/*.xml'
filter {
line -> line.replaceAll(/YOUR_REGEX_TO_REPLACE_SOMETHING/, 'REPLACED_RESULT_EXPRESSION')
}
}
into 'build/path/to/your/filtered/resources/'
In your "src" folder, create "flavor1" and "flavor2" folders with the same hierarchy as your "main" folder. If you have strings.xml in your main/res/values, you can do it in flavor1/res/values as well and have the replacement values in there. It may show errors in the IDE but it should still build and run.

Categories

Resources