本文介绍了无法向 Android 项目添加依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add 'commons-validator' to my android project in Android Studio based on gradle. I use UrlValidator for my needs.

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

So I add a dependency in build.gradle of an app module:

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
}

并在应用程序标签中将-library 用于 AndroidManifest:

And uses-library to the AndroidManifest in application tag:

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

但添加它后,我的项目无法运行.

But after adding it my project fails to Run.

错误:任务: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''完成非零退出值 2

我也明白

警告:Dependency commons-logging:commons-logging:1.2 在调试时被忽略,因为它可能与 Android 提供的内部版本冲突.有问题请用jarjar重新打包换类包

4 次:2 次调试,2 次发布.

4 times: two for debug and two for release.

推荐答案

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

I think the problem were transitive dependencies. After researching some SO's threads I wrote in my console:

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:

So I added this to 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 部分,但我试过它没有它对我有用.

Also some people told to add multiDexEnabled true to defaultConfig part but as I tried it works without it for me.

正如@Brucelet 所说 - 从清单中删除了 标签.

As @Brucelet said - removed <uses-library> tag from the manifest.

它运行正常,尽管 gradle 输出给出了很多 AGPBI 消息:

It runs and works correctly, although gradle output gives a lot of some AGPBI messages:

AGPBI: {"kind":"simple","text":"警告:忽略匿名内部类的 InnerClasses 属性","sources":[{}]}AGPBI:{"kind":"simple","text":"(org.apache.commons.validator.CreditCardValidator$1) 不带有","sources":[{}]}AGPBI: {"kind":"simple","text":"关联的 EnclosureMethod 属性.这个类可能是由 a","sources":[{}]}AGPBI: {"kind":"simple","text":"未针对现代 .class 文件格式的编译器.推荐的","sources":[{}]}AGPBI: {"kind":"simple","text":"解决方案是从源代码重新编译类,使用最新的编译器","sources":[{}]}AGPBI: {"kind":"simple","text":"并且没有指定任何\"-target\"类型选项.忽略","sources":[{}]}的结果AGPBI: {"kind":"simple","text":"这个警告是对这个类的反射操作将不正确","sources":[{}]}AGPBI: {"kind":"simple","text":"表明它不是一个内部类.","sources":[{}]}AGPBI: {"kind":"simple","text":"警告:忽略匿名内部类的 InnerClasses 属性","sources":[{}]}AGPBI:{"kind":"simple","text":"(org.apache.commons.validator.ValidatorResources$1) 不带有","sources":[{}]}AGPBI: {"kind":"simple","text":"关联的 EnclosureMethod 属性.这个类可能是由 a","sources":[{}]}AGPBI: {"kind":"simple","text":"未针对现代 .class 文件格式的编译器.推荐的","sources":[{}]}AGPBI: {"kind":"simple","text":"解决方案是从源代码重新编译类,使用最新的编译器","sources":[{}]}AGPBI: {"kind":"simple","text":"并且没有指定任何\"-target\"类型选项.忽略","sources":[{}]}的结果AGPBI: {"kind":"simple","text":"这个警告是对这个类的反射操作将不正确","sources":[{}]}AGPBI: {"kind":"simple","text":"表明它不是一个内部类.","sources":[{}]}

这篇关于无法向 Android 项目添加依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 12:16