简体   繁体   中英

How do I import an external library into Android Studio?

I need to use the commons-codec-1.9.jar from the commons-codec-1.9 library supplied by Apache. I've used this as a library in another project in NetBeans, so I already had the required .jar file that I needed to import. I copied the jar file from the NetBeans project and put it in my app project (app:libs folder) and right-clicked and chose the "Add as library..." option. I added it to my project dependencies as well via File > Project Structure > Dependencies. I created the import for the library in the classes I've written that use the library, and I get no errors and the code builds. However, when I run the app, it breaks and throws a NoSuchMethod exception... even though it is there.

Here are the contents of my build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "com.example.<path_stuff>"
        minSdkVersion 17
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/commons-codec-1.9.jar')
}

Here is my settings.gradle

include ':app'

I have also recently been getting a build error after I removed the commons-codec stuff from the libs folder and tried to re-add it. The error says something along the lines of "Build failed: could not find task 'assemble' in project " as if Android Studio thinks this is another project. Can someone please help me understand what is going on here and where I am going wrong?

No such method error details:

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String

Edit: Realized I included the incorrect build.gradle file. Updated settings.gradle to remove the include statement. Included correct build.gradle and NoSuchMethod error.

Remove include ':libs:commons-codec-1.9: from your settings.gradle .

Add compile fileTree(dir: 'libs', include: '*.jar') to your dependencies block in your module's build.gradle file.

android {
    ...
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

This will automatically compile any .jar file in your libs folder into your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM