6

I want to resolve manifestPlaceholders dependency for each build types and flavors. For example, I have

 productFlavors {
        dev {
            manifestPlaceholders = ['applicationLabel': 'DevFlavor']
        }

        prod {
            manifestPlaceholders = ['applicationLabel': 'ProdFlavor']
        }
.....

 buildTypes {
        debug {
            def old_name = manifestPlaceholders.get('applicationLabel'); // every time is null
//            def old_name = productFlavors.dev.manifestPlaceholders.get('applicationLabel');  // ITS OK, but not dynamic 
            manifestPlaceholders = [applicationLabel: old_name + ' Dev']
        }

Is the any solution to add 'Dev' suffix to debug product flavors?

Thanks for any help

Gorets
  • 2,434
  • 5
  • 27
  • 45
  • If you want to adjust the application's label for each flavor / build type, there is maybe a better way to do it, via resValue / buildConfig fields, e.g. set a resValue string to `app_name`, alter it in applicationVariants.all, and use it in your manifest as application label. Does this answer http://stackoverflow.com/questions/22506290/buildconfigfield-depending-on-flavor-buildtype provide some help? – Steffen Funke Aug 26 '15 at 06:25

1 Answers1

25

Just to give a working example to my comment above:

  1. declare a resValue in your defaultConfig which will become the Application's name. (Attention: if you choose to name it app_name, be sure to delete the original app_name property from your strings.xml file, or it will clash.)

    defaultConfig {
        // applicationId, versionCode, etc.
    
        (...)
    
        // define your base Applications name here
        resValue 'string', 'app_name', 'MyApp'
    }
    
  2. set your productFlavors as you did already. You can leave them empty if it is ok for you to concat your App's name with the flavor's name only, or provide an explicit resValue, which will override the value from defaultConfig.

    productFlavors {
        dev {
            // resValue 'string', 'app_name', 'MyAppDevFlavor'
        }
    
        prod {
            // resValue 'string', 'app_name', 'MyAppProdFlavor'
        }
    }
    
  3. configure the resValue's name at gradle configuration time

    android.applicationVariants.all { variant ->
        // get app_name field from defaultConfig
        def appName = variant.mergedFlavor.resValues.get('app_name').getValue()
    
        // concat new App name with each flavor's name
        appName = "${appName}"
        variant.productFlavors.each { flavor ->
            appName += " ${flavor.name}"
        }
    
        // optionally add buildType name
        appName += " ${variant.buildType.name}"
    
        // your requirement: if buildType == debug, add DEV to App name
        if(variant.buildType.name == "debug") {
            appName += " DEV"
        }
    
        // set new resVale
        variant.resValue 'string', 'app_name', appName
    }
    
  4. In your AndroidManifest, set the app_name field:

    <application
        ...
        android:label="@string/app_name"
        ...
        >
    

As I mentioned above, be sure to delete the default app_name property from strings.xml

Steffen Funke
  • 2,168
  • 1
  • 21
  • 18
  • When do you run the renaming logic? I'm logging it and it runs, I can see the modified name, but the apk always has the original name. I've put it in the defaultConfig section. – David Corsalini Dec 03 '15 at 15:21