How to add a jar in External Libraries in android studio

前端 未结 14 2056
北海茫月
北海茫月 2020-11-22 16:17

I am new to Android Studio. What I need to do is add a few jar files in the External Libraries below the < JDK > folder.

相关标签:
14条回答
  • 2020-11-22 16:39

    This is how you can add .jar file in Android Studio 2.1.3.

    1. Copy the .jar file

    2. paste the file in Libs folder and then right click on .jar file and press Add as library

    3. open build.gradle

    4. add lines under dependencies as shown in screenshot

    5. Now press play button and you are done adding .jar file

    0 讨论(0)
  • 2020-11-22 16:40

    Put your JAR in app/libs, and in app/build.gradle add in the dependencies section:

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

    0 讨论(0)
  • 2020-11-22 16:42

    A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.


    STEPS


    1. First switch your folder structure from Android to Project.

    1. Now search for the libs folder inside app - build folder.

    1. Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].

    Now you can start using the library in your project.

    0 讨论(0)
  • 2020-11-22 16:42

    Create "libs" folder in app directory copy your jar file in libs folder right click on your jar file in Android Studio and Add As library... Then open build.gradle and add this:

    dependencies {
        implementation files('libs/your jar file.jar')
    }
    
    0 讨论(0)
  • 2020-11-22 16:49

    Example with Parse jar...

    Add jars to libs folder from Project view … create lib folder if not exists

    Copy all jars there...

    Add libs to gradle.... in build.gradle file :

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.0'
        compile 'com.android.support:design:23.0.0'
        compile 'com.android.support:percent:23.0.0'
        compile 'com.parse.bolts:bolts-android:1.+'
        compile fileTree(dir: 'libs', include: 'Parse-*.jar’)
    }
    

    For add all jars of lib folder... change Parse-*.jar to *.jar

    0 讨论(0)
  • 2020-11-22 16:51

    The "official way" to add a jar into your android project as an external library, is to add the jar in dependencies { } section in build.gradle.

    If you've done all of the above, and none of the above works, then there are two other possibilities:

    1. If Android Studio or some other IDE doesn't give red underlined errors in editor but runs in error in the Java Compiler, CHECK YOUR JAR. Your jar may not have a proper Manifest included therefore the compiler doesn't know what the jar can provide/don't know it's package name
    2. Maybe it's folder structure is messed up. The folder structure doesn't match with its package name. package a.b.c; should be matching to folder a > folder b > folder c.
    3. The package name has some conflict. (Trust me, this happened to me and it took hours to figure it out.)

    However, if you are going with cordova, here are some tips of adding external jars.

    "build-extras.gradle" is the better way to manage your gradle file.

    Here are the steps to manage extra settings in cordova-based android project:

    1. Adding your jar into build-extras.gradle:
      //Other settings go here, e.g.: buildscript { ... }
      ext.postBuildExtras = {
      // you may have some other settings, e.g.: android { ... }
              dependencies {
                      compile files('libs/abc.jar')
              }
      }
      

    (More detailed steps here: Extending the cordova gradle file to include google services )

    1. In terminal, do:

    cordova build android

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