Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the device running API level using c code via NDK?

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.

like image 463
Jona Avatar asked Apr 17 '12 17:04

Jona


People also ask

What is NDK API level?

The Android NDK is a toolset that lets you embed components that make use of native code in your Android applications. Android applications run in the Dalvik virtual machine. The NDK allows you to implement parts of your applications using native-code languages such as C and C++.

What is NDK tool?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

What is NDK computer?

The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages. Get started. class MyActivity : Activity() {


1 Answers

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!).

like image 196
Quintin Willison Avatar answered Sep 21 '22 16:09

Quintin Willison