How To Include Transitive Dependencies - java

I have 2 gradle projects: an Android app and a RoboSpock test.
My build.gradle for the Android app has
. . .
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile ('com.actionbarsherlock:actionbarsherlock:4.4.0#aar') {
exclude module: 'support-v4'
}
}
. . .
and builds correctly by itself, e.g assembleRelease works.
I'm stuck getting the test to work. I gets lots of errors such as:
package com.google.zxing does not exist
Those seem to indicate that the .jar files aren't being picked up.
Here's my build.gradle for the test project:
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath 'org.robospock:robospock-plugin:0.4.0'
}
}
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: 'groovy'
dependencies {
compile "org.codehaus.groovy:groovy-all:1.8.6"
compile 'org.robospock:robospock:0.4.4'
}
dependencies {
compile fileTree(dir: ':android:libs', include: '*.jar')
compile (project(':estanteApp')) {
transitive = true
}
}
sourceSets.test.java.srcDirs = ['../android/src/', '../android/build/source/r/debug']
test {
testLogging {
lifecycle {
exceptionFormat "full"
}
}
}
project.ext {
robospock = ":estanteApp" // project to test
}
apply plugin: 'robospock'
As that shows, I've tried adding transitive = true and including the .jar files explicitly. But no matter what I try, I end up with the package does not exist error.

In Android Studio you no longer need a different project for testing. Put your testing code in the same module under "src/androidTest/java"
Then add the following to "build.gradle" in the "defaultConfig"
testPackageName "com.yourpackage.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testFunctionalTest true

Related

Error in .java that cannot be resolved after changing in gradle android

App works fine till yesterday but today when I opened the project, I get error only in .java
In a class, only in .java I get error as unresolved error java.
intent = Intent(this#UserImageActivity, UserDetailsActivity::class.java)
I used invalidate cache and restart but still I couldn't resolve the compile error.
build.gradle (app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
build.gradle (AppProject)
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
build.gradle (app)
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

Configure .jar to expose its dependencies

I have 3 modules: annotations, annotation_processor and app.
annotations/build.gradle
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
annotation_processor/build.gradle
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api project(":annotations")
api "com.squareup:javapoet:1.11.1"
}
repositories {
mavenCentral()
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
task deleteJar(type: Delete) {
delete 'libs/annotations.jar'
}
task createJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('libs/')
include('classes.jar')
rename('classes.jar', 'annotations.jar')
}
createJar.dependsOn(deleteJar, build)
and app/build.gradle
dependencies {
...
implementation files('libs/annotation_processor.jar')
annotationProcessor files('libs/annotation_processor.jar')
...
}
When I run the project I get the following error.
error: package com.annotations does not exist
If I include annotations as a project the project I can skip this error but then I get.
Caused by: java.lang.NoClassDefFoundError: com/annotations/ProviderApi
at com.annotation_processor.ProviderAnnotationProcessor.getSupportedAnnotationTypes(ProviderAnnotationProcessor.java:45)
at org.gradle.api.internal.tasks.compile.processing.DelegatingProcessor.getSupportedAnnotationTypes(DelegatingProcessor.java:47)
at org.gradle.api.internal.tasks.compile.processing.NonIncrementalProcessor.getSupportedAnnotationTypes(NonIncrementalProcessor.java:33)
at org.gradle.api.internal.tasks.compile.processing.DelegatingProcessor.getSupportedAnnotationTypes(DelegatingProcessor.java:47)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.access$101(TimeTrackingProcessor.java:37)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor$2.create(TimeTrackingProcessor.java:68)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor$2.create(TimeTrackingProcessor.java:65)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.track(TimeTrackingProcessor.java:117)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.getSupportedAnnotationTypes(TimeTrackingProcessor.java:65)
I can't access any of the dependencies (Java Poet and annotation module) from annotation_processor even though I have replaced implementation with api. Which should've expose the dependencies, but haven't.
I need the dependencies of the .jar file in the app module.
I'm new to Java Library and it may be I'm making a basic mistake, but can't seem to figure out, I've been at it for more than a day now.
You have to create a multi module gradle project.
I provide a small snippet of a multi module project.
project(':module1') {
dependencies {
compile project(':module-service-api')
compile 'org.apache.commons:commons-lang3:3.3.2'
compile 'log4j:log4j:1.2.17'
}
}
//module-app depends on module-service-impl
project(':module2') {
dependencies {
compile project(':module1')
}
For more details about multi module project, refer to the file build.gradle in the following project.
https://github.com/debjava/gradle-multi-module-project-1
If you want to create a fat jar or one jar, you have to include Gradle shadow plugin so that you can distribute the jar file along with other dependencies.
Refer below the link.
https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow

error( 23,13) Error:Failed to resolve: com.android.support:appcompat-v7:27.+ [duplicate]

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'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
// ButterKnife
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
// Parse SDK
compile 'com.parse:parse-android:1.16.0'
}
This is my app gradle dependencies. I don't know what to do to resolve it. I've tried installed from SDK Manager Android SDK Build Tools 26.0.1, and I also have latest version of Android support.
All current editions of Google libraries reside in Google's Maven repository (maven.google.com), not in the old offline-capable support repositories.
In your project-level build.gradle file, make sure that your allprojects closure looks like this:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
or, on Android Studio 3.0+, like this:
allprojects {
repositories {
jcenter()
google()
}
}
Even with Android Studio 3.0.+, I had to add this below (not just google()
like #CommonsWare recommended), to get pass the error
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
I resolved this issue by adding the below in project level build.gradle file.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url 'https://maven.google.com'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I had this error:
ERROR: Failed to resolve: support-annotations
For fix it open gradle.properties file and add
android.enableJetifier=true
#this line must be too:
android.useAndroidX=true

Gradle searching for a dependency that's not needed, thus failing

I have a multi-module project which uses Gradle. The two that are related to the issue at hand are manray-gradle-plugin which is a Gradle plugin project, providing a single code-generating task, and manray-core which is a plain Java library project which requires the aforementioned plugin to generate some code.
Now, for some reason, I can't compile the plugin project anymore. It gives the following error:
Could not find manray:manray-core:1.0
Searched in the following locations:
<Long list of locations here>
Required by:
project : > manray:manray-gradle-plugin:1.0
...which is weird because the plugin project doesn't reference the core project at all.
The plugin project's build.gradle is like this:
apply plugin: 'java-library'
apply plugin: 'maven'
dependencies {
compile gradleApi()
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup:javapoet:1.9.0'
compile 'com.github.javaparser:javaparser-core:2.5.1'
compile 'org.reflections:reflections:0.9.10'
compile 'io.github.lukehutch:fast-classpath-scanner:2.0.3'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
And the core project's build.gradle is like this:
apply plugin: 'java'
apply plugin: "manray-gradle-plugin"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
ext {
manrayOutputDir = file("$buildDir/generated-sources/manray/")
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = ["src/"]
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
processComponents {
generatedSourcesDirectory = manrayOutputDir
classpath = sourceSets.main.compileClasspath
}
compileJava.dependsOn processComponents
Finally, the whole project's build.gradle is like this:
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
}
dependencies {
classpath 'manray:manray-gradle-plugin:+'
}
}
allprojects {
apply plugin: "idea"
version = '1.0'
ext {
appName = "Manray"
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
Also, the settings.gradle looks like this:
include ':manray-gradle-plugin', ':manray-core'
What could cause the build to fail? I've tried cleaning the project, invalidating the gradle caches... Nothing seems to work here.

How to organise Gradle for Spring, Android and Plain JAR?

I want to have a project with 3 submodules as follows:
proj:spring
proj:plainjava
proj:android
proj:android:app
Where plainjava is a jar that is installed in the local repository. spring + android:app are dependant on it.
Currently if I try running gradle from the root directory or import it to the ide's, it results in various errors.
Execution failed for task ':android:app:compileDebugJavaWithJavac'.
Running separate task for android builds fine.
As I have figured out I need the following tasks to execute for the whole build in the following order:
proj:plainjava:publishMavenJavaPublicationToMavenLocal
proj:spring:bootRepackage
??? Task To build android succesfully, currently builds in android studio explicitly.
For the development purposes I also need to execute each task on it's own. Running
proj:spring:bootRepackage
Somehow triggers dependency resolution for the android, app and plainjava
actual files:
proj settings.gradle
include 'commonobjects', 'server', 'android', 'android:app'
proj build.gradle, has specific custom tasks that allow ci to build artifacts.
task myCleanLibraries {}
myCleanLibraries.dependsOn ':commonobjects:clean'
task myPublishLibraries {}
myPublishLibraries.dependsOn ':commonobjects:publishToMavenLocal'
task myCleanRest {}
myCleanRest.dependsOn ':server:clean', ':android:app:clean'
task myBuildRest {}
myBuildRest.dependsOn ':server:build', ':android:app:assembleDebug'
task myLibraries {}
myLibraries.dependsOn 'myCleanLibraries', 'myPublishLibraries'
myPublishLibraries.mustRunAfter 'myCleanLibraries'
task myRest {}
myRest.dependsOn 'myCleanRest', 'myBuildRest'
myBuildRest.mustRunAfter 'myCleanRest'
task myAll {}
myAll.dependsOn 'myLibraries', 'myRest'
myRest.mustRunAfter 'myLibraries'
proj:spring build.gradle
group 'com.example'
version '0.0.VERSIONNUMBER'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
defaultTasks 'clean', 'build'
buildscript {
ext {
springBootVersion = '1.5.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
jar {
baseName = 'server'
version = '0.0.9999'
}
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-logging'
compile 'org.projectlombok:lombok'
compile 'io.jsonwebtoken:jjwt:0.7.0'
compile 'mysql:mysql-connector-java:6.0.5'
compile 'com.example:commonobjects:0.0.9999'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
proj:commonobjects settings.gradle
rootProject.name = 'commonobjects'
proj:commonobjects build.gradle
group 'com.example'
version '0.0.9999'
apply plugin: 'java'
apply plugin: 'maven-publish'
jar {
baseName = 'commonobjects'
version = '0.0.9999'
}
repositories {
mavenCentral()
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
testCompile 'junit:junit:4.11'
}
proj:android settings.gradle
include ':app'
proj:android build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
}
}
proj:android:app build.gradle
def ANDROID_SUPPORT_DEPENDENCY_VERSION = '25.1.0'
def DAGGER_DEPENDENCY_VERSION = '2.10'
def OK_HTTP_DEPENDENCY_VERSION = '3.7.0'
def RETROFIT_DEPENDENCY_VERSION = '2.1.0'
def RETROFIT_JACKSON_DEPENDENCY_VERSION = '2.1.0'
def BUTTER_KNIFE_DEPENDENCY_VERSION = '8.5.1'
def JAVAX_ANNOTATION_JSR250_API_DEPENDENCY_VERSION = '1.0'
def GREEN_ROBOT_EVENT_BUS_DEPENDENCY_VERSION = '3.0.0'
def RX_JAVA_DEPENDENCY_VERSION = '2.0.5'
def RX_ANDROID_JAVA_DEPENDENCY_VERSION = '2.0.1'
defaultTasks 'clean', 'assembleDebug'
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
classpath "com.android.tools.build:gradle:2.3.1"
}
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
apply plugin: "com.android.application"
apply plugin: "idea"
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "myandroidappid"
minSdkVersion 14
targetSdkVersion 25
versionCode 9999
versionName "0.0.9999"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
// debug {
// buildConfigField "String", "PARSE_APPLICATION_ID", "\"1\""
// buildConfigField "String", "PARSE_API_KEY", "\"1\""
// }
release {
minifyEnabled true
proguardFiles "android-proguard-android.txt", "proguard-rules.txt"
// buildConfigField "String", "PARSE_APPLICATION_ID", "\"1\""
// buildConfigField "String", "PARSE_API_KEY", "\"1\""
}
}
packagingOptions {
pickFirst 'META-INF/LICENSE'
}
}
dependencies {
// Add jars supplied
compile fileTree(dir: 'libs', include: ['*.jar'])
// Test related
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'
androidTestCompile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
androidTestCompile "com.android.support.test:runner:0.5"
// MYOSCA dependencies
compile('com.example:commonobjects:0.0.9999')
// Android support libraries
compile "com.android.support:appcompat-v7:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
compile "com.android.support:design:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
compile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
// An HTTP & HTTP/2 client for Android and Java applications
compile "com.squareup.okhttp3:okhttp:${OK_HTTP_DEPENDENCY_VERSION}"
// Retrofit: A type-safe HTTP client for Android and Java
compile "com.squareup.retrofit2:retrofit:${RETROFIT_DEPENDENCY_VERSION}"
compile "com.squareup.retrofit2:converter-jackson:${RETROFIT_JACKSON_DEPENDENCY_VERSION}"
// Butterknife: Field and method binding for Android views
compile "com.jakewharton:butterknife:${BUTTER_KNIFE_DEPENDENCY_VERSION}"
annotationProcessor "com.jakewharton:butterknife-compiler:${BUTTER_KNIFE_DEPENDENCY_VERSION}"
// Dagger DI
compile "com.google.dagger:dagger:${DAGGER_DEPENDENCY_VERSION}"
annotationProcessor "com.google.dagger:dagger-compiler:${DAGGER_DEPENDENCY_VERSION}"
compile "com.google.dagger:dagger-android:${DAGGER_DEPENDENCY_VERSION}"
annotationProcessor "com.google.dagger:dagger-android-processor:${DAGGER_DEPENDENCY_VERSION}"
compile "com.google.dagger:dagger-android-support:${DAGGER_DEPENDENCY_VERSION}"
compile "com.google.dagger:dagger:${DAGGER_DEPENDENCY_VERSION}"
annotationProcessor "com.google.dagger:dagger-compiler:${DAGGER_DEPENDENCY_VERSION}"
provided "javax.annotation:jsr250-api:${JAVAX_ANNOTATION_JSR250_API_DEPENDENCY_VERSION}"
// Event bus
compile "org.greenrobot:eventbus:${GREEN_ROBOT_EVENT_BUS_DEPENDENCY_VERSION}"
// RxJava a library for composing asynchronous and event-based programs by using observable sequences.
compile "io.reactivex.rxjava2:rxjava:${RX_JAVA_DEPENDENCY_VERSION}"
compile "io.reactivex.rxjava2:rxandroid:${RX_ANDROID_JAVA_DEPENDENCY_VERSION}"
// Google Guava for preconditions
compile 'com.google.guava:guava:20.0'
}
Full solution available at: repo
Thanks for posting your build.gradle files. I believe your `settings.gradle is where you should start.
Since you want/have these modules in your current setup:
proj:spring
proj:plainjava
proj:android
proj:android:app
Let's map them to gradle modules:
proj/
|-spring/
|-build.gradle
|-plain-java/ <-- better naming conventions by separating names
|-build.gradle
|-android/
|-build.gradle
|-build.gradle <-- make sure to have root build.gradle
|-settings.gradle <-- should be root folder(proj)
Lets do the following:
rename the folder "plainjava" to "plain-java"
lets merge the "android/app" folder into just an "android"
this let's us consolidate everything down to just 3 projects
New settings.gradle:
rootProject.name = "proj"
include ":spring"
include ":plain-java"
include ":android"
Last thing, in your root directory, make move your buildscript dependencies in there so you can build from that directory.

Categories

Resources