Start a Project

Multidex:  a perfect solution for UNEXPECTED TOP-LEVEL EXCEPTION in Gradle

Multidex:

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.

But it is risky to follow because it may cause your application to run slow or may crash. There is a story behind it:

The installation of .dex files during startup onto a device’s data partition is complex and can result in Application Not Responding (ANR) errors if the secondary dex files are large. In this case, you should apply code shrinking techniques with ProGuard to minimize the size of dex files and remove unused portions of code.
Applications that use multidex may not start on devices that run versions of the platform earlier than Android 4.0 (API level 14) due to a Dalvik linearAlloc bug (Issue 22586). If you are targeting API levels earlier than 14, make sure to perform testing with these versions of the platform as your application can have issues at startup or when particular groups of classes are loaded. Code shrinking can reduce or possibly eliminate these potential issues.

 

When your application has multiple declarations of library in Gradle or has increased the size of the library and jar files than the given limits by android architecture, build system reports this error as follows:

UNEXPECTED TOP-LEVEL EXCEPTION:

You may try multiple options as:

1) Remove some jar file from Libs folder and copy to some other folder, And
Go to Project Properties > Select Java Build Path, Select Libraries, Select Add External Jar, Select the Removed jar to your project, Click save.this will be added under Referenced Library instead of Libs folder. Now clean and Run your project.

this will be added under Referenced Library instead of Libs folder. Now clean and Run your project.

2) It would be better to work with Proguard. It reduces the unnecessary code from our application. It will decrease the size of the dex file by half.

3) Setting up your app development project to use a multidex configuration.

In particular, you need to perform the following steps to use multidex:

Change your Gradle build configuration to enable multidex
Modify your manifest to reference the MultiDexApplication class
Modify your app Gradle build file configuration to include the support library and enable multidex output, as shown in the following Gradle build file:

In your manifest add the MultiDexApplication class from the multidex support library to the application element.

Testing Multidex Apps is also typical:

When using instrumentation tests with multidex apps, additional configuration is required to enable the test instrumentation. Because the location of code for classes in multidex apps is not within a single DEX file, instrumentation tests do not run properly unless configured for multidex.
To test a multidex app with instrumentation tests, configure the MultiDexTestRunner from the multidex testing support library. The following sample build.gradle file demonstrates how to configure your build to use this test runner:

Reference: http://developer.android.com/tools/building/multidex.html

 

 

Exit mobile version