How to create Product Flavor in Android Gradle (Free/Paid version of the app)

We will go through simple step by step example Android project, How to create Product Flavors in Android. I will try to explain following topics,

 

  1. I will create Free & Paid version of the app.
  2. Create different Label Text for Free & Paid.
  3. Create Different Activities for Free and Paid Apps.
  4. I will show you how to add dependency library for particular flavor only.
  5. Show and hide Google AdMob in Free & Paid Apps.

 

Create Free & Paid version of the App in build.gradle file

 

Create an Android App using Android Studio. Make sure you will have to create an Activity with a textview on it.

I have added a font file and set TypeFace for the textfield.

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.textView);

    Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/CevicheOne-Regular.ttf");
    textView.setTypeface(typeface);

}

 

and the activity_main.xml is

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.appsgit.productflavorexample.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:padding="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_value"
        android:gravity="center"
        android:textSize="40sp"
        android:layout_centerInParent="true"/>

</RelativeLayout>

 

If you run the App it should be like this.

 

 

 

Add the following groovy code in the build.gradle (App module)

productFlavors {
    free {
        applicationId "com.appsgit.productflavorexample.free"
    }

    paid {
        applicationId "com.appsgit.productflavorexample.paid"
    }
}

 

Your App module build.gradle file should be like below.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.appsgit.productflavorexample"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        free {
            applicationId "com.appsgit.productflavorexample.free"
        }

        paid {
            applicationId "com.appsgit.productflavorexample.paid"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

 

Now press “Sync” button in the top right in the yellow color bar.

 

 

 

The necessary Gradle build Source Set will be created now.

 

If you want to confirm, check the Build Variants table in the right side of the Android studio. it should be like below,

 

 

Build variants are created. Next we will create resource files for both Free and Paid,

 

Create different Label Text for Free & Paid Product flavors.

 

Right click on the “src” folder and select “new” -> “Android resource file”.  And type and fill the fields like below.

 

Select Source set as “paid” and create new one for “free” source set.

 

 

 

Create strings.xml for Free and Build Source Set. Your app structure should be like below now,

 

 

 

Note : Don’t forget that codes and Resource files in the “Main”  Source Folder will be shared among the Free and Paid Product flavors.

strings.xml file in the Free Source folder willbe like this.

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text_value">This is Free Activity..</string>
</resources>

 

strings.xml file in the Paid Source folder will be like this.

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text_value">This is Paid Activity..</string>
</resources>

 

Now Select the FreeDebug product’s build variants,

 

 

 

And run the project. You should see the screen like below.

 

 

 

 

Now select “PaidDebug”  like below

 

 

 

 

Now if you run the project. You should see the app like below.

 

 

 

 

 

You have learned how to share the resource files between Product favors.  Now we will see how to create variables specifically for product flavors.

 

Create (BuildConfig) variables in build.gradle file

 

productFlavors {
    free {
        applicationId "com.appsgit.productflavorexample.free"
        buildConfigField 'boolean', 'IS_PAID', 'false'
    }

    paid {
        applicationId "com.appsgit.productflavorexample.paid"
        buildConfigField 'boolean', 'IS_PAID', 'true'
    }
}

 

You can access this in MainActivity.java like below.

 

package com.appsgit.productflavorexample;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.textView);

        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/CevicheOne-Regular.ttf");
        textView.setTypeface(typeface);
        
        if (BuildConfig.IS_PAID) {
            //code willbe he for paid apps.
        } else {
          //logic for free apps...
        }

    }
}

 

 

 

I will continue the same post with some more information about Product flavor in the near future.

 

You can download the source code from here 

 

Cheers…!

 

 

About Zumry

Zumry Mohamed

Self Taught iOS & Android Mobile Application Developer.

Article written by zumrywahid

Self Taught iOS & Android Mobile Application Developer.

Be the first to comment

Leave a Reply

Your email address will not be published. Required fields are marked *