在Android Gradle任务中获取已编译的.class输出目录 [英] Get compiled .class output directory in Android Gradle task

查看:886
本文介绍了在Android Gradle任务中获取已编译的.class输出目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Android项目创建一个Gradle任务,该任务需要知道已编译.class文件的路径.我怎么能走这条路?我找到了建议(此处 Gradle SourceSet 的文档),sourceSets.main.output.classDir会给我这个路径,但是当我尝试使用它时,出现错误

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

在源集main上找不到属性'output'.

即使我使用以下build.gradle创建新的最小项目,也会发生这种情况:

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

这个最小的项目构建良好,但是如果我运行任务printClassesDir,则会出现该错误.如何在任务中获取.class文件输出目录?

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?

我还尝试了此问题的答案中的<build variant>.javaCompile.classpath的建议.我可以通过以下方式访问此属性

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

但是此文件集合为空.

作为参考,以下是我的最小项目的其他文件:

For reference, here are the other files for my minimal project:

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

推荐答案

我能够使用compile<Variant>JavaWithJavac任务找到此路径.这些是 JavaCompile 类型的,因此它们具有destinationDir属性:

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
}

这将打印build/intermediates/classes/debug目录,其中包含已编译的类文件.同样的东西也可以发布.

This prints the build/intermediates/classes/debug directory, which contains the compiled class files. The same thing works for release.

这篇关于在Android Gradle任务中获取已编译的.class输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆