9

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

12
  • Try to clean and build project Commented Mar 30, 2017 at 11:22
  • I have tried that, I also invalidate cache and restart the project. Nothing is helping :( Commented Mar 30, 2017 at 11:24
  • @MD : I have also tried this force 'com.squareup.retrofit2:retrofit:2.2.0';, but then it fails to compile gradle and gives this error message. Gradle sync failed: A conflict was found between the following modules: - com.squareup.okhttp3:okhttp:3.2.0 - com.squareup.okhttp3:okhttp:3.6.0 Commented Mar 30, 2017 at 11:26
  • post proguard-rules file Commented Apr 20, 2017 at 5:51
  • 1
    @Kirmani88 check Debugging dex issues @ medium.com/google-developers/… Commented Apr 25, 2017 at 17:54

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer, I have updated my question. I have enabled multidex in gradle file. I am also using NDK there.
did you add compile 'com.android.support:multidex:1.0.1' in dependency ?
Yes, its there.
i have set minimum SDK to 21
Add your multi dex to your application class too
1

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' 

Comments

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); } 

Comments

0

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!

1 Comment

Try to use the latest version of Facebook SDK. And one more thing Your "targetSDKVersion" differs from "compileSDKversion". Try to use the latest one for both.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.