Android Studio: Add jar as library?

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

    In Android Stuido, I like use Gradle to manage Gson lib.

    Add below dependency in your build.gradle file.

    repositories {mavenCentral()}
    
    dependencies {compile 'com.google.code.gson:gson:2.2.4'}
    

    Everything is OK.

    You can also see this post. The best way to integrate third party library in Android studio

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

    'compile files...' used to work for me, but not any more. after much pain, I found that using this instead works:

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

    I have no idea why that made a difference, but, at least the damn thing is working now.

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

    You can do this with two options.

    first simple way.

    Copy the .jar file to clipboard then add it to libs folder. To see libs folder in the project, choose the project from combobox above the folders.

    then right click on the .jar file and click add as a library then choose a module then ok. You can see the .jar file in build.gradle file within dependencies block.

     dependencies {
            implementation fileTree(dir: 'libs', include: ['*.jar'])
            implementation 'com.android.support:appcompat-v7:21.0.3'
            implementation project(':okhttp-2.0.0')
            implementation 'com.google.code.gson:gson:2.3.1'
        }
    

    Second way is that: We can add a .jar file to a module by importing this .jar file as a .jar module then add this module to any module we want.

    import module ---> choose your .jar file --> than import as a .jar -- enter image description here

    Then CTRL+ALT+SHIFT+S --> project sturure -->choose the module you want ato add a jar -->Dependencendies --> Module Dependency. build.gradle of the module will updated automatically. enter image description here

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

    I made it work by just adding one line to build.gradle:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
        implementation 'com.google.code.gson:gson:2.3.1'   ----------> I added this one
    }
    

    Do not forget to click Sync now in the top right corner.

    I'm using Android Studio 1.0.1.

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

    In Android Studio 2.1 I follow the this way,

    Goto app -> src-> main -> assets folder (If not available create it) -> put your JAR files

    In your build.gradle add dependency like this,

    implementation files('src/main/assets/jsoup.jar')
    implementation files('src/main/assets/org-apache-xmlrpc.jar')
    implementation files('src/main/assets/org.apache.commons.httpclient.jar')
    implementation files('src/main/assets/ws-commons-util-1.0.2.jar')
    

    Sync now. Now your JAR files ready to use.

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

    1) create an 'your_libs' folder inside the Project/app/src folder.

    2) Copy your jar file into this 'your_libs' folder

    3) In Android Studio, go to File -> Project Structure -> Dependencies -> Add -> File Dependency and navigate to your jar file, which should be under 'src/your_libs'

    3) Select your jar file and click 'Ok'

    and then you can see on your build.gradle like this : compile files('src/your_libs/your.jar')

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