Get compiled .class output directory in Android Gradle task

半世苍凉 提交于 2019-12-12 20:09:11

问题


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 { }

回答1:


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.



来源:https://stackoverflow.com/questions/36477988/get-compiled-class-output-directory-in-android-gradle-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!