Android NDK和Gradle:每种构建类型不同的Android.mk

前端之家收集整理的这篇文章主要介绍了Android NDK和Gradle:每种构建类型不同的Android.mk前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的本机库包含我想在编译时删除的日志.
通过在LOCAL_CFLAGS中定义预处理器宏ENABLE_DEBUG来显示日志,如下所示:

  1. include $(CLEAR_VARS)
  2. LOCAL_MODULE := native-stuff
  3. LOCAL_SRC_FILES := Native.cpp
  4. LOCAL_LDLIBS := -llog
  5. LOCAL_CFLAGS := -DENABLE_DEBUG
  6. include $(BUILD_SHARED_LIBRARY)

我正在使用Gradle通过Android Studio构建应用程序,我希望有另一个没有LOCAL_CFLAGS的Android.mk文件:= -DENABLE_DEBUG用于发布版本,有效地禁用日志记录.

我尝试通过在src下创建文件夹release / jni并在没有CFLAGS的情况下放置Android.mk的副本来实现.它成功构建和部署,但我仍然看到日志.这是我的build.gradle:

  1. apply plugin: 'com.android.library'
  2. android {
  3. compileSdkVersion 21
  4. buildToolsVersion "21.1.2"
  5. defaultConfig {
  6. minSdkVersion 17
  7. targetSdkVersion 21
  8. versionCode 1
  9. versionName "1.0"
  10. }
  11. sourceSets {
  12. main {
  13. //Tell Gradle where to put the compiled shared library
  14. jniLibs.srcDir 'src/main/libs'
  15. //disable automatic ndk-build call
  16. jni.srcDirs = [];
  17. }
  18. release {
  19. jni.srcDirs = [];
  20. }
  21. }
  22. buildTypes {
  23. release {
  24. minifyEnabled false
  25. proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
  26. }
  27. }
  28. // Tell Gradle the run the ndkBuild task when compiling
  29. tasks.withType(JavaCompile) {
  30. compileTask -> compileTask.dependsOn ndkBuild
  31. }
  32. // This task utilizes the Android.mk file defined in src/main/jni so that you
  33. // have more control over the build parameters (like library inclusion)
  34. // The system must define property 'androidNdkHome' in ~/.gradle/gradle.properties file
  35. // to point to NDK path
  36. task ndkBuild(type: Exec) {
  37. commandLine "$androidNdkHome/ndk-build",'-C',file('src/main/jni').absolutePath
  38. }
  39. }
  40. dependencies {
  41. compile fileTree(dir: 'libs',include: ['*.jar'])
  42. compile 'com.android.support:support-v4:21.0.2'
  43. }

我的项目结构如下所示:

  1. src/
  2. main/
  3. jni/
  4. Android.mk
  5. release/
  6. jni/
  7. Android.mk

有可能做我想要的吗?

最佳答案
我想我在这里有一个解决方案.它并不像我希望的那样漂亮(特别是因为它涉及创建一个临时文件),但我已经测试了它并且它按预期工作.

我写这个是为了使用另一个Application.mk进行调试构建,名为Application-debug.mk.你可以把APP_CFLAGS:= -DENABLE_DEBUG放在里面做你想要的.

关于它如何工作的评论代码中.如果是应用程序构建文件,请使用applicationVariants.all而不是libraryVariants.all:

  1. android.libraryVariants.all { variant -> // executes this block for each variant,current and futures
  2. def task = tasks.create(name: "ndk${variant.buildType.name.capitalize()}Build") { //create ndkReleaseBuild and ndkDebugBuild tasks
  3. doLast {
  4. exec { //execute all this block when the task is executed. (A Build task gets only the commandLine to be executed,so here we're using doLast.exec instead)
  5. def extraParameter = ""
  6. def rebuildFlag = ""
  7. if (variant.buildType.name == "debug")
  8. extraParameter = "NDK_APPLICATION_MK=" + file('src/main/jni/Application-debug.mk').absolutePath //reference to another Application.mk to use when doing a debug build.
  9. def lastBuildFile = file('src/main/jni/.lastbuild.tmp') //save the last build type to a temp file,to avoid triggering rebuilds all the time.
  10. if (!lastBuildFile.exists())
  11. lastBuildFile.createNewFile()
  12. def lastBuildType = lastBuildFile.text
  13. if (lastBuildType != variant.buildType.name) {
  14. println "== ndk build variant has changed,triggering full rebuild. =="
  15. rebuildFlag = "-B"
  16. }
  17. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  18. commandLine 'ndk-build.cmd',rebuildFlag,file('src/main').absolutePath,extraParameter
  19. } else {
  20. commandLine 'ndk-build',extraParameter
  21. }
  22. lastBuildFile.write(variant.buildType.name)
  23. }
  24. }
  25. }
  26. variant.javaCompile.dependsOn task
  27. }

我也把这个代码作为要点推出:https://gist.github.com/ph0b/92f1f664c453869636f8

猜你在找的Android相关文章