8

I have 2 build flavors, say, flavor1 and flavor2

I would like my application to be named, say, "AppFlavor1" when I build for flavor1 and "AppFlavor2" when I build for flavor 2

Here is a my manifest source's snippet

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

Here is a gradle file's source snippet

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

In string.xml default value is

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

When I try to build both flavors realize version, the app name is always app_name_string (String from string.xml file)

How do i solve this problem? thanks

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
BekaKK
  • 2,173
  • 6
  • 42
  • 80

3 Answers3

10

Just add the string

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

in the folders:

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

You can also add resource values directly to your build.gradle file

productFlavors { 
    photographer_prod {
      resValue "string", "app_name", "the app name.."
    }
    agent_prod {
      resValue "string", "app_name", "the app name.."
    }
  }
Boken
  • 4,825
  • 10
  • 32
  • 42
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks your attentions. I'm not using flavor1/res and flavor2/res folders – BekaKK Apr 06 '18 at 10:33
  • @BekaKK of course you have to use the name of your flavors, instead of flavor1, flavor2. Just create these folders. Otherwise you can do the same with gradle, check the updated answer – Gabriele Mariotti Apr 06 '18 at 10:35
  • I did not understand your answer correctly ,Can you show me gradle source's snippet ? @Gabriele Mariotti – BekaKK Apr 06 '18 at 10:36
  • @BekaKK your comment is not clear. You can do it in many ways. For example just use different strings.xml for the different flavors (the only different strings not all the file), or use the resValue property in build.gradle. Both are described in the answer. – Gabriele Mariotti Apr 06 '18 at 10:38
  • resValue use two parameters ,fist is app name and what's a second ? @Gabriele Mariotti – BekaKK Apr 06 '18 at 10:39
  • @BekaKK the last parameter is the value of the string in the xml. In your case the name of the app in the flavor. – Gabriele Mariotti Apr 06 '18 at 10:42
  • resValue "string", "app_name", "my_app_name here in Flavors1..", resValue "string", "app_name", "my_app_name here in Flavors2.." .. Right ? @Gabriele Mariotti – BekaKK Apr 06 '18 at 10:44
  • 3
    Yes and don't forget to remove app_name in `strings.xml` – Bek Apr 06 '18 at 10:49
3

In addition to Gabriele Mariotti's answer, make sure application / android:label attribute's value is set to @string/app_name in your AndroidManifest.xml file.

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
rockneverdies
  • 180
  • 1
  • 7
2

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

Bek
  • 7,790
  • 4
  • 18
  • 31