8

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?

dgw
  • 13,418
  • 11
  • 56
  • 54
red
  • 81
  • 1
  • 1
  • 2

8 Answers8

19

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

Community
  • 1
  • 1
John O'Connor
  • 5,244
  • 2
  • 24
  • 29
  • 1
    This is not a band-aid -- it is what you are supposed to do. See http://stackoverflow.com/questions/9824491/android-sdk-r17-ruins-working-projects/9824539#9824539 – CommonsWare Mar 29 '12 at 23:19
  • red, make sure to mark this as "accepted" if it helped you. Mark (commonsware) - Thanks for the tip. I'll remove that aside. – John O'Connor Mar 30 '12 at 00:33
  • Sorry to wake you up, but i use `compile 'org.jsoup:jsoup:1.8.3'` And it gives same error. What should I do. – Aawaz Gyawali Feb 28 '16 at 12:21
3

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?

mmaitlen
  • 824
  • 9
  • 17
2
  1. Right click on the project name > Properties > Java Build Path > tab Libraries then click on button Add External jars.
  2. select jar's path from your directory where you had downloaded jsoup-1.7.3.jar.
  3. After adding jar go to next tab Order and export and select checkbox jsoup-1.7.3.jar 4 click ok 5.clean build your project and then run.

I solved the same problem by following above steps

Antesh Sharma
  • 309
  • 2
  • 3
2

You don't need the line:

 <uses-library android:name = "org.jsoup.Jsoup"/>  

in your manifest file. All you need to do to use Jsoup is to ensure it's part of your build path by doing the following:

  • Right click on your project and select Properties
  • Select Java Build Path and select Add External JARs...
  • Navigate to the Jsoup jar file

Then, Jsoup will act like any other library in java. For example in my app I use it like so:

    try 
    {
                    Document doc = Jsoup.connect( myHtml ).get();

                    Elements table = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );

                    // Returns first row of the table which we want
                    Elements row = table.select( "tr.lightrow" );
                    Element record_One = row.first();
    }

Note - I have not included all my code for it because it's rather long. Anyway, after that you just import the classes as needed, for example in mine I use the following:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
Katana24
  • 8,706
  • 19
  • 76
  • 118
  • 1
    "Select Java Build Path and select Add External JARs..." -- that is very wrong for the R17 tools. All third-party JARs need to be in a `libs/` directory of the project, and do not need to be manually added to the build path now. – CommonsWare Mar 29 '12 at 23:18
  • http://stackoverflow.com/questions/9824491/android-sdk-r17-ruins-working-projects/9824539#9824539 – CommonsWare Mar 29 '12 at 23:40
  • Thanks. And here I thought my first answer was a good one...oh well back to the amateur android drawing board. – Katana24 Mar 29 '12 at 23:46
1

You are doing it wrong. You cant use Jsoup functions in the oncreate of Activity. You need to make either Async Task or thread.

This is to prevent doing blocking tasks in you activity.

Here is a nice blog that explain how to do it and provide sample code as well.

Sample Code Click here

Imran Jawaid
  • 471
  • 1
  • 10
  • 27
1

I was able to solve this problem by following:

  1. Placing the jsoup jar in the 'libs' folder (as noted in John's answer)
  2. Remove the "Android Dependencies" and "Android Private Libraries" from the build path
  3. Using Android Tools > Fix Project Properties to repopulate the above entries in the build path.
0

Just close the Eclipse and open it again. The problem will be solved.

Soheil
  • 5,229
  • 1
  • 18
  • 21
0

My solution was

  1. Right click on the project name > Properties > Java Build Path > tab Libraries > remove everything except the "Android X.X" (2.3.3 in my case) and the "Android Dependencies" (according to link found by @KarlKarlsom >android.foxykeep.com< from stack problem >stack<)
  2. I created folder libs in my main projects. As I was using jsoup so I moved it to this folder
  3. Restarted eclipse, clean up project didn't work

p.s I was using jsoup and sherlock. I didn'y change sherlock library, only my main project.

Community
  • 1
  • 1
deadfish
  • 11,996
  • 12
  • 87
  • 136