Get compiled .class output directory in Android Gradle task
Asked Answered
G

1

6

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 <build variant>.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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="a.b">
  <uses-sdk
      android:minSdkVersion="9"
      android:targetSdkVersion="22" />
  <application>
    <activity android:name=".Main">
      <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
</manifest>

src/main/java/a/b/Main.java

package a.b;
import android.app.Activity;
public class Main extends Activity { }
Guaiacum answered 7/4, 2016 at 13:41 Comment(0)
G
6

I was able to find this path using the compile<Variant>JavaWithJavac 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.

Guaiacum answered 7/4, 2016 at 13:41 Comment(1)
How to get the output of a source set?Verbal

© 2022 - 2024 — McMap. All rights reserved.