Android Studio: Add jar as library?

后端 未结 30 2369
天命终不由人
天命终不由人 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:54

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

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

    2. 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>')
      }
      
    3. 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.

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

      Menu

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

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

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

    Ive done above 3 steps and its work charm for me.

    (I am using Android Studio 2.1.2)

    Step 1

    • Add your jar package name (as an example compile 'com.squareup.okhttp3:okhttp:3.3.1') into gradle build script under build.gradle(Module:app).

    Step 2: Right click on app folder -> New -> Module

    Step 3: Click Import JAR/.AAR Package Then browse your package. as an example: OkHttp.jar

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

    Put the .jar files in libs folder of the Android project.

    Then add this line of code in the app's gradle file:

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

    For Android gradle plugin 3.0 and later, it is better to use this instead:

        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
    0 讨论(0)
  • 2020-11-21 06:55

    Unlike Eclipse we don't need to download jar and put it in /libs folder. Gradle handles these things we only need to add Gradle dependencies, Gradle downloads it and puts in gradle cache.

    We need to add dependencies as:

    dependencies {implementation 'com.google.code.gson:gson:2.2.4'}

    thats it However we can also download jar & add that as library but the best practice is to add Gradle dependencies.

    0 讨论(0)
  • 2020-11-21 06:55
    menu File -> project struct -> module select "app" -> dependencies tab -> + button 
    -> File dependency -> PATH/myfile.jar
    
    0 讨论(0)
  • 2020-11-21 06:55

    In android Studio 1.1.0 . I solved this question by following steps:

    1: Put jar file into libs directory. (in Finder)

    2: Open module settings , go to Dependencies ,at left-bottom corner there is a plus button. Click plus button then choose "File Dependency" .Here you can see you jar file. Select it and it's resolved.

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