我正在为我的 Android 项目创建一个 Gradle 任务,它需要知道编译后的路径 .class
文件。我怎样才能得到这条路?我找到了建议( here 和 here ;另见 the Gradle docs for SourceSet
) sourceSets.main.output.classDir
会给我这个路径,但是当我尝试使用它时,我得到了错误
Could not find property
'output'
on source setmain
.
即使我使用以下 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
我的任务中的文件输出目录?
我也试过<build variant>.javaCompile.classpath
的建议来自 this answer to another question .我能够通过
applicationVariants.find{it.name == 'debug'}.javaCompile.classpath
但是这个文件集合是空的。
作为引用,这里是我的最小项目的其他文件:
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 { }
最佳答案
我能够使用 compile<Variant>JavaWithJavac
找到这条路径任务。这些是类型 JavaCompile , 所以他们有 destinationDir
属性:
task printDebugClassesDir << {
println compileDebugJavaWithJavac.destinationDir
}
这会打印出 build/intermediates/classes/debug
目录,其中包含已编译的类文件。同样的事情适用于发布。
关于android - 在 Android Gradle 任务中获取编译后的 .class 输出目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36477988/