在Android中为不同构建类型使用不同的应用程序名称?

8
我有两种构建风格,称为flavor1和flavor2。
当我构建flavor1时,我希望我的应用程序被命名为“AppFlavor1”,当我构建flavor2时,我希望我的应用程序被命名为“AppFlavor2”。
下面是我的清单源的片段。
<application
    android:name=".PApplication"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ui.activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

这是一个Gradle文件的源代码片段。
apply plugin: 'com.android.application'
    android {

        compileSdkVersion 25
        buildToolsVersion '26.0.2'
        flavorDimensions "default"
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 25
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }

        signingConfigs {
            release {
            }
        }

        buildTypes {
            release {
                minifyEnabled false
                signingConfig signingConfigs.release
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }

        productFlavors.whenObjectAdded { flavor ->
            flavor.ext.set("app_name", "")
        }

        productFlavors {
            photographer_prod {
                app_name = "flovor1name"
                buildConfigField "String", "API_URL", ""
                buildConfigField "boolean", "IS_PHOTOGRAPHER_APP", "true"
                applicationId "me.flovor.package_1"
                versionCode 1048
                versionName "1.0.48"
            }

            agent_prod {
                app_name = "flovor2name"
                buildConfigField "String", "API_URL", ""
                buildConfigField "boolean", "IS_PHOTOGRAPHER_APP", "false"
                applicationId "me.flovor.package_2"
                versionCode 1016
                versionName "1.0.16"
            }


        }

        applicationVariants.all { variant ->
            def flavor = variant.productFlavors*.name[0]
            def appName = variant.productFlavors*.app_name[0]

            variant.mergeResources.doLast {
                File valuesFile = file("${variant.mergeResources.outputDir}/values/values.xml")
                if (valuesFile.exists()) {
                    String content = valuesFile.getText('UTF-8')
                    content = content.replaceAll("app_name_string", appName)
                    valuesFile.write(content, 'UTF-8')
                } else {
                    println("File: " + valuesFile.path + " does not exist")
                }
            }

            if (variant.buildType.name == "debug") {
                return;
            }

            variant.outputs.each { output ->
                def SEP = "_"
                def buildType = variant.variantData.variantConfiguration.buildType.name
                def version = variant.versionName
                def date = new Date();
                def formattedDate = date.format('ddMMyy_HHmm')

                def newApkName = flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"

                output.outputFileName = new File(output.outputFile.parent, newApkName)
            }
        }
    }

在 string.xml 文件中,default value 是指默认值。
<string name="app_name">app_name_string</string>

当我尝试构建两种版本的实现时,应用程序名称始终为 app_name_string(来自 string.xml 文件的字符串)

我该如何解决这个问题?谢谢


简单的谷歌搜索给了我答案。这是链接 https://dev59.com/PWIj5IYBdhLWcg3w2odr - rusted brain
我已经阅读了它,但在我的情况下,我该如何解决问题? - BekaKK
3个回答

10

只需添加字符串

<string name="app_name">app_name_string</string>

在文件夹中:

app/src/photographer_prod/res/values/strings.xml
app/src/agent_prod/res/values/strings.xml

您还可以将资源值直接添加到您的build.gradle文件中。

productFlavors { 
    photographer_prod {
      resValue "string", "app_name", "the app name.."
    }
    agent_prod {
      resValue "string", "app_name", "the app name.."
    }
  }

@BekaKK 当然,你必须使用你的口味名称,而不是flavor1,flavor2。只需创建这些文件夹即可。否则,您可以使用gradle执行相同操作,请查看更新的答案。 - Gabriele Mariotti
我没有正确理解你的回答,你能给我展示一下Gradle源代码片段吗?@Gabriele Mariotti - BekaKK
@BekaKK,您的评论不够清晰。有很多种方法可以实现。例如,只需为不同的风味使用不同的strings.xml文件(仅包含不同的字符串而非整个文件),或在build.gradle中使用resValue属性。这两种方法都在答案中有描述。 - Gabriele Mariotti
@BekaKK 最后一个参数是XML中字符串的值。在你的情况下,它是flavor中应用程序的名称。 - Gabriele Mariotti
3
好的,记得在strings.xml中删除app_name - Bek
显示剩余3条评论

3
除了Gabriele Mariotti的回答之外,还要确保在您的AndroidManifest.xml文件中设置应用程序/ android:label 属性的值为@ string / app_name

...甚至可以在不同的语言文件中。 - Rickard Elimää

2

移除 <string name="app_name">app_name_string</string>


我已经将它移除了,但是当我尝试运行项目时,项目包含错误。 - BekaKK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接