Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Android Studio don't recognize .jar library imports?

I'm trying to use some .jar files as libraries in my Android Studio project.

I've been looking on how to do it, and the regular way is to copy the .jar file in the libs folder, and then add it as library. I know the libs folder must be inside "app" module in the "project" view. But that option to add as library doesn't appear. So my approach is to add it manually doing right click on "app" and:

Open Module Setings/app/Dependencies/ and there add .jar files as file dependency.

Then, I go to my .class but the import suggestions don't let me select these libraries as import.

I have tried another approach from the "android" view. Doing right click on "app" and selecting "new module", there I select Import .JAR or .AAR Package and then I do the same as before from Open Module Setings, but this time I add it as Module Dependency.

In booth ways, the gradle.build file is updated adding these dependencies, but when going to the activity to select the import, it doesn't give me the chance to select the import from these ones.

I have spent a full day trying to solve this, but I don't get my activity to recognize the imports from these files, so I would appreciate if someone could tell me what I'm doing wrong or what I'm missing to do.

Top-level build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
         classpath 'com.android.tools.build:gradle:1.1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

app-level build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-v14'] } }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':activation')
    compile project(':mail')
}
like image 997
masmic Avatar asked Apr 15 '15 09:04

masmic


3 Answers

My current project using gson-2-2.4.jar and YouTubeAndroidPlayerApi.jar and some others. To import those jar, I put it in my libs folder. After that, go to build.gradle and delete some characters in it, and rewrite (1). Finally, click "sync now" (2). I don't know why my sync button enter image description here does not work when I add jar to libs, so I do that, and it work so well.

enter image description here

UPDATE

On Android 3.x.y we can use Sync Project with Gradle Files or Refresh all Gradle projects

enter image description here

like image 166
Robust Avatar answered Oct 09 '22 13:10

Robust


enter image description here Firstly, I import this JAR, AS can not recognize this JAR and I can not import. And than I press right click in this JAR and choose "Add as Library". AS can recognize this JAR and solve the problem!!

like image 42
Fantasy Fang Avatar answered Oct 09 '22 13:10

Fantasy Fang


With Respect to Android Studio 1.5.1:

You need to copy and paste the JAR file into:

APP_NAME/app/libs

Note that the libs folder will already exist. Once copied, change the project view from Android to Project files.

Open up the app/libs folder and right click the JAR file, then click Add As Library.

That should do it. I wasn't able successfully use the Add Module to import a JAR file.

like image 31
Yvan Pearson Avatar answered Oct 09 '22 12:10

Yvan Pearson