How to add a jar in External Libraries in android studio

前端 未结 14 2067
北海茫月
北海茫月 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:33

    If you dont see option "Add as Library", make sure you extract (unzip) your file so that you have mail.jar and not mail.zip.

    Then right click your file, and you can see the option "Add as library".

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

    Please provide the jar file location in build.gradle

    implementation fileTree(dir: '<DirName>', include: ['*.jar'])
    

    Example:

    implementation fileTree(dir: 'C:\Downloads', include: ['*.jar'])
    

    To add single jar file

    implementation files('libs/foo.jar')
    

    Note: compile is deprecated in latest gradle, hence use implementation instead.

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

    The GUI based approach would be to add an additional module in your project.

    1. From the File menu select Project Structure and click on the green plus icon on the top left.
    2. The new Module dialog pops
    3. From the phone and tablet application group select the "Import JAR or AAR package" option and click next.
    4. Follow the steps to create a new module that contains your JAR file.
    5. Click on the entry that corresponds to your main project and select the dependencies tab.
    6. Add a dependency to the module that you created in step 4.

    One final piece of advice. Make sure that the JAR file you include is build with at most JDK 1.7. Many problems relating to error message "com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)" root straight to this :0.

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

    If anyone is looking for another solution without actually copying the jar file(s) into the project directory, e.g. when using a jar in multiple projects:

    Open build.gradle and add

    def myJarFolder = 'C:/Path/To/My/Jars'
    
    [...]
    
    dependencies {
        [...]
        compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
        compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
        [...]
    }
    

    Note that of course you don't have to use the myJarFolder variable, I find it useful though. The path can also be relative, e.g. ../../Path/To/My/Jars.
    Tested with AndroidStudio 3.0

    Update: For Gradle Plugin > 3.0 use implementation instead of compile:

    dependencies {
            [...]
            implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
            implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
            [...]
        }
    
    0 讨论(0)
  • 2020-11-22 16:36

    A simple way to add Jar file Android Studio Steps:

    1. Copy and paste your jar file to libs folder of your project.
    2. Click File from File menu -> Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X).
    3. Select Modules at the left panel -> Dependencies tab.
    4. Add... → Project Library → Attach Jar.
    0 讨论(0)
  • 2020-11-22 16:37

    Step 1: Download any JAR file for your Project.

    Step 2: Copy .jar file and past in libs folder.

    Step 3: Click on File > Project Structure >Select app > Dependencies

    Step 4:

    Step 5:

    Step 6: After click Ok button then we can see the Dependencies add like this way:

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