How to install multiple APK of the same app without switching the user?

While developing one of my app, I felt the need of one necessary requirement which is to install two or more instances of the same app in your Android phone without creating a new user, pointing to different servers.

But before we move on to understanding how to install multiple variants of the same app in your phone, let’s see what is Application ID in Android build system.

According to Android Developers Blog, “Every Android app has a unique application ID that looks like a Java package name, such as com.example.myapp. This ID uniquely identifies your app on the device and in Google Play Store.”

Your application ID is defined with the applicationId property in your module’s build.gradle file, as shown here:

android {
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    ...
}

Here is the crack!

So, basically if we define multiple applicationId  according to different instances, we will land up having multiple APKs of the same app in the same device without changing or switching the user. 😉

Now the question arrises, how to define multiple applicationId of the same application?

The answer is simple, through different build variants.

When you build an APK for your app, the build tools tag the APK with the application ID defined in the defaultConfig block from the build.gradle file (as shown below). However, if you want to create different versions of your app to appear as separate listings on Google Play Store, such as a “free” and “pro” version, you need to create separate build variants that each have a different application ID.

In this case, each build variant should be defined as a separate product flavor. For each flavor inside the productFlavors block, you can redefine the applicationId property, or you can instead append a segment to the default application ID using applicationIdSuffix, as shown here:

android {
    defaultConfig {
        applicationId "com.example.myapp"
    }
    productFlavors {
        free {
            applicationIdSuffix ".free"
            resValue "string", "app_name", "My Free App"
        }
        pro {
            applicationIdSuffix ".pro"
            resValue "string", "app_name", "My Pro App"
        }
    }
}

This way, the application ID for the “free” product flavor is “com.example.myapp.free” and for “pro” it will be “com.example.myapp.pro” because the  applicationId will be suffixed by “free” and “pro” (by using applicationIdSuffix) respectively and so as the  applicationId  differs due to this suffix, there will be two instances of the same app in your device pointing to different environments!

Also, one thing to note, the two apps will have different app names as well. The “free” version will be named as “My Free App” and “pro” app will be named as “My Pro App”. Just make sure you remove the “app_name” from strings.xml file from where your application name comes because we are defining the resource value of “string” with the name “app_name” as “My Free App” and “My Pro App”.

Basically you can define almost any resource value in your gradle file and it will configure your app based on the build variants.

If you want to switch between different servers then the code below will might help you:

flavorDimensions "default"
productFlavors {
    STAGE {
        dimension "default"
        buildConfigField 'String', 'API_BASE_URL', "\"http://development.adhishlal.com/\""
        buildConfigField 'boolean', 'ISPRODUCTION', "false"
        applicationIdSuffix ".dev"
        resValue "string", "app_name", "My Free App"
    }
    PRODUCTION {
        dimension "default"
        buildConfigField 'String', 'API_BASE_URL', "\"http://adhishlal.com/\""
        buildConfigField 'boolean', 'ISPRODUCTION', "true"
        resValue "string", "app_name", "My Pro App"

    }

}

PS: Flavor Dimensions allow us to define groups of flavors. Gradle creates every combination between these groups for us.

Hope you like this blog. If so, do not forget to share it with your fellow Android Developer. 🙂