Android Studio: Add jar as library?

I'm trying to use the new Android Studio but I can't seem to get it working correctly.

I'm using the Gson library to serialize/deserialize JSON-objects. But the library somehow isn't included in the build.

I had created a new project with just a MainActivity . Copied gson-2.2.3.jar in the /libs folder and added it as a library dependancy(right click->Add as library). This includes the jar in android studio so it can be referenced from the source files.

When I try to run the project it cannot compile so I added:

compile files('libs/gson-2.2.3.jar')

to the dependencies in de .gradle file. After that it compiles correctly but when running the application I get a ClassDefNotFoundException .

Does anyone know what I'm doing wrong?


I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took:

  • Put the Gson jar (in my case, gson-2.2.4.jar ) into the libs folder
  • Right click it and hit 'Add as library'
  • Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files)

    Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar') ) in Android Studio 3.0+

  • Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean . I'm on Mac OS X, the command might be different on your system

  • After I did the above four, it started working fine. I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.

    [Edit - added the build.gradle step which is also necessary as others have pointed out]


    Here are the instructions for adding a local jar file as a library to a module:

  • Create a 'libs' folder in the top level of the module directory (the same directory that contains the 'src' directory)

  • In the build.gradle file add the following so that your dependencies closure has:

    dependencies {
        // ... other dependencies
        compile files('libs/<your jar's name here>')
    }
    
  • Android Studio should have already setup a gradlew wrapper. From the command line, navigate to the top level of your project (the directory that has a gradlew file).

    Run ./gradlew assemble . This should compile the project with the library. You may need to fix errors in your build.gradle file as necessary.

  • In order to have Android Studio recognize the local jar files as libraries for support while coding in the IDE, you need to take a few more steps:

    4.1. Right click on the module in the left hand panel and choose Open Library Settings .

    4.2. On the left panel of the dialog, choose Libraries .

    4.3. Click the + sign above the panel second from the left -> Java

    菜单

    4.4. Select your local jar and add it to the project.

  • You may need to run the above ./gradlew command one more time


  • In the project right click

    -> new -> module
    -> import jar/AAR package
    -> import select the jar file to import
    -> click ok -> done
    

    Follow the screenshots below:

    1:

    步骤1

    2:

    在这里输入图像描述

    3:

    在这里输入图像描述

    You will see this:

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

    上一篇: 排球Android网络库

    下一篇: Android Studio:将jar添加为库?