2

I've been building a custom Java library, and after importing it to my Android project, it results in a java.lang.NoClassDefFoundError when I try to use it.

My library used to be part of my project, but I decided to "split it", and create a Java library instead. It imports a few libraries needed to work (org.json...), and has two packages.

To include it into my Android project, I just copy/pasted my library to the libs folder of my Android project, and Eclipse detect it, and doesn't show me any error in my code, and the library seems to be added to the "Android Dependencies" folder. But, when I run the project, I have a java.lang.NoClassDefFoundError Exception in the first line my library is required.

If one of you could help me to figure out this issue, I'd be thankful. I can't even see what I've been doing wrong...

Edit: You can find the jar I'm using right here: https://docs.google.com/file/d/0B1rK0R07j--QZmJFMUpLVEdMNnc/edit?usp=sharing and the sources https://github.com/MagicMicky/HabitRPGJavaAPI

Note: The library seems to work with a normal Java project.

My LOGCAT below:

05-21 00:27:59.349: E/AndroidRuntime(11797): FATAL EXCEPTION: main
05-21 00:27:59.349: E/AndroidRuntime(11797): java.lang.NoClassDefFoundError: com.magicmicky.habitrpgmobileapp.MainActivity$1
05-21 00:27:59.349: E/AndroidRuntime(11797):    at com.magicmicky.habitrpgmobileapp.MainActivity.<init>(MainActivity.java:44)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at java.lang.Class.newInstanceImpl(Native Method)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at java.lang.Class.newInstance(Class.java:1319)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.os.Looper.loop(Looper.java:137)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at java.lang.reflect.Method.invokeNative(Native Method)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at java.lang.reflect.Method.invoke(Method.java:511)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-21 00:27:59.349: E/AndroidRuntime(11797):    at dalvik.system.NativeStart.main(Native Method)

with MainActivity.java:44 being the first line I use my library...

Edit2: And here is the firsts line of my Android Project:

package com.magicmicky.habitrpgmobileapp;

import java.util.ArrayList;
import java.util.List;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.magicmicky.habitrpgmobileapp.habits.HabitItem;
import com.magicmicky.habitrpgmobileapp.habits.User;
import com.magicmicky.habitrpgmobileapp.onlineapi.GetUser;
import com.magicmicky.habitrpgmobileapp.onlineapi.HostConfig;
import com.magicmicky.habitrpgmobileapp.onlineapi.OnHabitsAPIResult;
import com.magicmicky.habitrpgmobileapp.onlineapi.WebServiceInteraction.Answer;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends SherlockFragmentActivity {
    MyPagerAdapter fgsAdapter;
    private TextView username_TV;
    /* Other decarations...*/
    private User user;//Note that this line is from my library, but doesn't throw an exception
    OnHabitsAPIResult callback = new OnHabitsAPIResult() { // And this is line 44...
        Handler mainHandler;
        private int nbRequests=0;
        @Override
        public void onUserReceived(final User us) {
            mainHandler = new Handler(getMainLooper());
            Runnable myRunnable = new Runnable(){
                public void run() {
                    user=us;
                    notifyDataChanged();
                    afterResults();
                }
            };
            mainHandler.post(myRunnable);
        }
    };
    /* And code continues...*/

Note that this code works well if I'm copying/pasting the library files into the project (instead of importing the jar library)...

MagicMicky
  • 3,819
  • 2
  • 37
  • 53
  • 1
    If you are on the R22 tools, see if this helps: http://stackoverflow.com/questions/16596969/libraries-do-not-get-added-to-apk-anymore-after-upgrade-to-adt-22/16596990#16596990 – CommonsWare May 20 '13 at 19:39
  • Well I'm going to try to update to R22 to see if it changes things. – MagicMicky May 20 '13 at 22:40
  • Just updated to the R22, same problems, even when I export the different libraries... – MagicMicky May 20 '13 at 23:16

4 Answers4

2

From your question I'm guessing that you actually are working with an Android library project (since the part you split originally formed part of your Android project). So, you should take a look at the docs about Android Library projects.

Basically:

  1. They behave differently from other Java Libraries
  2. They need to be included differently as well

Best way to correct this in Eclipse:

  1. Mark your library project as an Android Library in its Project Properties (Right click your Project->Android)
  2. Add your project's dependency the same way(Right click your Project->Android->Add library)
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • Actually, it's not an android library. I tried to design it to be able to work with java project (I didn't use any of Android specific classes in it). My library project is a Java project, and I export it as a .jar before using it. – MagicMicky May 20 '13 at 22:42
  • I'd still check if **Eclipse** didn't decide to auto-magically import some stuff which turned it into an Android Library. Easiest way to do that: generate a stub Java project which only has a single class whose *main* method tries to use a class in your library. – DigCamara May 20 '13 at 22:58
  • Ok, just tried everything works out well. Using the same jar I'm trying to use in my Android project... I really can't figure out what's going on! – MagicMicky May 20 '13 at 23:15
  • Just edited my post with the first lines of my library, I hope it helps. – MagicMicky May 21 '13 at 17:38
1

Ok, problem solved...

It was apparently because when I exported my Java library to a .jar file, I was using a specific Compliance level for the application. Once I unchecked the box under (right click on the library project) -> Properties -> Java Compiler -> "Enable project Specific Compiler", everything works out well. The Android project isn't using any project specific compiler.

Hope it helps someone...

MagicMicky
  • 3,819
  • 2
  • 37
  • 53
0

Without the library or your android project to test against there are many possible reasons for this.

My recommendation would be as follows:

  1. Remove the library from your Android project.
  2. Right click on the project and go to Build-Path->Add External Archives
  3. In the window that pops up navigate to where your library is located and select it.

I have seen issues before with copy/pasting or dragging libraries into projects. If this fails to resolve your issue then feel free to elaborate with more information and I will try to help you get this sorted out.

  • 1
    Do not manually modify your build path (other than the limited R22 fix noted here: http://stackoverflow.com/questions/16596969/libraries-do-not-get-added-to-apk-anymore-after-upgrade-to-adt-22/16596990#16596990) – CommonsWare May 20 '13 at 23:21
0

I tried @MagicMicky's solution but that resulted in other errors such as something along the ines, Compiler compliance level mismatch. However, following steps fixed the problem for me,

Project Properties > Java Build Path > Order and Export Tab > Check the jar packages you have imported and hit ok

Dhunju_likes_to_Learn
  • 1,201
  • 1
  • 16
  • 25