Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

android - What is the best way to solve the UNEXPECTED TOP-LEVEL EXCEPTION?

When I make the application use many libraries. I usualy meet a error is UNEXPECTED TOP-LEVEL EXCEPTION

This error have some reason as:

  • Number function > 65k, we need enable multidex for it
  • There are some libs is nested, we need add exclude in build.gradle.

About second case, it is very difficult to detect what is the lib have been nested.

If you have experience on this issue, please share me.

UPDATE:

dependencies {
    compile files('libs/ormlite-android-4.48.jar')
    compile files('libs/ormlite-core-4.48.jar')
    //    compile 'com.obsez.android.lib.filechooser:filechooser:1.1.2'
    //    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'cn.lightsky.infiniteindicator:library:1.0.5'
    compile 'com.firebase:firebase-client-android:2.3.1'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'com.mcxiaoke.volley:library:1.0.15'
    compile('com.google.apis:google-api-services-drive:v2-rev170-1.20.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    compile('com.google.api-client:google-api-client-android:1.20.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    compile('com.google.android.gms:play-services:8.4.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile 'com.nononsenseapps:filepicker:2.4.1'
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best ways to solve UNEXPECTED TOP-LEVEL EXCEPTION? I will suggest you for this Two step to solve this.

  • Check the attached libraries Dependency/hierarchy
  • Check which library class or files are cause of the duplication files or methods count exceed 65k


  • How to Check attached libraries Dependency/hierarchy ?

First found the dependency which is used in library which is attached/compiled from the maven/jcenter/mavencentral.Major issue will come with support because it may possible that support-v4 & appcompact already contains in library which is attached from maven (like play service contains v4 ,appcompact attached then no need to attach support-v4 as it already contains.)

How to check Dependency/hierarchy of the attached library in gradle

Gradle menu > :modulename >> Tasks >> android >> Run "androidDependcies"

OR

Gradle menu > :app >> Tasks >> android >> Run "androidDependcies"

By doing so in Gradle Console you will get something like below as a output(output may depends on libraries attached) .Which can give idea about duplication/hierarchy of the libraries

release
+--- LOCAL: picasso-2.5.2.jar
+--- LOCAL: YouTubeAndroidPlayerApi.jar
+--- LOCAL: commons-lang-2.6.jar
+--- LOCAL: signpost-core-1.2.1.1.jar
+--- LOCAL: kxml2_2.3.0.jar
+--- LOCAL: classes.jar
+--- LOCAL: signpost-commonshttp4-1.2.1.1.jar
+--- LOCAL: json-simple-1.1.1.jar
+--- LOCAL: universal-image-loader-1.9.3.jar
+--- LOCAL: linkedin-j-.jar
+--- LOCAL: maps.jar
+--- LOCAL: nineoldandroids-2.4.0.jar
+--- com.android.support:multidex:1.0.1
+--- com.android.support:multidex:1.0.1
+--- com.android.support:appcompat-v7:23.1.1
|    --- com.android.support:support-v4:23.1.1
|         --- LOCAL: internal_impl-23.1.1.jar
+--- com.google.android.gms:play-services-maps:7.3.0
|    --- com.google.android.gms:play-services-base:7.3.0
|         --- com.android.support:support-v4:23.1.1
|              --- LOCAL: internal_impl-23.1.1.jar
+--- com.google.android.gms:play-services-gcm:7.3.0
|    --- com.google.android.gms:play-services-base:7.3.0
|         --- com.android.support:support-v4:23.1.1
|              --- LOCAL: internal_impl-23.1.1.jar
+--- com.google.android.gms:play-services-location:7.3.0
|    +--- com.google.android.gms:play-services-base:7.3.0
|    |    --- com.android.support:support-v4:23.1.1
|    |         --- LOCAL: internal_impl-23.1.1.jar
|    --- com.google.android.gms:play-services-maps:7.3.0
|         --- com.google.android.gms:play-services-base:7.3.0
|              --- com.android.support:support-v4:23.1.1
|                   --- LOCAL: internal_impl-23.1.1.jar
+--- com.google.android.gms:play-services-ads:7.3.0
|    +--- com.google.android.gms:play-services-base:7.3.0
|    |    --- com.android.support:support-v4:23.1.1
|    |         --- LOCAL: internal_impl-23.1.1.jar
|    --- com.google.android.gms:play-services-analytics:7.3.0
|         --- com.google.android.gms:play-services-base:7.3.0
|              --- com.android.support:support-v4:23.1.1
|                   --- LOCAL: internal_impl-23.1.1.jar
+--- com.google.android.gms:play-services-analytics:7.3.0
|    --- com.google.android.gms:play-services-base:7.3.0
|         --- com.android.support:support-v4:23.1.1
|              --- LOCAL: internal_impl-23.1.1.jar
+--- com.google.android.gms:play-services-plus:7.3.0
|    --- com.google.android.gms:play-services-base:7.3.0
|         --- com.android.support:support-v4:23.1.1
|              --- LOCAL: internal_impl-23.1.1.jar
--- com.android.support:recyclerview-v7:23.1.1
     --- com.android.support:support-v4:23.1.1
          --- LOCAL: internal_impl-23.1.1.jar

You can also check this from terminal for the above process .

gradlew app:dependencies

You can check below image as "picture is worth a thousand words"

picture is worth a thousand words

How to check which class or files causing duplicate entry/or method count exceed 65k ?

Rather than checking on message or error which you are getting Message tab of android studio check it in Gradle Console

If in message error is like while transferring classes to dex :app:transformClassesWithDexForDebug or :app:transformClassesWithDexForRelease maximum chances are like because of Multiple dex (method excess 65k limit).

Or if any other error than it will specify like duplicate entry related etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...