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

最近想要做一個android版的RSS Reader, 所以找了一些資料, 發現比較多範例是用jsoup這個第三方套件中的一些api來 取得xml的element, 也試了一下, 但一直出現:

 java.lang.NoClassDefFoundError: org.jsoup.Jsoup

因為我已經把jsoup.jar加到我的專案了(Select Android Project → Right Click →Properties → Java Build Path → Libraries → Add External JARs  → Select .jar→ OK→ OK )

我檢查了Android Project的Android Private Libraries和libs都有

但還是java.lang.NoClassDefFoundError:org.jsoup.Jsoup

所以我只好求助Google大神了

我先上StackOverFlow找到這個Android java.lang.NoClassDefFoundError: org.jsoup.Jsoup問題的答案

但試了一下發現這不適用我, 因為我的ADT已經upgrade到20+而且Android Project也有libs的資料夾(中文資源

但投票最多的答案有給一個linkNoClassDefFoundError – Eclipse and Android這個問答就解決我的問題了

看到第三個答案:

I didn’t have to put the jar-library in assets or lib(s), but only tick the box for this jar in Properties -> Java Build Path -> “Order and Export” (it was listed before, but not selected)  – by Till

 

其實第二個答案, 也有提到適合我的解決方法:

By adding the external jar into your build path just adds the jar to your package, but it will not be available during runtime.

In order for the jar to be available at runtime, you need to:

  • Put the jar under your assets folder
  • Include this copy of the jar in your build path
  • Go to the export tab on the same popup window
  • Check the box against the newly added jar  – by Anthony Forloney

其實, 這兩個答案說的都是一樣的事情(紅線的部份)

先撇開ADT Upgrade遇到的問題, 在ADT中加入第三方套件(.jar)的完整方式應該是:

Select Android Project → Right Click →Properties → Java Build Path → Libraries → Add External JARs  → Select .jar→ OK  → Order and Export  →Check the box against the newly added jar” → OK

看起來要Debug NoClassDefFoundError除了確認有沒有import正確的package, 再來就是檢查有沒有把.jar加入到Android Project重要的是要加入「正確」啊

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s