簡體   English   中英

如何基於構建類型設置app_name並使用Gradle構建風味

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

我讓我的bluid.gradle為調試和發布設置了不同的app_name,如下所示:

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->“應用開發”
  • appRelease->“應用”
  • clientDebug->“客戶端應用開發”
  • clientRelease->“客戶端應用”

我已經看到一些使用資源的答案,但是我想知道是否只有使用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

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

您可以將AndroidManifest.xml文件中的app_name用於啟動器活動,例如

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

嘗試這個

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

您可以執行以下操作:

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

找到了這個答案的解決方案

使用manifestPlaceholders

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM