HelloCW
HelloCW

Reputation: 2235

How to refer to a String var defined in build.gradle?

I hope to display different app name based free and pro version.

I define buildConfigField "String", "AppName", "\"Message Cleanup\"" in build.gradle, but I don't know how to apply to android:label in AndroidManifest.xml.

Could you help me ? Thanks!

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.dodata.messagecleanup" >



    <application
        android:allowBackup="true"
        android:icon="@mipmap/clenupmessage"
        android:label="AppName"
        android:theme="@style/AppTheme" >


        <activity android:name="ui.CleanupDelete"
            android:label="AppName">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>       


    </application>

</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "info.dodata.messagecleanup"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 7
        versionName "1.07"
        archivesBaseName = "MessageCleanup-V" + versionName
    }


    productFlavors {
        free {
            applicationId "info.dodata.messagecleanup"
            buildConfigField "String", "AppName", "\"Message Cleanup\""
        }


        pro {
            applicationId "info.dodata.messagecleanup.pro"
            buildConfigField "String", "AppName", "\"Message Cleanup Pro\""
        }
    }


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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.android.gms:play-services:7.3.0'
}

To kcoppock

The manifestPlaceholders can do that.

productFlavors {

wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}

baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "New"]
}

c360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"]
}

uc {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "uc"]
}

}

In AndroidManifest.xml, I can use it as the following code

 <meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />

Upvotes: 0

Views: 2114

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134654

Assuming you are using the standard project structure (e.g. you have something like src/main/res/values/strings.xml), create additional directories for each flavor:

src/free/res/values/strings.xml
src/pro/res/values/strings.xml

And define the same string key (e.g. app_name) in both of those files, with the correct value for each variant. So you'd have something like:

src/free/res/values/strings.xml

<string name="app_name">Message Cleanup</string>

src/pro/res/values/strings.xml

<string name="app_name">Message Cleanup Pro</string>

The resource merger will merge in these strings based on which variant is being built. Now you can just refer to the app_name string in code, and know that it'll render correctly based on the currently built flavor.

Upvotes: 2

Related Questions