Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create separate apk for separate flavor in android

I have used build.gradle(app) to create different flavors of apk. But installing different flavors of same apk overrides the previous one. I want to create different apks to run on same device simultaneously. I want to create different apk with different appicon which can be installed on same device and run simultaneously. Any link or tutorial or direct help is appreciated.

Thanks in advance.

like image 641
SimpleCoder Avatar asked Oct 14 '15 13:10

SimpleCoder


People also ask

How do you make flavors on Android?

To write flavor specific code, create the folder with the same name as the flavor. The java classes with the same name in the flavor folders won't override the main folder. The res folder in the main should only have those directories that are common to all flavors.

What is split config APK?

Split APKs are very similar to regular APKs — they include compiled DEX bytecode, resources, and an Android manifest. However, the Android platform is able to treat multiple installed split APKs as a single app.

What is build variant in Android?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

What is the difference between APK and AAB?

AAB is a container that hosts a base APK and multiple split-APKs. Basically, AAB is a publishing format that a developer submits to the Play Store while APK is the packaging format for Android apps that you install on your device.


3 Answers

Change the PackageName of the flavor

Sample Gradle File

apply plugin: 'com.android.application'

android {

    lintOptions {
        abortOnError false
    }


    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 16
    }

    buildTypes {
        debug {
            minifyEnabled false
            zipAlignEnabled true
        }
        release {
            minifyEnabled true
            zipAlignEnabled true
        }
    }
    productFlavors {
        Flavor1 {
            applicationId "com.falvor.one" //This is where you change the package name
        }
        Flavor2 {
            applicationId "com.falvor.two"
        }
    }
}

Flavor Hierarchy in Android

- src/main/java
- src/flavor1
--------------Java
----------------Your java files
--------------res
----------------Drawable
  • src/flavor2/java

For more understanding, follow this link

like image 187
Murtaza Khursheed Hussain Avatar answered Sep 29 '22 09:09

Murtaza Khursheed Hussain


You need to create new productFlavors in your gradle file, like this;

productFlavors {
        Flavor1 {
            applicationId 'com.project.fl1'
            signingConfig signingConfigs.xx
            versionCode 1
        }
        Flavor2 {
            applicationId 'com.project.fl2'
            signingConfig signingConfigs.xx
            versionCode 1
        }
        Flavor3 {
            applicationId 'com.project.fl3'
            signingConfig signingConfigs.xx
            versionCode 1
        }
}

The important thing here is to give each one a unique applicationId, they can then be installed on the same phone.

like image 31
vguzzi Avatar answered Sep 29 '22 10:09

vguzzi


This post explains exactly how to achieve what you want step by step.

Most importantly:

  1. add the product flavours container to the app build.gradle file

    productFlavors { free { applicationId "antoniocappiello.com.buildvariantsexample.free" } paid { applicationId "antoniocappiello.com.buildvariantsexample.paid" } }

  2. create inside src a directory with the exact name of the product flavour that you want to look different from the main variant, for example with the configuration at step 1 the directory name could be paid or free . And inside that directory create the subfolder res/drawable where you are going to place your new app launcher icon.

Directory structure example

like image 43
Antonio Cappiello Avatar answered Sep 29 '22 10:09

Antonio Cappiello