简体   繁体   English

如何使用 JNI android 获取应用程序构建配置(调试或发布)?

[英]How to get app Build Configuration (Debug or release) using JNI android?

I'm working on an so library in an android project.我正在一个 android 项目中开发一个 so 库。 I need to check the Build Configuration(Debug or release) of the app on native code using JNI.我需要使用 JNI 在本机代码上检查应用程序的构建配置(调试或发布)。 What can be the best way to do it?什么是最好的方法?

Assuming your package is com.example.hellojni , this grabs the DEBUG and BUILD_TYPE static fields from the BuildConfig class:假设您的包是com.example.hellojni ,这将从BuildConfig类中获取DEBUGBUILD_TYPE静态字段:

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )
{
    jclass cls_HelloJni = env->FindClass("com/example/hellojni/BuildConfig");
    jfieldID fid_HelloJNI_buildType = env->GetStaticFieldID(cls_HelloJni, "BUILD_TYPE", "Ljava/lang/String;");
    jfieldID fid_HelloJNI_debug = env->GetStaticFieldID(cls_HelloJni, "DEBUG", "Z");
    jstring jBuildType = (jstring) env->GetStaticObjectField(cls_HelloJni, fid_HelloJNI_buildType);
    jboolean jDebug = env->GetStaticBooleanField(cls_HelloJni,fid_HelloJNI_debug);
    const char * buildType = env->GetStringUTFChars(jBuildType, nullptr);
    std::string out = (std::stringstream() << "Build type is " << buildType << ", debug says " << (jDebug ? "debug" : "not debug")).str();
    env->ReleaseStringUTFChars(jBuildType, buildType);

    return (env)->NewStringUTF(out.c_str());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM