Full width home advertisement

Watch Tutorials

Post Page Advertisement [Top]

Integrating Android NDK with C++ Library

In this video, we explore how to integrate Android NDK using a C++ native library in an Android application. We have two TextViews: one showing the result directly from the C++ library and the other showing the result after some processing in Java. Let's dive into the details!

Watch the Video Tutorial

If you prefer a step-by-step walkthrough, watch the video tutorial below:

Overview

In this tutorial, we will use a simple diagram to illustrate how the activity interacts with the JNI bridge and the C++ class. The activity communicates with the JNI bridge, which in turn communicates with the C++ class. The responses are processed and returned to the activity to be displayed to the user.

Setting Up the Project

First, create a new project and select the C++ support option. Name the project and choose the default C++ version. Once the project is built, you will see a new directory for C++ files and a native-lib.cpp file with a basic function.


    public class MainActivity extends AppCompatActivity {
        static {
            System.loadLibrary("native-lib");
        }

        private native String stringFromJNI();
        private native String process();
    }

In the native-lib.cpp file, you will find a basic method. This method is called from the activity and returns a string to be displayed in the first TextView. Make sure the method signature matches between Java and C++:


    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_nativecr_MainActivity_stringFromJNI(JNIEnv* env, jobject /* this */) {
        return env->NewStringUTF("Hello from C++");
    }

Adding a Process Method

Next, we will add a new method that processes data in C++ and interacts with Java. This involves calling a method from the Java activity to get some data, processing it in C++, and returning the result:


    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_nativecr_MainActivity_process(JNIEnv* env, jobject obj) {
        jclass clazz = env->GetObjectClass(obj);
        jmethodID methodID = env->GetMethodID(clazz, "processInJava", "()Ljava/lang/String;");
        jstring javaResult = (jstring)env->CallObjectMethod(obj, methodID);
        const char *javaMessage = env->GetStringUTFChars(javaResult, nullptr);

        std::string resultFromJava = javaMessage;
        std::string resultFromCPlusPlus = "Processed in C++";
        std::string finalResult = resultFromCPlusPlus + resultFromJava;

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

In the activity, declare the process method as a native method and call it to update the second TextView:


    public class MainActivity extends AppCompatActivity {
        // Native methods
        private native String process();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView textView1 = findViewById(R.id.textView1);
            TextView textView2 = findViewById(R.id.textView2);
            textView1.setText(stringFromJNI());
            textView2.setText(process());
        }
    }

Conclusion

We have successfully integrated Android NDK with a C++ native library, implemented JNI methods, and displayed results in the TextViews.

No comments:

Post a Comment

Share your thoughts ...

Bottom Ad [Post Page]