Different activities in different flavors android studio

Published

I have the following build.gradle file where i declare the two versions of my project

defaultConfig {
        applicationId "com.company"
        minSdkVersion 21
        targetSdkVersion 31
        ...

}
flavorDimensions "version"
    productFlavors {
        create("product") {
            dimension "version"
            applicationIdSuffix ".product"
            versionCode 77
            versionName "1.11.21470"     
        }
        create("single_version") {
            dimension "version"
            applicationIdSuffix ".product.single_version"
            versionCode 8
            versionName "1.11.21453"
        }
    }


Now i have set the project structure as needed so the single_version can see specific layouts and other classes.

- Project
  - app
    - src
      --**main**
        -java folders
          -com.company.product
            -ui
            -model
              ...
              ...
          -res folders
          -AndroidManifest.xml

      --**single_version**
        -java folders
        -res folders

and the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.company.product">


    <uses-permission android:name="android.permission.INTERNET" />
    ....

    <application
        android:name=".NautilusApplication"
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        ....>
   

        <-- Declaring SplashActivity -->
        <activity
            android:name=".ui.splash.SplashActivity"
            android:theme="@style/NoActionBar"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <-- Declaring MainActivity -->
        <activity
            android:name=".ui.main.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustNothing" />

</application>

The project starts with a SplashScreen which then starts the MainActivity. SplashScreen is the same for both of flavors. However, i want each of the product and **single_version flavors to has its own MainActivity. Since that, i must create a new file in **single_version**/java which will be ui/MainActivity and a new AndroidManifest.xml file in the single_version.

However, i don’t know how to do the following 2 things

  1. What should i write in the new AndroidManifest.xml, meaning that should i declare all the other activities that are in the first AndroidManifest? And how do i declare a new one that has the same name as the main?

  2. How do i create an Intent in the SplashActivity which is common? Will the Android understand that in one case i want the com.company.product.ui.MainActivity and in the other case i want the com.company.product.single_version.ui.MainActivity?

Source: Android Questions

Published
Categorized as android, android-activity, android-manifest, android-productflavors, build.gradle

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq