我有一个 RN 代码,我想在 Android 上有 2 种口味。这些口味的不同之处在于:

  • ic_launcher & drawable
  • 字符串
  • applicationId

  • 我在 app/build.gradle 文件中添加了 product flavors,并为它们中的每一个添加了一个目录 res & AndroidManifest.xml。

    我使用了这个 Library 专门的 this 部分。并成功完成了前 2 项。
    至于 applicationId ,每当我更改 productFlavors 部分中的 applicationId 字段时,它都会给我这个错误:
     `react native error: activity class {mainactivity} does not exist.`
    

    我使用这些命令来运行应用程序:
      react-native run-android --variant flavortoneDebug
    
       react-native run-android --variant flavorttwoDebug
    

    我试图更改每个 flavor 的 list 文件中的 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 和其他网站,但找不到解决方案。

    我真的很感激你的帮助。

    最佳答案

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

    关于android - React Native - 如何让 productFlavors 具有不同的 applicationIds?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56632140/

    10-11 14:25