12

I get java.lang.NoClassDefFoundError: com.facebook.android.R$layout error when i link my project to facebookSDK jar file that i have copied into my project lib folder, instead of linking my project to facebookSDK library project in my workspace. It works fine when i link to library project in workspace.

Can anyone tell me how to solve this issue. I am using facebook sdk 3.0 for android. Thanks in advance.

EDIT: after generating jar file with warnings i got the following error in my logcat

01-17 12:42:04.790: E/AndroidRuntime(3073): FATAL EXCEPTION: main
01-17 12:42:04.790: E/AndroidRuntime(3073): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.games.game/com.facebook.LoginActivity}: android.content.res.Resources$NotFoundException: File 296108030489520 from xml type layout resource ID #0x7f030001
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.os.Looper.loop(Looper.java:143)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.ActivityThread.main(ActivityThread.java:4196)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at java.lang.reflect.Method.invoke(Method.java:507)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at dalvik.system.NativeStart.main(Native Method)
01-17 12:42:04.790: E/AndroidRuntime(3073): Caused by: android.content.res.Resources$NotFoundException: File 296108030489520 from xml type layout resource ID #0x7f030001
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1934)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1889)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.content.res.Resources.getLayout(Resources.java:740)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.Activity.setContentView(Activity.java:1702)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at com.facebook.LoginActivity.onCreate(LoginActivity.java:55)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
01-17 12:42:04.790: E/AndroidRuntime(3073):     ... 11 more
01-17 12:42:04.790: E/AndroidRuntime(3073): Caused by: java.io.FileNotFoundException: 296108030489520
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.content.res.AssetManager.openXmlAssetNative(Native Method)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:524)
01-17 12:42:04.790: E/AndroidRuntime(3073):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
01-17 12:42:04.790: E/AndroidRuntime(3073):     ... 20 more
glo
  • 1,408
  • 3
  • 25
  • 54
  • try copying your jar to libs folder instead of lib. – N-JOY Jan 17 '13 at 06:12
  • 4
    The 3.0 version of the SDK now includes views (like the LoginButton, FriendPicker, etc) that includes resource files. This means that the SDK can no longer be included as just a jar file, but MUST be included as an Android library. Sorry, but that's a limitation of the standard Android build system. – Ming Li Jan 17 '13 at 17:32
  • Thank you. Now i am linking it as a library project from my workspace as i see no other way. – glo Jan 18 '13 at 08:47

2 Answers2

8

It's because inside JAR doesn't contain resource folder of Facebook SDK Project.

There're 2 solutions for this:

  1. Add Facebook SDK Project as a project library.

  2. Copy file Facebook SDK JAR to folder libs of current projects and copy all resources from Facebook SDK Project to current project.

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
2

It means that your JAR file does not have the all class files.

NoClassDefFoundError appears only when it fails to find .class file of java class.

as the class file is not present you cant access any function or variable from that class.

To solve this issue,

n eclipse when you export the jar file it includes only the classes which don't have any errors or warnings.So, to generate jar file with all the classes including warnings you need to select generate Jar with warnings.

Then put this jar file in libs folder instead of lib folder.

Then add this jar to your build path.

As you want to import the resources,

Since Android makes R class automatically with resources files under /res folder, using R class as final static is impossible.

in your source code which will be exported in jar file, DON'T USE R variable because it will be replaced with final static memory address in compile time. Instead of using R, use method below.

 public static int getResourseIdByName(String packageName, String className, String name) {
       Class r = null;
       int id = 0;
    try {
        r = Class.forName(packageName + ".R");

        Class[] classes = r.getClasses();
        Class desireClass = null;

        for (int i = 0; i < classes.length; i++) {
            if(classes[i].getName().split("\\$")[1].equals(className)) {
                desireClass = classes[i];

                break;
            }
        }

        if(desireClass != null)
            id = desireClass.getField(name).getInt(desireClass);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return id;

    }

For example, if you have a layout named "main.xml", you can get it by calling method

int id = getResourceIdByName(context.getPackageName(), "layout", "main");

and if you have a string whose id is "text1", you can get it by calling method

int id = getResourceIdByName(context.getPackageName(), "string", "text1");

this method gives you your resource id in runtime. It uses reflection api to get R's status in runtime.

So now you can avoid using R variable and resources ewrrors by using this method

copy your res to target project.

And finally run your project.

dd619
  • 5,910
  • 8
  • 35
  • 60
  • I do not see any errors or warnings in my facebookSDK library bi=ut still how can i generate jar with warnings. – glo Jan 17 '13 at 06:23
  • right click on project->export jar->next->export jar file with compile warning – dd619 Jan 17 '13 at 06:30
  • **Found duplicate file for APK: bin/AndroidManifest.xml** i am getting this error even when i uncheck AndroidManifest.xml – glo Jan 17 '13 at 06:40
  • have you export jar with warning? – dd619 Jan 17 '13 at 06:49
  • Yes i have exported jar with warnings and errors – glo Jan 17 '13 at 06:50
  • and now r u getting classdefnotfounderror? – dd619 Jan 17 '13 at 06:56
  • I unchecked my bin folder while exporting and now i got the jar file but still i got error that some resource in my layout folder are missing – glo Jan 17 '13 at 07:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22882/discussion-between-glo-and-dd619) – glo Jan 17 '13 at 07:17