Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a string in AndroidManifest.xml for a buildvariant doesn't work for Gradle Android Plugin Version > 0.5.4

Just a few weeks ago I had the following question: How to replace a string for a buildvariant with gradle?

I also answered the question on my own.

Everything works fine until now: I just realized that my copy task doesn't work anymore. I spent a few hours on the problem until I realized that it depends on the Gradle Android Plugin Version: Everything until 0.5.4 works fine. For upper versions I won't get into my copy task.

Here is the console output:

// gradle android plugin version: 0.5.6 and 0.5.5 --> copy tas doesn't work
:etscanner:prepareFlavor1Flavor1ReviewDependencies
:etscanner:compileFlavor1Flavor1ReviewAidl
:etscanner:generateFlavor1Flavor1ReviewBuildConfig
:etscanner:mergeFlavor1Flavor1ReviewAssets
:etscanner:compileFlavor1Flavor1ReviewRenderscript
:etscanner:mergeFlavor1Flavor1ReviewResources
:etscanner:processFlavor1Flavor1ReviewManifest
:etscanner:processFlavor1Flavor1ReviewResources
:etscanner:compileFlavor1Flavor1ReviewNote: Some input files use or override a d
eprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: <path>.DetailAdapter
.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

:etscanner:dexFlavor1Flavor1Review
:etscanner:processFlavor1Flavor1ReviewJavaRes UP-TO-DATE
:etscanner:validateFlavor1Flavor1Signing
:etscanner:packageFlavor1Flavor1Review
:etscanner:zipalignFlavor1Flavor1Review

// gradle android plugin version: 0.5.4 --> copy task work
:etscanner:prepareFlavor1Flavor1ReviewDependencies
:etscanner:compileFlavor1Flavor1ReviewAidl
:etscanner:generateFlavor1Flavor1ReviewBuildConfig
:etscanner:mergeFlavor1Flavor1ReviewAssets
:etscanner:compileFlavor1Flavor1ReviewRenderscript
:etscanner:mergeFlavor1Flavor1ReviewResources
:etscanner:processFlavor1Flavor1ReviewManifest
...hey you are in the copy task!
:etscanner:processFlavor1Flavor1ReviewResources
:etscanner:compileFlavor1Flavor1ReviewNote: Some input files use or override a d
eprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: <path>DetailAdapter
.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

:etscanner:dexFlavor1Flavor1Review
:etscanner:processFlavor1Flavor1ReviewJavaRes UP-TO-DATE
:etscanner:validateFlavor1Flavor1Signing
:etscanner:packageFlavor1Flavor1Review
:etscanner:zipalignFlavor1Flavor1Review
:etscanner:assembleFlavor1Flavor1Review

It's really a strange thing... Has anyone an idea how to fix this?

UPDATE 1 2013-08-23

My build.gradle file:

buildscript {
    repositories {
        // maven central repo doesn't work with gradle android plugin version 0.5.+
        // error message is describe in this post:
        // https://plus.google.com/117954254390243608387/posts/RVBjoDMkLV5
        //mavenCentral()
        maven {
            url 'http://nexus/content/groups/public/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.4'
        // copy task doesn't work for following versions:
        //classpath 'com.android.tools.build:gradle:0.5.5'
        //classpath 'com.android.tools.build:gradle:0.5.6'
        //classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

dependencies {
    compile 'com.android.support:support-v4:13.0.+' // support lib
    //compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile project(':libraries:actionbarsherlock')
    compile project(':libraries:google-play-services_lib')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
        versionName = "2.3"
        versionCode = 4
    }

    // SIGN CONFIGS
    signingConfigs {
        flavor1 {
            storeFile file("keystore/myKeystore.keystore")
            storePassword = "store_password"
            keyAlias = "alias"
            keyPassword = "key_password"
        }
        flavor2 {
            storeFile file("keystore/myKeystore.keystore")
            storePassword = "store_password"
            keyAlias = "alias"
            keyPassword = "key_password"
        }
        debug {
            storeFile file("keystore/debug.keystore")
            storePassword = "android"
            keyAlias = "androiddebugkey"
            keyPassword = "android"
        }

    }

    // FLAVORS
    productFlavors {
        flavor1 {
            packageName 'myPackageName'
            signingConfig signingConfigs.flavor1
        }
        flavor2 {
            packageName 'myPackageName'
            signingConfig signingConfigs.flavor2
        }
    }

    // BUILDTYPES
    buildTypes {
        falvor1Review {
            versionNameSuffix = versionNameSuffixOfReviewVersion
            signingConfig signingConfigs.flavor1
        }
        flavor2Review {
            versionNameSuffix = versionNameSuffixOfReviewVersion
            signingConfig signingConfigs.flavor2
        }

        debug {
            packageNameSuffix ".debug"
            versionNameSuffix = versionNameSuffixOfReviewVersion
            signingConfig signingConfigs.debug
        }
    }

    // Override Data in Manifest
    android.applicationVariants.each { variant ->
        variant.processManifest.doLast {
            copy {
                // *** SET COPY PATHS ***
                try {
                    from("${buildDir}/manifests") {
                        //println "from: ${buildDir}/manifests"
                        include "${variant.dirName}/AndroidManifest.xml"
                        //println "included: ${variant.dirName}/AndroidManifest.xml"
                    }
                } catch (e) {
                    println "error: " + e
                }

                into("${buildDir}/manifests/${variant.name}")
                //println "into (neues Manifest): ${buildDir}/manifests/${variant.name}"

                // *** DEFINE VARS ***
                def brandVersion = variant.buildType.name
                def brandVersionString = brandVersion.toString()
                def appName = "empty"

                // *** SET APP NAME ***
                if (brandVersionString.contains("flavor1")) {
                    appName = "my app name for flavor 1"
                } else if (brandVersionString.contains("flavor2")) {
                    appName = "my app name for flavor 2"
                }

                println "...hey you are in the copy task"
                // *** REPLACE LINES IN MANIFEST ***
                // --- add appName
                filter {
                    String line ->
                        line.replaceAll("<application android:allowBackup=\"true\" android:icon=\"@drawable/ic_launcher\" android:label=\"todo\" android:theme=\"@style/AppTheme\">",
                                "<application android:allowBackup=\"true\" android:icon=\"@drawable/ic_launcher\" android:label=\"" + appName + "\" android:theme=\"@style/AppTheme\">");
                }
            }
        }

        // *** SET PATH TO NEW MANIFEST *** 
        variant.processResources.manifestFile = file("${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml")
        //println "newManifest: ${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml"
    }
}

UPDATE 2 2013-08-23

Yesterday I had another strange behaviour of AS 0.2.5, it made some really strange builds: As you see in my filter my previous appName in the Manifest is "todo":

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="todo"
    android:theme="@style/AppTheme">

When I made the build then the appName in the app was the right one. But in the Application Launcher and in Settings/Apps there was shown "todo" as appName.

After building the project in AS 0.2.0 everything works fine.

like image 528
owe Avatar asked Aug 21 '13 13:08

owe


2 Answers

I had the same problem as you and read through the release notes for 0.5.5 where I found the answer.

"access to the variants container don't force creating the task. This means android.[application|Library|Test]Variants will be empty during the evaluation phase. To use it, use .all instead of .each"

like image 181
Carl B Avatar answered Sep 18 '22 04:09

Carl B


If you'd like to try Alécio's solution and have deprecation warnings on a newer version of gradle, such as

WARNING [Project: :] variant.getProcessManifest() is deprecated. Call it on one of variant.getOutputs() instead.

You need to use 'variant.outputs.each { output ->' to iterate over each output, see the code below

applicationVariants.all { variant ->
    def flavor = variant.productFlavors[0].name
    def flavorPackageName = variant.productFlavors[0].applicationId
    variant.outputs.each { output ->
        output.processManifest.doLast {
            println("configuring AndroidManifest.xml for  " + flavor);
            copy {
                from("${buildDir}/intermediates/manifests") {
                    include "${variant.dirName}/AndroidManifest.xml"
                }
                into("${buildDir}/intermediates/filtered_manifests")
            }

            def manifestFile = new File("${buildDir}/intermediates/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
            def content = manifestFile.getText()
            def updatedContent = content.replaceAll("STRING_TO_BE_REPLACED", "NEW_STRING")
            manifestFile.write(updatedContent)
        }
        output.processResources.manifestFile = new File("${buildDir}/intermediates/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
    }
}
like image 22
RookieGuy Avatar answered Sep 22 '22 04:09

RookieGuy