Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run task before compilation using Android Gradle plugin

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.

like image 406
Michael Avatar asked May 31 '13 08:05

Michael


People also ask

Does Gradle run compile?

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.

What is Gradle assemble task?

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.


3 Answers

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.

like image 62
Peter Niederwieser Avatar answered Oct 19 '22 18:10

Peter Niederwieser


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)
  }
}
like image 28
Michael Avatar answered Oct 19 '22 17:10

Michael


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.

like image 15
Gennadiy Ryabkin Avatar answered Oct 19 '22 16:10

Gennadiy Ryabkin