问题
I have a very simple build.gradle file with the following content:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.1'
}
}
apply plugin: 'android'
android {
buildToolsVersion "17.0.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
task generateSources {
doFirst {
def script = "python GenerateSources.py".execute()
script.in.eachLine {line -> println line}
script.err.eachLine {line -> println "ERROR: " + line}
script.waitFor()
}
}
What I want is to run generateSources task before java compilation is started. I found several solutions how to do that, like compileJava.dependsOn("generateSources"), but unfortunately they give an error:
A problem occurred evaluating root project 'Android'.
> Could not find property 'compileJava' on root project 'Android'.
I don't know Gradle and can't understand what's wrong with this code. So I would like to know how I can fix this error.
回答1:
Apparently, the android plugin doesn't add a compileJava task (like the java plugin would). You can check which tasks are available with gradle tasks --all, and pick the right one for your (otherwise correct) dependency declaration.
EDIT:
Apparently, the android plugin defers creation of tasks in such a way that they can't be accessed eagerly as usual. One way to overcome this problem is to defer access until the end of the configuration phase:
gradle.projectsEvaluated {
compileJava.dependsOn(generateSources)
}
Chances are that there is a more idiomatic way to solve your use case, but quickly browsing the Android plugin docs I couldn't find one.
回答2:
The proper way to run a task before Java compilation on Android is to make a compilation task for each variant depend on your task.
afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(generateSources)
}
}
回答3:
You can see task execution in terminal running task for example gradle assemble. Try this one, it is started practically before anything.
gradle.projectsEvaluated {
preBuild.dependsOn(generateSources)
}
Edit, this may not work in Android Studio, as the Android Gradle DSL does not have a projectsEvaluated method.
回答4:
Try this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.1'
}
}
apply plugin: 'android'
android {
buildToolsVersion "17.0.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
task generateSources {
def script = "python GenerateSources.py".execute()
script.in.eachLine {line -> println line}
script.err.eachLine {line -> println "ERROR: " + line}
script.waitFor()
}
project.afterEvaluate {
preBuild.dependsOn generateSources
}
clean.dependsOn generateSources
clean.mustRunAfter generateSources
The last two lines are optional - they will invoke the "generateSources" task when executing a gradle clean
回答5:
For those having a multi-project build and needing to run a task for each project before they're built (but it should also be ok if you have only the app project), you can write the task in the root build configuration script inside the allprojects closure and executing the task right there.
In root project build.gradle:
allprojects {
repositories {
// ...
}
// ...
task mytask {
doFirst {
println project.projectDir.name
}
}
mytask.execute()
}
It will be executed for every build variant as well.
Gradle 4.1
来源:https://stackoverflow.com/questions/16853130/run-task-before-compilation-using-android-gradle-plugin