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:14

Easy steps to add external library in Android Studio

  1. If you are in Android View in project explorer, change it to Project view as below

enter image description here

  1. Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
  2. Now copy the blah_blah.jar into the 'libs' folder
  3. Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done

Please Note : If you are using 3rd party libraries then it is better to use dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.

Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'

Read more about Gradle Dependency Mangement

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

With Android Studio 3+:

You should just be able to simply copy the jar file to the libs folder right under the app folder.

... myproject\app\libs\myfile.jar

Then select Project Files from the drop-down on the Projects window, right click on the project, select Synchronize to see the file in Project Files. It will automatically add the dependencies in the gradle file (Module:app).

dependencies {
...
    implementation files('libs/myfile.jar')

Here is another solution:

Go to the Project Files view (select Project Files from the dropdown).

enter image description here

Select New... Directory, create a folder named libs right under app.

enter image description here

Open up File Explorer, copy and paste your jar file into the libs folder.

In Android Studio, right click on the jar file, and select Add as a Library... from the popup menu.

enter image description here

You should see the file listed in the dependencies list in the gradle file:

dependencies {
...
    implementation files('libs/myfile.jar')
}

Open up your java file, and add the import statement there:

import com.xxx.xxx;
查看更多
初与友歌
4楼-- · 2018-12-31 01:16

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.

查看更多
回忆,回不去的记忆
5楼-- · 2018-12-31 01:18

Create a folder libs. Add your .jar file. Right click on it and you will find add jar as dependency. Click on it. Its all you need to do. You can find the dependencies added to your build.gradle file.

查看更多
临风纵饮
6楼-- · 2018-12-31 01:19

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

查看更多
明月照影归
7楼-- · 2018-12-31 01:20
  1. Download Library file from website
  2. Copy from windows explore
  3. Paste to lib folder from Project Explorer
  4. Ctrl+Alt+Shift+S open Project Structure
  5. Select Dependencies Tab, add the file by using +
  6. Tool bar Sync project with gradle file by using button

That solved my problem. Try, if anyone want more details let me know.

查看更多
登录 后发表回答