问题
I'm trying to add 'commons-validator' to my android project in Android Studio based on gradle. I use UrlValidator for my needs.
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
}
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.
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
and also I get
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 times: two for debug and two for release.
回答1:
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
which gave me following output:
_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
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'
}
Also some people told to add multiDexEnabled true to defaultConfig part but as I tried it works without it for me.
As @Brucelet said - removed <uses-library> tag from the manifest.
It runs and works correctly, although gradle output gives a lot of some AGPBI messages:
AGPBI: {"kind":"simple","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]} AGPBI: {"kind":"simple","text":"(org.apache.commons.validator.CreditCardValidator$1) 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$1) 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":[{}]}
回答2:
Try removing the <uses-library> tag. That's for requiring the user to have a certain external library installed before they can install your app. The gradle dependency should be sufficient since you want to include the library internally within your code.
来源:https://stackoverflow.com/questions/33830463/cant-add-dependency-to-android-project