【问题标题】:Different app names for different build flavors in Android?Android中不同构建风格的不同应用名称?
【发布时间】:2018-04-06 10:16:10
【问题描述】:

我有 2 种构建风味,例如风味 1 和风味 2

我希望我的应用程序在为风味 1 构建时命名为“AppFlavor1”,在为风味 2 构建时命名为“AppFlavor2”

这是我的清单源的 sn-p

<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文件的源sn-p

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中默认值为

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

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

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

【问题讨论】:

标签: android gradle android-gradle-plugin


【解决方案1】:

只需添加字符串

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

【讨论】:

  • 感谢您的关注。我没有使用 flavor1/res 和 flavor2/res 文件夹
  • @BekaKK 当然,您必须使用风味名称,而不是风味 1、风味 2。只需创建这些文件夹。否则你可以用 gradle 做同样的事情,检查更新的答案
  • 我没有正确理解你的答案,你能告诉我gradle source的sn-p吗? @Gabriele Mariotti
  • @BekaKK 您的评论不清楚。您可以通过多种方式做到这一点。例如,只需为不同的风格使用不同的 strings.xml(唯一不同的字符串不是所有文件),或者使用 build.gradle 中的 resValue 属性。两者都在答案中进行了描述。
  • 是的,别忘了删除strings.xml中的app_name
【解决方案2】:

除了 Gabriele Mariotti 的回答之外,请确保在您的 AndroidManifest.xml 文件中将 application / android:label 属性的值设置为 @string/app_name

【讨论】:

  • ...它甚至可以是不同的语言文件。
【解决方案3】:

删除&lt;string name="app_name"&gt;app_name_string&lt;/string&gt;

【讨论】:

  • 我删除了它,但是当我尝试运行项目时,项目包含错误@Bek
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-09
相关资源
最近更新 更多