Search code examples
androidnoclassdeffounderror

NoClassDefFoundError attempting to use FatSecret REST API in Android Studio


I am using the FatSecretAPI in order to retreive items from their database.

I have used this library - http://ranesr.github.io/fatsecret4j/ and done the following:

  1. Created a new module in Android Studio as a new Java Library
  2. Copied the relevant java files across as per the structure provided in the link
  3. Located File > Project Structure... > Modules > my app > Dependencies and added the fatsecret module as a dependency
  4. Called the FatSecretAPI class and instantiated with both my keys as per:

FatSecretAPI api = new FatSecretAPI("my auth key", "my shared key");

  1. Cleaned and Rebuilt the project.
  2. Upon running the app and opening the activity this call is in, I get the following error:

    FATAL EXCEPTION: main
            Process: com.example.chris.rapical, PID: 7140
            java.lang.NoClassDefFoundError: Failed resolution of: Lcom/fatsecret/platform/FatSecretAPI;
            at com.example.chris.rapical.FoodSearchOnlineActivity.onCreate(FoodSearchOnlineActivity.java:51)
            at android.app.Activity.performCreate(Activity.java:6500)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
            at android.app.ActivityThread.access$1000(ActivityThread.java:198)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
            Caused by: java.lang.ClassNotFoundException: Didn't find class "com.fatsecret.platform.FatSecretAPI" on path: DexPathList[[zip file "/data/app/com.example.chris.rapical-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.example.chris.rapical.FoodSearchOnlineActivity.onCreate(FoodSearchOnlineActivity.java:51) 
            at android.app.Activity.performCreate(Activity.java:6500) 
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
            at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:145) 
            at android.app.ActivityThread.main(ActivityThread.java:6837) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
           Suppressed: java.lang.ClassNotFoundException: com.fatsecret.platform.FatSecretAPI
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 15 more
            Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
    

Things I have tried:

  • Cleaning and rebuilding the app
  • Deleting the /class folder and re-building
  • Running 'gradlew clean' on CMD at project location

All 3 have no successs.


Solution

  • Figured it out - When I was going to create the new Module, I selected Java library instead of Android library. Duh.