How can I use external JARs in an Android project?

2018-12-31 01:42发布

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 and Export is ignored it seems, and all classes from the external JAR are missing at runtime.

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.

12条回答
冷夜・残月
2楼-- · 2018-12-31 02:08

Turns out I have not looked good enough at my stack trace, the problem is not that the external JAR is not included.

The problem is that Android platform is missing javax.naming.* and many other packages that the external JAR has dependencies too.

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

查看更多
听够珍惜
3楼-- · 2018-12-31 02:09

Goto Current Project

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

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

查看更多
后来的你喜欢了谁
4楼-- · 2018-12-31 02:10

Yes, you can use it. Here is how:

  1. Your Project -> right click -> Import -> File System -> yourjar.jar
  2. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

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

查看更多
与风俱净
5楼-- · 2018-12-31 02:10

Android's Java API does not support javax.naming.* and many other javax.* stuff. You need to include the dependencies as separate jars.

查看更多
其实,你不懂
6楼-- · 2018-12-31 02:10

in android studio if using gradle

add this to build.gradle

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

and add the jar file to libs folder

查看更多
像晚风撩人
7楼-- · 2018-12-31 02:11

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.

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).

In the UI choose:

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

The link is save in the .project file:

<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 (../../../../../).

Then add it to the libraries:
Project -> Properties -> Java Build Path -> Libraries -> Add 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.

查看更多
登录 后发表回答