Thomas Keller
Thomas Keller

Reputation: 6060

How do I include `applicationIdSuffix` in some generated resource value?

Imagine this setup:

android  {
   buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
   }
   productFlavors {
       foo {
           applicationId defaultConfig.applicationId + '.foo'
       }
   }

}

How can I set up a dynamic string value such as

resValue "string", "package_name", applicationId

so that it includes the applicationIdSuffix for debug builds?

If I add this to defaultConfig, its my defaultConfig's applicationId that is set. If I add this to the flavor configuration, it is missing the applicationIdSuffix (this is null at this level).

Any hints?

Upvotes: 4

Views: 2895

Answers (1)

dev.bmax
dev.bmax

Reputation: 10621

The cool part about applicationIdSuffix is that you can use it in flavors as well as in build types. Check this out:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "dev.bmax.suffixtext"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix '.debug'
        }
    }

    productFlavors {
        prod {
            applicationIdSuffix '.prod'
        }
        mock {
            applicationIdSuffix '.mock'
        }
    }
}

Now, when I build my 'prodDebug' variant the final application ID is 'dev.bmax.suffixtext.prod.debug', which is what you want if I understand your question correctly.

EDIT

  1. You can access the variant's name in a Gradle task like this:

    applicationVariants.all { variant ->
        // Use 'variant.name'
    }
    
  2. Gradle Android Plugin version 0.14.3 added support of the variant specific BuildConfigField/resValue:

    applicationVariants.all { variant ->
        variant.resValue "string", "name", "value"
    }
    

Upvotes: 4

Related Questions