Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get classpath for gradle project using Android plugin

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?

like image 612
Steve Mitcham Avatar asked Sep 29 '22 21:09

Steve Mitcham


2 Answers

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
like image 58
AndroidGuy Avatar answered Oct 03 '22 08:10

AndroidGuy


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

like image 38
Fernando Jascovich Avatar answered Oct 03 '22 07:10

Fernando Jascovich