How to change app name per Gradle build type

前端 未结 10 846
慢半拍i
慢半拍i 2020-11-29 20:09

I am trying to figure out a way to be able to change my application\'s app name per build type in gradle.

For instance, I would like the debug version to have

相关标签:
10条回答
  • 2020-11-29 20:27

    You can use something like this

     buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-DEBUG'
            resValue "string", "app_name", "AppName debug"
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            zipAlignEnabled true
            resValue "string", "app_name", "AppName"
        }
    }
    

    You can use @string/app_name in AndroidManifest.xml files.

    Make sure you remove app_name from values/ folder (no entry by this name).

    0 讨论(0)
  • 2020-11-29 20:27

    To support translations make this:

    1. remove string "app_name"

    2. add to gradle

     buildTypes {
        admin {
           resValue "string", "app_name", "@string/app_name_admin"
        }
        release {
            resValue "string", "app_name", "@string/app_name_release"
        }
        debug {
            resValue "string", "app_name", "@string/app_name_debug"
        }
    }
    

    3. Set app name in Manifest as "@string/app_name"

    4. Add to strings.xml values

    <string name="app_name_admin">App Admin</string>
    <string name="app_name_release">App  release</string>
    <string name="app_name_debug">App debug</string>
    
    0 讨论(0)
  • 2020-11-29 20:28

    You can do this with gradle:

    android {
        buildTypes {
            release {
                manifestPlaceholders = [appName: "My Standard App Name"]
            }
            debug {
                manifestPlaceholders = [appName: "Debug"]
            }
        }
    }
    

    Then in your AndroidManifest.xml put:

    <application
        android:label="${appName}"/>
        <activity
            android:label="${appName}">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    Note: it also works with productFlavors.

    0 讨论(0)
  • 2020-11-29 20:32

    You can use strings.xml in different folders, see Android separate string values for release and debug builds.

    So, create this file:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Your app name</string>
    </resources>
    

    Then paste it to app\src\debug\res\values\ and app\src\release\res\values\ folders. Replace "Your app name" in debug and release files. Remove app_name item from strings.xml in app\src\main\res\values\ folder.

    In AndroidManifest you will have the same

    <application
        android:label="@string/app_name"
        ...
    

    No changes at all. Even if you added a library with it's AndroidManifest file and strings.xml.

    0 讨论(0)
  • 2020-11-29 20:38

    If by "app name", you mean android:label on <application>, the simplest solution is to have that point at a string resource (e.g., android:label="@string/app_name"), then have a different version of that string resource in a src/debug/ sourceset.

    You can see that in this sample project, where I have a replacement for app_name in src/debug/res/values/strings.xml, which will be applied for debug builds. release builds will use the version of app_name in src/main/.

    0 讨论(0)
  • 2020-11-29 20:38

    There are multiple ways you can do.

    you can create manifestPlaceholders OR resValue in app level build.gradle. e.g.

    buildTypes {
         release {
              ...
              manifestPlaceholders = [appLabel: "My App"]
              //resValue "string", "appLabel", '"My App"'
         }
         debug {
              ...
              manifestPlaceholders = [appLabel: "My App - Debug"]
              //resValue "string", "appLabel", '"My App - Debug"'
         }
    }
    

    OR

    If you have productFlavors, you can create there

    flavorDimensions "env"
    productFlavors {
        dev {
            dimension "env"
            ...
            manifestPlaceholders = [appLabel: "My App - Development"]
            //resValue "string", "appLabel", '"My App - Development"'
        }
        prod {
            dimension "env"
            ...
            manifestPlaceholders = [appLabel: "My Awesome App"]
            //resValue "string", "appLabel", '"My Awesome App"'
        }
    }
    

    Then in AndroidManifest.xml if you are using manifestPlaceholders, just change android:label="${appLabel}" as below OR if you are using resValue, just change android:label=@string/appLabel

    <application
        ...
        android:label="${appLabel}"> //OR `android:label=@string/appLabel`
        
        <activity
            ...
            android:label="${appLable}">
    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    NOTE: Make sure to change android:lable as well in <activity> of LAUNCHER category. If it doesn't require to use android:label in <activity>, just remove this.


    If you do not want to add in build.gradle directly, you can add in values/string.xml of selected ProductFlavors. e.g.

    Add

    <string name="appLabel">My App - Development</string> 
    

    in app/src/dev/res/values/string.xml

    and

    <string name="appLabel">My Awesome App</string> 
    

    in app/src/prod/res/values/string.xml

    0 讨论(0)
提交回复
热议问题