React Native-如何使productFlavors具有不同的applicationId?

时间:2019-06-17 13:15:14

标签: android react-native build.gradle android-productflavors android-flavordimension

我有一个RN代码,我想在Android上有2种口味。这些风味在以下方面有所不同:

  1. ic_launcher和可绘制
  2. 字符串
  3. applicationId

我在app / build.gradle文件中添加了产品口味,还为每个口味添加了一个目录res&AndroidManifest.xml。

我专门使用了Library的这一部分this。并成功完成了前两项。 至于 applicationId ,每当我更改productFlavors部分中的applicationId字段时,都会出现此错误:

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

我使用以下命令来运行应用程序:

  react-native run-android --variant flavortoneDebug

   react-native run-android --variant flavorttwoDebug

我尝试更改每种口味的清单文件中的package =“”字段,但这给了我这个错误:

       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"
                    }

目录按以下顺序:

 -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>

这是app / build.gradle代码:

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'
}
}

我搜索了其他库,也搜索了stackoverflow和其他网站,但找不到解决方案。

我非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试react-native run-android --variant flavortoneDebug --appIdSuffix flavorone