How to correctly add third-party jar packages to Android Projects

Source: Internet
Author: User

Adding a third-party jar package in the android project is not a complicated problem, but it does bring great troubles to many developers. I have encountered a situation where calss not found exception, error inflating class, and other third-party jar packages are not actually identified, but the error points to other problems, when encountering such problems, we are easily misled by these errors that are not accurate enough. Therefore, it is important to correctly add third-party jar packages. The information on this issue on the Internet is also messy, so I hope to use this article to give a clear explanation and answer to this question.

The first thing I want to talk about is why this problem occurs? We used to add third-party jar packages by adding External Archives in the previous Java program development process. The reason why third-party jar packages cannot be introduced in the same way in Android Projects, the following is an explanation from Russ Bateman, one of eclipse J2EE developers:

I'm an eclipse Jee developer and have been in the habit for policyears of adding third-party Libraries via the "user library" mechanic in build path. of course, there are at least 3 ways to add a third-party library, the one I use is the most elegant, in my humble opinion.

This will not work, however, for Android, whose Dalvik "JVM" cannot handle an ordinary Java-compiled class, but must have it converted to a special format. this does not happen when you add a library in the way I'm wont to do it.

Instead, follow the (widely available) Instructions for importing the third-party library, then adding it using build path (which makes it known to eclipse for compilation purposes ). here is the step-by-step:

  1. Download the library to your host development system.
  2. Create a new folder,Libs, In your eclipse/Android project.
  3. Right-clickLibsAnd choose import-> General-> file system, then next, browse in the filesystem to find the Library's parent directory (I. e.: Where you downloaded it ).
  4. Click OK, then click the directory name (not the checkbox) in the left pane, then check the relevant jar in the right pane. This puts the library into your project (physically ).
  5. Right-click on your project, choose build path-> Configure build path, then click the libraries tab, then add jars..., navigate to your new jar inLibsDirectory and add it. (This, incidentally, is the moment at which your new jar is converted for use on Android .)

What you 've done here accomplishes two things:

  1. Includes a Dalvik-converted jar in your android project.
  2. Makes Java definitions available to eclipse in order to find the third-party classes when developing (that is, compiling) Your project's source code.

 

It means that the Dalvik Virtual Machine of Android cannot directly process compiled java. class files. Therefore, adding a third-party jar package directly requires some processing before it can be recognized and used by Android projects. Follow these steps:

  1. Download third-party jar packages
  2. Create a libs directory under the android Project (as far as I understand and test the experience, the name is not fixed, you can take other names)
  3. In eclipse, right-click the libs directory, select import> General> file system, select the directory where the jar package is located, and select the jar package under this directory.

Note: By this step, you have successfully added the jar package to the project (but it has not been recognized by the android Virtual Machine. Therefore, if you use the class in the jar package, compilation fails)

4. Right-click the project name, select build path> Configure build path, and select the library tab. Click Add jars on the right and select the jar package under the libs directory

Note: This step helps the android virtual machine process the previously added jar package.

Theoretically, this step can be used normally. However, if an error occurs during debugging at this step, right-click the libs directory and choose build path-> Configure as source folder, this step allows you to use third-party jar packages normally and continue your android development journey.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.