无法将依赖项添加到 Android 项目

Can't add dependency to Android Project

我正在尝试将 'commons-validator' 添加到我在 Android Studio 中基于 gradle 的 android 项目。我使用 UrlValidator 来满足我的需要。

所以我在应用模块的build.gradle中添加了一个依赖:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'commons-validator:commons-validator:1.4.1' // this one
}

并将库用于Android应用程序标签中的清单:

 <uses-library android:name="org.apache.commons.validator.routines.UrlValidator"
        android:required="true"/>

但是添加后我的项目失败运行。

Error:Execution failed for task ':app:dexDebug'. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

我也得到了

Warning:Dependency commons-logging:commons-logging:1.2 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages

4 次:调试两次,发布两次。

尝试删除 <uses-library> 标签。这是为了要求用户在安装您的应用程序之前安装某个外部库。 gradle 依赖项应该足够了,因为您想在代码内部包含该库。

我认为问题出在传递依赖上。在研究了一些 SO 的线程后,我在控制台中写道:

cd app/ #to enter app module folder
../gradlew dependencies

这给了我以下输出:

_debugCompile - ## Internal use, do not manually configure ##
+--- commons-validator:commons-validator:1.4.1
|    +--- commons-beanutils:commons-beanutils:1.8.3
|    |    \--- commons-logging:commons-logging:1.1.1 -> 1.2
|    +--- commons-digester:commons-digester:1.8.1
|    +--- commons-logging:commons-logging:1.2
|    \--- commons-collections:commons-collections:3.2.1

所以我将这个添加到 build.gradle:

compile('commons-validator:commons-validator:1.4.1'){
        exclude group: 'commons-logging'
        exclude group: 'commons-collections'
        exclude group: 'commons-digester'
        exclude group: 'commons-beanutils'
}

还有一些人告诉我将 multiDexEnabled true 添加到 defaultConfig 部分,但正如我所尝试的那样,它对我来说没有它也能工作。

正如@Brucelet 所说 - 从清单中删除了 <uses-library> 标签。

虽然 gradle 输出给出了很多 AGPBI 消息,但它运行和工作正常:

AGPBI: {"kind":"simple","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]} AGPBI: {"kind":"simple","text":"(org.apache.commons.validator.CreditCardValidator) that doesn\u0027t come with an","sources":[{}]} AGPBI: {"kind":"simple","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]} AGPBI: {"kind":"simple","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]} AGPBI: {"kind":"simple","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]} AGPBI: {"kind":"simple","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]} AGPBI: {"kind":"simple","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]} AGPBI: {"kind":"simple","text":"indicate that it is not an inner class.","sources":[{}]} AGPBI: {"kind":"simple","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]} AGPBI: {"kind":"simple","text":"(org.apache.commons.validator.ValidatorResources) that doesn\u0027t come with an","sources":[{}]} AGPBI: {"kind":"simple","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]} AGPBI: {"kind":"simple","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]} AGPBI: {"kind":"simple","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]} AGPBI: {"kind":"simple","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]} AGPBI: {"kind":"simple","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]} AGPBI: {"kind":"simple","text":"indicate that it is not an inner class.","sources":[{}]}