问题
I downloaded a React-native project from the web called react-native-obd2. Before I got started I just wanted to run the example that was already given in the repo but while running react-native run-android
I am getting stuck at an error.
: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
I have been breaking my head for over 3 hours to solve this bugger but it wouldn't go.
I have tried various solutions one of the most logical solution to me was that javax/inject/Inject.class
is being called twice somewhere.
So I typed this command to get the gradle classpath../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 (*)
but I don't see javax written twice. It was also mentioned that check the versions of the downloaded project and your file but I couldn't find any significant difference.
Here is the 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'
}
Could anyone please guide me where to look?
EDIT 1
In my quest to find out where javax.inject module was used twice I did a grep -r "javax.inject" in the node_modules folder and found this
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'
Is there something wrong here? I am not very well versed with Java and gradle build system to comprehend it.
回答1:
In your build.gradle
file, can you try changing the following line:
compile "com.facebook.react:react-native:+" // From node_modules
To this:
compile("com.facebook.react:react-native:+") {
exclude module: 'javax.inject'
}
The error basically mean that the @Inject
annotation is being included in your final APK multiple times and it's coming from different places. It's hard to pinpoint exactly where, but this transitive dependency from react-native is most likely one of the sources. Excluding it will hopefully leave you with just one "copy" of it in your final apk.
来源:https://stackoverflow.com/questions/43277745/error-while-compiling-react-native-duplicate-entry-javax-inject-inject-class