简体   繁体   中英

How to create java library using external dependencies in Gradle?

I've been struggling for 4+ days now on Gradle's dependencies in Android Studio.

I'm trying to create a java library (MyLibrary) that requires some external dependencies, namely commons-codec. I added it inside my build.gradle, which looks something like this.

    apply plugin: 'com.android.library'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
        useLibrary 'org.apache.http.legacy'

        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        lintOptions {
            abortOnError false
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.3.0'
        compile 'com.google.android.gms:play-services:8.4.0'
        compile 'commons-codec:commons-codec:1.10'
    }

I was able to assemble my debug and release AAR files in Android Studio. Now in my Android project, I added MyLibrary by importing it as an AAR module and setup the build.gradle file for my project to look like this:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

    //    useLibrary 'org.apache.http.legacy'

        defaultConfig {
            applicationId "com.my.package"
            minSdkVersion 21
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    repositories {
        mavenCentral()
        flatDir {
            dirs 'libs'
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.2.0'
        compile 'com.google.android.gms:play-services:7.8.+'
        compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
        compile 'commons-codec:commons-codec:1.10'
        compile project(':MyLibrary')
    }

The project build, and the app installed but crashes when it executes the part of the code that uses common-codec.

FATAL EXCEPTION: Thread-172468 java.lang.NoSuchMethodError: No static method encodeHexString([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Hex; or its super classes (declaration of 'org.apache.commons.codec.binary.Hex' appears in /system/framework/ext.jar) at org.apache.commons.codec.digest.DigestUtils.sha256Hex

My question is, why common-codec isn't included in MyLibrary?

Thanks

I think you are interpreting the error wrong. It is a NoSuchMethodException not a ClassNotFoundException . The former means the class is found (somewhere on the classpath) it just doesn't have a method with a signature matching what you are trying to call.

Therefore, there must be a different (ie older) version of commons-codec on the runtime classpath.

After googling the error I see that this is a somewhat common issue with android and commons-codec since apparently android framework bundles its own version. See this question for details and possible solutions: Method not found using DigestUtils in Android

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