Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - How to have productFlavors with different applicationIds?

I have a RN code and I want to have 2 flavors on Android. These flavors are different in:

  1. ic_launcher & drawable
  2. strings
  3. applicationId

I added to product flavors in the app/build.gradle file and also added a directory res & AndroidManifest.xml for each of them.

I used this Library specially this part. and successfully done the first 2 items. As for applicationId, whenever i change the applicationId field in productFlavors part it give me this error:

 `react native error: activity class {mainactivity} does not exist.`

I use these commands for running the app:

  react-native run-android --variant flavortoneDebug

   react-native run-android --variant flavorttwoDebug

I tried to change the package="" field in each flavor's manifest file but it gave me this error:

       Manifest merger failed : Overlay manifest:package attribute declared at 
 AndroidManifest.xml:2:11-38 value=(com.examle.flavorone)
            has a different value=(com.examle.flavorone) declared in main manifest at AndroidManifest.xml:2:3-24
             Suggestion: remove the overlay declaration at AndroidManifest.xml       and place it in the build.gradle:
                    flavorName {
                            applicationId = "com.examle.flavorone"
                    }

Directories are in this order:

 -example 
  -android
     ---
     -app
        ---
        -src
           -flavorone
             -res
             -AndroidManifest.xml
           -flavortwo
              -res
              -AndroidManifest.xml
           -main
              -java
              -res
               -AndroidManifest.xml
           -debug
                -AndroidManifest.xml

  -ios
  -node_modules
  -src

main/AndroidManifest.xml

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

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

<application
 android:name=".MainApplication"
 android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
 android:roundIcon="@mipmap/ic_launcher"
 android:allowBackup="false"
 android:theme="@style/AppTheme">
 <activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
 <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" 
 />
 </application>

  </manifest>

flavorone/AndroidManifest.xml & flavortwo/AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example">
 <application
    android:supportsRtl="true">
 </application>
 </manifest>

here is app/build.gradle code:

apply plugin: "com.android.application"

project.ext.envConfigFiles = [
dev: ".env",
flavorone:".env.flavorone",
flavortwo:".env.flavortwo"
]
 apply from: project(':react-native-config').projectDir.getPath() + 
 "/dotenv.gradle"
 import com.android.build.OutputFile
 project.ext.react = [
  entryFile: "index.js"
 ]
apply from: "../../node_modules/react-native/react.gradle"
def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false
android {
compileSdkVersion rootProject.ext.compileSdkVersion

 compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
 }
defaultConfig {
    applicationId   "com.example"
    minSdkVersion rootProject.ext.minSdkVersion
    resValue "string", "com.example.flavorone", "com.example"
    resValue "string", "com.example.flavortwo", "com.example"
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode 3
    versionName "3.0"

}

  flavorDimensions "default"
  productFlavors {
             flavorone{
                 applicationId "com.example.flavorone"
                 resValue "string", "com.example.flavorone", 
         "com.example"
                  minSdkVersion rootProject.ext.minSdkVersion
                  targetSdkVersion rootProject.ext.targetSdkVersion
                  versionCode 3
                  versionName "3.0"
                  dimension "default"
                  }

            flavortwo{
                applicationId "com.example.flavortwo"
                resValue "string", "com.example.flavortwo", 
          "com.example"
                minSdkVersion rootProject.ext.minSdkVersion
                targetSdkVersion rootProject.ext.targetSdkVersion
                versionCode 3
                versionName "3.0"
                dimension "default"
            }
        }
 splits {
  abi {
    reset()
    enable enableSeparateBuildPerCPUArchitecture
    universalApk false  // If true, also generate a universal APK
    include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
   }
  }
buildTypes {
release {
    minifyEnabled enableProguardInReleaseBuilds
    proguardFiles getDefaultProguardFile("proguard-android.txt"), 
    "proguard-rules.pro"
 }
 }
// applicationVariants are e.g. debug, release
  applicationVariants.all { variant ->
  variant.outputs.each { output ->
    // For each separate APK per architecture, set a unique version code 
    as described here:
    // http://tools.android.com/tech-docs/new-build-system/user- 
    guide/apk-splits
    def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, 
   "x86_64": 4]
    def abi = output.getFilter(OutputFile.ABI)
    if (abi != null) {  // null for the universal-debug, universal- 
   release variants
        output.versionCodeOverride =
                versionCodes.get(abi) * 1048576 + 
  defaultConfig.versionCode
    }
   }
  }


dependencies {
implementation project(':react-native-config')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat- 
v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+"  // From node_modules
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
}

I've searched for other libraries and also I've searched stackoverflow and other websites but I couldn't find a solution.

I'd really appreciate the help.

like image 886
Hadis Avatar asked Jan 01 '23 21:01

Hadis


1 Answers

Try react-native run-android --variant flavortoneDebug --appIdSuffix flavorone

like image 116
fearmear Avatar answered Jan 14 '23 04:01

fearmear