How to avoid two flavours of an app opening the same app?

308 Views Asked by At

I have different flavours configured for my application. When I install both flavours on the same device there is a huge problem.

When I open App1(Flavour 1) and then minimise it(Click on Home button) and try to open App2(Flavour 2) it opens App1(Flavour1). In order to open App2 I have to kill App1 from recents and then open App2. This happens vice versa too.

I tried using different flavour dimensions too. Here is my current configuration.

android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
flavorDimensions("default")
defaultConfig {
    applicationId "com.xx.aa"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
lintOptions {
    disable 'RestrictedApi'
}

productFlavors {
    aa {
        applicationId "com.xx.aa"
        versionCode 12
        versionName "1.0.1"
        manifestPlaceholders = [
                appName: 'aa',
                appId  : 'com.xx.aa'
        ]
        buildConfigField "boolean", "XXXX", "false"
    }
    bb {
        applicationId "com.xx.bb"
        versionCode 2
        versionName "1.2.3"
        manifestPlaceholders = [
                appName: 'bb',
                appId  : 'com.xx.bb'
        ]
        buildConfigField "boolean", "XXXX", "true"
    }
}

My Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xx.aa">

<!-- few permissions-->
<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

<application
    android:name=".global.aaApplication"
    android:allowBackup="false"
    android:fullBackupContent="false"
    android:icon="@drawable/ic_launcher_default"
    android:label="${appName}"
    android:roundIcon="@drawable/ic_launcher_default"
    android:supportsRtl="true"
    android:theme="@style/AppCompactTheme">
    <activity
        android:name=".activity.SplashActivity"
        android:screenOrientation="portrait"/>
.
.
.

</application>
</manifest>
3

There are 3 best solutions below

1
Archana On

Remove applicationId "com.xx.aa" from the defaultConfig. You don't have to specify here when you are using productFlavors.

1
khcpietro On

I don't know what's your exact objective, below task will stop your app before launch.

task stopApp(type: Exec) {
    android.applicationVariants.all { variant ->
        android.productFlavors.all { flavor ->
            if (variant.flavorName == flavor.name) {
                def applicationId = [variant.mergedFlavor.applicationId, flavor.applicationIdSuffix].findAll().join()
                def command = ['adb', 'shell', 'am', 'force-stop', applicationId]
                commandLine command
            }
        }
    }
}

Add this task to your Run Configuration > Before launch.

Or simply (if it's OK) add uninstallAll task.

enter uninstall gradle task

3
Miguel Isla On

In your manifest you are specifying "com.xx.aa" as your package. This can cause problems between both or your apps (aa and bb). Also remove it from your defaultConfig.

I have an app with several flavours all with the same manifest package but in order to install them all in the same device I change applicationIdSuffix parameter and all the app can be used at the same time.