我已经在 Android Studio 中的 Gradle 依赖项上苦苦挣扎了 4 天以上。
我正在尝试创建一个需要一些外部依赖项的 java 库(MyLibrary),即 commons-codec。我将它添加到我的 build.gradle 中,看起来像这样。
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'
}
我能够在 Android Studio 中组装我的调试和发布 AAR 文件。现在在我的 Android 项目中,我通过将 MyLibrary 导入为 AAR 模块来添加它,并为我的项目设置 build.gradle 文件,如下所示:
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')
}
项目构建,应用程序已安装,但在执行使用 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
我的问题是,为什么 MyLibrary 中不包含 common-codec?
谢谢
最佳答案
我认为您对错误的解释是错误的。这是一个 NoSuchMethodException
不是 ClassNotFoundException
.前者意味着找到了该类(在类路径上的某处),它只是没有一个方法,其签名与您尝试调用的内容相匹配。
因此,commons-codec
必须有不同的(即较旧的)版本。在运行时类路径上。
谷歌搜索错误后,我发现这是 android 和 commons-codec
的一个常见问题。因为显然android框架 bundle 了自己的版本。有关详细信息和可能的解决方案,请参阅此问题:Method not found using DigestUtils in Android
关于java - 如何在 Gradle 中使用外部依赖项创建 java 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36511890/