java.lang.NoClassDefFoundError retrofit2.Utils

后端 未结 11 1509
梦毁少年i
梦毁少年i 2020-12-30 10:40

I\'m using Retrofit to handle the Serverside Data from Mobile. After Implementing retrofit, I am Getting the below Exception.

What am I doing wrong?

11条回答
  •  难免孤独
    2020-12-30 11:21

    This error will come because of MultiDexApplication .I have face this kind of issue with some other library not same library but some other library.It will error of the retrofit library because its initialization of app start up where dex(in which your retrofit library code is converted to dex) file is not to set(install).

    To resolve that you need to handle Multiple Dex file. with the help of application build.gradle & Application class

    below changes which is required in build.gradle file

    dexOptions {
            incremental true
            // here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY
    
            javaMaxHeapSize "4g"
        }
    
    
    dependencies {
         compile 'com.android.support:multidex:1.0.1'
        //    your dependencies which you are using.
    
    }
    

    entire build.gradle

    android {
        signingConfigs {
            /*
            releasebuild {
                keyAlias 'hellotest'
                keyPassword 'hellotest'
                storeFile file('path to keystore')
                storePassword 'hellotest'
            }
            */
        }
        compileSdkVersion 'Google Inc.:Google APIs:22'
        buildToolsVersion '23.0.0'
        /* if you got error regarding duplicate file of  META-INF/LICENSE.txt from jar file
        packagingOptions {
            exclude 'META-INF/LICENSE.txt'
        }
        */
        dexOptions {
            jumboMode = true
            incremental true
            // here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY
    
            javaMaxHeapSize "4g"
        }
        defaultConfig {
            multiDexEnabled true
            applicationId "com.myapp.packagenme"
            minSdkVersion 17
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.releasebuild
            }
            debug {
                signingConfig signingConfigs.releasebuild
            }
        }
    }
    
    dependencies {
         compile 'com.android.support:multidex:1.0.1'
        //    your dependencies which you are using.
    
    }
    

    If your app uses extends the Applicationclass, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. To install multipledex file context using Applicaiton class which should extends [MultiDexApplication][2]

    public class MyAppClass extends MultiDexApplication{
    @Override
        protected void attachBaseContext(Context newBase) {
            MultiDex.install(newBase);
            super.attachBaseContext(newBase);
        }
    }
    

    Suggestion

    Selectively compiling APIs into your executable

    Don't use entire google play service use only required library.From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

    compile 'com.google.android.gms:play-services:8.4.0'
    

    with these lines:

    compile 'com.google.android.gms:play-services-fitness:8.4.0'
    compile 'com.google.android.gms:play-services-wearable:8.4.0'
    

    With the reference of my answer https://stackoverflow.com/a/34948154/1140237

提交回复
热议问题