owe owe - 1 year ago 130
Android Question

How to replace a string for a buildvariant with gradle in android studio?

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):

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
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:

include '**/*.xml'
filter{String line -> line.replaceAll(package_permission, 'com.example.rev.flavor1.permission.MAPS_RECEIVE')}
into '$buildDir/res'

owe owe
Answer Source

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:
        include "my.file"
    // you have to use a new path for youre modified file
  • Replace a line in General:
    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 -> 
                include "${variant.dirName}/AndroidManifest.xml"

            // define a variable for your key:
            def gmaps_key = "<your-key>"

                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")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download