android - Android 中不同构建风格的不同应用程序名称?

标签 android gradle android-gradle-plugin

我有 2 种构建风格,比如 flavor1 和 flavor2

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

这是我的 list 来源的 fragment

<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 文件的源代码 fragment

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 文件的字符串)

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

最佳答案

只需添加字符串

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

关于android - Android 中不同构建风格的不同应用程序名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690518/

相关文章:

android - 什么时候会重新添加动态添加的 fragment ?

java - Gradle 无法使用 JavaFX 插件从 src/test 访问模块 src/main 中定义的类

android - 通过 Android Studio 运行单元测试第一次工作然后失败

android-从数据库显示数据到 ListView 并每次刷新

Android AppWidget onClick 在垃圾收集后无法正常工作

java - gradle 中的原生依赖

gradle - Gradle测试任务的简单克隆未在IntelliJ中识别为测试

android - Gradle中各种写依赖的方式有什么区别?

Android 数据绑定(bind)编译警告 : Method references using '.' is deprecated

android - 搜索 Android 存储中所有音乐文件(或特定扩展名)的 Activity