I'm creating a Gradle task for my Android project that needs to know the path to the compiled .class files. How can I get this path? I've found suggestions (here and here; also see the Gradle docs for SourceSet) that sourceSets.main.output.classDir will give me this path, but when I try to use it, I get the error
Could not find property 'output' on source set main.
This happens even when I create a new, minimal project, with the following build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
apply plugin: 'android'
android {
compileSdkVersion 22
buildToolsVersion "20.0.0"
task printClassesDir << {
println sourceSets.main.output.classesDir
}
}
This minimal project builds fine, but will give that error if I run the task printClassesDir. How can I get the .class file output directory in my task?
I've also tried the suggestion of .javaCompile.classpath from this answer to another question. I was able to access this property with
applicationVariants.find{it.name == 'debug'}.javaCompile.classpath
but this file collection is empty.
For reference, here are the other files for my minimal project:
src/main/AndroidManifest.xml
package="a.b">
android:minSdkVersion="9"
android:targetSdkVersion="22" />
src/main/java/a/b/Main.java
package a.b;
import android.app.Activity;
public class Main extends Activity { }
解决方案
I was able to find this path using the compileJavaWithJavac tasks. These are of type JavaCompile, so they have destinationDir properties:
task printDebugClassesDir << {
println compileDebugJavaWithJavac.destinationDir
}
This prints the build/intermediates/classes/debug directory, which contains the compiled class files. The same thing works for release.