【问题标题】:Change to androidX in react-native在 react-native 中更改为 androidX
【发布时间】:2019-06-18 14:29:47
【问题描述】:

我正在尝试将我的 react-native 项目更改为 androidx。 我想这样做是因为我在尝试运行我的项目时突然遇到了这个错误:

com.android.build.api.transform.TransformException: Error while generating the main dex list.

我正在阅读有关该问题的一些帖子,并尝试添加这些行:

android.useAndroidX=true
android.enableJetifier=true

因为当我使用堆栈跟踪时,我收到了这个错误:

Program type already present: androidx.versionedparcelable.CustomVersionedParcelable react native

我将此行添加到我的 local.propeties 文件中,但是当我这样做时,我发现我需要更改所有 node-module 文件以使用 androidx

此外,我试图将包 react-native-device-info 更新到最新版本 2.1.2,但结果仍然相同...

我的 react 原生版本:

react-native-cli: 2.0.1
react-native: 0.50.4

我正在附加我的文件:

app/build.gradle:

apply plugin: "com.android.application"

 buildscript {
   repositories {
     maven { url 'https://maven.fabric.io/public' }
   }
   dependencies {
     // The Fabric Gradle plugin uses an open ended version to react
     // quickly to Android tooling updates
     classpath 'io.fabric.tools:gradle:1.+'
   }
 }
 apply plugin: 'io.fabric'
 repositories {
   maven { url 'https://maven.fabric.io/public' }
 }
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 28
    buildToolsVersion "28.0.3"
    dexOptions {
            javaMaxHeapSize "4g"
        }
    defaultConfig {
        applicationId "com.x"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 14
        versionName "2.3.0"
        multiDexEnabled true
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.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]
            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 {
    compile project(':react-native-orientation')
    compile project(':react-native-device-information')
    compile project(':react-native-ble-manager')
    compile project(':react-native-fabric')
    compile project(':react-native-vector-icons')
    compile project(':react-native-spinkit')
    compile project(':react-native-push-notification')
    compile project(':react-native-picker')
    compile project(':react-native-linear-gradient')
    compile project(':react-native-image-crop-picker')
    compile project(':react-native-firebase')
    compile project(':react-native-fcm')
    compile project(':react-native-config')
    implementation project(':react-native-picker')
    implementation project(':react-native-device-info')
    implementation project(':react-native-fcm')
    implementation project(':react-native-firebase')
    implementation project(':react-native-svg')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-spinkit')
    implementation project(':react-native-push-notification')
    implementation project(':react-native-linear-gradient')
    implementation project(':react-native-image-crop-picker')
    implementation project(':react-native-config')
    implementation project(':react-native-picker-module')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.google.android.gms:play-services-gcm:15.0.1"
    implementation "com.android.support:appcompat-v7:28.0.0-rc02"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-messaging:17.0.0' //17.3.1
    implementation 'com.android.support:multidex:1.0.3'
    //implementation 'com.google.firebase:firebase-notifications'
    compile('com.crashlytics.sdk.android:crashlytics:2.9.2@aar') {
         transitive = true;
    }

}

// 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'
}
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

android/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:4.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

//com.android.ddmlib.DdmPreferences.setTimeOut(50000)

allprojects {
    repositories {
                // Add jitpack repository (added by react-native-spinkit)
                maven { url "https://jitpack.io" }
        mavenLocal()
        google()
        jcenter()
        // maven{
        //     url 'https://maven.google.com'
        // }
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }

    }
}

ext {
  compileSdkVersion = 28
  targetSdkVersion = 28
  buildToolsVersion = "28.0.3"
  supportLibVersion = "28.0.0-rc02"
  googlePlayServicesVersion = "15.0.1"
  oreoEXPERIMENTAL = "yes"
}

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 28
                buildToolsVersion '28.0.3'
            }
        }
    }
}


我的 androidManifest.xml 应用程序:

  <application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme"
      tools:replace="android:appComponentFactory"
      android:appComponentFactory="whateverString"
>

现在有什么办法可以解决这个问题吗?我突然无法运行我的项目

【问题讨论】:

  • 有人有解决办法吗?
  • 所以发生在我身上:-/
  • @AsifAli 你找到解决方案了吗?你知道为什么会这样吗?
  • 还没有@JJ Redikes,我认为一些内部依赖项无法用android x编译..需要单独查看每个依赖项
  • 许多库受到新版 google 服务的影响。对我们来说,它是 react-native-push-notification 和 react-native-device-info。由于您的 app/build.gradle 中有第一个依赖项,因此请在此处查看此解决方案:github.com/facebook/react-native/issues/…。这为我们解决了它

标签: android react-native androidx


【解决方案1】:

android/app/build.gradle 中的这些行为我修复了它(基于this GitHub comment)。 react-native-push-notification 依赖于导致失败的 androidx。

implementation(project(':react-native-push-notification')) {
    exclude group: 'com.google.android.gms'
}
implementation ("com.google.android.gms:play-services-gcm:16.1.0") {
    force = true
}
implementation ("com.google.android.gms:play-services-base:16.1.0") {
    force = true
}
implementation ("com.google.firebase:firebase-core:16.0.9") {
    force = true
}
implementation ("com.google.firebase:firebase-messaging:18.0.0") {
    force = true
}

如果您还没有 react-native-push-notification 依赖项,您可以检查您的 androidx 依赖项所在的位置:

cd android &amp;&amp; ./gradlew app:dependencies

(有关该方法的更多详细信息,请参阅this comment。)

【讨论】:

    【解决方案2】:

    根本原因是相关迁移到Androidx,google play服务更新为androidX

    如果您使用的是real-native-info,请升级。 2.1.2

    将解决您的问题
    yarn uprade react-native-info@2.1.2
    

    here apply link

    similar question这个问题

    similar question's second这个问题

    应用注意:将com.google.android.gms 升级到 17.0.0 会导致很多 问题。

    【讨论】:

    • 感谢我使用 npm 的回答,什么是 react-native-info 以及我应该执行的命令是什么?正如你在我的 gradle 文件中看到的那样,没有提到 react-native-info...
    • @JJRedikes yarn upgrade react-native-device-info@2.1.2cd android &amp;&amp; gradlew clean
    • 我需要保留以下行: android.useAndroidX=true android.enableJetifier=true 还是删除它们?不仅如此,我的 com.google.gms 版本是 4.1.0 而不是 17...
    • 删除那部分似乎不错。如果你需要这种语法,android.useAndroidX=true android.enableJetifier=false
    • 你不能做一个假,第二个是真的..它们必须是相同的值
    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多