Steve Mitcham
Steve Mitcham

Reputation: 5313

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?

Upvotes: 5

Views: 5180

Answers (4)

Lana F
Lana F

Reputation: 1

Here is another example a gradle task that generates javadocs with umlgraph + graphiz in an android project and includes classpath for all variants using the coding example given in the user1737310's previous answer. It is manually including android.jar from the selected runtime, I am still looking for a way to retrieve it dynamically.

    task javadoc(dependsOn: build) {
    setDescription('Generates Javadoc API documentation with UMLGraph diagrams')
    setGroup(JavaBasePlugin.DOCUMENTATION_GROUP)

    doLast {
        def javaFilePath = file('src/main/java')
        def cp = [System.getenv('ANDROID_HOME')+'/platforms/android-26/android.jar'];
        project.android.applicationVariants.all { v ->
            v.getCompileClasspath(null).getFiles().each{
                File f->
                    cp.add(f.getAbsolutePath())//this is the one of classpath
            }
        }
        def classpath = ":"+cp.join(':')
        if (javaFilePath.exists()) {
            ant.javadoc(classpath: (configurations.umljavadoc).asPath + classpath, 
                        sourcepath: file('src/main/java'), 
                        packagenames: '*',
                        destdir: "${docsDir}/javadoc",
                        private: 'false',
                        docletpath: configurations.umljavadoc.asPath) {
                doclet(name: 'org.umlgraph.doclet.UmlGraphDoc') {
                    param(name: '-inferrel')
                    param(name: '-inferdep')
                    param(name: '-qualify')
                    param(name: '-postfixpackage')
                    param(name: '-hide', value: 'java.*')
                    param(name: '-collpackages', value: 'java.util.*')
                    param(name: '-nodefontsize', value: '9')
                    param(name: '-nodefontpackagesize', value: '7')
                    param(name: '-link', value: 'http://java.sun.com/j2se/1.5.0/docs/guide/javadoc/doclet/spec')
                    param(name: '-link', value: 'http://java.sun.com/j2se/1.5/docs/api')
                }
            }
        }
    }
}

`

Upvotes: 0

user1737310
user1737310

Reputation: 11

project.android.applicationVariants.all { v ->
            v.getCompileClasspath(null).getFiles().each{
                File f->
                    f.getAbsolutePath()//this is the one of classpath
            }
        }

Upvotes: 1

Fernando Jascovich
Fernando Jascovich

Reputation: 449

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

Upvotes: 2

AndroidGuy
AndroidGuy

Reputation: 3451

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

Upvotes: 3

Related Questions