Android Studio: Add jar as library?

2018-12-31 00:51发布

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?

30条回答
临风纵饮
2楼-- · 2018-12-31 01:22

I have read all the answers here and they all seem to cover old versions of Android Studio!

With a project created with Android Studio 2.2.3 I just needed to create a libs directory under app and place my jar there. I did that with my file manager, no need to click or edit anything in Android Studio.

Why it works? Open Build / Edit Libraries and Dependencies and you will see:

{include=[*.jar], dir=libs}
查看更多
春风洒进眼中
3楼-- · 2018-12-31 01:22
  1. Added the libs folder at the level of app.
  2. Added all the jars in this project.
  3. Next, selected all the jars, in the libs folder,
  4. right click on the selected items, and say add library
  5. then you will find the jars expansion option, within the project explorer itself.

I observed CTRL + ALT + SHIFT + S --> project structure --> app-module -->Dependencies" already had an entry as (dir: 'libs', include: '*.jar') under compile-option, initially. And after adding the jar's as per the steps stated above, the build.gradle got the entries for the new added jar's, itself.

查看更多
深知你不懂我心
4楼-- · 2018-12-31 01:23

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

enter image description here

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.

查看更多
牵手、夕阳
5楼-- · 2018-12-31 01:25

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"

查看更多
妖精总统
6楼-- · 2018-12-31 01:26

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.

查看更多
梦该遗忘
7楼-- · 2018-12-31 01:29

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

(I am using Android Studio 2.1.2)

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

查看更多
登录 后发表回答