{"id":842,"date":"2023-11-08T15:59:37","date_gmt":"2023-11-08T12:59:37","guid":{"rendered":"https:\/\/barisuslucan.com\/en\/?p=842"},"modified":"2023-11-08T16:02:27","modified_gmt":"2023-11-08T13:02:27","slug":"kotlin-and-c-module-interaction-android-ndk","status":"publish","type":"post","link":"https:\/\/barisuslucan.com\/en\/kotlin\/kotlin-and-c-module-interaction-android-ndk\/","title":{"rendered":"Kotlin and C++ Module Interaction: Android NDK"},"content":{"rendered":"<p>In certain situations, you may need to utilize a C++ module within Kotlin through the Android NDK. In this article, I will elucidate the process of executing C++ code in Kotlin and explore the reasons why such a need may arise.<\/p>\n<p><strong>There may be several reasons why C++ code is needed in Kotlin:<\/strong><\/p>\n<p><span style=\"color: #00ffff;\"><strong>Performance:<\/strong> <\/span>C++ is usually faster than Kotlin because it is a low-level language and closer to the hardware. This is important for applications that are CPU-intensive.<\/p>\n<p><span style=\"color: #00ffff;\"><strong>Existing Libraries:<\/strong><\/span> There are many powerful and optimized libraries written in C++. Using these libraries can be more efficient for solving specific problems.<\/p>\n<p><strong><span style=\"color: #00ffff;\">Language Features:<\/span><\/strong> C++ has some language features that may not be available in Kotlin. These features may be required for an application that needs them.<\/p>\n<p><strong><span style=\"color: #00ffff;\">Platform Independence:<\/span><\/strong> C++ code can run on various platforms. This allows the same code to be used on multiple platforms.<\/p>\n<p><strong><span style=\"color: #00ffff;\">Hardware Access:<\/span><\/strong> C++ provides more direct access to the hardware. This is useful for situations that require hardware-specific operations.<\/p>\n<p>For these reasons, running C++ code in Kotlin is sometimes necessary. However, this may not always be the right approach and it is important to carefully evaluate the requirements of the project to determine what is the best solution for each situation.<\/p>\n<h4><strong>Android NDK and JNI<\/strong><\/h4>\n<p>The Android NDK (Native Development Kit) is a toolkit that enables the use of C and C++ code in conjunction with Android. The NDK operates in conjunction with the Java Native Interface (JNI), managing the interaction between Java code and native code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-843 \" src=\"https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/11\/Android-NDK-and-JNI.jpg\" alt=\"Android NDK and JNI\" width=\"516\" height=\"206\" srcset=\"https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/11\/Android-NDK-and-JNI.jpg 827w, https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/11\/Android-NDK-and-JNI-300x120.jpg 300w, https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/11\/Android-NDK-and-JNI-768x306.jpg 768w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/p>\n<h4><strong><br \/>Incorporating a C++ Module into a Kotlin Project<\/strong><\/h4>\n<p>It is relatively straightforward. Open Android Studio and your project, then follow these steps:<\/p>\n<ol>\n<li>Navigate to: File &gt; New &gt; New Module &gt; Android Native Library.<br \/>2. This will integrate the C++ module into your project, and you will observe the presence of your C++ module directory. In this article, I will refer to it as &#8220;nativelib,&#8221; which is the name of my project&#8217;s module.<\/li>\n<\/ol>\n<p>Now, let&#8217;s analyze nativelib.cpp and the NativeLib Kotlin class.<\/p>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"extern &quot;C&quot; JNIEXPORT jstring JNICALL\nJava_com_example_nativelib_NativeLib_stringFromJNI(\n        JNIEnv* env,\n        jobject \/* this *\/) {\n    std::string hello = &quot;Hello from C++&quot;;\n    return env-&gt;NewStringUTF(hello.c_str());\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">extern <\/span><span style=\"color: #A3BE8C\">&quot;C&quot;<\/span><span style=\"color: #D8DEE9FF\"> JNIEXPORT jstring JNICALL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">Java_com_example_nativelib_NativeLib_stringFromJNI<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        JNIEnv<\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9FF\"> env,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        jobject <\/span><span style=\"color: #616E88\">\/* this *\/<\/span><span style=\"color: #D8DEE9FF\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    std::<\/span><span style=\"color: #88C0D0\">string<\/span><span style=\"color: #D8DEE9FF\"> hello <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">&quot;Hello from C++&quot;<\/span><span style=\"color: #D8DEE9FF\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> env<\/span><span style=\"color: #81A1C1\">-&gt;<\/span><span style=\"color: #88C0D0\">NewStringUTF<\/span><span style=\"color: #D8DEE9FF\">(hello.<\/span><span style=\"color: #88C0D0\">c_str<\/span><span style=\"color: #D8DEE9FF\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p><br \/>JNIEnv is the Java Native Interface (JNI) environment. It is a data structure that provides access to the Java environment from native code. The JNI environment is passed to native functions by the Java Virtual Machine (JVM) when a native function is called from Java code.<\/p>\n<p>jobject is a handle to a Java object. It is a value that represents a Java object in native code. The JNI environment provides functions for creating, accessing, and destroying jobjects.<\/p>\n<p>In the provided code snippet, the <strong><em>Java_com_example_nativelib_NativeLib_stringFromJNI()<\/em><\/strong> function is a native function that is called from Java code. The function takes two arguments:<\/p>\n<p><strong>JNIEnv* env:<\/strong> This is the JNI environment.<br \/><strong>jobject \/* this *\/:<\/strong> This is a handle to the Java object that called the native function. The placeholder \/* this *\/ indicates that the value of this argument is not used in the function.<br \/>The function creates a std::string object that contains the string &#8220;Hello from C++&#8221;. It then calls the env-&gt;NewStringUTF() function to create a new Java string object from the std::string object. Finally, it returns the Java string object to the JVM.<\/p>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class NativeLib {\n\n    \/**\n     * A native method that is implemented by the 'nativelib' native library,\n     * which is packaged with this application.\n     *\/\n    external fun stringFromJNI(): String\n\n    companion object {\n        \/\/ Used to load the 'nativelib' library on application startup.\n        init {\n            System.loadLibrary(&quot;nativelib&quot;)\n        }\n    }\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">NativeLib<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\">\/**<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">     * A native method that is implemented by the &#39;nativelib&#39; native library,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">     * which is packaged with this application.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">     *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">external<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">fun<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">stringFromJNI<\/span><span style=\"color: #D8DEE9FF\">(): String<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">companion<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">object<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\">\/\/ Used to load the &#39;nativelib&#39; library on application startup.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">init<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            System.<\/span><span style=\"color: #88C0D0\">loadLibrary<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #A3BE8C\">&quot;nativelib&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p><br \/>This code defines a Kotlin class NativeLib. The class has one method, stringFromJNI(), which is a native method. This means that the method is implemented in native code, such as C or C++. The external keyword tells Kotlin that the method is implemented in native code and that it needs to be loaded from a native library.<\/p>\n<p>The class also has a companion object, which is a special type of object that is associated with the class itself, rather than with individual instances of the class. The companion object contains an initialization block (init), which is used to load the &#8216;nativelib&#8217; native library on application startup. The System.loadLibrary() function is used to load the library from the application&#8217;s APK file.\u00a0<\/p>\n<p>Let us create a function in C++ that adds two integers and returns the result as an integer.<\/p>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;jni.h&gt;\n#include &lt;string&gt;\n\n\nextern &quot;C&quot; JNIEXPORT jint JNICALL\nJava_com_example_nativelib_NativeLib_sumTwoNumbers(JNIEnv *env,jobject, int x, int y) {\n    return x+y;\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">#include <\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">jni.h<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">#include <\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">string<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">extern <\/span><span style=\"color: #A3BE8C\">&quot;C&quot;<\/span><span style=\"color: #D8DEE9FF\"> JNIEXPORT jint JNICALL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">Java_com_example_nativelib_NativeLib_sumTwoNumbers<\/span><span style=\"color: #D8DEE9FF\">(JNIEnv <\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9FF\">env,jobject, int x, int y) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> x<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\">y;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p><br \/>Subsequently, create an external function to facilitate access to this C++ function in Kotlin<\/p>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class NativeLib {\n    external fun sumTwoNumbers(x:Int, y:Int): Int\n    companion object {\n        init {\n            System.loadLibrary(&quot;nativelib&quot;)\n        }\n    }\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">NativeLib<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">external<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">fun<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sumTwoNumbers<\/span><span style=\"color: #D8DEE9FF\">(x:Int, y:Int): Int<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">companion<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">object<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">init<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            System.<\/span><span style=\"color: #88C0D0\">loadLibrary<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #A3BE8C\">&quot;nativelib&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class MainActivity : ComponentActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        val nativeLib = NativeLib()\n        val total = nativeLib.sumTwoNumbers(15, 17)\n        setContent {\n            TestCProjectTheme {\n                Surface(\n                    modifier = Modifier.fillMaxSize(),\n                    color = MaterialTheme.colorScheme.background\n                ) {\n                    Text(\n                        text = total.toString()\n                    )\n                }\n            }\n        }\n    }\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">MainActivity<\/span><span style=\"color: #D8DEE9FF\"> : ComponentActivity() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">override<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">fun<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">onCreate<\/span><span style=\"color: #D8DEE9FF\">(savedInstanceState: Bundle?) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">super<\/span><span style=\"color: #D8DEE9FF\">.<\/span><span style=\"color: #88C0D0\">onCreate<\/span><span style=\"color: #D8DEE9FF\">(savedInstanceState)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> nativeLib <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">NativeLib<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> total <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> nativeLib.<\/span><span style=\"color: #88C0D0\">sumTwoNumbers<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #B48EAD\">15<\/span><span style=\"color: #D8DEE9FF\">, <\/span><span style=\"color: #B48EAD\">17<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">setContent<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">TestCProjectTheme<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #88C0D0\">Surface<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                    modifier <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> Modifier.<\/span><span style=\"color: #88C0D0\">fillMaxSize<\/span><span style=\"color: #D8DEE9FF\">(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                    color <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> MaterialTheme.colorScheme.background<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                    <\/span><span style=\"color: #88C0D0\">Text<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                        text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> total.<\/span><span style=\"color: #88C0D0\">toString<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                    )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p><br \/>Here, it is important to note that the C++ code&#8217;s function name must include the module name you used when creating the C++ module.<\/p>","protected":false},"excerpt":{"rendered":"<p>In certain situations, you may need to utilize a C++ module within Kotlin through the Android NDK. In this article, I will elucidate the process of executing C++ code in Kotlin and explore the reasons why such a need may arise. There may be several reasons why C++ code is needed in Kotlin: Performance: C++ is usually faster than Kotlin because it is a low-level language and closer to the hardware. This is important for applications that are CPU-intensive. Existing Libraries: There are many powerful and optimized libraries written in C++. Using these libraries can be more efficient for solving&#8230;<\/p>\n","protected":false},"author":1,"featured_media":827,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[29],"tags":[],"class_list":["post-842","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":6,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":849,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/842\/revisions\/849"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/media\/827"}],"wp:attachment":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}