I've been building some tasks for a gradle multi-project build and have a need to get the class path for a project. The build script has projects that use the Java plugin and projects that use the Android plugin.
For the Java projects I was able to use the top voted answer in this question to get the class path using configurations.runtime.asPath
; however, this is not working for the Android projects because there is no configurations.runtime property.
How can generate a classpath for a gradle project using the Android plugin?
Android projects may build multiple versions of the app. These are called variants. The most basic variants are "debug" and "release" The following code should create the classpath assignment for all the variants in a project. Place this code in the "build.gradle" file for the module.
android.applicationVariants.each { variant ->
variant.javaCompile.classpath += configurations.provided
}
You should be able to refer to a specific variant using the variant name:
debug.javaCompile.classpath
Here is a gradle task that generates the module jar and includes also the test classpath for all variants. It is including libraries and the android.jar from selected runtime.
I've added two commandline executions for updating some env var inside emacs and for killing any running beanshell (with previous classpath).
task classpath(type: Jar) {
from android.sourceSets.main.java.srcDirs,
android.sourceSets.test.java.srcDirs
outputs.upToDateWhen { false }
doLast {
println "Building classpath..."
def cp2 = [android.getBootClasspath()[0], it.archivePath]
android.applicationVariants.all { v ->
cp2 += v.getApkLibraries()
}
def classpath = cp2.unique().join(":")
println "Updating emacs..."
exec {
executable "sh"
args "-c", "emacsclient --eval '(setenv \"CLASSPATH\" \""+classpath+"\")'"
}
exec {
executable "sh"
args "-c", "emacsclient --eval '(jdee-bsh-exit)'"
}
}
}
Be aware that I'm using ":" for joining the classpath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With