Getting TypeNotPresentException followed by ClassN

2019-06-16 06:33发布

问题:

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 ClassNotFoundException or 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.

NOTE : 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)
            at $Proxy0.userLogin(Native Method)
            at com.codebrew.embazaar.MainActivity.loginUser(MainActivity.java:529)
            at com.codebrew.embazaar.MainActivity.onClick(MainActivity.java:324)
            at android.view.View.performClick(View.java:4748)
            at android.view.View$PerformClick.run(View.java:19535)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: com.codebrew.embazaar.pojo.UserLoginPojo
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:251)
            at libcore.reflect.ParameterizedTypeImpl.getRawType(ParameterizedTypeImpl.java:61)
            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)
            at $Proxy0.userLogin(Native Method)
            at com.codebrew.embazaar.MainActivity.loginUser(MainActivity.java:529)
            at com.codebrew.embazaar.MainActivity.onClick(MainActivity.java:324)
            at android.view.View.performClick(View.java:4748)
            at android.view.View$PerformClick.run(View.java:19535)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: com/codebrew/embazaar/pojo/UserLoginPojo
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:251)
            at libcore.reflect.ParameterizedTypeImpl.getRawType(ParameterizedTypeImpl.java:61)
            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)
            at $Proxy0.userLogin(Native Method)
            at com.codebrew.embazaar.MainActivity.loginUser(MainActivity.java:529)
            at com.codebrew.embazaar.MainActivity.onClick(MainActivity.java:324)
            at android.view.View.performClick(View.java:4748)
            at android.view.View$PerformClick.run(View.java:19535)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.codebrew.embazaar.pojo.UserLoginPojo" on path: DexPathList[[zip file "/data/app/com.codebrew.embazaar-11.apk"],nativeLibraryDirectories=[/data/app-lib/com.codebrew.embazaar-11, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:251)
            at libcore.reflect.ParameterizedTypeImpl.getRawType(ParameterizedTypeImpl.java:61)
            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)
            at $Proxy0.userLogin(Native Method)
            at com.codebrew.embazaar.MainActivity.loginUser(MainActivity.java:529)
            at com.codebrew.embazaar.MainActivity.onClick(MainActivity.java:324)
            at android.view.View.performClick(View.java:4748)
            at android.view.View$PerformClick.run(View.java:19535)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

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)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

**IMPORTANT: 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 MainActivity CommonPojo and gradle file

回答1:

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:

  • remove multiDexEnabled = true, but you may exceed the 65K method limit.
  • follow the instructions on https://developer.android.com/tools/building/multidex.html, specifically adding the

    compile 'com.android.support:multidex:1.0.0'
    

    dependency, and making your application object inherit or start at MultiDexApplication.