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

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.

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

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

查看更多
旧人旧事旧时光
4楼-- · 2018-12-31 01:33

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.

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

In the project right click

-> new -> module
-> import jar/AAR package
-> import select the jar file to import
-> click ok -> done

Follow the screenshots below:

1:

Step 1

2:

enter image description here

3:

enter image description here

You will see this:

enter image description here

查看更多
大哥的爱人
6楼-- · 2018-12-31 01:36

My answer is basically gathering some of the right but incomplete answers provided above.

  1. Open build.gradle
  2. Add the following:

    dependencies {
    compile 'com.android.support:appcompat-v7:19.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.3'
    }
    

    This will allow support for two different ways of adding dependencies. The compile fileTree(dir: 'libs', include: ['*.jar']) (as @Binod mentioned) tells the compiler to look under the folder libs for ANY jar. It is a good practice to create such a folder 'libs' which will contain the jar packages that our application needs to use.

But this will also allow support for Maven dependency. The compile 'com.google.code.gson:gson:2.3' (as mentioned by @saneryee) it is another recommended way to add dependencies that are in a central remote repository and not in our /libs "local repository". It is basically telling gradle to look for that version of that package and it's telling the compiler to consider it when compiling the project (having it in the classpath)

PS: I use both

查看更多
春风洒进眼中
7楼-- · 2018-12-31 01:38

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

查看更多
登录 后发表回答