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

dev90
  • 7,187
  • 15
  • 80
  • 153
  • Try to clean and build project – M D Mar 30 '17 at 11:22
  • I have tried that, I also invalidate cache and restart the project. Nothing is helping :( – dev90 Mar 30 '17 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` – dev90 Mar 30 '17 at 11:26
  • post proguard-rules file – PEHLAJ Apr 20 '17 at 05:51
  • @user12345 I have not enabled Pro-guard in my project – dev90 Apr 21 '17 at 07:18
  • Do you really need this line? `force 'com.squareup.okhttp3:okhttp:3.6.0'` Try running Gradle dependencies report. – gustavogbc Apr 24 '17 at 14:24
  • have you tried the latest version of facebook sdk 4.22.0, they fixed a few bugs after the version you're using, might fix the problem. – Ankit Arora Apr 25 '17 at 16:04
  • 1
    @Kirmani88 check Debugging dex issues @ https://medium.com/google-developers/making-the-most-of-the-apk-analyzer-c066cb871ea2 – Raghunandan Apr 25 '17 at 17:54
  • Have you tried using the most recent version of the SDK? 4.22? – BlackHatSamurai Apr 25 '17 at 19:59
  • how you fix that? Have the same problem only on lollipop devices – no news May 17 '17 at 13:12
  • @Kirmani88 Did you solve this issue. I am getting the same issue for API 21 and 22 only. minSdk is 21. – X-HuMan Sep 27 '17 at 13:56
  • @karate: The problem was in gradle configuration. Make sure `preDexlibraries` command is not there. Thats one case there can be other multiple issues – dev90 Oct 10 '17 at 07:27

4 Answers4

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

Manohar
  • 22,116
  • 9
  • 108
  • 144
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'
Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
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);
}
DevKRos
  • 422
  • 2
  • 15
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!

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
  • 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. – Zohaib Hassan Apr 25 '17 at 21:41