Hot questions for Handling TypeNotPresentException in Retrofit
Getting TypeNotPresentException followed by ClassNotFoundException or NoClassDefFoundError on All android versions except android L
Question: I am getting very interesting bug in one of my apps. My app is working fine on android L, But as soon as i try to run it on other android versions, I am getting
NoClassDefFoundError while trying to call web services using Retrofit, even though all those classes are existing and working fine on my android L device. I don't know what's happening right now, app is running in other versions as long as i don't make a Network Request but as soon as i go for Network Request, it gives me either ClassNotFoundException or NoClassDefFoundError. Also I am using Android Studio, so if someone has faced this kind of issue earlier, then please help, i am in severe need.
App is working fine on Android L and also classes in which i am getting ClassNotFoundException or NoClassDefFoundError are not version specific i.e it was not like that they are supported in android L or so. All classes in which i am getting these errors are general custom/model classes and has no link with the higher android versions or something.
And these are my Logcat errors
This is the exception while hitting login api
java.lang.TypeNotPresentException: Type com.codebrew.embazaar.pojo.UserLoginPojo not present at libcore.reflect.ParameterizedTypeImpl.getRawType(ParameterizedTypeImpl.java:63) at libcore.reflect.ParameterizedTypeImpl.getResolvedType(ParameterizedTypeImpl.java:72) at libcore.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:70) at libcore.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.java:55) at libcore.reflect.ParameterizedTypeImpl.getResolvedType(ParameterizedTypeImpl.java:71) at libcore.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:70) at libcore.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.java:55) at libcore.reflect.Types.getTypeArray(Types.java:50) at java.lang.reflect.Method.getGenericParameterTypes(Method.java:216) at retrofit.RestMethodInfo.parseResponseType(RestMethodInfo.java:250) at retrofit.RestMethodInfo.<init>(RestMethodInfo.java:97) at retrofit.RestAdapter.getMethodInfo(RestAdapter.java:213) at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:236)
and this one i am getting while hitting forgot password network request
java.lang.NoClassDefFoundError: com.codebrew.embazaar.MainActivity$7$1 at com.codebrew.embazaar.MainActivity$7.onClick(MainActivity.java:392) at android.view.View.performClick(View.java:4748) at android.view.View$PerformClick.run(View.java:19535) ...
As I debugged my code, am getting TypeNotPresentException which is an unchecked exception followed by these ClassNotFoundException or NoClassDefFoundError. I am mentioning this here because i think may be this thing can make some clarity about my question/problem.
Here is my gradle file>
Answer: This is because you have
multiDexEnabled = true
on your gradle file.
When you enable multidex, you have to make sure that all the classes.dex files are loaded when your application starts. Otherwise it will fail loading classes that is not in the first classes.dex file.
This is not needed in Android L, since it supports loading multiple dex files on startup, not only one.
In order to accomplish this, you can try one of the following:
multiDexEnabled = true , but you may exceed the 65K method limit.
2. follow the instructions on https://developer.android.com/tools/building/multidex.html, specifically adding the
dependency, and making your application object inherit or start at