Eclipse中为Android应用添加jar包

来源:互联网 发布:手机淘宝怎么取消订单 编辑:程序博客网 时间:2024/06/05 03:45

跑书上的例子,用JBOX2D做android小应用时,一直提示找不到类。


后来,在这个网页找到了答案:

http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project

看第一个回答

Now for the missing class problem.

I'm an Eclipse Java EE developer and have been in the habit for many years of adding third-partylibraries via the "User Library" mechanism 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-click libs and choose Import -> General -> File System, then Next, Browse in the filesystem to find the library's parent directory (i.e.: where you downloaded it to).
  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 in the libs directory 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.

大概意思是:

Eclipse 上至少有 3 种添加外部 JAR 的方法。但是,

普通的 java 编译 class 文件,在安卓上是不能运行的,因为安卓的 Dalvik "JVM" 与其它 JVM 有所不同。使用下面的方法可以将普通 class(jar) 文件转换为安卓可用的:

1.下载 jar 到任意目录

2.在 Eclipse 的 android 工程下添加 libs 文件夹

3.右击 libs 文件夹,选择 Import -> General -> File System ,点击 Next ,点击 Browse 并找到刚才下载 JAR 的目录

4.点击 OK 后,在左边栏选择文件夹名(注意不是选择复选框),并在右边栏选择要用的 JAR 文件

5.右键点击工程,选择 Build Path -> Configure Build Path ,然后选择 Libraries tab , Add JARs,在 libs 目录下找到刚才添加的 JAR 包


大部分网站介绍的都不适用于android,这个方法记下了,以后好找。

0 0
原创粉丝点击