gradle 获取编译是否是debug_在Android Gradle任务中获取已编译的.class输出目录

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

package="a.b">

android:minSdkVersion="9"

android:targetSdkVersion="22" />

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

package a.b;

import android.app.Activity;

public class Main extends Activity { }

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值