Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Gradle to execute custom build step before starting compilation

I've started using Gradle today and after searching for an hour and trying every possible answer from SO (e.g. 1) and different blogs (e.g. 2) and documentations (e.g. 3) I need some help.

My question is simple: How to execute a custom build-step (in my case the execution of ndk-build with a customized Android.mk) as part of the regular build-process?

The build.gradle looks like this:

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "myApp.prototype"
        minSdkVersion 16
        targetSdkVersion 19

        testApplicationId "myApp.prototype.test"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

    sourceSets.main.jni.srcDirs = []

    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        def rootDir = project.rootDir
        def localProperties = new File(rootDir, "local.properties")
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }

        def ndkDir = properties.getProperty('ndk.dir')
        println ndkDir

        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine "$ndkDir\\ndk-build.cmd",
                    'NDK_PROJECT_PATH=build/intermediates/ndk',
                    'NDK_LIBS_OUT=src/main/jniLibs',
                    'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                    'NDK_APPLICATION_MK=src/main/jni/Application.mk'
        } else {
            commandLine "$ndkDir/ndk-build",
                    'NDK_PROJECT_PATH=build/intermediates/ndk',
                    'NDK_LIBS_OUT=src/main/jniLibs',
                    'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                    'NDK_APPLICATION_MK=src/main/jni/Application.mk'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.google.android.gms:play-services-location:6.5+'
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.code.gson:gson:2.2.4'

    compile fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
}

When executing gradle ndkBuild from the command-line, everything works fine. But I want that Android Studio automatically runs ndkBuild when it runs the rest of the Android compile procedures (such as generateDebugSources, preBuild, preDebugBuild, ...).

I have tried to attach myself to these events like this:

gradle.projectsEvaluated {
    preBuild.dependsOn(ndkBuild)
}

but regardless where I put that code, or what task I use from the variety of tasks available (when running gradle tasks), nothing seems to work.

like image 847
Alexander Pacha Avatar asked Dec 27 '14 23:12

Alexander Pacha


People also ask

How do I run a specific module in Gradle?

You can use task's fully qualified name to execute a specific task in a specific subproject. For example: gradle :services:webservice:build will run the build task of the webservice subproject. The fully qualified name of a task is simply its project path plus the task name.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.


1 Answers

Have you tried adding a dependency for ndkBuild on JavaCompile tasks ?

android {
...
    tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn ndkBuild
        }
}
like image 115
ph0b Avatar answered Oct 03 '22 01:10

ph0b