Eclipse中为Android应用添加jar包

跑书上的例子,用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,这个方法记下了,以后好找。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值