Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get compiled .class output directory in Android Gradle task

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 { }
like image 348
Dan Getz Avatar asked Apr 07 '16 13:04

Dan Getz


People also ask

Where is the output of Gradle build?

By default, gradle outputs generated source files into build/classes directory.

Where does gradle output jar?

The Jar is created under the $project/build/libs/ folder.

What is ProcessResources in gradle?

Class ProcessResources Copies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.


1 Answers

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.

like image 105
Dan Getz Avatar answered Sep 30 '22 10:09

Dan Getz