Very simple question - how to add commons-io dependency to gradle Android project?
I tried the following
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
}
but it does not work
The error is
Gradle: A problem occurred configuring project ':LearnIt'.
Failed to notify project evaluation listener.
Could not resolve all dependencies for configuration ':LearnIt:_DebugCompile'.
> Could not find commons-io:commons-io:2.0.1.
Required by:
learnit:LearnIt:unspecified
As of now (May 2014) if you use the default generated project it is actually amazingly simple (though difficult to find instructions!
Open the second level build.gradle, and add the following line to the dependencies {:
compile "commons-io:commons-io:+"
That will get the latest version of commons-io. My complete file looks like this:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 18
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile "commons-io:commons-io:+"
}
you need to declare a repository where you want to resolve the commons-io library from (e.g. MavenCentral):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories{
mavenCentral()
}
dependencies {
compile files('libs/android-support-v4.jar')
compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
}
Use gradlePlease to get the dependency.
Add the following to your app/build.gradle file:
dependencies {
compile 'org.apache.commons:commons-io:1.3.2'
}
//UPDATED
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
Update 2020 using gradle
// Home Page : https://commons.apache.org/
// IO - https://commons.apache.org/proper/commons-io/
implementation group: 'commons-io', name: 'commons-io', version: '2.7'
// String / Text
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.8'
Related
With the new update of JDA 4.2.0 trying to run the jar file on the VPS fails.
I have tried various options, such as
creating a fat jar
searching for other similar issues
source 1
source 2
Yet none of these options seemed to work, since the VPS console output stays unchanged:
Error: Could not find or load main class ...
Caused by: java.lang.NoClassDefFoundError: net/dv8tion/jda/api/hooks/ListenerAdapter
So my current question is, what should my gradle.build look like, or is this a problem of the VPS not refreshing correctly? Or perhaps another problem I'm not aware of.
As for what my gradle.build looks like:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
}
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
group '...'
version '2.0-SNAPSHOT'
repositories {
mavenCentral()
jcenter(
)
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'net.dv8tion:JDA:4.2.0_168'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'
compile 'com.vdurmont:emoji-java:5.1.1'
}
jar {
manifest {
attributes(
'Main-Class': '...'
)
}
}
NOTE: In the previous snapshot of 1.0-SNAPSHOT this following gradle.build worked to start up the project:
plugins {
id 'java'
id 'application'
}
group '...'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'net.dv8tion:JDA:3.5.0_329'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'
compile 'com.vdurmont:emoji-java:5.1.1'
}
You can use this build.gradle to build a working shadow jar:
plugins {
id 'application' // this allows you to set a mainClassName
id 'com.github.johnrengelman.shadow' version '6.0.0' // this adds the shadowJar task
}
group '...'
version '2.0-SNAPSHOT'
mainClassName = 'your main class goes here' // this sets the main class property in your manifest automatically
repositories {
jcenter() // jcenter is a superset of mavenCentral
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'net.dv8tion:JDA:4.2.0_191'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'
compile 'com.vdurmont:emoji-java:5.1.1'
}
Run the shadowJar gradle task to compile your jar file. This will then be placed inside build/libs and has the format [name]-all.jar. Do not forget to replace the mainClassName with the fully qualified name of your main class.
I am using Gradle 5.2, spotbugs-gradle-plugin version 2.0.0 and tried to generate SpotBugs report on my project. I used the following configuration on my project but it always create XML reports instead of html report.
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:+'
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:2.0.0"
}
}
allprojects {
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.github.spotbugs'
group = 'com.myproject'
version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile('com.google.cloud:google-cloud-storage:+') {
exclude group: "com.google.guava", module: "guava"
}
compile group: 'org.apache.commons', name: 'commons-collections4', version: '+'
compile group: 'com.google.guava', name: 'guava', version: '+'
testImplementation('org.junit.jupiter:junit-jupiter:+')
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
}
project(':myproject') {
dependencies {
compile group: 'com.github.javafaker', name: 'javafaker', version: '+'
compile group: 'org.jsonschema2pojo', name: 'jsonschema2pojo-core', version: '+'
compile group: 'commons-lang', name: 'commons-lang', version: '+'
compile group: 'org.mongodb', name: 'bson', version: '+'
}
}
tasks.withType(JavaCompile) {
options.compilerArgs << '-Xlint:unchecked'
options.deprecation = true
}
spotbugsMain {
reports {
xml.enabled = false
html.enabled = true
}
}
What I did wrong here?
Kindly provide your inputs to create HTML report instead of XML!
I was trying to create a new project which has two flavor, and the sdk is independent of app.
So I modified these build.gradle files like below:
For the android lib module: sdk:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
en
zh
}
publishNonDefault true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile(name: 'HERE-sdk', ext: 'aar')
}
repositories {
flatDir {//HERE SDK
dirs 'libs'
}
mavenCentral()
}
For the android app module: app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "cn.hudplay.testgradle"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
en
zh
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
enCompile project(path:":sdk",configuration:"enRelease")
zhCompile project(path:":sdk",configuration:"zhRelease")
//compile(name: 'HERE-sdk', ext: 'aar')//this seems to be workaround if un-blocked
}
repositories {
flatDir {//HERE SDK
dirs 'libs'
}
mavenCentral()
}
Then it told me Failed to resolve ::HERE-sdk:, and no other reasons..
I tried to move Here-sdk related gradle code to app's build.gradle, no more wrong.But I do need it in the sdk module..
What should I do..Anyone could help me?
Workaround used temporarily :
compile HERE-sdk for both app and sdk, no more wrong again. But I still feel something not right there...
Hi HERE updated the documentation maybe that help you with the issue. I had the same problem too and it worked out following these steps here:
https://github.com/heremaps/here-android-sdk-examples
What worked for me using the code above as an example (and if someone looks for a solution that works) was this approach:
copy the "\HERE-sdk\libsHERE-sdk.aar" file (dowloaded from your account on Here) to your \App\libs folder, then:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar','*.aar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
//compile(name: 'HERE-sdk', ext: 'aar')
}
NOTE: what changes was only 2 lines:
COMMENTED -> //compile(name: 'HERE-sdk', ext: 'aar') and ADDED -> '.aar' extension to array scope here -> compile fileTree(dir: 'libs', include: ['.jar','*.aar']).
OBS: I know thi's an old question and maybe someone out there in this outside world must have already answered it, but I leave here my humble contribution. If someone wants to employ me, I'm looking for a job hehehe
I got the aar compile without error by adding by adding it from
Open Module Settings
Step 1 , add the aar into app/libs folder
Step 2, Right Click App and negative to Open Module Settings
Step 3, Click on dependencies on the left and then click on the + under Declared Dependencies
Step 4, Select the libs/HERE-sdk.aar from the drop down and then click OK
Using zip64 true doesn't create a usable jar file. It can't locate the main class inside that jar, although the file, structure and location of the Manifest.mf are exactly the same as in previous builds that worked.
The real problem is: I don't need sonarqube or gatling in my final build (those are test-related), but AFAIK there is no way to exclude plugins.
The "fatJar"-task is for creating the jar.
Any kind of help is highly appreciated.
plugins {
id "org.sonarqube" version "2.2.1"
id "com.github.lkishalmi.gatling" version "0.4.1"
}
// Run in terminal with "gradle sonarqube"
sonarqube {
properties {
property "sonar.projectName", "asd"
property "sonar.projectKey", "org.sonarqube:java-gradle-simple"
property "sonar.host.url", "http://asd"
property "sonar.login", "asd"
property "sonar.password", "asd"
}
}
// Run in terminal with "gradle gatlingrun", start the application before.
gatling {
logLevel 'ERROR'
simulations = {
include "**/LoginAndSync.scala"
}
}
group 'asd'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task fatJar(type: Jar) {
//zip64 true
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'application.Asd'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.4.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.4.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '1.4.1.RELEASE'
compile group: 'org.springframework', name: 'spring-orm', version: '4.3.3.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.80'
compile group: 'io.gatling.highcharts', name: 'gatling-charts-highcharts', version: '2.2.3'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.4.1.RELEASE'
testCompile group: 'com.h2database', name: 'h2', version: '1.4.193'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '4.0.0.RELEASE'
}
This is the Exception I get:
xecution failed for task ':fatJar'.
> archive contains more than 65535 entries.
To build this archive, please enable the zip64 extension.
See: https://docs.gradle.org/3.3/dsl/org.gradle.api.tasks.bundling.Zip.html#org.gradle.api.tasks.bundling.Zip:zip64
Thanks to M.Deinum, I did a new approach:
I added spring-boot as plugin in build.gradle
plugins {
id 'org.springframework.boot' version '1.5.1.RELEASE'
}
and can now just use gradle build in the console, to get a running jar, which can be found in ./build/libs/.
I posted my library in Artifactory
https://imagizer.imageshack.us/v2/806x253q90/631/kkK1Yn.png
this is my Gradle
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle',
version: '3.0.1')
}
}
repositories {
jcenter()
mavenCentral()
mavenLocal()
maven {
url 'http://myartifactory:8081/artifactory/gradle-local'
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'com.android.library'
apply plugin: 'android-apt'
def AAVersion = '3.2'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
}
}
dependencies {
compile fileTree(include: ['*.jar', '*.so'], dir: 'libs')
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:support-v4:21.0.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.github.chrisbanes.photoview:library:1.2.3'
compile 'it.sephiroth.android.exif:library:+'
compile 'com.joanzapata.android:android-iconify:1.0.8'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.afollestad:material-dialogs:0.6.0'
compile 'com.github.lzyzsd:circleprogress:1.0.1#aar'
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile(group: 'com.mylibrary.android.common', name: 'android.common', version: '1.0.0', ext: 'aar')
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'com.mypackage.library'
}
}
I get this error
https://imagizer.imageshack.us/v2/519x72q90/540/8ojerK.png
Gradle I found that running the route using the url wrong use "/" instead of "."
Try to find
...8081/artifactory/repo/com/mypackage/android/common/android.common/1.0.0/android.common-1.0.0.aar
but should be
...8081/artifactory/repo/com.mypackage.android.common/android.common/1.0.0/android.common-1.0.0.aar
I'm doing wrong, wrong as was published in the artefactory
Your artifact is not complaint to the standard Maven layout, in which the groupId should be separated by /, not by ..
It's not a big deal, Gradle can handle it easly, you just can't declare the repository as maven, but as ivy instead.
Actually, it will be easier to use the artifactory plugin for resolution, it also supports both Maven and Ivy layouts.
Also, please remember to set the repository in Artifactory to be with correct layout (not Maven2, probably Ivy).
And frankly, I think maybe it worth redeploying the artifact under Maven layout, it will make your life easier.