I have an Android project, inside I have a library module, my project works fine, compile the project I get the .aar included in a test project and everything ok.
Now,I have added in my library module a Activity that extends Application, Y Added this class in the manifiest.xml of the library
<application
android:allowBackup="true"
android:label="#string/app_name"
android:name=".MyActivityExtendsApplications"
When I test the project it works perfect, but when I take the .aar and test it on other project fails. The problem is that it does not find the activity extend Application ... I have decompiled the aar and I see that everything is correct, all class are inside the folder
The error is
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mylibrary.MyActivityExtendsApplications" on path: DexPathList[[zip file "/data/app/com.myapplication-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myapplication-1/lib/arm, /vendor/lib, /system/lib]]
Suppressed: java.lang.ClassNotFoundException: com.mylibrary.MyActivityExtendsApplications
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I adding grandle dependency to library .aar In all cases in the same way
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:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
testCompile 'junit:junit:4.12'
compile 'mylibrary-debug.mylibrary-debug#aar'
}
repositories{
flatDir{
dirs 'libs'
}
}
I check merged manifest in my test Project and the activity is there
application
android:name="com.mylibrary.MyActivityExtendsApplications"
android:allowBackup="true"
And I can import this Activity in my test Project and see the code
And I added mylibrary in libs folder.
When I remove the activity MyActivityExtendsApplications, my library works fine.
The issue may be the the Android Beacon Library aar files are missing when you generate your library aar, and the exception description is simply misleading. I don't think classes from aar files are automatically merged when generating a new aar file. You might check to see if these files (like Beacon.class) are missing from your aar.
If this is indeed the problem, there are two possible solutions:
In your application, reference both your library aar file and the Android Beacon Library aar file.
Merge the classes from the first aar file into your new aar file. I have done this before with rather a rather inelegant shell script that I am happy to share, but there may be a better way to accomplish the same thing.
Related
What can cause this error for class DrawableWrapper from the support lib?
Caused by: java.lang.ClassNotFoundException: Didn't find class
"android.support.v4.graphics.drawable.DrawableWrapper" on path:
DexPathList[[zip file "/data/app/com.example.banyan.tasty-
1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
Try updating your support library to 27.1.0.
Make sure all of your "com.android.support:*" dependencies are at the same version.
Looks like one of the support libraries uses something that is missing in the other one.
i fixed same error updating version
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:support-v4:27.0.2'
To
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
I started getting this exception in Android 5 and Android 4 users after removing com.android.support-v13 from my build.gradle file:
configurations.all {
exclude group: 'com.android.support', module: 'support-v13'
}
I removed the above exclude configuration and no longer saw this crash.
In my case, I used support library version '27.0.2' for my project. It was working perfectly. However, when I copy & paste the project to different location and I opened in Android studio. That time, android studio added below line in build.gradle (path: MyProject/app/build.gradle)
compile 'com.android.support:support-v4:27.1.1'
Then I got the below exception.
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.graphics.drawable.DrawableWrapper" on path: DexPathList
I removed the line from build.gradle (MyProject/app/build.gradle). Now it is working fine.
Try this:
import android.support.v7.graphics.drawable.DrawableWrapper;
import android.support.v7.graphics.drawable.DrawableWrapper;
or
import android.support.v4.graphics.drawable.WrappedDrawable; will help.
Also, when you use Drawable inner = ((WrappedDrawable) drawable).getWrappedDrawable();, you can add #SuppressLint("RestrictedApi") above your method.
Try:
enable D8
if you using
AS 3.1
I am using FileProvider in my app. As usual I declared <Provider> tag in AndroidManifest.xml file as below.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.jk.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
When I run it on android device which has lollipop version it works fine. when I try it on kitkat version it shows following errors:
FATAL EXCEPTION: main
Process: com.jk.android.perfectphotoeditor2018, PID: 24992
java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.jk.android.perfectphotoeditor2018-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.jk.android.perfectphotoeditor2018-2, /system/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:5071)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4648)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4588)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.jk.android.perfectphotoeditor2018-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.jk.android.perfectphotoeditor2018-2, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.ActivityThread.installProvider(ActivityThread.java:5056)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4648)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4588)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
at dalvik.system.NativeStart.main(Native Method)
I had tried many solutions like this but it doesn't work for me. so, help me for solve this problem.
build.gradle dependencies:
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
testCompile 'junit:junit:4.12'
/* Add the CSDK framework dependencies (Make sure these version numbers are correct) */
// compile 'com.aviary.android.feather.sdk:aviary-sdk:3.6.3'
compile 'com.android.support:appcompat-v7:25.4.0'
compile 'com.adobe.creativesdk.foundation:auth:0.9.1251'
compile 'com.adobe.creativesdk:image:4.8.4'
compile 'com.localytics.android:library:4.0.1'
compile 'com.android.support:design:25.4.0'
compile 'com.android.support:multidex:1.0.2'
compile 'com.android.support:support-v4:25.4.0'
compile 'com.intuit.sdp:sdp-android:1.0.4'
compile 'com.github.bumptech.glide:glide:4.3.1'
compile 'com.google.android.gms:play-services-ads:9.4.0'
implementation 'com.android.support:support-v4:25.4.0'
}
If you have migrated to AndroidX you have to change the name in AndroidManifest.xml to androidx.core.content.FileProvider
That was the crash in my case.
I have the same question, and this documentation solve my problem.
https://developer.android.com/studio/build/multidexandroid multidex
before android 5.0, you must use follow ways to multidex:
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.3'
}
and then modify application, select any one of three ways:
if you not implement application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
</manifest>
if you have custom application, you can modify like this:
public class MyApplication extends MultiDexApplication { ... }
if you have replace base application, you can modify like this:
public class MyApplication extends SomeOtherApplication {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Hope this can solve you problem.
The following code works for me (I've migrated to AndroidX):
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:ignore="WrongManifestParent">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
in my project when set "multiDexEnabled" false, error fixed.
go to manifest and add
android {
defaultConfig {
multiDexEnabled false
}}
hope this is helpful
Just delete the intermediates folder.
After thorough reading of Android Multidex documentation, I found that there is one configuration we generally miss which causes java.lang.NoClassDefFoundError. If you have done regular multidex configuration for 65K reference limit and and still unable to get rid of java.lang.NoClassDefFoundError then you must follow these steps.
Create a file called multidex-config.txt at app level(same directory as the build.gradle file)
Add following line (the absolute class name for which you are getting exception)
android.support.v4.content.FileProvider
Modify your build.gradle as follow
android {
buildTypes {
release {
multiDexKeepFile file('multidex-config.txt')
...
}
}
}
Note : You can add it for specific build type or both by declaring it in defaultConfig.
What happens when you build a multidex enabled app :
The Android build tools construct a primary DEX file (classes.dex) and supporting DEX files (classes2.dex, classes3.dex, and so on) as needed. The build system then packages all DEX files into your APK.
At runtime, the multidex APIs use a special class loader to search all of the available DEX files for your methods (instead of searching only in the main classes.dex file).
Cause of Exception :
When building each DEX file for a multidex app, the build tools perform complex decision-making to determine which classes are needed in the primary DEX file so that your app can start successfully. If any class that's required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError.
This shouldn't happen for code that's accessed directly from your app code because the build tools recognize those code paths, but it can happen when the code paths are less visible such as when a library you use has complex dependencies. For example, if the code uses introspection or invocation of Java methods from native code, then those classes might not be recognized as required in the primary DEX file.
So if you receive java.lang.NoClassDefFoundError, then you must manually specify these additional classes as required in the primary DEX file by declaring them with the multiDexKeepFile (shown above) or the multiDexKeepProguard (refer documentation) property in your build type. If a class is matched in either the multiDexKeepFile or the multiDexKeepProguard file, then that class is added to the primary DEX file.
For complete documentation on multidex refer https://developer.android.com/studio/build/multidex
Click to AndroidManifest.xml look at the bottom of the files page you gonna see "Text" and "Merged Manifes" then click to "Merged Manifest" -> Search for "android.support.v4.content.FileProvider" then change it with "androidx.core.content.FileProvider"
Thats it, exact solution.
change
<provider android:name="android.support.v4.content.FileProvider"
to
<provider android:name="androidx.core.content.FileProvider"
tested and works for me
FileProvider was introduced with Android API Level 22 which is Lollipop, that is why you did not face the ClassNotFoundException when testing on a Lollipop device. Android Kitkat does not have the FileProvider class and hence you are facing the exception. If you are looking to access files this post should help you out
My Android Studio main project uses two library projects (let's call it A and B). both A and B use my custom library (let's call C).
Upon compiling the main project, I got the following error:
Error:Execution failed for task
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/mycompany/Tracking.class
How can I exclude the offending class so that it does not get included more than once?
Thanks.
It means that there is the same class Tracking.class in both dependence libraries.
This errors usually occurs when importing .jar files.
Firstly, try to remove this in your build.gradle.
compile fileTree(dir: 'libs', include: '*.jar')
If it does not work, you should exclude the common module in one of these two libraries:
compile (A or B) {
exclude module: C
}
Please provide details of your build.gradle file if these above solution does not work.
I am building a library in Android Studio which has multiple Gradle dependencies such as the estimote and firebase sdk. When I export the library as an aar and then import it to a sample project, I get a run time error:
E/UncaughtException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/estimote/sdk/BeaconManager;
at com.mgenio.sdk.BeaconSDK.init(BeaconSDK.java:87)
at com.mgenio.sdk.BeaconSDK.checkSDK(BeaconSDK.java:214)
at com.mgenio.sdk.BeaconSDK.access$800(BeaconSDK.java:33)
at com.mgenio.sdk.BeaconSDK$3.onResponse(BeaconSDK.java:186)
at com.mgenio.sdk.BeaconSDK$3.onResponse(BeaconSDK.java:182)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.estimote.sdk.BeaconManager" on path: DexPathList[[zip file "/data/app/com.mgenio.smartscansample-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mgenio.smartscansample-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
Is there a special way to export my aar library so it includes these external dependencies? Thanks!
I had a similar issue some months ago, if you're publishing your library in a repository, then you have to add an xml or another kind of file that contains the dependencies your library have.
And if you need yo use any class of those dependencies, when you compile your library in the gradle file in your app module, you have to add transitive=true to have access to this classes or resources like this
compile('mx.segundamano.doubleseekbarview'){
transitive=true
}
EDIT
If you're publishing to Bintray - Maven repository, add this lines to your gradle file at the same level where your bintray configuration is
task generateSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier 'sources'
}
artifacts {
archives generateSourcesJar
}
Is there a special way to export my aar library so it includes these external dependencies?
No
Exept you deploy your aar into a maven or ivy repository.
These aar dependency have some inbuild functionality in itself such as ic-launcher icon , image,logo, animation and much more.
Dhamija Dependency is one of them. such as :-
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.0#aar'
compile 'com.daimajia.androidanimations:library:1.0.8#aar
but you can use a best commercial dependency inside aar Remove above dependency and use this :
implementation 'com.airbnb.android:lottie:2.5.4'
hope this will resolve your issue !
You just add the same dependencies to your sample project and let Gradle take care of it.
I have built a new project in Android Studio using the new project templates provided as part of the tool. All of the code has been generated by Studio I have not made any amendments yet.
I am attempting to run the code but the app fails with the following errors, not sure what the problem is so any help appreciated.
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\RichardKavanagh\AppData\Local\Android\android-sdk\build-tools\19.0.1\dx.bat --dex --output D:\Android\Projects\MyHealthRecord\app\build\libs\app-debug.dex D:\Android\Projects\MyHealthRecord\app\build\classes\debug D:\Android\Projects\MyHealthRecord\app\build\dependency-cache\debug D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\android-support-v7-appcompat-5a78dab7e2789bbe64f4bc80d106ca75c04dcf6f.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\classes-f9b947272e9f33ba50355b52d82755584f9c0c58.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\support-v4-19.0.0-31a2c13df80d37d62ca50fec3bde6da0ca706223.jar
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 12.948 secs
Like everyone else said here, the support library (com.android.support) is being included more than once in your project. Try adding this in your build.gradle at the root level and it should exclude the support library from being exported via other project dependencies.
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
If you have more then one support libs included in the dependencies like this, you may want to remove one of them:
dependencies {
compile 'com.android.support:support-v4:19.1.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
This gets conflicted if you have the support jar in your libs folder.
If you have the support jar in your project libs folder and you have the module dependency added to compile 'com.android.support:support-v4:13.0.+' the UNEXPECTED_TOPLEVEL_DEPENDANCY exception will be thrown.
Because you may include two same libs in your project.
check your build.gradle file.
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/android-support-v4.jar')
}
if your file includes compile 'com.android.support:appcompat-v7:+' and compile files('libs/android-support-v4.jar'), it will have this problems.
delete this sentence: compile files('libs/android-support-v4.jar')
That's how I fix this problem.
The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let’s say you have an App structure that looks like this:
So you have the main “app” and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library, 2) genes_nine_old_androids_library, & 3) swipe_list_view_library.
My name is Gene, so that’s why there are all these “gene” libraries.
Inside the build.gradle for “app”, I have:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
//compile project(':libraries:genes_nine_old_androids_library')
compile project(':libraries:swipe_list_view_library')
}
Inside the build.gradle for gene_test_library, I have nothing:
dependencies {
}
Inside build.gradle for gene_nine_old_androids_library, I have:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
Inside build.gradle for swipe_list_view_library, I have:
dependencies {
compile 'com.nineoldandroids:library:2.4.0+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
This line of code “compile fileTree(dir: 'libs', include: ['*.jar'])” just means “hey, look inside the ‘libs’ folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.
So let’s say I uncomment out //compile project(':libraries:genes_nine_old_androids_library')
In the build.gradle for the “app” module. Then I would get the “UNEXPECTED TOP-LEVEL EXCEPTION:” error. Why is that?
Well, writing //compile project(':libraries:genes_nine_old_androids_library') inside the build.gradle for “app”, is the same as taking the build dependencies of “genes_nine_old_androids_library” module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library') statement, the build.gradle for “app” module becomes:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
***compile fileTree(dir: 'libs', include: ['*.jar'])***
***compile 'com.android.support:appcompat-v7:21.0.0'***
compile project(':libraries:swipe_list_view_library')
}
Notice how now “compile 'com.android.support:appcompat-v7:21.0.0'” shows up 2x. That’s where the error is coming from.
I found 2 reason for this issue:
Sometimes its because of multiple included libraries. For example you add
compile 'com.nineoldandroids:library:2.4.0'
in your gradle and add another library that it also use "nineoldandroids" in it's gradle!
As Android Developer Official website said:
If you have built an Android app and received this error, then congratulations, you have a lot of code!
So, why?
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.
Then what should you do?
Avoiding the 65K Limit - How?
Review your app's direct and transitive dependencies - Ensure any large library dependency you include in your app is used in a manner that outweighs the amount of code being added to the application. A common anti-pattern is to include a very large library because a few utility methods were useful. Reducing your app code dependencies can often help you avoid the dex reference limit.
Remove unused code with ProGuard - Configure the ProGuard settings for your app to run ProGuard and ensure you have shrinking enabled for release builds. Enabling shrinking ensures you are not shipping unused code with your APKs.
Configuring Your App for Multidex with Gradle - How?
1.Change your Gradle build configuration to enable multidex.
put
multiDexEnabled true
in the defaultConfig, buildType, or productFlavor sections of your Gradle build file.
2.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: If your app uses extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. For more information, see the MultiDexApplication reference documentation.
Also this code may help you:
dexOptions {
javaMaxHeapSize "4g"
}
Put in your gradle(android{ ... } ).
Hi all I had the same issue that was being caused by a duplicate support version 4 file that I had included while trying to get parse integrated. Deleted the extra inclusion from the libs directory and it works fine now!
This happens when a library is getting compiled twice (i.e it is added two time). It can be support library or any other, it doesn't matter.
The common case is that you have added a compile statement of a library which is already in your libs/ directory. All the *.jar files are compiled automatically. Thus, adding a compile statement is causing the error. Removing that statement might fix this issue. If this is not applicable then we already have some awesome answers.
This might be the dumbest answer, but this worked for me :
I removed and added all the libraries on my project manually.(One after the other) And voila, it worked.
Build -> Rebuild project
Note: No library of mine was compiled twice.
Make sure you have downloaded Support Repository to use support library dependency in build.gradle.
If these all are there already installed sync your project with gradle once using the button available.
In my case TOP LEVEL EXCEPTION was throw because of a special char in project path. Just closed the project, changed "á" to "a" and reopened the project. Works!
Suddenly, without any major change in my project, I too got this error.
All the above did not work for me, since I needed both the support libs V4 and V7.
At the end, because 2 hours ago the project compiled with no problems, I simply told Android Studio to REBUILD the project, and the error was gone.
I had similar problem when I tried to build a signed apk for my app.
Strange, it happened only when I wanted to build a release apk, while on debug apk everything worked OK.
Finally, looking on this thread, I checked for support library duplications in build.gradle and removed any duplications but this wasn't enough..
I had to do clean project and only then finally I've got it to work.
I know that the problem was answered, but this could happen again and my solution was a little different from the ones that I found. In my case the solution wasn't related to include two different libraries in my project. See code below:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
This code was giving that error "Unexpected Top-Level Exception".
I fix the code making the following changes:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
I solved my problem with adding these in build gradle:
defaultConfig {
multiDexEnabled true
dependencies {
compile 'com.android.support:multidex:1.0.0'
another solution can be removing unnecessary libraries