如何在Android项目中使用外部jar ?

时间:2022-03-15 10:36:25

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 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。

12 个解决方案

#1


434  

Eclipse

Eclipse

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

向Android项目或任何Java项目添加外部jar的一个好方法是:

  1. Create a folder called libs in your project's root folder
  2. 在项目的根文件夹中创建一个名为libs的文件夹
  3. Copy your JAR files to the libs folder
  4. 将JAR文件复制到libs文件夹
  5. 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

    现在右键单击Jar文件,然后选择Build Path > Add to Build Path,它将在项目中创建一个名为“引用库”的文件夹

    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 *******

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

Step to add external library in Android Studio

在Android Studio中添加外部库。

  1. If you are in Android View in project explorer, change it to Project view as below
  2. 如果您在project explorer中的Android视图中,请将其更改为project View,如下所示

如何在Android项目中使用外部jar ?

  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. 右键单击想要添加外部库的模块,然后选择新的> Directroy并将其命名为“libs”
  3. Now copy the blah_blah.jar into the 'libs' folder
  4. 现在复制blah_blah。jar文件到'libs'文件夹中
  5. 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
  6. 右键单击blah_blah。jar,然后选择“Add as Library.. .”。这将自动添加和进入构建。gradle作为编译文件('libs/blah_blah.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.

请注意:如果您正在使用第三方库,那么最好使用传递依赖,在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

阅读更多关于等级依赖性管理。

#2


128  

Yes, you can use it. Here is how:

是的,你可以用它。这里是:

  1. Your Project -> right click -> Import -> File System -> yourjar.jar
  2. 您的项目->右键单击->导入->文件系统-> yourjar.jar
  3. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
  4. 您的项目->右键单击->属性-> Java构建路径->库->添加Jar -> yourjar.jar

This video might be useful in case you are having some issues.

如果你有什么问题,这个视频可能会有用。

#3


5  

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.

hess失败时,hessdroid工作的原因是这两个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://*.com/a/13144382/545064

在我的例子中,我可以访问源代码,并且可以使用我在这里描述的方法为它创建一个Android jar文件:https://*.com/a/13144382/545064

#4


5  

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。安卓工作室是这样的:

  1. Add jar file to libs directory (such as copy/paste)
  2. 将jar文件添加到libs目录(如复制/粘贴)
  3. Right-Click on jar file and select "Add as Library..."
  4. 右键单击jar文件并选择“Add as Library…”
  5. click "Ok" on next dialog or renamed if you choose to.
  6. 单击“确定”在下一个对话框或重命名,如果您选择。

That's it!

就是这样!

#5


4  

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平台缺少了java . named。*以及许多外部JAR也具有依赖性的其他包。

Adding external JAR files, and setting Order and Export in Eclipse works as expected with Android projects.

添加外部JAR文件,并在Eclipse中设置订单和导出,这些操作与Android项目的预期一致。

#6


3  

Goto Current Project

转到当前项目

RightClick->Properties->Java Build Path->Add Jar Files into Libraries -> Click OK

右键单击->属性->Java构建路径->向库中添加Jar文件->单击OK

Then it is added into the Referenced Libraries File in your Current Project .

然后将它添加到当前项目中引用的库文件中。

#7


1  

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.命名。*和许多其他javax。*的东西。您需要将依赖项包含为独立的jar。

#8


1  

If using Android Studio, do the following (I've copied and modified @Vinayak Bs answer):

如果使用Android Studio,请执行以下操作(我已经复制并修改了@Vinayak Bs的答案):

  1. Select the Project view in the Project sideview (instead of Packages or Android)
  2. 选择Project sideview中的Project视图(而不是package或Android)
  3. Create a folder called libs in your project's root folder
  4. 在项目的根文件夹中创建一个名为libs的文件夹
  5. Copy your JAR files to the libs folder
  6. 将JAR文件复制到libs文件夹
  7. The sideview will be updated and the JAR files will show up in your project
  8. 侧视图将被更新,JAR文件将显示在您的项目中
  9. Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
  10. 现在右键单击要导入的每个JAR文件,然后选择“Add as Library…”,包括在你的项目中
  11. After that, all you need to do is reference the new classes in your code, eg. import javax.mail.*
  12. 之后,您需要做的就是在代码中引用新的类。进口javax.mail。*

#9


1  

in android studio if using gradle

在android studio中使用gradle

add this to build.gradle

添加这个build.gradle

compile fileTree(dir: 'libs', include: ['*.jar'])

and add the jar file to libs folder

并将jar文件添加到libs文件夹。

#10


1  

If you are using gradle build system, follow these steps:

如果您使用的是等级构建系统,请遵循以下步骤:

  1. put jar files inside respective libs folder of your android app. You will generally find it at Project > app > libs. If libs folder is missing, create one.

    将jar文件放在android应用程序的各个libs文件夹中。通常可以在项目>应用程序> libs中找到。如果缺少libs文件夹,请创建一个。

  2. add this to your build.gradle file your app. (Not to your Project's build.gradle)

    将此添加到您的构建中。渐变文件你的应用。

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        // other dependencies
    }
    

    This will include all your jar files available in libs folder.

    这将包括在libs文件夹中可用的所有jar文件。

If don't want to include all jar files, then you can add it individually.

如果不想包含所有jar文件,那么可以单独添加它。

compile fileTree(dir: 'libs', include: 'file.jar')

编译文件树(目录:'libs',包括:'file.jar')

#11


0  

create a folder (like lib) inside your project, copy your jar to that folder. now go to configure build path from right click on project, there in build path select

在项目中创建一个文件夹(如lib),将jar复制到该文件夹。现在去配置构建路径从右键单击项目,那里有构建路径选择

'add jar' browse to the folder you created and pick the jar.

“添加jar”浏览到您创建的文件夹并选择jar。

#12


0  

Copying the .jar file into the Android project's folder isn't always possible.
Especially if it's an output of another project in your workspace, and it keeps getting updated.

将.jar文件复制到Android项目的文件夹并不总是可能的。特别是如果它是工作空间中另一个项目的输出,并且不断更新。

To solve this you'll have to add the jar as a linked file to your project, instead of importing it (which will copy it locally).

要解决这个问题,您必须将jar作为一个链接文件添加到项目中,而不是导入它(它将在本地复制它)。

In the UI choose:

在UI中选择:

Project -> Import -> File System -> yourjar.jar -> (Options area) Advanced -> Create link in workspace.

项目->导入->文件系统-> yourjar。jar ->(选项区)高级->在工作区中创建链接。

The link is save in the .project file:

该链接保存在.project文件中:

<linkedResources>
    <link>
        <name>yourjar.jar</name>
        <type>1</type>
        <locationURI>PARENT-5-PROJECT_LOC/bin/android_ndk10d_armeabi-v7a/yourjar.jar</locationURI>
    </link>
</linkedResources>

PARENT-5-PROJECT_LOC means relative to the project file, 5 directories up (../../../../../).

家长-5- project_loc表示相对于项目文件,有5个目录。

Then add it to the libraries:
Project -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

然后添加到库中:项目->属性-> Java构建路径->库->添加Jar -> yourjar.jar

In the same window choose the Order and Export tab and mark your jar so it will be added to the apk.

在同一个窗口中,选择Order和Export选项卡并标记jar,以便将其添加到apk。

#1


434  

Eclipse

Eclipse

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

向Android项目或任何Java项目添加外部jar的一个好方法是:

  1. Create a folder called libs in your project's root folder
  2. 在项目的根文件夹中创建一个名为libs的文件夹
  3. Copy your JAR files to the libs folder
  4. 将JAR文件复制到libs文件夹
  5. 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

    现在右键单击Jar文件,然后选择Build Path > Add to Build Path,它将在项目中创建一个名为“引用库”的文件夹

    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 *******

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

Step to add external library in Android Studio

在Android Studio中添加外部库。

  1. If you are in Android View in project explorer, change it to Project view as below
  2. 如果您在project explorer中的Android视图中,请将其更改为project View,如下所示

如何在Android项目中使用外部jar ?

  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. 右键单击想要添加外部库的模块,然后选择新的> Directroy并将其命名为“libs”
  3. Now copy the blah_blah.jar into the 'libs' folder
  4. 现在复制blah_blah。jar文件到'libs'文件夹中
  5. 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
  6. 右键单击blah_blah。jar,然后选择“Add as Library.. .”。这将自动添加和进入构建。gradle作为编译文件('libs/blah_blah.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.

请注意:如果您正在使用第三方库,那么最好使用传递依赖,在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

阅读更多关于等级依赖性管理。

#2


128  

Yes, you can use it. Here is how:

是的,你可以用它。这里是:

  1. Your Project -> right click -> Import -> File System -> yourjar.jar
  2. 您的项目->右键单击->导入->文件系统-> yourjar.jar
  3. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
  4. 您的项目->右键单击->属性-> Java构建路径->库->添加Jar -> yourjar.jar

This video might be useful in case you are having some issues.

如果你有什么问题,这个视频可能会有用。

#3


5  

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.

hess失败时,hessdroid工作的原因是这两个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://*.com/a/13144382/545064

在我的例子中,我可以访问源代码,并且可以使用我在这里描述的方法为它创建一个Android jar文件:https://*.com/a/13144382/545064

#4


5  

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。安卓工作室是这样的:

  1. Add jar file to libs directory (such as copy/paste)
  2. 将jar文件添加到libs目录(如复制/粘贴)
  3. Right-Click on jar file and select "Add as Library..."
  4. 右键单击jar文件并选择“Add as Library…”
  5. click "Ok" on next dialog or renamed if you choose to.
  6. 单击“确定”在下一个对话框或重命名,如果您选择。

That's it!

就是这样!

#5


4  

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平台缺少了java . named。*以及许多外部JAR也具有依赖性的其他包。

Adding external JAR files, and setting Order and Export in Eclipse works as expected with Android projects.

添加外部JAR文件,并在Eclipse中设置订单和导出,这些操作与Android项目的预期一致。

#6


3  

Goto Current Project

转到当前项目

RightClick->Properties->Java Build Path->Add Jar Files into Libraries -> Click OK

右键单击->属性->Java构建路径->向库中添加Jar文件->单击OK

Then it is added into the Referenced Libraries File in your Current Project .

然后将它添加到当前项目中引用的库文件中。

#7


1  

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.命名。*和许多其他javax。*的东西。您需要将依赖项包含为独立的jar。

#8


1  

If using Android Studio, do the following (I've copied and modified @Vinayak Bs answer):

如果使用Android Studio,请执行以下操作(我已经复制并修改了@Vinayak Bs的答案):

  1. Select the Project view in the Project sideview (instead of Packages or Android)
  2. 选择Project sideview中的Project视图(而不是package或Android)
  3. Create a folder called libs in your project's root folder
  4. 在项目的根文件夹中创建一个名为libs的文件夹
  5. Copy your JAR files to the libs folder
  6. 将JAR文件复制到libs文件夹
  7. The sideview will be updated and the JAR files will show up in your project
  8. 侧视图将被更新,JAR文件将显示在您的项目中
  9. Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
  10. 现在右键单击要导入的每个JAR文件,然后选择“Add as Library…”,包括在你的项目中
  11. After that, all you need to do is reference the new classes in your code, eg. import javax.mail.*
  12. 之后,您需要做的就是在代码中引用新的类。进口javax.mail。*

#9


1  

in android studio if using gradle

在android studio中使用gradle

add this to build.gradle

添加这个build.gradle

compile fileTree(dir: 'libs', include: ['*.jar'])

and add the jar file to libs folder

并将jar文件添加到libs文件夹。

#10


1  

If you are using gradle build system, follow these steps:

如果您使用的是等级构建系统,请遵循以下步骤:

  1. put jar files inside respective libs folder of your android app. You will generally find it at Project > app > libs. If libs folder is missing, create one.

    将jar文件放在android应用程序的各个libs文件夹中。通常可以在项目>应用程序> libs中找到。如果缺少libs文件夹,请创建一个。

  2. add this to your build.gradle file your app. (Not to your Project's build.gradle)

    将此添加到您的构建中。渐变文件你的应用。

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        // other dependencies
    }
    

    This will include all your jar files available in libs folder.

    这将包括在libs文件夹中可用的所有jar文件。

If don't want to include all jar files, then you can add it individually.

如果不想包含所有jar文件,那么可以单独添加它。

compile fileTree(dir: 'libs', include: 'file.jar')

编译文件树(目录:'libs',包括:'file.jar')

#11


0  

create a folder (like lib) inside your project, copy your jar to that folder. now go to configure build path from right click on project, there in build path select

在项目中创建一个文件夹(如lib),将jar复制到该文件夹。现在去配置构建路径从右键单击项目,那里有构建路径选择

'add jar' browse to the folder you created and pick the jar.

“添加jar”浏览到您创建的文件夹并选择jar。

#12


0  

Copying the .jar file into the Android project's folder isn't always possible.
Especially if it's an output of another project in your workspace, and it keeps getting updated.

将.jar文件复制到Android项目的文件夹并不总是可能的。特别是如果它是工作空间中另一个项目的输出,并且不断更新。

To solve this you'll have to add the jar as a linked file to your project, instead of importing it (which will copy it locally).

要解决这个问题,您必须将jar作为一个链接文件添加到项目中,而不是导入它(它将在本地复制它)。

In the UI choose:

在UI中选择:

Project -> Import -> File System -> yourjar.jar -> (Options area) Advanced -> Create link in workspace.

项目->导入->文件系统-> yourjar。jar ->(选项区)高级->在工作区中创建链接。

The link is save in the .project file:

该链接保存在.project文件中:

<linkedResources>
    <link>
        <name>yourjar.jar</name>
        <type>1</type>
        <locationURI>PARENT-5-PROJECT_LOC/bin/android_ndk10d_armeabi-v7a/yourjar.jar</locationURI>
    </link>
</linkedResources>

PARENT-5-PROJECT_LOC means relative to the project file, 5 directories up (../../../../../).

家长-5- project_loc表示相对于项目文件,有5个目录。

Then add it to the libraries:
Project -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

然后添加到库中:项目->属性-> Java构建路径->库->添加Jar -> yourjar.jar

In the same window choose the Order and Export tab and mark your jar so it will be added to the apk.

在同一个窗口中,选择Order和Export选项卡并标记jar,以便将其添加到apk。