如何在Android项目中使用外部JAR? [英] How can I use external JARs in an Android project?

查看:379
本文介绍了如何在Android项目中使用外部JAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Android项目,并在我的项目中添加了一个外部JAR(hessian-4.0.1.jar)。然后,我将JAR添加到构建路径中,并在Order and Export中进行检查。

I have created an Android project and added an external JAR (hessian-4.0.1.jar) to my project. I then added the JAR to the build path and checked it off in Order and Export.

看起来,排序和导出被忽略,外部JAR的所有类都缺少在运行时。

Order and Export is ignored it seems, and all classes from the external JAR are missing at runtime.

使用Eclipse插件构建Android应用程序时,是否有一个技巧来正确地包含外部JAR所需的类?我不想使用蚂蚁或Maven。

Is there a trick to properly include the needed classes from an external JAR when building an Android application using the Eclipse plugin? I do not want to use ant or Maven.

推荐答案

Eclipse

将外部JAR添加到您的Android项目或任何Java项目中的一个好方法是:

A good way to add external JARs to your Android project or any Java project is:


  1. 在项目的根文件夹中创建名为 libs 的文件夹

  2. 将您的 JAR文件复制到 libs 文件夹

  3. 现在,右键单击Jar文件,然后选择Build Path> Add to Build
    路径
    ,它将在
    项目中创建一个名为参考库的文件夹

  1. Create a folder called libs in your project's root folder
  2. Copy your JAR files to the libs folder
  3. Now right click on the Jar file and then select Build Path > Add to Build Path, which will create a folder called 'Referenced Libraries' within your project

通过这样做,不要
丢失您的图书馆
,您的
硬盘驱动器上每当您将项目转移到另一台计算机时。

By doing this, you will not lose your libraries that are being referenced on your hard drive whenever you transfer your project to another computer.

******更新*******

****** Update *******

Android Studio中添加外部库的步骤


  1. 如果你在 roid 在项目资源管理器中查看,将其更改为项目视图如下

  1. If you are in Android View in project explorer, change it to Project view as below


  1. 右键单击想要添加外部库的所需模块,然后选择新建> Directroy 并将其命名为 libs

  2. 现在复制 blah_blah.jar 进入' libs '文件夹

  3. 右键点击blah_blah.jar,然后选择'添加为图书馆.. '。这将自动添加并输入 build.gradle 作为编译文件('libs / blah_blah.jar')并同步该gradle。你完成了

  1. Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
  2. Now copy the blah_blah.jar into the 'libs' folder
  3. Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done

请注意:如果您使用第三方库,那么最好使用传递依赖关系其中,Gradle脚本自动下载JAR和依赖关系JAR,当gradle脚本运行时。

Please Note : If you are using 3rd party libraries then it is better to use transitive dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.

例如:编译com.google .android.gms:play-services-ads:9.4.0'

详细了解 Gradle Dependency Mangement

这篇关于如何在Android项目中使用外部JAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆