如何在 Android 项目中使用外部 JAR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use external JARs in an Android project?
提问by Vinayak Bevinakatti
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.
我创建了一个 Android 项目并向我的项目添加了一个外部 JAR (hessian-4.0.1.jar)。然后我将 JAR 添加到构建路径并在 Order and Export 中将其选中。
Order and Export is ignored it seems, and all classes from the external JAR are missing at runtime.
Order 和 Export 似乎被忽略了,并且来自外部 JAR 的所有类在运行时都丢失了。
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 插件构建 Android 应用程序时,是否有从外部 JAR 正确包含所需类的技巧?我不想使用 ant 或 Maven。
回答by Vinayak Bevinakatti
For Eclipse
对于日食
A good way to add external JARs to your Android project or any Java project is:
将外部 JAR 添加到您的 Android 项目或任何 Java 项目的好方法是:
- Create a folder called
libs
in your project's root folder - Copy your JAR files to the
libs
folder 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 librariesthat are being referenced on your hard drive whenever you transfer your project to another computer.
libs
在项目的根文件夹中创建一个名为的文件夹- 将您的JAR 文件复制到
libs
文件夹 现在右键单击 Jar 文件,然后选择 Build Path > Add to Build Path,这将在您的项目中创建一个名为“Referenced Libraries”的文件夹
通过这样做,当您将项目转移到另一台计算机时,您将不会丢失在您的硬盘驱动器上引用的库。
For Android Studio
对于 Android Studio
- If you are in AndroidView in project explorer, change it to Projectview as below
- 如果您在项目资源管理器中的Android视图中,请将其更改为项目视图,如下所示
- Right click the desired module where you would like to add the external library, then select New > Directroyand name it as 'libs'
- Now copy the blah_blah.jarinto the 'libs' folder
- Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradleas compile files('libs/blah_blah.jar')and sync the gradle. And you are done
- 右键单击要添加外部库的所需模块,然后选择New > Directroy并将其命名为“ libs”
- 现在将blah_blah.jar复制到“ libs”文件夹中
- 右键单击 blah_blah.jar,然后选择“添加为库..”。这将自动在build.gradle 中添加和条目作为编译文件('libs/blah_blah.jar')并同步 gradle。你已经完成了
Please Note :If you are using 3rd party libraries then it is better to use transitive dependencieswhere Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.
请注意:如果您使用的是 3rd 方库,那么最好使用传递依赖项,其中 Gradle 脚本会在 gradle 脚本运行时自动下载 JAR 和依赖项 JAR。
Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'
例如:编译'com.google.android.gms:play-services-ads:9.4.0'
Read more about Gradle Dependency Mangement
阅读有关Gradle 依赖管理的更多信息
回答by MannyNS
Yes, you can use it. Here is how:
是的,你可以使用它。方法如下:
- Your Project -> right click -> Import -> File System -> yourjar.jar
- Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
- 您的项目 -> 右键单击 -> 导入 -> 文件系统 -> yourjar.jar
- 您的项目 -> 右键单击 -> 属性 -> Java 构建路径 -> 库 -> 添加 Jar -> yourjar.jar
This videomight be useful in case you are having some issues.
如果您遇到一些问题,此视频可能会很有用。
回答by Danny Remington - OMS
I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.
我目前使用的是 SDK 20.0.3,以前的解决方案都不适合我。
The reason that hessdroid works where hess failed is because the two jar files contain java that is compiled for different virtual machines. The byte code created by the Java compiler is not guaranteed to run on the Dalvik virtual machine. The byte code created by the Android compiler is not guaranteed to run on the Java virtual machine.
hessdroid 在 hess 失败的地方工作的原因是因为两个 jar 文件包含为不同的虚拟机编译的 java。Java 编译器创建的字节码不能保证在 Dalvik 虚拟机上运行。Android 编译器创建的字节码不能保证在 Java 虚拟机上运行。
In my case I had access to the source code and was able to create an Android jar file for it using the method that I described here: https://stackoverflow.com/a/13144382/545064
就我而言,我可以访问源代码并能够使用我在此处描述的方法为其创建一个 Android jar 文件:https: //stackoverflow.com/a/13144382/545064
回答by shanabus
I know the OP ends his question with reference to the Eclipse plugin, but I arrived here with a search that didn't specify Eclipse. So here goes for Android Studio:
我知道 OP 参考 Eclipse 插件结束了他的问题,但我到达这里时进行了未指定 Eclipse 的搜索。所以这里适用于 Android Studio:
- Add
jar
file to libs directory (such as copy/paste) - Right-Click on
jar
file and select "Add as Library..." - click "Ok" on next dialog or renamed if you choose to.
- 将
jar
文件添加到 libs 目录(如复制/粘贴) - 右键单击
jar
文件并选择“添加为库...” - 单击下一个对话框中的“确定”或重命名(如果您选择)。
That's it!
就是这样!
回答by shanabus
Turns out I have not looked good enough at my stack trace, the problem is not that the external JAR is not included.
结果我的堆栈跟踪看起来不够好,问题不在于没有包含外部 JAR。
The problem is that Android platform is missing javax.naming.* and many other packages that the external JAR has dependencies too.
问题是 Android 平台缺少 javax.naming.* 和许多其他外部 JAR 也具有依赖项的包。
Adding external JAR files, and setting Order and Export in Eclipse works as expected with Android projects.
添加外部 JAR 文件以及在 Eclipse 中设置 Order 和 Export 与 Android 项目按预期工作。
回答by kalyan G
Goto Current Project
转到当前项目
RightClick->Properties->Java Build Path->Add Jar Files into Libraries -> Click OK
右键单击-> 属性-> Java 构建路径-> 将 Jar 文件添加到库中-> 单击确定
Then it is added into the Referenced Libraries File in your Current Project .
然后将其添加到 Current Project 中的 Referenced Libraries 文件中。
回答by Rodrigo
Android's Java API does not support javax.naming.* and many other javax.* stuff. You need to include the dependencies as separate jars.
Android 的 Java API 不支持 javax.naming.* 和许多其他 javax.* 东西。您需要将依赖项包含为单独的 jars。
回答by kumaheiyama
If using Android Studio, do the following (I've copied and modified @Vinayak Bs answer):
如果使用 Android Studio,请执行以下操作(我已复制并修改了 @Vinayak Bs 答案):
- Select the Project view in the Project sideview (instead of Packages or Android)
- Create a folder called libsin your project's root folder
- Copy your JAR files to the libs folder
- The sideview will be updated and the JAR files will show up in your project
- Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
- After that, all you need to do is reference the new classes in your code, eg.
import javax.mail.*
- 在项目侧视图中选择项目视图(而不是 Packages 或 Android)
- 在项目的根文件夹中创建一个名为libs的文件夹
- 将 JAR 文件复制到 libs 文件夹
- 侧视图将更新,JAR 文件将显示在您的项目中
- 现在右键单击要导入的每个 JAR 文件,然后选择“添加为库...”,这会将其包含在您的项目中
- 之后,您需要做的就是在代码中引用新类,例如。
import javax.mail.*
回答by g_vk
in android studio if using gradle
如果使用 gradle,则在 android studio 中
add this to build.gradle
将此添加到 build.gradle
compile fileTree(dir: 'libs', include: ['*.jar'])
and add the jar file to libs folder
并将 jar 文件添加到 libs 文件夹
回答by Rohit Arya
If you are using gradlebuild system, follow these steps:
如果您使用的是gradle构建系统,请按照下列步骤操作:
put
jar
files inside respectivelibs
folder of your android app. You will generally find it atProject
>app
>libs
. Iflibs
folder is missing, create one.add this to your
build.gradle
file yourapp
. (Not to yourProject
'sbuild.gradle
)dependencies { compile fileTree(dir: 'libs', include: '*.jar') // other dependencies }
This will include all your
jar
files available inlibs
folder.
将
jar
文件放在libs
您的 android 应用程序的相应文件夹中。您通常会在Project
>app
>处找到它libs
。如果libs
文件夹丢失,请创建一个。将此添加到您的
build.gradle
文件中app
。(不是你Project
的build.gradle
)dependencies { compile fileTree(dir: 'libs', include: '*.jar') // other dependencies }
这将包括
jar
文件libs
夹中可用的所有文件。
If don't want to include all jar
files, then you can add it individually.
如果不想包含所有jar
文件,则可以单独添加。
compile fileTree(dir: 'libs', include: 'file.jar')
compile fileTree(dir: 'libs', include: 'file.jar')