Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build android with gradle, replace string each product flavor

Tags:

android

gradle

Before i build android project to two different application paid and free.

I changed each values and strings so yesterday I made a big mistake.

So, I'm laerning how to use gradle to build my app.

My app have some differents.

  1. app name (just add suffix '-Free') -> values/string.xml

  2. change flag in my *.java

// signingConfigs is ommited.

productFlavors{
    free{
        packageName "my.app.free"
        versionCode 20
        signingConfig signingConfigs.freeConfing

        copy{
            from('/res'){
                include '**/*.xml'
            }
            into 'build/res/'

            filter{
                String line -> line.replaceAll("android:label=\"@string/app_name\"", "android:label=\"@string/app_name_free\"")
            }
        }
        copy{
            from('/src'){
                include '**/*.java'
            }
            into 'build/src/'

            filter{
                String line -> line.replaceAll("public static final Boolean IS_FULL_VER = true;", "public static final Boolean IS_FULL_VER = false;")
            }
        }
    }
    paid{
        packageName "my.app.paid"
        versionCode 20
        signingConfig signingConfigs.paidConfing
    }
}

but, built app changed nothing at all.

What i missed?

like image 946
Flask_KR Avatar asked Oct 10 '13 12:10

Flask_KR


1 Answers

See the documentation on product flavors:

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors

In your build.gradle, in each flavor, you can define flags to be generated in your BuildConfig.java file:

productFlavors {
    free {
        packageName "com.company.appfree"
        buildConfig  "public final static com.company.common.MonetizationType monetizationType = com.company.common.MonetizationType.FREE;"
    }

    paid {
        packageName "com.company.apppaid"
        buildConfig  "public final static com.company.common.MonetizationType monetizationType = com.company.common.MonetizationType.PAID;"
    }
}

This example uses an enum (that you need to define somewhere in your java code):

public enum MonetizationType {
    PAID, FREE
}

You can now use this anywhere like this:

if (BuildConfig.monetizationType == MonetizationType.FREE) { ... } 

For overriding resources, you can create different resource files in the source folders for each flavor:

Use the following structure

app/build.gradle
app/ [.. some other files...]
app/src/main/
app/src/main/java
app/src/main/res
app/src/main/assets
app/src/main/AndroidManifest.xml


app/src/free/res/values/apptitle.xml
app/src/paid/res/values/apptitle.xml

apptitle.xml would be a string resource file (just like strings.xml), but with only one string: the one you want to be different depending on the flavor. (You don't need have a apptitle.xml in your main/res directory).

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <string name="app_title">App Title (or whatever you want)</string>
</resources>

You might be able to override strings in different ways, but I like to keep the overridden strings separate from the rest for clarity.

like image 196
treesAreEverywhere Avatar answered Sep 26 '22 12:09

treesAreEverywhere