首页 > 解决方案 > 运行 react-native 应用程序时显示:app:transformClassesWithMultidexlistForDebug'

问题描述

我有一个反应本机应用程序,其中所有依赖项都正常工作,但现在当我尝试运行该应用程序时,它给了我这个错误, com.android.build.api.transform.TransformException: Error while generating the main dex list.我一直在寻找很长时间,但没有发现任何有用的东西。

这是我的AndroidManifest文件:

<?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.movit_driver1">

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

        <application
          android:name=".MainApplication"
          android:label="@string/app_name"
          android:icon="@mipmap/ic_launcher"
          android:allowBackup="false"
          android:theme="@style/AppTheme"
          tools:replace="android:appComponentFactory"
          android:appComponentFactory="whateverString">
          
          <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" />
          <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="myapikey"/>
          <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
          <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
          </intent-filter>
          </service>
          <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
          <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
          </intent-filter>
          </service>

          <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher" />
          <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/notificationColor" />


          <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id"/>

            <receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
          <receiver android:enabled="true" android:exported="true"  android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
            <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED"/>
              <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
              <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
              <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
          </receiver>
            
        </application>

    </manifest>

这是我的安卓build gradle

        buildscript {
        ext {
            buildToolsVersion = "28.0.3"
            minSdkVersion = 16
            compileSdkVersion = 27
            targetSdkVersion = 26
            supportLibVersion = "27.1.1"
            multiDexEnabled = true
            googlePlayServicesVersion = "+" // default: "+"
            firebaseVersion = "+"
        }
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.2.0'
            classpath 'com.google.gms:google-services:4.2.0'

        }
    }

    task wrapper(type: Wrapper) {
        gradleVersion = '4.6'
        distributionUrl = distributionUrl.replace("bin", "all")
    }

    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'com.android.support'
                        && !details.requested.name.contains('multidex') ) {
                    details.useVersion "28.0.0"
                }
            }
        }
        afterEvaluate {
            android {
                compileSdkVersion 28
                buildToolsVersion "28.0.3"

                defaultConfig {
                    targetSdkVersion 28
                }
            }
        }
    }

最后是我的应用程序build gradle

  android {
        compileSdkVersion rootProject.ext.compileSdkVersion
        buildToolsVersion '28.0.3'

        defaultConfig {
            applicationId "com.movit_driver1"
            minSdkVersion rootProject.ext.minSdkVersion
            targetSdkVersion rootProject.ext.targetSdkVersion
            versionCode 1
            versionName "1.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 
                include "armeabi-v7a", "x86"
            }
        }
        buildTypes {
            release {
                minifyEnabled enableProguardInReleaseBuilds
                proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
                signingConfig signingConfigs.release
            }
        }
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def versionCodes = ["armeabi-v7a":1, "x86":2]
                def abi = output.getFilter(OutputFile.ABI)
                if (abi != null) { 
                    output.versionCodeOverride =
                            versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
                }
            }
        }
    }

    dependencies {
        compile project(':rn-fetch-blob')
        compile project(':react-native-vector-icons')
        compile project(':react-native-maps')
        compile project(':react-native-image-picker')
        compile project(':react-native-firebase')
        compile project(':react-native-document-picker')
        compile project(':react-native-device-info')
        implementation 'com.google.firebase:firebase-core:+'
        implementation "com.google.firebase:firebase-firestore:+"
        implementation "com.google.firebase:firebase-messaging:+"
        implementation "com.android.support:multidex:1.0.2"
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation "com.facebook.react:react-native:+" 
        implementation 'com.google.android.gms:play-services-base:+'
        implementation 'com.google.android.gms:play-services-maps:+'
        implementation 'com.android.support:appcompat-v7:+'
        implementation 'com.android.support:design:+'
    }

    task copyDownloadableDepsToLibs(type: Copy) {
        from configurations.compile
        into 'libs'
    }

当我尝试运行该项目时,它显示错误。所以,我是否必须将项目迁移到 androidx,因为我不想这样做。任何帮助将不胜感激。

标签: react-native

解决方案


我遇到了类似的问题,有相同的错误消息,但是我找不到确切的原因,但这是我所做的:

我已经使用 --stacktrace 执行了构建。此时很明显,问题出在 androidx 依赖项上。

gradlew -q dependencies yourProject:dependencies

只有一个包使用了 androidx 依赖项。更新了那个特定的包,随着更新,依赖关系消失了——恢复正常。

希望能帮助到你!


推荐阅读