gradle - 编译 React-native 重复条目 : javax/inject/Inject. 类时出错

标签 gradle react-native build.gradle gradlew

我从网上下载了一个名为 react-native-obd2 的 React-native 项目。 .在我开始之前,我只想运行 repo 中已经给出的示例,但是在运行 react-native run-android 时我陷入了一个错误。

:app:transformClassesWithJarMergingForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: javax/inject/Inject.class

我已经花了 3 个多小时来解决这个问题,但它不会去。

我尝试了各种解决方案,对我来说最合乎逻辑的解决方案之一是 javax/inject/Inject.class在某处被调用两次。

所以我输入了这个命令来获取 gradle 类路径。./gradlew -q dependencies app:dependencies --configuration compile
compile - Classpath for compiling the main sources.
+--- project :react-native-obd2
|    +--- com.android.support:appcompat-v7:23.0.1 -> 24.1.0
|    |    +--- com.android.support:animated-vector-drawable:24.1.0
|    |    |    \--- com.android.support:support-vector-drawable:24.1.0
|    |    |         \--- com.android.support:support-v4:24.1.0
|    |    |              \--- com.android.support:support-annotations:24.1.0
|    |    +--- com.android.support:support-v4:24.1.0 (*)
|    |    \--- com.android.support:support-vector-drawable:24.1.0 (*)
|    +--- com.facebook.react:react-native:+ -> 0.34.1
|    |    +--- javax.inject:javax.inject:1
|    |    +--- com.android.support:appcompat-v7:23.0.1 -> 24.1.0 (*)
|    |    +--- com.android.support:recyclerview-v7:23.0.1 -> 24.1.0
|    |    |    +--- com.android.support:support-v4:24.1.0 (*)
|    |    |    \--- com.android.support:support-annotations:24.1.0
|    |    +--- com.facebook.fresco:fresco:0.11.0
|    |    |    +--- com.facebook.fresco:drawee:0.11.0
|    |    |    |    +--- com.android.support:support-v4:23.2.1 -> 24.1.0 (*)
|    |    |    |    \--- com.facebook.fresco:fbcore:0.11.0
|    |    |    +--- com.facebook.fresco:fbcore:0.11.0
|    |    |    \--- com.facebook.fresco:imagepipeline:0.11.0
|    |    |         +--- com.android.support:support-v4:23.2.1 -> 24.1.0 (*)
|    |    |         +--- com.facebook.fresco:fbcore:0.11.0
|    |    |         +--- com.parse.bolts:bolts-tasks:1.4.0
|    |    |         +--- com.nineoldandroids:library:2.4.0
|    |    |         \--- com.facebook.fresco:imagepipeline-base:0.11.0
|    |    |              +--- com.android.support:support-v4:23.2.1 -> 24.1.0 (*)
|    |    |              +--- com.facebook.fresco:fbcore:0.11.0
|    |    |              +--- com.parse.bolts:bolts-tasks:1.4.0
|    |    |              \--- com.nineoldandroids:library:2.4.0
|    |    +--- com.facebook.fresco:imagepipeline-okhttp3:0.11.0
|    |    |    +--- com.facebook.fresco:fbcore:0.11.0
|    |    |    +--- com.squareup.okhttp3:okhttp:3.0.1 -> 3.4.1
|    |    |    |    \--- com.squareup.okio:okio:1.9.0
|    |    |    \--- com.facebook.fresco:imagepipeline:0.11.0 (*)
|    |    +--- com.facebook.soloader:soloader:0.1.0
|    |    +--- com.fasterxml.jackson.core:jackson-core:2.2.3
|    |    +--- com.google.code.findbugs:jsr305:3.0.0
|    |    +--- com.squareup.okhttp3:okhttp:3.4.1 (*)
|    |    +--- com.squareup.okhttp3:okhttp-urlconnection:3.4.1
|    |    |    \--- com.squareup.okhttp3:okhttp:3.4.1 (*)
|    |    +--- com.squareup.okhttp3:okhttp-ws:3.4.1
|    |    |    \--- com.squareup.okhttp3:okhttp:3.4.1 (*)
|    |    +--- com.squareup.okio:okio:1.9.0
|    |    \--- org.webkit:android-jsc:r174650
|    +--- com.github.pires:obd-java-api:1.0-RC16
|    \--- org.roboguice:roboguice:3.+ -> 3.0.1
+--- project :react-native-vector-icons
|    \--- com.facebook.react:react-native:+ -> 0.34.1 (*)
+--- project :react-native-sp
|    +--- com.android.support:appcompat-v7:23.0.1 -> 24.1.0 (*)
|    \--- com.facebook.react:react-native:+ -> 0.34.1 (*)
+--- project :react-native-sensor-manager
|    \--- com.facebook.react:react-native:0.12.+ -> 0.34.1 (*)
+--- project :react-native-dialogs
|    +--- com.facebook.react:react-native:0.20.+ -> 0.34.1 (*)
|    \--- com.github.afollestad.material-dialogs:commons:0.8.6.2
|         +--- com.android.support:appcompat-v7:24.1.0 (*)
|         \--- com.github.afollestad.material-dialogs:core:0.8.6.2
|              +--- com.android.support:appcompat-v7:24.1.0 (*)
|              +--- com.android.support:recyclerview-v7:24.1.0 (*)
|              +--- com.android.support:support-v4:24.1.0 (*)
|              +--- me.zhanghai.android.materialprogressbar:library:1.1.7
|              |    +--- com.android.support:appcompat-v7:24.0.0 -> 24.1.0 (*)
|              |    \--- com.android.support:support-annotations:24.0.0 -> 24.1.0
|              \--- com.android.support:support-annotations:24.1.0
+--- com.android.support:appcompat-v7:23.0.1 -> 24.1.0 (*)
\--- com.facebook.react:react-native:+ -> 0.34.1 (*)

但我没有看到 javax 写了两次。
还提到检查下载的项目和您的文件的版本,但我找不到任何显着差异。

这是 build.gradle
apply plugin: "com.android.application"

import com.android.build.OutputFile
apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.obd2example"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    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]
            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
            }
        }
    }
}

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    compile project(':react-native-obd2')
    compile project(':react-native-vector-icons')
    compile project(':react-native-sp')
    compile project(':react-native-sensor-manager')
    compile project(':react-native-dialogs')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "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'
}

谁能指导我在哪里看?

编辑 1
为了找出在哪里使用了 javax.inject 模块两次,我在 node_modules 文件夹中执行了 grep -r "javax.inject"并找到了这个
react-native/android/com/facebook/react/react-native/0.34.1/react-native-0.34.1.pom:      <groupId>javax.inject</groupId>
react-native/android/com/facebook/react/react-native/0.34.1/react-native-0.34.1.pom:      <artifactId>javax.inject</artifactId>
react-native/ReactAndroid/src/main/third-party/java/jsr-330/BUCK:    url = 'mvn:javax.inject:javax.inject:jar:1',
react-native/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java:import javax.inject.Provider;
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleSpec.java:import javax.inject.Provider;
react-native/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java:import javax.inject.Provider;
react-native/ReactAndroid/build.gradle:    compile 'javax.inject:javax.inject:1'

这里有什么问题吗?我不太熟悉 Java 和 gradle 构建系统来理解它。

最佳答案

在您的 build.gradle文件,您可以尝试更改以下行:
compile "com.facebook.react:react-native:+" // From node_modules
对此:

compile("com.facebook.react:react-native:+") {
  exclude module: 'javax.inject'
}

该错误基本上意味着 @Inject注释多次包含在您的最终 APK 中,并且来自不同的地方。很难确定确切的位置,但这种来自 react-native 的传递依赖很可能是其中的来源之一。排除它有望在您的最终 apk 中只留下一个“副本”。

关于gradle - 编译 React-native 重复条目 : javax/inject/Inject. 类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277745/

相关文章:

spring - 无法在spring boot中启动嵌入式Tomcat

android-studio - 当我尝试同步 Gradle 文件时出现以下错误 :ERROR: Failed to resolve: android. recyclerview:recyclerview: Affected Modules: app

javascript - 通过 Websocket 连接将文件上传到 Django Channels(来自 React Native 应用程序)

android - 错误 :(27, 0) 在项目 ':library' 中找不到路径为 ':app' 的项目

dependency-injection - Gradle 无法使用 fileTree 将依赖项注入(inject)子项目

node.js - 如何在nodejs中使用pdfkit将png格式保存为pdf格式[包含代码]

javascript - 无法在 Android 设备上运行 React-Native 示例代码

android - Gradle : No tests found

java - 在签名配置中写密码安全吗?

SqLite 和 Gradle