Configure Gradle to execute custom build step before starting compilation

自作多情 提交于 2019-12-09 04:55:31

问题


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.


回答1:


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

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


来源:https://stackoverflow.com/questions/27672553/configure-gradle-to-execute-custom-build-step-before-starting-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!