Android Studio: Add jar as library?

后端 未结 30 2434
天命终不由人
天命终不由人 2020-11-21 05:54

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

相关标签:
30条回答
  • 2020-11-21 06:42

    On Mac OS X:

    1. Add jar as library (drag jar to libs, right click add as lib)

    2. Add compile statement to build.grade

    3. Install gradle v1.6 (use homebrew)

      • brew install gradle
      • gradle -v
      • if not v1.6, upgrade homebrew
    4. gradle clean (rebuild android did not work)

    This sorted me out.

    0 讨论(0)
  • 2020-11-21 06:43

    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:

    1. Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
    2. Right click it and hit 'Add as library'
    3. 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+

    4. 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]

    0 讨论(0)
  • 2020-11-21 06:43

    Download & Copy Your .jar file in libs folder then adding these line to build.gradle:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.google.code.gson:gson:2.3.1'
    }
    

    Do not forget to click "Sync now"

    0 讨论(0)
  • 2020-11-21 06:44

    IIRC, simply using "Add as library" isn't enough for it to compile with the project.

    Check Intellij's help about adding libraries to a project

    The part that should interest you the most is this:

    (In File > Project Structure) Open the module settings and select the Dependencies tab.

    On the Dependencies tab, click add and select Library.

    In the Choose Libraries dialog, select one or more libraries and click Add Selected.

    If the library doesn't show up in the dialog, add it in the Libraries settings, right below Modules.

    You shouldn't need to add compile files() anymore, and the library should be properly added to your project.

    0 讨论(0)
  • 2020-11-21 06:44

    1. Put the jar (in my case, gson-2.2.4.jar) into the libs folder.

    2. Ensure that compile files (libs/gson-2.2.4.jar) is in your build.gradle file.

    3. Now Click on the "Sync Project with Gradle files"(Left to AVD manager Button on the topbar).

    After I did the above three, it started working fine.

    0 讨论(0)
  • 2020-11-21 06:44

    For newer Android 1.0.2 the following is already there in your build.gradle file

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

    Add the library jar to your libs folder -> right click the library -> click add as a library -> it asks you for the project to add it for -> select your project-> click ok The following line is automatically added to build.gradle

    implementation files('libs/android-query.jar')
    

    That did it for me. nothing more was required. i have shown this for android aquery another third party library for android.

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