I'm working on an so library in an android project. I need to check the Build Configuration(Debug or release) of the app on native code using JNI. What can be the best way to do it?
Asked
Active
Viewed 618 times
1
-
1Gradle creates a `BuildConfig` class that has a `BUILD_TYPE` field. See this question: [How to get the build/version number of your Android application?](https://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application) and adapt to JNI. – Botje Mar 16 '20 at 07:55
-
Sorry sir, But I'm looking for this solution inside a JNI Native library. – Shobhit Bajpai Apr 11 '20 at 08:19
1 Answers
4
Assuming your package is com.example.hellojni
, this grabs the DEBUG
and BUILD_TYPE
static fields from the BuildConfig
class:
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());
}

Botje
- 26,269
- 3
- 31
- 41
-
Thanks a lot sir, your answer is also going to solve many of my problems. :) – Shobhit Bajpai Apr 13 '20 at 04:36