android studio 3.0.1 showing unable to merge dex why? - android

I upgraded the android studio 2.x.x to android studio 3.0.1 after importing the project gradle building is successful. But when installing app showing the error message
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
my app grdle is given below
Why it's showing this error and how to fix it? unable to install the app on the emulator.
Already tried the clean and rebuild option in android studio but it doesn't fix the issue

Try with the following
If your minSdkVersion is set to 21 or higher
android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 26
multiDexEnabled true
}
...
}
if your minSdkVersion is set to 20 or lower,
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 26
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
If this does not resolve, try through these posts post 1 , android_dev or post 2

To remove this Dex issue just implement one dependency.
This issue occur when we are using multiple different service from the same server. Suppose we are using ads and Firestore in a project and both have a repository maven. so we need to call different data with on repository we need dex dependency to implement.
The new update Dependency:-
implementation 'com.android.support:multidex:1.0.3'
or use testCompile inside compile such as :
implementation 'com.google.android.gms:play-services-ads:15.0.1'
testCompile 'com.google.firebase:firebase-firestore:17.0.1'
and also make multiDexEnabled true;
100 % it will work !

Related

Unable to Merge Dex - Android Studio 3

I'm running the application in Android 5.1 device and it's running well after some time i have uninstalled the android studio and now i have installed Android Studio 3.0.1 and working on Elementary OS Loki and trying to run it on Android 8.0 device and when I start new app and run it then all goes well but the existing project is not running it gives error while running
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException:
com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
And I have build.gradle file as below
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "za.co.neilson.alarm"
multiDexEnabled true
minSdkVersion 16
targetSdkVersion 26
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:26.0.0'
implementation files('lib/android-support-v7-appcompat.jar')
}
I have googled and tried other solutions like
multiDexEnabled true
And rebuild / clean project but it did not work
Since you are using compile 'com.android.support:appcompat-v7:26.0.0' there are no reasons to add the jar file (also pay attention since the aar contains the jar file but also the resources).
Remove this line:
implementation files('lib/android-support-v7-appcompat.jar')
and delete the lib/android-support-v7-appcompat.jar file
I have googled and tried other solutions like
multiDexEnabled true
It is completely useless in your case. Use it when your app and the libraries it references exceed 65,536 methods, not when you are adding twice the same classes.
Try to change your gradle version to 3.0.1 or 2.3.3. By changing to one of them it may work for you.
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
//or try classpath 'com.android.tools.build:gradle:2.3.3'
}
After this clean and rebuild the project.
Hope it helps you.

Android Studio 3.0 Execution failed for task: unable to merge dex

android studio was getting build error while build execution with following:
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex"
My app:build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.pdroid.foodieschoice"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.firebaseui:firebase-ui-auth:2.3.0'
testCompile 'junit:junit:4.12'
}
configurations.all {
resolutionStrategy {
force 'com.android.support:appcompat-v7:26.0.1'
force 'com.android.support:support-compat:26.0.1'
force 'com.android.support:support-core-ui:26.0.1'
force 'com.android.support:support-annotations:26.0.1'
force 'com.android.support:recyclerview-v7:26.0.1'
}
}
apply plugin: 'com.google.gms.google-services'
any solutions
Edit: i have done with sample through firebase github site and solved
For Cordova based project, run cordova clean android before build again, as #mkimmet suggested.
This error happens when you add an external library which may not be compatible with your compileSdkVersion .
Be careful when you are adding an external library.
I spent 2 days on this problem and finally it got solved following these steps.
Make sure all your support libraries are same as compileSdkVersion of your build.gradle(Module:app) in my case it is 26.
In your defaultConfig category type multiDexEnabled true. This is the important part.
Go to File | Settings | Build, Execution, Deployment | Instant Run and try to Enable/Disable Instant Run to hot swap... and click okay
Sync Your project.
Lastly, Go to Build | click on Rebuild Project.
Note: Rebuild Project first cleans and then builds the project.
Try to add this in gradle
android {
defaultConfig {
multiDexEnabled true
}
}
Resolution:
Refer to this link: As there are various options to shut the warning off depending on the minSdkVersion, it is set below 20:
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 26
multiDexEnabled true
}
... }
dependencies { compile 'com.android.support:multidex:1.0.3' }
If you have a minSdkVersion greater than 20 in your build.gradle set use the following to shut down the warning:
android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 26
multiDexEnabled true
}
... }
Update dependencies as follows:
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
Again the only difference is the keywords in dependencies:
minSdkVersion below 20: use compile
minSdkVersion above 20: use implementation
I hope this was helpful, please upvote if it solved your issue, Thank you for your time.
Also for more info, on why this occurs, please read the first paragraph in the link, it will explain thoroughly why? and what does this warning mean.
Simply try doing a "Build -> Clean Project". That solved the problem for me.
For me, adding
multiDexEnabled true
and
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/notice'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license'
exclude 'META-INF/license.txt'
}
into the app level Build.gradle file solved the issue
Go to your module level build.gradle file and add the following lines to the code
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
That solved the problem easily. Check this documentation
Use multiDexEnabled true as below.
{
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
and
implementation 'com.android.support:multidex:1.0.3'
this Solution worked for me.
I tried many solutions as mentioned above including the multiDexEnabled true but none of that worked for me.
Here is the solution which worked for me - copy this code in app\build.gradle file
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support' && requested.name != 'multidex') {
details.useVersion "${rootProject.ext.supportLibVersion}"
}
}
}
Make sure to run gradlew clean before running the code
Same thing happened to me, figured out that I was in the middle of updating to AS 3.0.1 and after I did the update, cleaned and rebuilt the app, everything was fixed.
For me what fixed this error was changing one line in my app's gradle dependencies.
from this:
compile('com.google.android.gms:play-services-gcm:+') {
force = true
}
To this:
compile('com.google.android.gms:play-services-gcm:11.8.0') {
force = true
}
Also be sure your app is subclassing MultiDexApplication
import android.support.multidex.MultiDexApplication
class App : MultiDexApplication()
or if not subclassing Application class, add to AndroidManifest.xml
<application
android:name="android.support.multidex.MultiDexApplication"
This is what works for me. clean prepare and run.
cordova clean android;
ionic cordova prepare andriod;
ionic cordova run andriod;
hope it helps.
I also got the similar error.
Problem :
Solution :
Main root cause for this issue ismultiDex is not enabled.
So in the Project/android/app/build.gradle, enable the multiDex
For further information refer the documentation: https://developer.android.com/studio/build/multidex#mdex-gradle
Are you in between SDK or platform updates?
If yes complete those completely and then try to continue.
I normally update the individual packages that need to be updated as to taking the entire update which could go up to 2.5 GB sometimes. Doing this complete update sometimes fails. I had a number of SDK updates once I upgraded to Android Studio 3.0 and was getting the above error since all packages had not been updated. Once I updated all packages the above error was gone.
In my case changing firebase library version from 2.6.0 to 2.4.2 fixed the issue
For me, the problem was the use of Java 1.8 in a module, but not in the app module. I added this to the app build gradle and worked:
android{
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Same problem. I have enabled multidex:
defaultConfig {
applicationId "xxx.xxx.xxxx"
minSdkVersion 24
targetSdkVersion 26
multiDexEnabled true
I have cleared cache, ran gradle clean, rebuild, make, tried to make sure no conflicts in imported libraries (Removed all transitive dependencies) and made all updates. Still:
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Turns out the build did not like:
implementation 'org.slf4j:slf4j-android:1.7.21'
When the version of Android Studio is 3.0.1, Gradle Version is 4.1 and Android PluginVersion is 3.0.0, it will encounter this problem. Then I downgrade Gradle Version is 3.3, Android Android is zero, there is no such problem.
I had this when requested SDK version didn't match the dependencies. You can click the line highlighted and fix this clicking the red light bulb. No need to find the actual version, just let the IDE figure it out for you. And add google repo to maven config.
I was receiving the same error and in my case, the error was resolved when I fixed a build error which was associated with a different build variant than the one I was currently building.
I was building the build variant I was looking at just fine with no errors, but attempting to debug caused a app:transformDexArchiveWithExternalLibsDexMergerForDebug error. Once I switched to build the other build variant, I caught my error in the build process and fixed it. This seemed to resolve my app:transformDexArchiveWithExternalLibsDexMergerForDebug issue for all build variants.
Note that this error wasn't within the referenced external module but within a distinct source set of a build variant which referenced an external module. Hope that's helpful to someone who may be seeing the same case as me!
I Have Done and fixed this issue by just doing this jobs in my code
Open ->build.gradle
Change value
from
compile 'com.google.code.gson:gson:2.6.1'
to
compile 'com.google.code.gson:gson:2.8.2'
1)Please add multiDexEnabled true
2)if you get compilation error then check your app level build.gradle weather same dependencies are implemented with different versions.
3)Remove one if you find same dependencies.
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
hope it works fine :) Happy Coding
The easiest way to avoid suck kind of error is:
-Change library combilesdkversion as same as your app compilesdkversion
-Change library's supportLibrary version as same as your build.gradle(app)

Application crashing only on lollipop devices

Application is working fine on all OS, but when i add facebook SDK, it gives me following error message on lollipop devices.
compile 'com.facebook.android:facebook-android-sdk:4.20.0'
Caused by: java.lang.ClassNotFoundException: Didn't find class
"retrofit2.Retrofit$Builder" on path: DexPathList[[zip file
"/data/app/com.myapp.app-3/base.apk", zip file
java.lang.NoClassDefFoundError: Failed resolution of:
Lretrofit2/Retrofit$Builder;
Caused by: java.lang.ClassNotFoundException: Didn't find class "retrofit2.Retrofit$Builder" on path: DexPathList[[zip file
"/data/app/com.myapp.app-3/base.apk", zip file "/data/app/
com.myapp.app-3/split_lib_dependencies_apk.apk",
This is my gradle file.
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId 'com.myapp.app'
minSdkVersion 21
targetSdkVersion 23
versionCode 122
versionName "1.2.5"
multiDexEnabled true
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86"
}
configurations.all {
resolutionStrategy {
force 'com.squareup.okhttp:okhttp:2.4.0'
force 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
force 'com.squareup.okio:okio:1.11.0'
force 'com.squareup.okhttp3:okhttp:3.6.0'
force 'com.squareup.retrofit2:retrofit:2.2.0'
force 'com.android.support:recyclerview-v7:25.0.0'
force 'com.android.support:support-v4:25.0.0'
force 'com.android.support:cardview-v7:25.0.0'
force 'com.android.support:appcompat-v7:25.0.0'
force 'com.android.support:design:25.0.0'
force 'com.android.support:support-annotations:25.0.0'
force 'com.google.android.gms:play-services-ads:9.0.1'
force 'com.google.android.gms:play-services-location:9.0.1'
force 'com.google.android.gms:play-services-auth-base:9.0.1'
force 'com.google.android.gms:play-services-base:9.0.1'
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
Kindly guide me what can be the reason of this crash, and why its only crashing on lollipop devices and working fine on Marshmallow and Nougat.
EDIT
If I don't use facebook sdk, everything seems fine, no crashes. Maybe Facebook sdk cause this problem but I don't know why
You are using too many libraries so your methods have crossed over 64k so
you have to enable Multidex for you project
inside your build.gradle
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 25
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
and in AndroidManifest.xml
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
see This for more info
You are getting ClassNotFoundException
The ClassNotFoundException is thrown when the Java Virtual Machine
(JVM) tries to load a particular class and the specified class cannot
be found in the classpath.
At first you should set Multidex .
Android application (APK) files contain executable bytecode files in
the form of Dalvik Executable (DEX) files, which contain the compiled
code used to run your app. The Dalvik Executable specification limits
the total number of methods that can be referenced within a single DEX
file to 65,536, including Android framework methods, library methods,
and methods in your own code. Getting past this limit requires that
you configure your app build process to generate more than one DEX
file, known as a multidex configuration.
You should use Latest Version
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId 'com.myapp.app'
minSdkVersion 21
targetSdkVersion 25 // Good approach using same version of compileSdkVersion
versionCode 122
versionName "1.2.5"
multiDexEnabled true
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86"
}
configurations.all {
resolutionStrategy {
force 'com.squareup.okhttp:okhttp:3.7.0'
force 'com.squareup.okhttp:okhttp-urlconnection:2.7.5'
force 'com.squareup.okio:okio:1.12.0'
force 'com.squareup.okhttp3:okhttp:3.7.0'
force 'com.squareup.retrofit2:retrofit:2.2.0'
force 'com.android.support:recyclerview-v7:25.2.0'
force 'com.android.support:support-v4:25.2.0'
force 'com.android.support:cardview-v7:25.2.0'
force 'com.android.support:appcompat-v7:25.2.0'
force 'com.android.support:design:25.2.0'
force 'com.android.support:support-annotations:25.2.0'
force 'com.google.android.gms:play-services-ads:10.2.1'
force 'com.google.android.gms:play-services-location:10.2.1'
force 'com.google.android.gms:play-services-auth-base:10.2.1'
force 'com.google.android.gms:play-services-base:10.2.1'
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
After that Clean-Rebuild-Run .
FYI
You can use below version if 4.20.0 crashing .
compile 'com.facebook.android:facebook-android-sdk:4.16.0'
In my Case this solved my problem
In Application Class you need to
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
The main issue I can see is that you have exceeded the methods limit by adding a lot of libraries in your project. Android app has a certain limit of methods. To make your code efficient I'll suggest you to remove all unnecessary libraries like why are you using "Retrofit" and "OkHttp" together? You can get all the work done by any one of them.
But still if you want to use all these libraries, then you have to allow your app to "Multidex" usage.
Here is the way to allow "Multidex" usage.
https://developer.android.com/studio/build/multidex.
But I won't suggest to use this. Because this will put load on your app and will require more space in Phone's ram
Choice is yours!

Android Studio: Why am i getting multi dex error on brand new Google Maps API project?

I'm relatively new to Android programming and I am trying to create a Google Maps project. I used the template option in Android Studio, and I added the key for the API.
I haven't added any of my own code and left the template code as is because I just wanted to run the code and see what it looks like, however, I keep getting a multi dex error when I try to run this on the emulator causing the build to fail. It's weird to me that I am getting this error because I haven't added ANY code at all and am using what the Google Maps template has from Android Studio.
Anyone know why this error shows up on a brand new project? The error I see is pasted below.
Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_91\bin\java.exe'' finished with non-zero exit value 2
as quoted below:
Your probably compiling all of the play-services API's using compile 'com.google.android.gms:play-services:9.2.0'... Now we can selectively compile API's to avoid dex limit of 64K. For Google Maps use com.google.android.gms:play-services-maps:9.2.0... – Loki Jul 1 at 19:01
Answer from Loki worked and was very simple to do.
setting up google play services
Go through this link just add the dependencies which you want in your application.
This will prevent 64k exceeding error.
Happy coding.
For those who tried all the above methods and failed. Try changing the mini sdk version to 21 in the build.gradile file. For me it was on 16 and when I changed it to 21 the multidex error was gone for good.
minSdkVersion 21
The issue is you currently have a high number of methods. There can only be 65536 methods for dex. You can try enabling multiDex by editing your build.gradle file.
android {
//stuff
defaultConfig {
...
// Enable multiDex support.
multiDexEnabled true
}
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
Also, oops as the poster above mentioned in your manifest you also need to add that you are using the multidex class from the multidex support library.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.dexapp">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
Change your Gradle build configuration to enable multidex
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc2"
defaultConfig {
applicationId "com.try.app"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
In your manifest add the MultiDexApplication class from the multidex support library to the application element.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
Note: Visit https://developer.android.com/studio/build/multidex.html for more info.
Set property multiDexEnabled to true in defaultConfig. This error occurs when method references increase the limit of 65k. Check Google Play Service APIs.
android{
...
defaultConfig {
...
multiDexEnabled true
}
...
}
Here is a good source to get started with Google Maps Android API
I removed the dependencies as suggested by Loki and it works.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.google.android.gms:play-services-maps:9.4.0'
}

Google Messaging Service 8.3 brings ZipException error

I'm trying to figure out how to solve this sudden duplicate entry problem:
Error:Gradle: Execution failed for task ':app:packageDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry:com/google/android/gms/internal/zzqq$zza.class
I have just increased Google Cloud Messaging version from 8.1 to 8.3.
dependencies {
compile 'com.google.android.gms:play-services-gcm:8.3.0'
}
Any pointer?
Thanks
UPDATE
My build.gradle
android {
compileSdkVersion project.properties['compileSdkVersion'].toInteger()
buildToolsVersion project.properties['buildToolsVersion']
defaultConfig {
minSdkVersion project.properties['minSdkVersion']
targetSdkVersion project.properties['targetSdkVersion'].toInteger()
versionCode project.properties['versionCode'].toInteger()
versionName project.properties['versionName']
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
}
UPDATE 2
Something similar is happening with 8.4, too
Error:Gradle: Execution failed for task ':app:transformClassesWithJarMergingForFabricStagingDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException:
duplicate entry: com/google/android/gms/internal/zzat.class
Are you sure you upgraded from 8.1? Users have been reporting this as a known bug when upgrading from 7.8 to 8.1: https://code.google.com/p/android/issues/detail?id=187464
Anyways, just stay on the highest version which is still working for you, until they fix it in a later version.
Turns out that the conflict is caused by one of the libraries in my project. It's using play-services-ads:8.1 and, apparently, being shrinked/obfuscated with Proguard, it ends to contain a zzat.class file, similar to the one contained in play-services-basement:8.4.0.
I'm trying to solve this issue forcing play-services-ads to use a newer version with this change in my build.gradle:
configurations.all {
resolutionStrategy {
force 'com.google.android.gms:play-services-ads:8.4.0'
}
}
This could have side effects in particular scenarios, but, right now, it seems to be the only fix.

Categories

Resources