[FIXED] How to get Version.Code in Android using C++?

Issue

I use native C++ with my Android project.

In Java or Kotlin we can use BuildVersion.Code to get the Android code from the Gradle file. How can I get this version code in C++?

Solution

This is working for me:

jclass build_config_class = env->FindClass("com/example/app/BuildConfig");
jfieldID version_code_id = env->GetStaticFieldID(build_config_class, "VERSION_CODE", "I");
jfieldID version_name_id = env->GetStaticFieldID(build_config_class, "VERSION_NAME",
                                                      "Ljava/lang/String;");

int version_code = env->GetStaticIntField(build_config_class, version_code_id);
jstring version_name = (jstring) env->GetStaticObjectField(build_config_class, version_name_id);

You need to change com/example/app in the code to match your package name.

Answered By – gioravered

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published