[英]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. 我已经在Android Studio的Gradle依赖项上苦苦挣扎了4天以上。
I'm trying to create a java library (MyLibrary) that requires some external dependencies, namely commons-codec. 我正在尝试创建一个Java库(MyLibrary),该库需要一些外部依赖关系,即commons-codec。 I added it inside my build.gradle, which looks something like this.
我将其添加到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'
}
I was able to assemble my debug and release AAR files in Android Studio. 我能够在Android Studio中组装调试并发布AAR文件。 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:
现在在我的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')
}
The project build, and the app installed but crashes when it executes the part of the code that uses common-codec. 项目构建和应用程序已安装,但在执行使用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? 我的问题是,为什么MyLibrary中不包含common-codec?
Thanks 谢谢
I think you are interpreting the error wrong. 我认为您将错误解释为错误。 It is a
NoSuchMethodException
not a ClassNotFoundException
. 这是一个
NoSuchMethodException
而不是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. 因此,运行时类路径上必须有一个不同(即较旧)的
commons-codec
版本。
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. 搜索错误后,我发现这是android和
commons-codec
一个普遍问题,因为显然android框架捆绑了自己的版本。 See this question for details and possible solutions: Method not found using DigestUtils in Android 请参阅此问题以获取详细信息和可能的解决方案: 在Android中使用DigestUtils找不到方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.