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.
... 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
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();
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)聽
Do you guys use multidex? If so, make sure that in the Application.attachBaseContext() you're initializing it correctly.
yup, i'm using multiDexEnabled true,
would you like to show the snippet?
Yes, I used multiDexEnabled true too :-)
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 ....
Use either MultidexApplication or initialize it manually.
Here is documentation http://developer.android.com/tools/building/multidex.html
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.
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 ?
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'
}
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').
Closing since this was determined to be a build system problem and not a Retrofit bug.
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 :)
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
Most helpful comment
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 :)