Flipper: Gradle error when building

Created on 11 Jun 2018  路  29Comments  路  Source: facebook/flipper

I'm trying to test Sonar with android, I created a new application with android studio, and added the dependency to the build.gradle

dependencies {
    // ...
    debugImplementation 'com.facebook.sonar:sonar:0.0.1'
}

When I try to build the app, I get the following error:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/0.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/62.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/90.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/96.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/97.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/98.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/99.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/100.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/101.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/102.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/103.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/104.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/105.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/106.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/107.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/108.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/109.jar
    at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:55)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:104)
    at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)
    at sun.reflect.GeneratedMethodAccessor465.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
    ... 32 more
Caused by: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/0.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/62.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/90.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/96.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/97.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/98.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/99.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/100.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/101.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/102.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/103.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/104.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/105.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/106.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/107.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/108.jar, /Users/rigor789/Code/SonarTest/app/build/intermediates/transforms/dexBuilder/debug/109.jar
    at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:124)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:109)
    at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:101)
    at com.android.build.gradle.internal.transforms.ExternalLibsMergerTransform.transform(ExternalLibsMergerTransform.kt:123)
    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:221)
    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:217)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
    ... 47 more
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
    at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:76)
    at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:45)
    at com.android.tools.r8.D8.run(D8.java:67)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:107)
    ... 52 more
Caused by: com.android.tools.r8.utils.AbortException
    at com.android.tools.r8.utils.Reporter.failIfPendingErrors(Reporter.java:77)
    at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:58)
    at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:67)
    ... 55 more

In the Android Issues section this error appears as well:

Program type already present: android.support.v4.app.BackStackState$1
Message{kind=ERROR, text=Program type already present: android.support.v4.app.BackStackState$1, sources=[Unknown source file], tool name=Optional.of(D8)}
Android

Most helpful comment

There is definitely a packaging problem with version 0.0.1 of the library. It includes all class files of the dependencies (including the support library, Guava files, OkHttp, ...):

The exclude group statements for debugImplementation suggested above won't work in this case. They only work for dependencies that are declared in the pom file (sonar-0.0.1 does not declare any dependencies there - they are all bundled inside of the aar).

The only way I could get it to work is to manually extract the aar file (and the contained classes.jar file), repackage them, then directly reference them in my build.gradle file.

It went something like this:

mkdir -p ~/tmp/sonar-0.0.1
cp ~/.gradle/caches/modules-2/files-2.1/com.facebook.sonar/de84b64d6ab7a9b71e80875f5fb6cabf040eb06/sonar-0.0.1.aar ~/tmp/sonar-0.0.1/
cd ~/tmp/sonar-0.0.1
unzip sonar-0.0.1.aar -d aar-0.0.1
cp aar-0.0.1/classes.jar .
unzip classes.jar -d classes-0.0.1

# Now delete all the unwanted packaged class files, e.g.
rm -rf classes-0.0.1/android classes-0.0.1/javax classes-0.0.1/jsr-305

# Repackage classes file
jar cvf classes.jar -C classes-0.0.1/ .

# Repackage aar file
cp classes.jar aar-0.0.1/
jar cvf sonar-0.0.1-LOCAL.aar -C aar-0.0.1/ .

# Now use the new aar file directly:

cp sonar-0.0.1-LOCAL.aar ~/path/to/android-project/app/libs

# build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

# app/build.gradle

dependencies {
    // ...
    debugImplementation(name: 'sonar-0.0.1-LOCAL', ext: 'aar')
}

All 29 comments

I tried to use the following snippet (I can't remember where it was from, I used it in a past project)

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == "com.android.support") {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "27.+"
            }
        }
    }
}

But that din't seem to solve the issue.

I've got the similar error in the Android Issues section:

Program type already present: okio.Util
Message{kind=ERROR, text=Program type already present: okio.Util, sources=[Unknown source file], tool name=Optional.of(D8)}

looks like sonar has all dependencies packaged inside of it. try to implement like this

debugImplementation ('com.facebook.sonar:sonar:0.0.1'){
exclude group: 'android'
exclude group: 'javax'
exclude group: 'jsr-305'
exclude group: 'okhttp3'
exclude group: 'okio'
exclude group: 'org'
exclude group: 'com.google'
}

@TurKorsaN I couldn't get it to work, the same error is thrown (even after cleaning and trying to re-build)

There is definitely a packaging problem with version 0.0.1 of the library. It includes all class files of the dependencies (including the support library, Guava files, OkHttp, ...):

The exclude group statements for debugImplementation suggested above won't work in this case. They only work for dependencies that are declared in the pom file (sonar-0.0.1 does not declare any dependencies there - they are all bundled inside of the aar).

The only way I could get it to work is to manually extract the aar file (and the contained classes.jar file), repackage them, then directly reference them in my build.gradle file.

It went something like this:

mkdir -p ~/tmp/sonar-0.0.1
cp ~/.gradle/caches/modules-2/files-2.1/com.facebook.sonar/de84b64d6ab7a9b71e80875f5fb6cabf040eb06/sonar-0.0.1.aar ~/tmp/sonar-0.0.1/
cd ~/tmp/sonar-0.0.1
unzip sonar-0.0.1.aar -d aar-0.0.1
cp aar-0.0.1/classes.jar .
unzip classes.jar -d classes-0.0.1

# Now delete all the unwanted packaged class files, e.g.
rm -rf classes-0.0.1/android classes-0.0.1/javax classes-0.0.1/jsr-305

# Repackage classes file
jar cvf classes.jar -C classes-0.0.1/ .

# Repackage aar file
cp classes.jar aar-0.0.1/
jar cvf sonar-0.0.1-LOCAL.aar -C aar-0.0.1/ .

# Now use the new aar file directly:

cp sonar-0.0.1-LOCAL.aar ~/path/to/android-project/app/libs

# build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

# app/build.gradle

dependencies {
    // ...
    debugImplementation(name: 'sonar-0.0.1-LOCAL', ext: 'aar')
}

I have the same issue:
AGPBI: {"kind":"error","text":"Program type already present: androidx.fragment.app.FragmentManager$OnBackStackChangedListener","sources":[{}],"tool":"D8"}

Hope that the official can provide AAR file

I have the same issue:

com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:

@jiangkang This just means you have other conflicting dependencies. The steps above only removed the support library, annotations and a few other things. Your project might have dependencies on OkHttp or Okio, for example. You'll need to remove those as well.

@friederbluemle Thanks for the tip!

I followed your steps and was able to strip out the duplicated javax classes that gradle/D8 was complaining about, but I'm still seeing a similar error - D8: Program type already present: javax.annotation.concurrent.ThreadSafe (even though I've double checked and removed all the javax.* classes before recompiling as a local AAR.

Any ideas?

@erawhctim Hmm, is it possible that your existing dependencies have some conflict?

Try

./gradlew :app:dependencies

I assume you already completely cleaned the project after modifying the aar file, to remove any chance for cached file problems? You may also try ./gradlew --stop to stop running instances the Gradle wrapper (although unlikely related to that).

@friederbluemle Hmm, I think you might be right. Looks like realm-android-library depends on FindBugs' JSR305 so maybe the javax.* classes are coming in from there.

debugCompileClasspath - Resolved configuration for compilation for variant: debug
+--- io.realm:realm-annotations:5.3.0
+--- io.realm:realm-android-library:5.3.0
|    +--- com.google.code.findbugs:jsr305:3.0.2
|    +--- com.getkeepsafe.relinker:relinker:1.2.2
|    \--- io.realm:realm-annotations:5.3.0
+--- io.realm:realm-android-kotlin-extensions:5.3.0
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10
|         \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.10
|              \--- org.jetbrains:annotations:13.0
+--- project :sonar-0.0.1-LOCAL
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10 (*)
\--- org.jetbrains.anko:anko-commons:0.10.4
     \--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.10 (*)

I tried excluding that from the realm gradle plugin declaration like this, but that didn't work like I expected it to:

classpath("io.realm:realm-gradle-plugin:5.3.0") {
    exclude group: 'com.google.code.findbugs', module: 'jsr305'
}

I have a hard time understanding how the Sonar library in its current form would work with any external real application out there. I am facing similar issue but with OkHttp3

Program type already present: okhttp3.Authenticator$1
Message{kind=ERROR, text=Program type already present: okhttp3.Authenticator$1, sources=[Unknown source file], tool name=Optional.of(D8)}

And since OkHttp3 is pretty common [sarcasm], how on earth can Sonar in its current form work in real production apps? So this library has to be slimmed down and not ship with all these baked in dependencies.

met the same issue again:

:app:transformDexArchiveWithExternalLibsDexMergerForDebug
-- | --

how can I fix ???

@priteshrnandgaonkar and I are still working on a full solution, but we've got a stop-gap that should already help a great deal.

If you include this repository (while we're waiting for JCenter to approve a new package) ...

repositories {
    maven { url 'https://dl.bintray.com/facebook/maven/' }
}

... and depend on ...

debugImplementation 'com.facebook.sonar:sonar:0.0.4

... then you should be able to use Sonar in your Android project. For now we DO NOT support x86_64 due to an OpenSSL compilation issue, but armv7, armv8 and x86 should all work just fine.

Another update on this: The extra repository is no longer necessary as the new dependency got promoted into JCenter proper.

com.facebook.sonar:sonar:0.0.4 depends on
+--- com.facebook.litho:litho-core:0.15.1-SNAPSHOT
| +--- com.facebook.litho:litho-widget:0.15.1-SNAPSHOT

But they are not on jcenter nor on the https://dl.bintray.com/facebook/maven/

@sgrekov You can get them from by adding https://oss.sonatype.org/content/repositories/snapshots/ as repository. But thanks for pointing that out; that should not be necessary.

I'll bump the Litho dependencies back to stable and push out another release with them included.

Pushed 0.0.5 which no longer depends on Litho snapshots.

@passy Don't forget to push com.facebook.sonar:fbjni too. 0.0.5 is currently not available but required by the lib.

@Swordsman-Inaction Thanks! The task silently failed to publish the release because the javadoc generation failed with an odd error. Uploading now.

@passy Hi, got an error, found both libs have libsonarfb.so after unpack the aar.

Execution failed for task ':mobile:transformNativeLibsWithMergeJniLibsForDebug'.
> More than one file was found with OS independent path 'lib/armeabi-v7a/libsonarfb.so'

@Swordsman-Inaction You can add the following

   packagingOptions {
        pickFirst 'lib/armeabi-v7a/libsonarfb.so'
        pickFirst 'lib/x86/libsonarfb.so'
        pickFirst 'lib/x86_64/libsonarfb.so'
        pickFirst 'lib/arm64-v8a/libsonarfb.so'
    }

@Swordsman-Inaction The workaround from @priteshrnandgaonkar should work, but it's still something we shouldn't force you to do. I'll try to exclude the library from getting bundled into the sonar AAR. Pulling it in from one place should be enough.

One more try. :) Published 0.0.8 which no longer includes two copies of libsonarfb.so, so you don't have to use pickFirst or other hacks. x86_64 is still not supported, but we're looking into that as part of #111 as we're trying to find a version of OpenSSL that's compatible with folly and compiles with modern NDK versions.

Is this issue resolved? I still face it.

maven { url 'https://dl.bintray.com/facebook/maven/' }
debugImplementation 'com.facebook.sonar:sonar:0.0.9'

@shaishavgandhi05

Yes, the newer versions address this. Closing this here now. :)

I have solved my issue by using this configurations in gradle file. You save my day. Thanks !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orlandobustamantep picture orlandobustamantep  路  3Comments

usrbowe picture usrbowe  路  3Comments

justintuchek picture justintuchek  路  3Comments

usrbowe picture usrbowe  路  4Comments

abaker picture abaker  路  3Comments