Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get gradle build type in product flavor

I need to create different app names depending on the product flavour used.

While this was easy by simply setting a string resource, I can no longer do that because when the app is uploaded to hockeyapp the app name is set as '@string/app_name' instead of the value of app_name.

I have made some progress by setting the label in the manifest to be '${applicationName}' and setting the value with

manifestPlaceholders = [ applicationName : appName ];

in the product flavour block so that the value gets set at compile time.

The problem comes when I try to append the build type to the application name. I can't seem to find a way to know what build type is currently being used within the product flavour.

This is a stripped down version of the build for readability

android {
    buildVersionName "1.0.0

    buildTypes {
        release {
            ... nothing special
        }
        uat {
            signingConfig signingConfigs.debug
            buildType = "uat"
            applicationIdSuffix = "." + buildType
        }
        debug {
            signingConfig signingConfigs.debug
            buildType = "uat"
            applicationIdSuffix = "." + buildType
        }
    }
    productFlavors{
        flavor1{
            def appName = "app name " + buildType;
            manifestPlaceholders = [ applicationName : appName ];
            applicationId [id]
            def clientIteration = [client iteration]
            versionName buildVersionName + clientIteration
            versionCode [version code]
        }
        flavor2{
            ... same as above with different app name
        }
        flavor3{
            ... same as above with different app name
        }
    }
}

This code works fine except the variable 'buildType' is always the last buildtype (in this case debug) which means the app name always has debug on the end.

Probably worth noting that I don't need to have anything appended on the end of the app name for releases.

like image 936
Stimsoni Avatar asked Apr 30 '15 02:04

Stimsoni


2 Answers

You can append the values like this

   android {

    productFlavors {
        Foo {
             applicationId "com.myexample.foo"
             manifestPlaceholders = [ appName:"Foo"]
        }

        Bar {
             applicationId "com.myexample.bar"
             manifestPlaceholders = [ appName:"Bar"]
        }
    }

    buildTypes {
        release {
            manifestPlaceholders = [ appNameSuffix:""]
        }

        debug {
            manifestPlaceholders = [ appNameSuffix:".Debug"]
            applicationIdSuffix ".debug"
        }
    }
}

and in the manifest

<application
        android:label="${appName}${appNameSuffix}"
        ...
 </application>

If you want to access different values based on build type you can do it like this

buildTypes {
    debug{
        buildConfigField "String", "Your_string_key", '"yourkeydebugvalue"'
        buildConfigField "String", "SOCKET_URL", '"some text"'
        buildConfigField "Boolean", "LOG", 'true'
    }
    release {
        buildConfigField "String", "Your_string_key", '"yourkeyreleasevalue"'
        buildConfigField "String", "SOCKET_URL", '"release text"'
        buildConfigField "Boolean", "LOG", 'false'

    }
}

And to access those values using build variants:

 if(!BuildConfig.LOG)
      // do something with the boolean value

Or

view.setText(BuildConfig.yourkeyvalue);
like image 99
Ivan Milisavljevic Avatar answered Sep 30 '22 05:09

Ivan Milisavljevic


I know I'm a bit late for the party but if you want different names based on the flavours, you should have something like this:

  productFlavors{
    flavour 1 {
        applicationId "your_app_id"
        resValue "string", "app_name", "Flavour 1 app name"
        .......
    }

    flavour 2 {
        applicationId "your_app_id"
        resValue "string", "app_name", "Flavour 2 app name"
        .......

    }
}

and in your AndroidManifest.xml:

    android:label="@string/app_name"

Hope this helps.

like image 40
android enthusiast Avatar answered Sep 30 '22 05:09

android enthusiast