根据构建类型通过Gradle设置Android.mk标志 [英] Set Android.mk flag through Gradle depending on build type

查看:183
本文介绍了根据构建类型通过Gradle设置Android.mk标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Android.mk 文件中设置一个标志,该标志告知C代码是否为调试模式.

I need to set a flag inside Android.mk file which tells to the C code whether it's debug mode or not.

LOCAL_MODULE := auth
LOCAL_SRC_FILES := auth.c
LOCAL_CFLAGS := -DDEBUG_MODE=0
LOCAL_EXPORT_CFLAGS := $(LOCAL_CFLAGS)
LOCAL_LDLIBS := -llog
LOCAL_C_INCLUDES := auth.h

include $(BUILD_SHARED_LIBRARY)

问题在于,在构建发行版时,我经常忘记更改值,反之亦然,所以我期待一种自动执行发行版的方法.

The problem is that I often forget to change the value when building a release and viceversa so I'm looking forward to a way that does it automatically.

我尝试将 Android.mk 更改为:

LOCAL_CFLAGS := -DDEBUG_MODE

并将 build.gradle 更改为以下内容:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                cFlags = " -DDEBUG_MODE=0 "
            }
        }
        debug {
            debuggable true
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                cFlags = " -DDEBUG_MODE=1 "
            }
        }
    }

    sourceSets.main {
        jni.srcDirs = ['src/main/none']
        jniLibs.srcDirs = ["src/main/libs"]
    }

    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
    return void
}

这还不够,该标志不会根据构建类型而改变.我发现有关此的信息很少,上面的代码是我在互联网上阅读的结果.您对如何使其正常工作有任何建议吗?

This was not enough, the flag is not changing depending on build type. I found few information about this and the above code is the result of what I read on the internet. Do you have any suggestion about how to get it to work properly?

推荐答案

在构建类型中使用 externalNativeBuild {ndkBuild {".请注意,标记将添加到 APP_CFLAGS (即所有模块)中,而不是 LOCAL_CFLAGS 中.

Use externalNativeBuild { ndkBuild { within the build type. Note that the flags will be added to APP_CFLAGS (i.e. all modules), not LOCAL_CFLAGS.

build.gradle:

build.gradle:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.ndkbuildtest"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            ndkBuild {
                abiFilters "armeabi-v7a"
                arguments "APP_STL:=gnustl_static"
                cppFlags "-std=c++11","-frtti","-fexceptions"
            }
        }
    }
    buildTypes {
        release {
            externalNativeBuild {
                ndkBuild {
                    cFlags "-DDEBUG_MODE=0"
                }
            }
        }
        debug {
            externalNativeBuild {
                ndkBuild {
                    cFlags "-DDEBUG_MODE=1"
                }
            }
        }
    }
    externalNativeBuild {
        ndkBuild {
            path "Android.mk"
        }
    }
}

Android.mk:

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := native-lib
LOCAL_SRC_FILES := src/main/cpp/native-lib.cpp
include $(BUILD_SHARED_LIBRARY)

native-lib.cpp:

native-lib.cpp:

#include <jni.h>
#include <string>

#if DEBUG_MODE
#error "Debug mode enabled!
#endif

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_ndkbuildtest_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

这篇关于根据构建类型通过Gradle设置Android.mk标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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