简体   繁体   English

如何引用在build.gradle中定义的String var?

[英]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. 我在buildConfigField "String", "AppName", "\\"Message Cleanup\\""定义了buildConfigField "String", "AppName", "\\"Message Cleanup\\"" ,但我不知道如何将其应用于AndroidManifest.xml中的android:label。

Could you help me ? 你可以帮帮我吗 ? Thanks! 谢谢!

AndroidManifest.xml 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 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. manifestPlaceholders可以做到这一点。

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 在AndroidManifest.xml中,我可以将其用作以下代码

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

Assuming you are using the standard project structure (eg you have something like src/main/res/values/strings.xml ), create additional directories for each flavor: 假设您使用的是标准项目结构(例如,您具有src/main/res/values/strings.xml ),则为每种风味创建其他目录:

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

And define the same string key (eg app_name ) in both of those files, with the correct value for each variant. 并在两个文件中定义相同的字符串键(例如app_name ),并为每个变量指定正确的值。 So you'd have something like: 所以你会有类似的东西:

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

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

src/pro/res/values/strings.xml 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. 现在,您只需在代码中引用app_name字符串,即可知道它将根据当前构建的样式正确呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM