Fix Gradle Error: UNEXPECTED TOP-LEVEL EXCEPTION
1 min read

Fix Gradle Error: UNEXPECTED TOP-LEVEL EXCEPTION

Fix Gradle Error: UNEXPECTED TOP-LEVEL EXCEPTION

Today I got the lovely UNEXPECTED TOP-LEVEL EXCEPTION from the dexer in android. Looking on the net yielded several causes and resolutions.

Note: I'm using Gradle 2.10, Android Studio 2.0.0-beta2

Caching

Apparently, sometimes Gradle is shooting itself in the foot with the caching. The first thing you can do is to clean your project. Remove all build directories, all Gradle caches and rebuild.

Some references:

Duplicate inclusion

It looks like this is the most common reason for such error. >Duplicate inclusion can occur when your app uses library X and a library Y , but Y is also using X.

Most notable example is support-v4 in com.android.support which got included in support-v13.

You can investigate this at some extend by showing the dependencies in gradle.

More references:

Java version mismatch

This is what actually happened to me... I've got an android project with a main application app and a library lib. App is an android application (and marked in build.gradle accordingly). Lib was defined as a java library (using the java gradle plugin). Dex was looking at the classes defined in lib and could not understand them ... because they were compiled as JAVA_1_8 while the android app was JAVA_1_7.

My solution (somewhat bad and overkill) was to convert lib to be an android library (which required adding a dummy Manifest file and changing the gradle file accordingly). This may pan out well since I want to keep it common to several modules.

If you have a simple jar, you may want to look at fixing the code generation in gradle to JAVA_1_7 instead.

HTH,