Alexander Pacha
Alexander Pacha

Reputation: 9730

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.

Upvotes: 6

Views: 3334

Answers (1)

ph0b
ph0b

Reputation: 14473

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

android {
...
    tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn ndkBuild
        }
}

Upvotes: 8

Related Questions