Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError on Android version lower than Lollipop

I have a Nexus 5 device with Android 21 on it. I am testing my project on it with no problem. However, if I test on an Emulator with Google APIs 19 it shows the error java.lang.NoClassDefFoundError. How could this error occur if it is running well on Lollipop? I even asked my friend with a Sony 4.4 device and it also crashes on his phone. I have my Compile SDK version to 21 and Build Tools set to 21.1.2. I tried gradle clean and I am working on Android Studio. Any help would be highly appreciated.

This error occurs only on devices that are lower than Lollipop:

This is what appears in Logcat:

02-10 07:46:19.200    2238-2238/com.myproject.android.indonesia E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myproject.android.indonesia, PID: 2238
    java.lang.NoClassDefFoundError: com.myproject.android.indonesia.task.GetProjectTask$1
            at com.myproject.android.indonesia.task.GetProjectTask.execute(GetProjectTask.java:27)
            at com.myproject.android.indonesia.activity.SplashActivity.launchProjectTask(SplashActivity.java:111)
            at com.myproject.android.indonesia.activity.SplashActivity.onCreate(SplashActivity.java:71)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Here is the snippet that calls the getProjectTask()

GetProjectTask mGetProjectTask = new GetProjectTask(this);
mGetProjectTask.mUrl.append(Constants.Link.HOST).append(Constants.Webservice.GET_PROJECT);
mGetProjectTask.params.put(Constants.Key.PRJ_LICENCE, Constants.Constant.LICENCE);
mGetProjectTask.params.put(Constants.Key.LANGUAGE, currentLanguage);
mGetProjectTask.params.put(Constants.Key.CTY_NAME, Constants.Value.APP_CTY_NAME);
mGetProjectTask.execute();

UPDATE It seems that I needed to use the latest version of one of the SDKs I am using to avoid this error (Aviary SDK). When I upgraded, this error did not appear anymore.

like image 550
SleepNot Avatar asked Feb 10 '15 12:02

SleepNot


2 Answers

Your class (the first anonymous class in GetProjectTask) extends a class that is not available on that platform.

like image 171
StenSoft Avatar answered Nov 11 '22 12:11

StenSoft


If you look carefully at the API documentation you will notice "added in API x" at the top right corner of class documentation, and similar text in each and every API method.

For example see AnimatedVectorDrawable - it was added at API level 21 (Lollipop).

Attempting to use a class that was added at API 21 on older devices will fail with the exception that you hit. That is not surprising since unfortunately the newer features are not available in older OS versions.

You can overcome this exception in several ways: using compatibility libraries, enabling new features only if the phone supports it (by checking Build.Version.SDK_INT or using api level specific res folder eg. layout-v21.

Another alternative is to set the minSdkVersion to 21, then your app will refuse to install on older devices (and will not be available in their Google Play stores).

See Android docs on compatibility for more details on this subject.

like image 33
Iftah Avatar answered Nov 11 '22 11:11

Iftah