Different application names for different build options?

I have 2 build aromas, say flavor1 and flavor2.

I want my app to be named, say, “AppFlavor1” when I create for flavor1 and “AppFlavor2” when I create for flavor 2.

This is not the name of the activity I want to change. I want to change the name of the application that appears in the phone menu and elsewhere.

From build.gradle I can set various parameters for my tastes, but it seems not for the app label. And I also cannot change the application label programmatically based on some variable.

So how do people deal with this?

+61
android gradle
Nov 07 '13 at 8:21
source share
9 answers

Instead of changing the main strings.xml file with a script and risk running your original control, why not rely on the standard merge of Android Gradle build behavior?

My build.gradle contains

 sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } release { res.srcDir 'variants/release/res' } debug { res.srcDir 'variants/debug/res' } } 

So now I can define the string app_name in variants/[release|debug]/res/strings.xml . And all that I want to change, too!

+16
Nov 15 '13 at 8:55
source share

Remove username from strings.xml (else gradle will complain about duplication of resources) Then modify the assembly file as follows:

 productFlavors { flavor1{ resValue "string", "app_name", "AppFlavor1" } flavor2{ resValue "string", "app_name", "AppFlavor2" } } 

This is less damaging than creating a new strings.xml file in different built-in sets or creating your own scripts.

I have a production application using this method

https://play.google.com/store/apps/details?id=co.getintouch.play with the application name "InTouch Messenger: Premium Edition"

https://play.google.com/store/apps/details?id=co.getintouch with the application name "InTouch Messenger"

 productFlavors { main{ resValue "string", "app_name", "InTouch Messenger" } googlePlay{ resValue "string", "app_name", "InTouch Messenger: GPE Edition" } } 

Remember to remove app_name from strings.xml!

+175
Jun 16 '15 at 22:35
source share

You can add a string resource file for each flavor, and then use these resource files to change the name of your application. For example, in one of my applications I have a free and paid version. To rename them "Lite" and "Pro", I created the meta_data.xml file and added my app_name value to this XML and removed it from strings.xml . Then in app/src create a folder for each flavor (see below, for example, structure). Inside these directories add res/values/<string resource file name> . Now, when you create, this file will be copied to your assembly, and your application will be renamed.

File structure:

 app/src /pro/res/values/meta_data.xml /lite/res/values/meta_data.xml 
+8
Dec 15
source share

Another option that I actually use is to change the manifest for each application. Instead of copying a resource folder, you can create a manifest for each flavor.

 sourceSets { main { } release { manifest.srcFile 'src/release/AndroidManifest.xml' } debug { manifest.srcFile 'src/debug/AndroidManifest.xml' } } 

You should have the main AndroidManifest in the src main directory, which will be the main one. Then you can define a manifest with only some parameters for each such taste (src / release / AndroidManifest.xml):

 <manifest package="com.application.yourapp"> <application android:icon="@drawable/ic_launcher"> </application> </manifest> 

To debug AndroidManifest (src / debug / AndroidManifest.xml):

 <manifest package="com.application.yourapp"> <application android:icon="@drawable/ic_launcher2"> </application> </manifest> 

The compiler will merge the manifest, and you may have an icon for each flavor.

+5
Sep 16 '15 at 18:03
source share

If you want to support localization of the application name in different ways, you can achieve it as follows:

1) Specify android:label in <application> available in AndroidManifest.xml as follows:

 <application ... android:label="${appLabel}" ... > 

2) Specify the default value for appLabel in the application level build.gradle :

manifestPlaceholders = [appLabel:"@string/defaultName"]

3) Cancel the product aroma value as follows:

 productFlavors { AppFlavor1 { manifestPlaceholders = [appLabel:"@string/flavor1"] } AppFlavor2 { manifestPlaceholders = [appLabel:"@string/flavor2"] } } 

4) Add a resource string for each of the strings (defaultName, flavor1, flavor2) in your strings.xml . This will allow you to localize them.

+3
Jun 16 '18 at 13:13
source share

First of all, answer this question: "Can the user install both versions of your application on the same device?"

I am using a Python script that corrects the source code. It contains some reusable functions and, of course, knowledge that needs to be fixed in this particular project. So the script is application dependent.

There are many fixes, the fix data is stored in the Python dictionary (including application package names, they are BTW different from the Java package name), one dictionary for each flavor.

As for l10n, lines can point to other lines, for example. in my code I have:

 <string name="app_name">@string/x_app_name_xyz</string> <string name="x_app_name_default">My Application</string> <string name="x_app_name_xyz">My App</string> 
+2
Nov 07. '13 at 8:32
source share

How can I make string / app_name different for each taste?

I wanted to write an update, but realized that it is more than the original answer saying that I am using a Python script that corrects the source code.

The Python script has a parameter, the name of the directory. This directory contains attributes for every taste, resources such as startup icons, and a properties.txt file using the Python dictionary.

 { 'someBoolean' : True , 'someParam' : 'none' , 'appTitle' : '@string/x_app_name_xyz' } 

The Python script loads the dictionary from this file and replaces the value between <string name="app_name"> and </string> the properties['appTitle'] value.

Below is the code based on as-is / as-was, etc.

 for strings_xml in glob.glob("res/values*/strings.xml"): fileReplace(strings_xml,'<string name="app_name">',properties['appTitle'],'</string>',oldtextpattern=r"[a-zA-Z0-9_/@\- ]+") 

to read properties from one or more of these files:

 with open(filename1) as f: properties = eval(f.read()) with open(filename2) as f: properties.update(eval(f.read())) 

and fileReplace function:

 really = True #False for debugging # In the file 'fname', # find the text matching "before oldtext after" (all occurrences) and # replace 'oldtext' with 'newtext' (all occurrences). # If 'mandatory' is true, raise an exception if no replacements were made. def fileReplace(fname,before,newtext,after,oldtextpattern=r"[\w.]+",mandatory=True): with open(fname, 'r+') as f: read_data = f.read() pattern = r"("+re.escape(before)+r")"+oldtextpattern+"("+re.escape(after)+r")" replacement = r"\g<1>"+newtext+r"\g<2>" new_data,replacements_made = re.subn(pattern,replacement,read_data,flags=re.MULTILINE) if replacements_made and really: f.seek(0) f.truncate() f.write(new_data) if verbose: print "patching ",fname," (",replacements_made," occurrence" + ("s" if 1!=replacements_made else ""),")",newtext,("-- no changes" if new_data==read_data else "-- ***CHANGED***") elif replacements_made: print fname,":" print new_data elif mandatory: raise Exception("cannot patch the file: "+fname+" with ["+newtext+"] instead of '"+before+"{"+oldtextpattern+"}"+after+"'") 

The first lines of the script:

 #!/usr/bin/python # coding: utf-8 import sys import os import re import os.path import shutil import argparse import string import glob from myutils import copytreeover 
0
Nov 08 '13 at 11:44
source share

This can be easily done in buildTypes

 buildTypes { debug { buildConfigField("String", "server_type", "\"TEST\"") resValue "string", "app_name", "Eventful-Test" debuggable true signingConfig signingConfigs.debug_key_sign } stage { buildConfigField("String", "server_type", "\"STAGE\"") resValue "string", "app_name", "Eventful-Stage" debuggable true signingConfig signingConfigs.debug_key_sign } release { buildConfigField("String", "server_type", "\"PROD\"") resValue "string", "app_name", "Eventful" minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //TODO - add release signing } } 

Just remove app_name from strings.xml

0
May 25 '18 at 20:38
source share

In the AndroidManifest file in the application tag, you have this line:

 android:label 

And there you can tell how the application’s label appears in the application’s menu on the device

-3
Nov 07 '13 at 8:58
source share



All Articles