Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android java.lang.NoClassDefFoundError: org.jsoup.Jsoup

I work with eclipse Version: Indigo Service Release 2 Build id: 20120216-1857. The Android version ist 2.2. I make an app to test a connect and parse a web site like this:

public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        Document doc = Jsoup.connect("http://example.com/").get();
        Elements divs = doc.select("div#test");

    for (Element div : divs) {
            System.out.println(div.text());
    }
    } catch (Exception e) {
    }
    }
}

Manifest file:

android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name"
    android:configChanges="orientation"
    >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>            
        <uses-library android :name="org.jsoup.Jsoup" />
    </activity>
</application>

I add the library jsoup-1.6.1.jar to JAVA Buildpath, but I become the runtime error: E/AndroidRuntime(736): java.lang.NoClassDefFoundError: org.jsoup.Jsoup

How can I resolve this error?

like image 659
red Avatar asked Mar 29 '12 22:03

red


2 Answers

I encountered this exact problem after a recent update of the ADT component of Android. It looks like Android is ignoring the build path and instead using the ANT defaults.

I solved this by removing my jar file from the build path, creating a folder in the "root" of the app heirarchy (alongside src, gen, etc) called "libs", and putting my .jar there. When you clean / build and re-launch the app, the error should go away.

FYI - this is occurring because the JAR file is not getting packaged into the .apk files - this is why it builds correctly, but isn't available during run-time on your Android device.

see NoClassDefFoundError - Eclipse and Android

like image 142
John O'Connor Avatar answered Oct 06 '22 01:10

John O'Connor


There's also the issue that jars have to be in the libs (with an 's') directory and not lib....could this be your problem?

like image 41
mmaitlen Avatar answered Oct 06 '22 01:10

mmaitlen