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.
Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.
Assemble is a lifecycle task with no actions attached. Lifecycle tasks often combine other tasks and are useful to run in your development workflow. The purpose of assemble is to combine all tasks which produce a distribution or artifact into a single task.
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.
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)
}
}
You can see task execution in terminal running task for example gradle assemble
. Try this one, it is started practically before anything.
android {
...
gradle.projectsEvaluated {
preBuild.dependsOn(generateSources)
}
...
}
Edit, this may not work in Android Studio, as the Android Gradle DSL does not have a projectsEvaluated
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With