Android Gradle plugin 3.0.1 - Error converting bytecode to dex - android

Upgrading from Android Gradle 2.3.3 to 3.0.1 I was getting the following build error:
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/common/base/internal/Finalizer;
This appeared to be an issue with Guava being included as an implementation, by my app; unit tests (Robolectric) and Instrumented Tests.
I resolved this build error by excluding Guava from Robolectric:
testImplementation ("org.robolectric:robolectric:$rootProject.ext.roboelectricVersion") {
exclude group: 'com.google.guava'
}
And for Instrumented tests, if I set the minSdkVersion to 21 it would resolve this build error there, via:
minSdkVersion 19
// Gradle plugin 3.0.1 broke instrumented tests with error: "Multiple dex files define ...Finalizer". Setting minSdkVersion = 21 clears this error
gradle.startParameter.taskNames.find {
if (it.contains("AndroidTest")) {
minSdkVersion 21
}
}
However, when I run a build for a device < 21, then I still get the build error.
So, the issue appears to be with Guava with Multidex support prior to Android 5.0.
Anyone else experiencing this issue and/or have suggestions for working around it?

Ok, the issue did seem to be with Guava. Taking a guess that it is the size of that library and the build having an issue with fitting it in the main DEX (the dex required for loading and initializing the app).
I switched out Guava for this lightweight Stream library and was able to build successfully for all devices.

Related

Android Gradle Build: duplicate entry: META-INF/app_release.kotlin_module

I'm trying to build a release app via Android Studio > Generate Signed Bundle or APK > Android App Bundle > Release
However gradle fails with
: > Task :core:transformClassesWithMergeClassesForRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':core:transformClassesWithMergeClassesForRelease'.
> 1 exception was raised by workers:
java.util.zip.ZipException: duplicate entry: META-INF/app_release.kotlin_module
In my build.gradle I've tried adding:
packagingOptions {
exclude 'META-INF/app_release.kotlin_module'
}
But it isn't making any difference whatsoever.
How do I fix this?
For extra context, it's a multi module project.
I have a core module, and an installed module which is declared in the core build.gradle with dynamicFeatures = [":installed"]
Thanks
Please make sure all your dependencies are api or implementation,
I have the flowing dependency grap.
meemo_sdk:
api project(":gvoice")
app project:
implementation project(":gvoice")
implementation project("meemo_sdk")
It complains "META-INF/gvoice_debug.kotlin_module" collision.
After change api to implementation, it works!
So I figured it out.
I pressed shift twice in Android studio (to open up the search everywhere dialog) and searched for app_release.kotlin_module
I saw two files, that were under two of my dependencies (which funnily enough were libraries that I had created!)
I opened up these library projects, and in the build.gradle file I had to add:
ext {
PUBLISH_GROUP_ID = 'com.companyname'
PUBLISH_ARTIFACT_ID = 'packagename'
}
android {
...
compileOptions {
kotlinOptions.freeCompilerArgs += ['-module-name', "$PUBLISH_GROUP_ID.$PUBLISH_ARTIFACT_ID"]
}
}
Rebuilt the library projects with new versions, used these new versions in my other project, and it started compiling :)
Build -> Clean Project Worked for me
I found the same issue comes randomly by using Android Studio 4.2.1 and Java8, I sorted out successfully by deleting .gradle file in the project (not the main one) every time I get the error.
Java version: JDK8221
Kotlin: 1.5.0
AndroidStudio: 4.2.1
Not the best solution but is a good workaround for now.

what happened jar (library ) compile version is different with app compile version?

For example
android jar library is compiled with
compile sdk version : 20
target sdk version : 20
And my app
compile sdk version : 21
and target sdk version :21
what happen my app use this jar library.
can it be compiled and executed in android api 21 os?
i have faced this kind of problem once (and there are many question's on SOF) which arise due to difference in version of the lib/*.jar file uses and there project is using, and the build failed because on task was getting failed.
org.gradle.api.tasks.TaskExecutionException: Execution failed for task
':app:transformClassesWithDexBuilderForDebug'.
I am not saying you will get this error for sure but there is a high risk of gradle build failure if the dispute happen's.
Reference:
Question 1
Question 2

Multidex issue for androidTest with gradle plugin 3.0.0

Once I updated my app to gradle plugin 3.0.0 I started having issue with multidex, but only when I run androidTest.
My app was already multidex and with gradle plugin 2.3.3 both assembleDebug and assembleDebugAndroidTest were working fine. Then I just updated gradle plugin to 3.0.0 and I started receiving the following exception:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
I tried different options like defining custom multidex runner for tests, adding androidTestCompile 'com.android.support:multidex-instrumentation:1.0.2' dependency, but nothing helped. I also didn't find in release notes of gradle plugin 3.0.0, if something changed there for android tests.
Does anybody have similar issue for androidTest?
I believe it is a known issue: https://issuetracker.google.com/issues/37324038
The issue is that legacy multidex (min API < 21) is not supported in Android Gradle plugin 3.0.0 for androidTest APK's specifically. I was able to get it working by making a product flavor with min API >= 21 like so:
productFlavors {
local {
minSdkVersion 21
}
ci {
minSdkVersion 17
}
}
And then running androidTests using the connectedLocalDebugAndroidTest task

Error converting bytecode to dex: Cause: java.util.zip.ZipException: invalid stored block lengths

After updating my Android SDK suddenly i am getting the above error , any one facing this? i am using NDK also in my app which is working an hour back before upgrading Android studio and SDK.
As mentioned in this post
com.android.build.transform.api.TransformException
Adding this entry in build.gradle solved my problem.
defaultConfig {
multiDexEnabled true
}

Compiling android application using Java 8

My application is not yet migrated to gradle, we still use Ant build script.
Recently one of our dependent libraries started using Java 8 features.
In order to compile using Java-8 lib, configured ant script to use java 1.8.
with this change, the compilation is successful, but failing in dex phase.
With 20.x build tools I got
"Bad Class File Magic (cafebabe)" error.
With 24.x build tools I got
"[dex] PARSE ERROR: unsupported class file version 52.0"
Seems to get rid of the dex error, i need to use jack toolchain, however as mentioned earlier, I am not using Gradle builds.
Is there any way to use dex with java-8 code? or Jack tools is the only solution?

Categories

Resources