Gradle 编译React本机重复项时出错:javax/inject/inject.class

Gradle 编译React本机重复项时出错:javax/inject/inject.class,gradle,react-native,build.gradle,gradlew,Gradle,React Native,Build.gradle,Gradlew,我从网上下载了一个名为React的本地项目。在开始之前,我只想运行repo中已经给出的示例,但在运行react native run android时,我遇到了一个错误 :app:transformClassesWithJarMergingForDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:transformClassesWith

我从网上下载了一个名为React的本地项目。在开始之前,我只想运行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依赖项应用程序:依赖项-配置编译

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

这是建筑,格雷德尔

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文件中,是否可以尝试更改以下行:

编译com.facebook.react:react native:+//来自节点_模块

为此:

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

这个错误基本上意味着@Inject注释被多次包含在最终APK中,并且来自不同的地方。很难精确地指出具体位置,但来自react native的这种可传递依赖性很可能是其中的一个来源。排除它将有希望在您的最终apk中只留下一份副本。

遗憾的是,它不起作用,尽管根据逻辑,它在理想情况下应该有。我应该发布更多的东西来帮助调试这个问题吗?但是如果您看到依赖关系图,javax.inject只在依赖关系图中的react native+下编写了一次。因此,如果不起作用,可能我们找错了地方。您是否也可以尝试查看其他react本机依赖项?编译项目:react-native-obd2“编译项目”:react-native向量图标“编译项目”:react-native sp“编译项目”:react-native传感器管理器“编译项目”:react-native对话框“由于依赖项可能已在jar中编译,因此您将无法对其进行grep。你有这些lib的源代码以便你可以检查它们吗?我到底在寻找什么?即使他们有javax.inject,他们自己也不需要它吗?实际上,我试图从每个依赖项中排除javax.inject,但每次我的编译都被暂停,并询问javax.inject是否丢失,请将其包括在内。那真的有帮助吗?无论如何都会尝试这样做?对,当您将闭包传递给Groovy方法时,您需要显式地将括号添加到它前面的参数:
compile("com.facebook.react:react-native:+") {
  exclude module: 'javax.inject'
}