1081

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?

2
  • Refer : stackoverflow.com/a/6859020/28557 Commented Aug 26, 2016 at 18:50
  • 4
    Although this doesn't solve the problem, just wanna point that "compile" was deprecated in favor of "implementation". Commented Jun 17, 2018 at 14:32

36 Answers 36

1
2
1

compile fileTree(dir: 'libs', include: '*.jar') works fine but not compile files(...) have tested with Studio Beta 0.8.1

Sign up to request clarification or add additional context in comments.

Comments

1

Here are various ways to do this in Kotlin DSL (build.gradle.kts).
We assume that the library files are in project/app/libs/ directory.

  • Method 1

    implementation( files( "libs/library-1.jar", "libs/library-2.jar", "$rootDir/foo/my-common-library.jar" ) ) 
  • Method 2

    implementation( fileTree("libs/") { // You can add as many include or exclude calls as you want include("*.jar") include("another-library.aar") // aar is similar to jar exclude("bad-library.jar") } ) 
  • Method 3

    implementation( fileTree( // Here, instead of repeating include or exclude, assign a list "dir" to "libs/", "include" to "*.jar", "exclude" to listOf("bad-library-1.jar", "bad-library-2.jar") ) ) 
  • Method 4

    repositories { flatDir { dirs("libs/", "lib/") } } dependencies { // Could also write implementation(":jsoup:1.4.13") implementation("org.jsoup:jsoup:1.4.13") // NOTE: Add @aar suffix for AAR files implementation("ir.mahozad.android:pie-chart:0.7.0@aar") } 

You can use Ant patterns in include and exclude calls as shown above.

See Gradle documentations for more information about this.

Thanks to this post and this post for providing helpful answers.

Comments

0

DO this :

Create libs folder under the application folder. Add .jar files to libs folder. Then add .jar files to app's build.gradle dependency. Finally Sync project with Gradle files.

Comments

0

In my case the added library missed some dependencies, but unfortunately Android Studio (0.8.14) has kept this as a secret.

There was no need to manually configure anything! I just added the missing libraries and used the default dependency configuration in the app build.gradle file, like this

dependencies { compile 'com.google.code.gson:gson:2.+' compile fileTree(dir: 'libs', include: ['*.jar']) } 

Comments

0

I don' know why but my Android Studio 0.8.14 goes crazy when I try to implement these solutions using Gradle. I admit my poor knowledge of this great build tool but what does Studio mutilate my project for? I manage to get it working this way: put android-support-v13.jar into 'libs' directory, then F4 on my project and add File dependency where I pointed android-support-v13.jar.

Comments

0
 for .aar files dependencies { implementation( fileTree("libs") { include("*.aar") } ) ... } or for .jar files dependencies { implementation( fileTree("libs") { include("*.jar") } ) ... } 

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.