配置摇篮开始在编译之前执行自定义生成步骤 [英] Configure Gradle to execute custom build step before starting compilation

查看:162
本文介绍了配置摇篮开始在编译之前执行自定义生成步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用摇篮今天搜索了一个小时,从SO(如<一个尝试每一种可能的答案后, href="https://stackoverflow.com/questions/20674650/how-to-configure-ndk-with-android-gradle-plugin-0-7">1)和不同的博客(如 2 )及文件(如的 3 )我需要一些帮助。

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.

我的问题很简单:如何执行自定义生成步骤作为常规构建过程的一部分(在我的案件的执行NDK,建立与自定义Android.mk)?

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?

在build.gradle看起来是这样的:

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')
}

在通过命令行执行摇篮ndkBuild ,一切工作正常。但我想那Android的Studio自动运行ndkBuild当它运行的Andr​​oid的编译过程的其余部分(如generateDebugSources,preBuild,preDebugBuild,...)。

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)
}

但无论在​​哪里我把code,或者我从各种可用的任务(运行摇篮任务时),似乎没有任何工作。对使用何种任务

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.

推荐答案

您是否尝试过在添加依赖于的 ndkBuild JavaCompile 的任务?

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

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

这篇关于配置摇篮开始在编译之前执行自定义生成步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆