How can I use external JARs in an Android project?

前端 未结 12 1388
甜味超标
甜味超标 2020-11-21 11:40

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.

相关标签:
12条回答
  • 2020-11-21 11:41

    I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.

    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.

    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

    0 讨论(0)
  • 2020-11-21 11:41

    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.

    0 讨论(0)
  • 2020-11-21 11:45

    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:

    1. Add jar file to libs directory (such as copy/paste)
    2. Right-Click on jar file and select "Add as Library..."
    3. click "Ok" on next dialog or renamed if you choose to.

    That's it!

    0 讨论(0)
  • 2020-11-21 11:45

    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.

    0 讨论(0)
  • 2020-11-21 11:51

    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 .

    0 讨论(0)
  • 2020-11-21 11:53

    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.

    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.

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

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

    0 讨论(0)
提交回复
热议问题