programing tip

Gradle 빌드 유형별로 앱 이름을 변경하는 방법

itbloger 2020. 7. 13. 21:38
반응형

Gradle 빌드 유형별로 앱 이름을 변경하는 방법


빌드 유형마다 응용 프로그램의 앱 이름을 gradle로 변경할 수있는 방법을 찾으려고합니다.

예를 들어 디버그 버전 <APP_NAME>-debug과 qa 버전을 갖고 싶습니다 <APP-NAME>-QA.

나는 익숙하다 :

debug {
        applicationIdSuffix '.debug'
        versionNameSuffix '-DEBUG'
}

그러나 실행기에있을 때 앱 변경 사항을 적용하는 gradle 명령을 찾을 수없는 것 같습니다.


"앱 이름"으로 android:labelon 을 의미 하는 <application>경우 가장 간단한 해결책은 해당 지점을 문자열 리소스 (예 :) android:label="@string/app_name"에두고 src/debug/소스 세트 에 해당 문자열 리소스의 다른 버전을 갖는 것 입니다.

이 샘플 프로젝트 에서 in에 대한 대체품이 app_name있는 빌드 프로젝트src/debug/res/values/strings.xml적용됩니다 debug. release의 버전을 사용하는 빌드 app_name의를 src/main/.


이런 식으로 사용할 수 있습니다

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

AndroidManifest.xml 파일에서 @ string / app_name사용할 수 있습니다 .

값 / 폴더에서 app_name 을 제거하십시오 (이 이름으로 항목이 없음).


gradle 로이 작업을 수행 할 수 있습니다.

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

그런 다음에 AndroidManifest.xml넣어 :

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

참고 : 또한 작동합니다 productFlavors.


번역을 지원하려면 다음을 수행하십시오.

1. 문자열 "app_name"을 제거하십시오.

2. 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. 매니페스트의 앱 이름을 "@ string / app_name"으로 설정

4. strings.xml 값에 추가

<string name="app_name_admin">App Admin</string>
<string name="app_name_release">App  release</string>
<string name="app_name_debug">App debug</string>

The app name is user-visible, and that's why Google encourages you to keep it in your strings.xml file. You can define a separate string resource file that contains strings that are specific to your buildTypes. It sounds like you might have a custom qa buildType. If that's not true, ignore the qa part below.

└── src
    ├── debug
    │   └── res
    │       └── buildtype_strings.xml
    ├── release
    │   └── res
    │       └── buildtype_strings.xml
    └── qa
        └── res
            └── buildtype_strings.xml

We need a solution to support app name with localization (for multi language). I have tested with @Nick Unuchek solution, but building is failed (not found @string/) . a little bit change to fix this bug: build.gradle file:

android {
    ext{
        APP_NAME = "@string/app_name_default"
        APP_NAME_DEV = "@string/app_name_dev"
    }

    productFlavors{

        prod{
            manifestPlaceholders = [ applicationLabel: APP_NAME]
        }

        dev{
            manifestPlaceholders = [ applicationLabel: APP_NAME_DEV ]
        }

    }

values\strings.xml:

<resources>
    <string name="app_name_default">AAA prod</string>
    <string name="app_name_dev">AAA dev</string>

</resources>

values-en\strings.xml:

<resources>
    <string name="app_name_default">AAA prod en</string>
    <string name="app_name_dev">AAA dev en</string>

</resources>

Manifest.xml:

<application
    android:label="${applicationLabel}" >
</application>

For a more dynamic gradle based solution (e.g. set a base Application name in main's strings.xml once, and avoid repeating yourself in each flavor / build type combination's strings.xml), see my answer here: https://stackoverflow.com/a/32220436/1128600


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.


As author asks to do this in Gradle, we can assume he want to do it in the script and not in the configuration files. Since both Android Studio and Gradle has been heavily updated and modified in the last year (~2018) all other answers above, seem overly contorted. The easy-peasy way, is to add the following to your app/build.gradle:

android {
    ...
    buildTypes {
        ...
        // Rename/Set default APK name prefix (app*.apk --> AwesomeApp*.apk)
        android.applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def appName = "AwesomeApp"
                outputFileName = appName+"-${output.baseName}-${variant.versionName}.apk"
        }
    }
}

참고URL : https://stackoverflow.com/questions/24785270/how-to-change-app-name-per-gradle-build-type

반응형