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

查看:33
本文介绍了如何在 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.

Order 和 Export 似乎被忽略了,并且来自外部 JAR 的所有类在运行时都丢失了.

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

在使用 Eclipse 插件构建 Android 应用程序时,是否有从外部 JAR 正确包含所需类的技巧?我不想使用 ant 或 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.

推荐答案

For 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 文件,然后选择构建路径 > 添加到构建路径,它将在您的文件夹中创建一个名为参考库"的文件夹项目

  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.

对于 Android Studio

  1. 如果您在 Android 项目资源管理器中查看,请将其更改为 项目 视图,如下所示
  1. If you are in Android View in project explorer, change it to Project view as below

  1. 右键单击要添加外部库的所需模块,然后选择New > Directroy 并将其命名为libs"
  2. 现在将 blah_blah.jar 复制到libs"文件夹
  3. 右键单击 blah_blah.jar,然后选择添加为库..".这将自动在 build.gradle 中添加和条目作为 compile files('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

请注意:如果您使用的是 3rd 方库,那么最好使用传递依赖,其中 Gradle 脚本会在 gradle 脚本运行时自动下载 JAR 和依赖 JAR.

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 依赖管理

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

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