简体   繁体   English

如何基于构建类型设置app_name并使用Gradle构建风味

[英]How to set app_name based on built type and build flavor using gradle

I have my bluid.gradle set different app_names for debug and release like this: 我让我的bluid.gradle为调试和发布设置了不同的app_name,如下所示:

buildTypes {
    debug {
        ...
        resValue "string", "app_name", "App Dev"
    }

    release {
        ...
        resValue "string", "app_name", "App"
    }
}

Now, I want to add two flavors for two different targets of the app: 现在,我想为应用程序的两个不同目标添加两种口味:

buildTypes {
    debug {
        ...
        resValue "string", "app_name", "App Dev"
    }

    release {
        ...
        resValue "string", "app_name", "App"
    }
}

productFlavors {
    app {
        ...
        resValue "string", "app_name", "App"
    }
    client {
        ...
        resValue "string", "app_name", "Client App"
    }
}

The question is, how can I set the app_name to match this output: 问题是,如何设置app_name以匹配此输出:

  • appDebug -> "App Dev" appDebug->“应用开发”
  • appRelease -> "App" appRelease->“应用”
  • clientDebug -> "Client App Dev" clientDebug->“客户端应用开发”
  • clientRelease-> "Client App" clientRelease->“客户端应用”

I have seen some answers using resources, but I want to know if there is a way to do so just using gradle. 我已经看到一些使用资源的答案,但是我想知道是否只有使用gradle才能做到这一点。

just like this 像这样

build.gradle : build.gradle

buildTypes {
        debug {
            minifyEnabled false
            manifestPlaceholders = [app_name          : "@string/app_name_debug_version"
                                    , app_icon        : "@mipmap/ic_launcher_debug"
            ]
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable false
            manifestPlaceholders = [app_name          : "@string/app_name"
                                    , app_icon        : "@mipmap/ic_launcher"
            ]
        }
}

productFlavors {
    client {}
    app {}
}

AndroidManifest.xml : AndroidManifest.xml

<application
        android:name=".common.base.App"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="${app_icon}"
        android:label="${app_name}"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        tools:replace="android:label,android:icon">

You can make use of app_name in AndroidManifest.xml file for Launcher activity like 您可以将AndroidManifest.xml文件中的app_name用于启动器活动,例如

<activity
         android:name="com.aone.adesa.Main.SplashScreenActivity"
         android:configChanges="orientation|screenSize"
         android:label="@string/app_name"
</activity

Try this 尝试这个

flavorDimensions "stage", "production"
fav1Debug {      
    dimension "stage"
    resValue "string", "app_name", "App Debug"
}
fav1Production {      
    dimension "production"
    resValue "string", "app_name", "App Production"
}

fav2Debug {  
     dimension "stage"
    resValue "string", "app_name", "Client Debug"
}

fav2Production {      
    dimension "production"
    resValue "string", "app_name", "Client Production"
}

You can do something like this: 您可以执行以下操作:

flavorDimensions "roadmap"

productFlavors{

    production {
        dimension "roadmap"
        applicationId "com.yourappid"
        versionCode 1
        versionName "0.1.0"
    }

    preview {
        dimension "roadmap"
        applicationIdSuffix ".beta"
        versionNameSuffix "-beta"
        versionCode 1
        versionName "0.1.0"
    }

    demo {
        dimension "roadmap"
        applicationIdSuffix ".alpha"
        versionNameSuffix "-alpha"
        versionCode 1
        versionName "0.1.0"
    }

Found the solution on this answer 找到了这个答案的解决方案

Using manifestPlaceholders 使用manifestPlaceholders

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

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