Can gradle android apk project dependencies another apk project's jar task? - gradle

I have a project with two modules:
first one HostProject
second one SubProject
LibProject is a android apk project but has a jar task to make a jar from the code.
jar task:
task sourcesJar(type: Jar) {
from 'build/intermediates/classes/debug'
archiveName 'SubProject.jar'
}
In HostProject I want to use libproject's code by the jar file. Is there someway easy to add dependency?
HostProject build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
****compile project(':LibProject')****
}
gradle show error: resolves to an APK archive which is not supported as a compilation dependency
can I use the dependencies in HostProject's build.gradle without add new task?

Related

How Can I Use An Extracted Dependency As A Dependency In Gradle?

Some of my dependencies are in Artifactory as zip files that have been distributed by a vendor.
The jars are required to compile and the dll/so and html templates are required for debugging/runtime.
Creating a dependency on local files is straightforward:
dependencies {
compile fileTree(dir: "${projectDir}/sec-lib", includes: ['*.jar'])
}
Extracting dependencies is straightforward too:
dependencies {
compile 'com.thirdparty.sec-lib:1.0#zip'
}
task copyToLib(type: Copy) {
into "${projectDir}/sec-lib"
from configurations.compile
}
How do I use them together though?
I can change the zip to a new configuration so only that one is extracted:
configurations {
zipCompile
}
dependencies {
zipCompile 'com.thirdparty.sec-lib:1.0#zip'
compile fileTree(dir: "${projectDir}/sec-lib", includes: ['*.jar'])
}
task copyToLib(type: Copy) {
into "${projectDir}/sec-lib"
from configurations.zipCompile
}
However, the copyToLib task has to run in order for the dependencies to resolve but the dependencies need to resolve for the copyToLib task to run.
How do I fix this circular dependency issue?
I don't want to have to have a README that says "make sure you run this task first or the project wont compile or import into your IDE".
I also have multiple zip files like this I need to deal with.

War task of Gradle build adding extra jars than those from dependencies in build.gradle

We have recently migrated to Gradle build and I have added a war that task that has web.xml and dependent jars along with jar created in the jar task.
task testWar(type: War)
{
archiveName 'test.war'
webXml = file('WebContent/WEB-INF/web.xml')
into ('WEB-INF/lib')
{
from configurations.compile
from jar
}
}
This creates the war but the size of WEB-INF/lib is double the size of the libs actually given in dependencies. It might be adding the jars that the dependent jars depend on. But Ant build just works fine with just the dependent jars.
Is there any way to create war with just the jars provided in dependencies?
Note: https://docs.gradle.org/current/userguide/war_plugin.html didnt help as I need all the jars in dependencies, just want to avoid extra dependent jars
You can use gradle dependencies to know which libraries are causing the inclusion of these additional dependencies.
Once you have that then simply exclude the dependency you want to remove from your package.
dependencies {
compile (group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.0') {
exclude group: 'org.slf4j', module: 'slf4j-api'
}
}

IntelliJ Gradle deletes its own modules unexpectedly

This is the dialog indicating the modules which are being deleted
but when I import a new project with build.gradle and I do not enable auto import now I have a dialog asking my if I would like to enable auto import, and the modules are no longer deleted automatically.
Here is the build.gradle file
/*------------------------------------------------------------------------------
Gradle latest file.
Build command:
./gradlew deploy
Create Javadocs at ./latest/docs/javadoc/index.html
./gradlew buildJavadocs
Other commands of interest:
./gradlew wrapper --gradle-version 4.7
./gradlew tasks
./gradlew properties
Manual search for dependencies in Gradle repository:
https://bintray.com/bintray/jcenter/io.appium%3Ajava-client
------------------------------------------------------------------------------*/
group 'com.my.group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/'
}
}
}
sourceCompatibility = 1.8
repositories {
jcenter()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile 'junit:junit:4.12'
compile 'org.seleniumhq.selenium:selenium-java:3.11.0'
compile 'io.appium:java-client:6.1.0' // /compile 'io.appium:java-client:6.0.0-BETA5'
//compile 'io.appium:java-client:6.0.0-BETA5' // compile 'io.appium:java-client:6.1.0'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'com.google.guava:guava:24.1-jre'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
}
task copyToLib(type: Copy, dependsOn: build) {
String dst = "$rootDir/jar"
description "Copy library JAR dependencies to $dst"
from configurations.runtime
into "$dst"
}
task copyToOut(type: Copy, dependsOn: build) {
String dst = "$rootDir/out"
description "Copy library JAR output to $dst"
from jar
into "$dst"
}
task deploy {
dependsOn clean
dependsOn build
dependsOn copyToLib
dependsOn copyToOut
}
task buildJavadocs(type: Javadoc) {
exclude 'srcOld/**'
classpath += sourceSets.test.compileClasspath
source += sourceSets.main.allJava
}
edit
When Gradle deletes the modules, it also delete gradle.build and the gradle wrapper. I've already created a brand new project with the default Java template, and copied and pasted my code into it, but the problem persists.
In case of a Gradle projects IDE configures the project structure including the modules that are included in project according your the build.gradle script. E.g. if you have Create separate module per source set option enabled in IDE Gradle settings IDE creates separate module for each Grale source set. It does it when re-importing build.gradle file. If you have auto-import enabled in IDE Gradle configuration it does it automatically.
Given the limited information, I'll give you an ad-hoc fix, just add -----test as a sub-project directory to your root projects settings.gradle using
include 'root-proj-dir:dir1:dir2:...:----test'
where the directory structure is
root-proj-dir
|
|
----dir1
| |
| |
| ----dir2
| |
| |
| ....----test
----settings.gradle
|
----build.gradle
For more info on dealing with multi-module (or multi-project in Gradle terminology ) builds refer this link

How can I clone a repo from GitHub using gradle?

I have a project which depends on other repos. My goal is to write a gradle task that clones the required repos automatically.
The problem I am having is that when I run my task, gradlew fails because it tries to compile the project dependencies before running my task. Here is what I have:
settings.gradle
include ':app'
include 'testRepo'
project(':testRepo').projectDir = new File('../testRepo')
build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'org.ajoberstar:gradle-git:1.5.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
build.gradle(app)
task clone << {
Grgit.clone(dir: file('../testRepo'), uri: "https://github.com/fakeAccount/testRepo.git")
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile project(':testRepo')
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
}
When I do ./gradlew clone I get:
"Cannot evaluate module testRepo : Configuration with name 'default' not found"
This is because it tries to compile the testRepo project before cloning from GitHub. If I take out the compile project(':testRepo') it works fine.
Any ideas as to how to make this work?

is this a gradle eclipse task bug or inconsistent on purpose?

So I have the following dependencies section in gradle
dependencies {
compile project(':sdi-master')
compile fileTree(dir: '../webserver/lib', include: '*.jar')
compile fileTree(dir: '../webserver/play-1.2.4/framework/lib', include: '*.jar')
compile fileTree(dir: '../webserver/play-1.2.4/framework', include: 'play-*.jar')
}
I also have a copy jars task like so
task deleteJars(type: Delete) {
ext.collection = files { genLibDir.listFiles() }
delete ext.collection
}
task copyJars(type: Copy) {
from(configurations.compile) {}
into genLibDir
}
copyJars.dependsOn('deleteJars')
classes.dependsOn('copyJars')
Notice how it depends on sdi-master which then has ONE compile fileTree as well. When I run copyJars, as expected, I get all the jars from the sdi-master as well copied into genLibDir. When I run the eclipse task however, those jars do NOT show up in the .classpath file as I would expect so my project doesn't compile in eclipse.
Is this a gradle eclipse task bug I need to report or is this supposed to be the behavior(though it seems very inconsistent with the copy jars using configurations.compile.
thanks,
Dean
Eclipse understands transitive dependencies, so the dependencies of sdi-master will not (and should not) show up in the current project's .classpath file. They should just show up in sdi-master's .classpath file and should be marked as exported there.

Resources