How can I use external JARs in an Android project?

前端 未结 12 1466
甜味超标
甜味超标 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:55

    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

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

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

    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

    0 讨论(0)
  • 2020-11-21 12:00

    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.

    0 讨论(0)
  • 2020-11-21 12:04

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

    0 讨论(0)
  • 2020-11-21 12:05

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

    1. Select the Project view in the Project sideview (instead of Packages or Android)
    2. Create a folder called libs in your project's root folder
    3. Copy your JAR files to the libs folder
    4. The sideview will be updated and the JAR files will show up in your project
    5. Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
    6. After that, all you need to do is reference the new classes in your code, eg. import javax.mail.*
    0 讨论(0)
  • 2020-11-21 12:06

    For Eclipse

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

    1. Create a folder called libs in your project's root folder
    2. Copy your JAR files to the libs folder
    3. 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.

    For Android Studio

    1. If you are in Android View in project explorer, change it to Project view as below

    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. Now copy the blah_blah.jar into the 'libs' folder
    3. 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

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