I have two flavors of my project:
flavor1 -> packagename: com.example.flavor1 flavor2 -> packagename: com.example.flavor2
Now I want to build a buildvariant of flavor1 and flavor2. The only difference of the buildvariant is another packagename.
My project uses MapFragments and has just one Manifest - so I put the the permission name of MAPS_RECEIVE in my string resource files of the respective flavors.
The question is: how can I replace a string resource of a buildvariant?
I tried the following approach (described in this post):
buildTypes{ flavor1Rev{ packageName 'com.example.rev.flavor1' filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: ['package_permission' : 'com.example.rev.flavor1.permission.MAPS_RECEIVE']) } }
But using this I got this error:
Could not find method filter() for arguments [{tokens={package_permission=com.example.rev.flavor1.permission.MAPS_RECEIVE}}, BuildTypeDsl_D ecorated{name=ReplaceTokens, debuggable=false, jniDebugBuild=false, renderscript DebugBuild=false, renderscriptOptimLevel=3, packageNameSuffix=null, versionNameS uffix=null, runProguard=false, zipAlign=true, signingConfig=null}] on BuildTypeD sl_Decorated{name=buderusFinal, debuggable=false, jniDebugBuild=false, renderscr iptDebugBuild=false, renderscriptOptimLevel=3, packageNameSuffix=null, versionNa meSuffix=null, runProguard=false, zipAlign=true, signingConfig=null}.
Do I have to define an own task for the filter method?
EDIT [2013_07_09]:
String in src/flavor1/res:
<string name="package_permission">package_permission</string>
Code in build.gradle to replace the string:
buildTypes{ flavor1Rev{ copy{ from('src/res/'){ include '**/*.xml' filter{String line -> line.replaceAll(package_permission, 'com.example.rev.flavor1.permission.MAPS_RECEIVE')} } into '$buildDir/res' } } }
To change its path go to this path File > Settings... > Build, Execution, Deployment > Gradle In the Global Gradle settings Change Service directory path to what you want.
A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.
compileOnly is the replacement — the equivalent configuration that is being deprecated is provided .
I solved the problem on my own, so here is the solution "step by step" - perhaps it will help some other newbies to gradle :)
Copy Task in General:
copy{ from("pathToMyFolder"){ include "my.file" } // you have to use a new path for youre modified file into("pathToFolderWhereToCopyMyNewFile") }
Replace a line in General:
copy { ... filter{ String line -> line.replaceAll("<complete line of regular expression>", "<complete line of modified expression>") } }
I think the biggest problem was to get the right paths, because I had to make this dynamically (this link was very helpful for me). I solved my problem by replacing the special lines in the manifest and not in the String-file.
The following example shows how to replace the "meta-data"-tag in the manifest to use youre google-maps-api-key (in my case there are different flavors that use different keys ):
android.applicationVariants.each{ variant -> variant.processManifest.doLast{ copy{ from("${buildDir}/manifests"){ include "${variant.dirName}/AndroidManifest.xml" } into("${buildDir}/manifests/$variant.name") // define a variable for your key: def gmaps_key = "<your-key>" filter{ String line -> line.replaceAll("<meta-data android:name=\"com.google.android.maps.v2.API_KEY\" android:value=\"\"/>", "<meta-data android:name=\"com.google.android.maps.v2.API_KEY\" android:value=\"" + gmaps_key + "\"/>") } // set the path to the modified Manifest: variant.processResources.manifestFile = file("${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml") } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With