How can I use external JARs in an Android project?

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.


Eclipse

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

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

  • If you are in Android View in project explorer, change it to Project view as below
  • 在这里输入图像描述

  • Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
  • Now copy the blah_blah.jar into the 'libs' folder
  • 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
  • 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.

    Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'

    Read more about Gradle Dependency Mangement


    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
  • This video might be useful in case you are having some issues.


    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

    链接地址: http://www.djcxy.com/p/65836.html

    上一篇: 无法执行dex:多个dex文件定义Lcom / myapp / R $ array;

    下一篇: 我如何在Android项目中使用外部JAR?