5

I'm starting to work on Android with the NDK and I want to check what Android API level the device is running on my c code. How can I do that?

At first I thought I could use definition __ANDROID_API__ under /android/api-level.h but that was a wrong assumption.

**Note: I'm NOT asking how to check API level via java.

Jona
  • 12,642
  • 13
  • 82
  • 124
  • 1
    Is your code running via the JNI interface or are you running it as an executable directly on the underlying os layer? – Samveen Apr 27 '12 at 10:47
  • Then FABUs's answer would work out perfectly. You just need to define a function in the c code ,lets say 'setVersion', which stores the version into a static variable , and the rest of your C code can access that variable. The setVersion function can be called as part of the initialization. Refer to http://stackoverflow.com/questions/3423754/retrieving-android-api-version-programmatically for info on getting the Android API version in Java. – Samveen Apr 28 '12 at 06:54
  • Mmm just seems weird we cant include one of the libraries from Android that has that information. I'm not an expert in C/C++ but for some reason I thought maybe some Android lib would be dynamically linked on a phone and provide the correct API level. Or at least call the proper function to get the API level. Just passing the Api level via JNI is kinda dirty solution but up to now it seems the better one. However I think possibly calling the API level class directly from JNI could work much better. – Jona Apr 28 '12 at 18:06
  • I agree with you on that. It would be nice to have a native library that gives all this kind of information. However, from the system architect's point of view, it makes sense to have a crippled set of libraries so as to discourage people from creating binary applications, instead pushing for app development in Java. – Samveen Apr 28 '12 at 20:32

3 Answers3

8

I've just been working on some JNI code and wanted to query the running OS build version as described by Jona. I wanted to do this as early as possible (ie in JNI_OnLoad) so would rather not hand it in from Java as described by FUBUs. Since API Level 4 this information has been available as the int field SDK_INT in android.os.Build.VERSION which is what I'm looking up in this snippet:

static const char* TAG = "testjnjni";

static bool _disableHttpKeepAliveOnBuggyPlatforms(JNIEnv *env)
{
    // Based on article here:
    //   http://android-developers.blogspot.co.uk/2011/09/androids-http-clients.html
    // Which references the issue documented here:
    //   http://code.google.com/p/android/issues/detail?id=2939
    // We need to set "http.keepAlive" to "false" if running an OS version earlier than Froyo (API Level 8)

    if ((*env)->ExceptionCheck(env))
        return false; // already got an exception pending

    bool success = true;

    // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
    jclass versionClass = (*env)->FindClass(env, "android/os/Build$VERSION");
    if (NULL == versionClass)
        success = false;

    jfieldID sdkIntFieldID = NULL;
    if (success)
        success = (NULL != (sdkIntFieldID = (*env)->GetStaticFieldID(env, versionClass, "SDK_INT", "I")));

    jint sdkInt = 0;
    if (success)
    {
        sdkInt = (*env)->GetStaticIntField(env, versionClass, sdkIntFieldID);
        __android_log_print(ANDROID_LOG_VERBOSE, TAG, "sdkInt = %d", sdkInt);
    }

    if (success && sdkInt < 8)
    {
        jclass systemClass = (*env)->FindClass(env, "java/lang/System");
        if (NULL == systemClass)
            success = false;

        jmethodID setPropertyMethodID = NULL;
        if (success)
            success = (NULL != (setPropertyMethodID = (*env)->GetStaticMethodID(env, systemClass, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")));

        jstring propString = NULL;
        if (success)
            success = (NULL != (propString = (*env)->NewStringUTF(env, "http.keepAlive")));

        jstring valueString = NULL;
        if (success)
            success = (NULL != (valueString = (*env)->NewStringUTF(env, "false")));

        jobject oldValueString = NULL;
        if (success)
        {
            __android_log_print(ANDROID_LOG_VERBOSE, TAG, "Disabling http.keepAlive");
             oldValueString = (*env)->CallStaticObjectMethod(env, systemClass, setPropertyMethodID, propString, valueString);
        }

        // cleanup
        (*env)->DeleteLocalRef(env, propString);
        (*env)->DeleteLocalRef(env, valueString);
        (*env)->DeleteLocalRef(env, oldValueString);
        (*env)->DeleteLocalRef(env, systemClass);
    }

    // cleanup
    (*env)->DeleteLocalRef(env, versionClass);

    return success;
}

All the information I needed to write this code is clearly documented in the PDF entitled "The Java Native Interface: Programmer's Guide and Specification" by Sheng Liang which used to be available from Oracle's site here but can also be purchased as a book (e.g. here). JNI is a very powerful technology and I would strongly recommend any developer wanting to get to grips with it reads that PDF as well as the Android Developers' JNI Tips.

Oh, and finally, it cannot be stressed how important it is to understand local and global references. Android's Developers blog has a good article here covering changes in ICS (nothing that veers away from the JNI specification but good points to reiterate!).

Quintin Willison
  • 575
  • 5
  • 12
  • This is a great method of getting the value via JNI access to the actual Build class. Thanks! – Jona May 17 '12 at 16:34
4

There is exists fully native solutions:

  1. Prior Android L you can use __system_property_get("ro.build.version.sdk") from sys/system_properties.h
  2. For Android L and newer you can use popen("getprop ro.build.version.sdk")

Use the compile time toolchain constant check #if __ANDROID_API__ < 21 to switch between those two implementations.

Community
  • 1
  • 1
CAMOBAP
  • 5,011
  • 8
  • 53
  • 84
1

You can pass one time, the API Level what you get in JAVA to the C code and stock it in global variable. For me, is the easier way to do that.

FUBUs
  • 617
  • 5
  • 9