Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoClassDefFoundError on Android 4.1.2 #1628

Closed
morningstar84 opened this issue Feb 26, 2016 · 14 comments
Closed

NoClassDefFoundError on Android 4.1.2 #1628

morningstar84 opened this issue Feb 26, 2016 · 14 comments

Comments

@morningstar84
Copy link

Hi, everybody.
I write because I have encountered a problem while using the retrofit2 library on Samsung Galaxy S2 (Android 4.1.2).
While performing any network request, the application dies with the following stack trace:

java.lang.ExceptionInInitializerError at com.vwgroup.audiservice.bolts.TaskManager.registrationTask(TaskManager.java:41) at com.vwgroup.audiservice.ui.activity.ImbServiceTest.performNetworkrequest(ImbServiceTest.java:49) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619) Caused by: java.lang.NoClassDefFoundError: retrofit2.Retrofit$Builder at com.vwgroup.audiservice.network.ServiceGenerator.(ServiceGenerator.java:24) ... 33 more

The same code run without any issue on Android 5.0, 5.1, 6

My Gradle dependencies are as follows:

compile ('com.squareup.retrofit2:retrofit:2.0.0-beta4'){ exclude group: 'com.android.support' } compile ('com.squareup.retrofit2:converter-gson:2.0.0-beta4'){ exclude group: 'com.android.support' } compile ('com.squareup.retrofit2:converter-simplexml:2.0.0-beta4'){ exclude module: 'xpp3' exclude module: 'stax' exclude module: 'stax-api' exclude group: 'com.android.support' } compile 'com.squareup.okhttp:okhttp:2.7.4'

Any help will be appreciated.

thanks

@ghost
Copy link

ghost commented Feb 26, 2016

today i got same error after update to version:

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

previously i was using version :

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta2'

and everything running well.

error :

Could not find class 'retrofit2.Retrofit$Builder'

code :

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://myserver.com")
    .addConverterFactory(GsonConverterFactory.create())
    build();

@hbenslama
Copy link

I got same error using the beta4 version :
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' }

And my ServiceGenerator :
`public class ServiceGenerator {

public static final String API_BASE_URL = "http://my_url.com";

private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

/**
 * Instanciate Service
 * @param serviceClass
 */
public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.build();
    return retrofit.create(serviceClass);
}

}
`

and i got this error :

Caused by: java.lang.NoClassDefFoundError: retrofit2.Retrofit$Builder at com.wasten.service.ServiceGenerator.<clinit>(ServiceGenerator.java:13) at com.wasten.activity.child.UserListActivity.getUserListData(UserListActivity.java:84)  at com.wasten.activity.child.UserListActivity.onCreate(UserListActivity.java:54)  at android.app.Activity.performCreate(Activity.java:5047)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)  at android.app.ActivityThread.access$700(ActivityThread.java:134)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:4867)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)  at dalvik.system.NativeStart.main(Native Method) 

@artem-zinnatullin
Copy link
Contributor

Do you guys use multidex? If so, make sure that in the Application.attachBaseContext() you're initializing it correctly.

@ghost
Copy link

ghost commented Feb 27, 2016

yup, i'm using multiDexEnabled true,
would you like to show the snippet?

@morningstar84
Copy link
Author

Yes, I used multiDexEnabled true too :-)

@hbenslama
Copy link

Yes, I used multiDexEnabled true too, but i don't think that this is the cause of the problem,
I have invalidate Cache using Android Studio,
I have deleted the cache gradle using Terminal ..
But i have always the problem,
im using Android 4.1.2 and retrofit2.

i got always :
Caused by: java.lang.NoClassDefFoundError: retrofit2.Retrofit$Builder at ....

@artem-zinnatullin
Copy link
Contributor

Use either MultidexApplication or initialize it manually.

Here is documentation http://developer.android.com/tools/building/multidex.html

@ghost
Copy link

ghost commented Feb 28, 2016

today, i have created new simple app using retrofit2 beta4 and used multiDexEnabled true. But the error not appear. my app running well.
But my other app using retrofit2 beta4 and multiDexEnabled true still error.

@hbenslama
Copy link

Hi Guys,

I solved my problem by changing one library in gradle,
the cause of this exception isn't retrofit2-beta4, but another library that use an older version of retrofit, so that create the conflict in the classpath during runtime.

retrofit solved the conflict problem by renaming package to retrofit2.

@PaijoRX can u paste here all your dependancies that you use in Gradle please ?

@ghost
Copy link

ghost commented Feb 28, 2016

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        applicationId "com.simp.ams"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile project(':swiperefreshlayoutlibrary')
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
    compile 'com.android.support:design:23.1.1'
    compile 'com.google.android.gms:play-services:8.4.0'
}

@ghost
Copy link

ghost commented Feb 28, 2016

finally i solved my error.
remove multiDexEnabled true from gradle and replace compile 'com.google.android.gms:play-services:8.4.0' whit specific depedencies i used ('com.google.android.gms:play-services-location:8.4.0').

@JakeWharton
Copy link
Member

Closing since this was determined to be a build system problem and not a Retrofit bug.

@at-longcb
Copy link

Ya, Exactly multiDexEnabled true is the problem here. Instead of remove it, you can add make your application extends android.support.multidex.MultiDexApplication, dependence is
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
So, To me, This is how I solve problem :)

@VishalJogiya
Copy link

VishalJogiya commented May 8, 2017

at-longcb commented on 16 Mar 2016

I tried removing " multiDexEnabled true" ,
tried adding compile 'com.android.support:multidex:1.0.0'

# But Finally I fixed this issue by extending my base Application class
by public class MyApplication extends MultiDexApplication { ... }

Updated retrofit lib from 1.9.0 -to - :2.2.0'

dependencies {
........
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.android.support:multidex:1.0.1'
}

defaultConfig {
.......
multiDexEnabled true
}

Please Check this link to know more about multidex enable

(https://developer.android.com/studio/build/multidex.html)

Hope This would help to someone else facing the same issue
--Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants