如何使用gradle基于构建类型设置app_name并构建flavor

时间:2017-09-28 16:53:06

标签: android android-studio gradle

我让我的bluid.gradle设置了不同的app_names用于调试和发布,如下所示:

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

    release {
        ...
        resValue "string", "app_name", "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"
    }
}

问题是,如何设置app_name以匹配此输出:

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

我已经看到了一些使用资源的答案,但我想知道是否有办法只使用gradle。

5 个答案:

答案 0 :(得分:1)

就像这样

<强>的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

<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">

答案 1 :(得分:0)

您可以在AndroidManifest.xml文件中使用app_name进行Launcher活动,例如

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

答案 2 :(得分:0)

试试这个

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"
}

答案 3 :(得分:0)

您可以这样做:

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"
    }

答案 4 :(得分:0)

this answer

上找到解决方案

使用manifestPlaceholders