{"id":2828,"date":"2025-04-28T10:42:12","date_gmt":"2025-04-28T10:42:12","guid":{"rendered":"https:\/\/adjoe.io\/company\/engineer-blog\/mobile-app-security-android-native-code\/"},"modified":"2026-11-12T22:56:04","modified_gmt":"2026-11-12T22:56:04","slug":"mobile-app-security-android-native-code","status":"publish","type":"engineer_blog","link":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/","title":{"rendered":"Mobile App Security: How Native Code Protects Sensitive Logic in Android"},"content":{"rendered":"<p class=\"wp-block-paragraph\">A cutting-edge mobile application may be leading in innovation, but it is still vulnerable to potential threats and exploits in mobile app security.\u00a0Your competitive advantage is sitting at the heart of your application code.<\/p><p class=\"wp-block-paragraph\">Whether it\u2019s new algorithms, advanced encryption routines, or innovative approaches for data collection, it is a prime target for unauthorized access or reverse engineering.\u00a0<\/p><p class=\"wp-block-paragraph\">What\u2019s the most effective way to secure sensitive logic in Android apps?<\/p><p class=\"wp-block-paragraph\">At <a href=\"https:\/\/adjoe.io\/solutions\/advertise-your-app\/display-ads\/\">adjoe Programmatic<\/a>, we have integrated <strong>native code into our SDK<\/strong> to secure sensitive application logic while maintaining performance and usability.\u00a0<\/p><p class=\"wp-block-paragraph\">In this article, we explore the challenges of securing Android code, making a case for why native code<strong> <\/strong>protects sensitive logic and is the preferred method for mobile app security.\u00a0<\/p><h2 class=\"wp-block-heading\">Android\u2019s Reverse Engineering Risk: Why Obfuscation Isn\u2019t Enough<\/h2><p class=\"wp-block-paragraph\">Android\u2019s open architecture allows developers to create diverse applications, but exposes them to security threats, including the risk of reverse engineering.\u00a0<\/p><p class=\"wp-block-paragraph\">With tools like <a href=\"https:\/\/github.com\/skylot\/jadx\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">JADX<\/a> and <a href=\"https:\/\/apktool.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">ApkTool<\/a>, attackers can easily decompile APK files, turning obfuscated Java or Kotlin code back into semi-readable logic.\u00a0<\/p><p class=\"wp-block-paragraph\">While obfuscation tools like <a href=\"https:\/\/www.guardsquare.com\/proguard\">ProGuard<\/a> rename classes, methods, and variables, they do not fundamentally hide the underlying algorithms or workflows.<\/p><p class=\"wp-block-paragraph\">For example, let\u2019s imagine an app implementing a proprietary algorithm for generating secure session tokens based on user data and timestamps:<\/p><p class=\"wp-block-paragraph\"><strong>Original Kotlin Code (Before Obfuscation):<\/strong><\/p><p class=\"wp-block-paragraph\">Although variable names and method identifiers are obfuscated, the token-generation logic is still clearly visible. An attacker could reverse-engineer this process to predict or forge session tokens, compromising application security.<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">Kotlin<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-kotlin\">fun generateSessionToken(userId: String, timestamp: Long): String {\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0val salt = &quot;someSecureSalt&quot;\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0val data = &quot;$userId|$timestamp|$salt&quot;\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0return data.hashCode().toString()\u00a0\u00a0\n\n}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\"><strong>Decompiled Code (After Obfuscation)<\/strong><\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">Java<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-java\">public String a(String a, long b) {\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0String c = &quot;someSecureSalt&quot;;\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0String d = a + &quot;|&quot; + b + &quot;|&quot; + c;\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0return Integer.toString(d.hashCode());\u00a0\u00a0\n\n}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">This example illustrates the <strong>limitations of obfuscation<\/strong> in protecting sensitive code.\u00a0To protect sensitive data or critical logic, stronger measures are required.\u00a0<\/p><p class=\"wp-block-paragraph\">To learn more about obfuscation, check out the article about <a href=\"https:\/\/adjoe.io\/company\/engineer-blog\/improving-code-obfuscation-in-android-apps\/\">improving code obfuscation in Android apps<\/a>.<\/p><h2 class=\"wp-block-heading\">Native Code: A Reliable Defense Mechanism<\/h2><p class=\"wp-block-paragraph\">Native code, typically written in C or C++, is a game-changer for securing sensitive logic. Unlike Java\/Kotlin, <strong>native code compiles directly into machine instructions<\/strong>, making it far less vulnerable to reverse engineering.\u00a0<\/p><p class=\"wp-block-paragraph\">Native binaries are difficult to analyze due to their lower-level nature. Even if attackers access the compiled library, interpreting its purpose requires advanced knowledge and effort, significantly raising the bar for potential breaches and reducing the risk of exposure.<\/p><p class=\"wp-block-paragraph\">Other than <strong>increased complexity for reverse-engineering<\/strong>, here are some advantages of using native code in Android:<\/p><ul class=\"wp-block-list\"><li><strong>Improved Performance<\/strong>: Native code is optimized for execution at the hardware level, often yielding better performance for intensive computations, which makes it a valuable choice for efficiency.<\/li><\/ul><ul class=\"wp-block-list\"><li><strong>Enhanced Flexibility<\/strong>: Native code can run across platforms like Android and iOS, making it an ideal choice for SDKs that target a wide range of devices.<\/li><\/ul><h3 class=\"wp-block-heading\">Decompiled Native Code: Significantly Harder to Analyze<\/h3><p class=\"wp-block-paragraph\">To further illustrate the difference in reverse engineering complexity, let\u2019s examine how the native version of the generateSessionToken function appears when reverse-engineered using tools like IDA Pro or Ghidra.<br><br>Even though the logic in C++ is straightforward, its compiled output in native assembly is significantly more complex and obfuscated by nature. Here\u2019s a snippet of what disassembled native code might look like:<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">CMake<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code\">.text:0001234C \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 LDR \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 R1, [SP,#0x14+var_4]\n.text:00012350 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ADD \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 R1, R1, #0x1F\n.text:00012354 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MOV \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 R0, R4\n.text:00012358 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 BL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 _Znwj\n.text:0001235C \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MOV \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 R2, R0\n.text:00012360 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MOV \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 R0, R4\n.text:00012364 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MOV \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 R1, R5\n.text:00012368 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 BL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 _ZNSsC1ERKSs\n.text:0001236C \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ADD \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SP, SP, #0x10\n.text:00012370 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 POP \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 {R4-R7,PC}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">You can see this is far from the readable Kotlin or Java decompiled output. To understand what\u2019s happening here, an attacker would need expertise in ARM assembly and knowledge of how the compiler lays out C++ constructs \u2014 a much higher bar than skimming through Java source code.<\/p><p class=\"wp-block-paragraph\">This difference is why native code can act as a \u201cblack box\u201d, hiding your algorithmic logic behind machine-level instructions that are hard to interpret and even harder to replicate.<\/p><h2 class=\"wp-block-heading\">How to Protect Android Logic with Native Code?<\/h2><p class=\"wp-block-paragraph\">Now let\u2019s walk through how to leverage native code for protecting logic in practice. Here\u2019s a breakdown of the approach we took in implementing adjoe Programmatic SDK:<\/p><h3 class=\"wp-block-heading\">1. Identifying Sensitive Application Components<\/h3><p class=\"wp-block-paragraph\">The first step is identifying which parts of your application or SDK require protection. While native code provides advantages in security and performance, it comes with trade-offs in development complexity and debugging.\u00a0<\/p><p class=\"wp-block-paragraph\">That\u2019s why it\u2019s important to isolate only the truly sensitive logic; for example:<\/p><ul class=\"wp-block-list\"><li>Custom data encryption or obfuscation routines.<\/li><li>Logic containing sensitive information about users or revealing users\u2019 identities.<\/li><li>Fraud detection logic, such as anti-tampering mechanisms.<\/li><\/ul><p class=\"wp-block-paragraph\">By limiting the scope of native code to the core sensitive components, you reduce maintenance overhead while significantly increasing protection.<\/p><p class=\"wp-block-paragraph\">We will use the <strong>session token generation logic<\/strong> for this guide.<\/p><h3 class=\"wp-block-heading\">2. Developing the Native Layer in C\/C++<\/h3><p class=\"wp-block-paragraph\">The next step is to write your sensitive logic purely in C++, without worrying yet about how it connects to your Kotlin\/Java layer.\u00a0<\/p><p class=\"wp-block-paragraph\">This step focuses solely on implementing the secure functionality, in this case, the session token generation.<\/p><p class=\"wp-block-paragraph\">Here\u2019s a native implementation that mimics the original logic from Kotlin:<\/p><p class=\"wp-block-paragraph\"><strong>File: token-generator.cpp<\/strong><\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">C<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-c\">#include &lt;string&gt;\n\n#include &lt;sstream&gt;\n\nstd::string generateSessionToken(const std::string&amp; userId, long timestamp) {\n\n\u00a0\u00a0\u00a0\u00a0const std::string salt = &quot;someSecureSalt&quot;;\n\n\u00a0\u00a0\u00a0\u00a0std::stringstream ss;\n\n\u00a0\u00a0\u00a0\u00a0ss &lt;&lt; userId &lt;&lt; &quot;|&quot; &lt;&lt; timestamp &lt;&lt; &quot;|&quot; &lt;&lt; salt;\n\n\u00a0\u00a0\u00a0\u00a0std::string data = ss.str();\n\n\u00a0\u00a0\u00a0\u00a0std::hash&lt;std::string&gt; hasher;\n\n\u00a0\u00a0\u00a0\u00a0size_t hashed = hasher(data);\n\n\u00a0\u00a0\u00a0\u00a0return std::to_string(hashed);\n\n}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">Remember that as a bonus, since your logic is now written in standard C++, it remains portable and reusable across different platforms-even outside Android if needed.<\/p><p class=\"wp-block-paragraph\">At this point, the function is secure and self-contained.\u00a0<\/p><p class=\"wp-block-paragraph\">The next step is to integrate it into your Android project using Android <a href=\"https:\/\/developer.android.com\/ndk\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">NDK (Native Development Kit)<\/a> and expose it to your Android module using <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/technotes\/guides\/jni\/spec\/jniTOC.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">JNI (Java Native Interface)<\/a>.<\/p><h3 class=\"wp-block-heading\">3. Integrating Native Code into Android Module<\/h3><p class=\"wp-block-paragraph\">To connect your native code with your Android project, you\u2019ll need to configure the Android NDK and use JNI to bridge between Java\/Kotlin and C++.<\/p><p class=\"wp-block-paragraph\">In this example, we will be simply using <a href=\"https:\/\/cmake.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">CMake<\/a> alongside Gradle to build the native library. You can review the <a href=\"https:\/\/developer.android.com\/ndk\/guides\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Android Documentation<\/a> for more advanced settings and different build options based on your needs.<\/p><h4 class=\"wp-block-heading\">3.1. Add NDK and CMake Configuration<\/h4><p class=\"wp-block-paragraph\">a. Create the Native Directory Structure<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">HTML<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-markup\">&lt;module-name&gt;\/\n\n\u2514\u2500\u2500 src\/\n\n\u00a0\u00a0\u00a0\u00a0\u2514\u2500\u2500 main\/\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2514\u2500\u2500 cpp\/\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500\u2500 adjoe-sample.cpp\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500\u2500 token-generator.cpp\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2514\u2500\u2500 CMakeLists.txt<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">The adjoe-sample.cpp file is the entry point of your native library and will contain the JNI bridge wrapping your token generation logic.<\/p><p class=\"wp-block-paragraph\">The CMakeLists.txt file is a script used by CMake. This file tells CMake how to build your project\u2014what source files to compile, what libraries to link, what include directories to use, etc.<\/p><p class=\"wp-block-paragraph\">Here\u2019s a simple CMakeLists.txt to build your native library:<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">CMake<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code\">cmake_minimum_required(VERSION 3.22.1)\n\nproject(&quot;AdjoeSample&quot;)\n\nadd_library(\n    ${CMAKE_PROJECT_NAME} \n    SHARED\n    adjoe-sample.cpp\n)\n\ntarget_link_libraries(\n    ${CMAKE_PROJECT_NAME}\n    android\n)\n<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">b. Configure Gradle for Native Build<\/p><p class=\"wp-block-paragraph\">Android Gradle Plugin comes with support for building native libraries with CMake. You can use externalNativeBuild, the NDK workflow that\u2019s a part of the Android Gradle Plugin, to configure CMake.<\/p><p class=\"wp-block-paragraph\"><strong>File: &lt;module-name&gt;\/build.gradle.kts<\/strong><\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">Kotlin<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-kotlin\">android {\n\n\u00a0\u00a0\u00a0\u00a0...\n\n\u00a0\u00a0\u00a0\u00a0defaultConfig {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0...\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0externalNativeBuild {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cmake {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cppFlags += &quot;-std=c++11&quot;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0externalNativeBuild {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cmake {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ The path to the `CMakeLists.txt` created in previous steps\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0path = file(&quot;src\/main\/cpp\/CMakeLists.txt&quot;)\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">c. Load the Native Library<\/p><p class=\"wp-block-paragraph\">The last step of integration is to load the compiled native library in your application runtime:<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">Kotlin<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-kotlin\">package io.adjoe.programmatic.sample\n\nobject TokenGenerator {\n\n    init {\n        \/\/ The library name must match the CMake project name defined in the `CMakeLists.txt` file\n        System.loadLibrary(&quot;AdjoeSample&quot;)\n    }\n}  <\/code><\/pre>\n\t<\/div>\n<h4 class=\"wp-block-heading\">3.2. Create the JNI Bridge<\/h4><p class=\"wp-block-paragraph\">Now that the native code is set up, you\u2019ll need to expose the generateSessionToken function to your Java\/Kotlin code via the Java Native Interface (JNI).\u00a0 JNI allows your Java\/Kotlin layer to call C++ functions directly.\u00a0<\/p><p class=\"wp-block-paragraph\">For this purpose, you need to implement a bridge function using JNI. The function must follow a specific naming convention and use JNI types to be callable from your Android code.<\/p><p class=\"wp-block-paragraph\">a. Define the Bridge Function in Kotlin<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">Kotlin<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-kotlin\">package io.adjoe.programmatic.sample\n\nobject TokenGenerator {\n\n\u00a0\u00a0\u00a0\u00a0init {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.loadLibrary(&quot;AdjoeSample&quot;)\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0external fun nativeGenerateSessionToken(userId: String, timestamp: Long): String\n\n}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\">b. Implement the JNI Binding in C++<\/p><p class=\"wp-block-paragraph\"><strong>File: adjoe-sample.cpp<\/strong><\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">C++<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-cpp\">#include &lt;jni.h&gt;\n\n#include &lt;string&gt;\n\n#include &quot;token-generator.cpp&quot;\n\nextern &quot;C&quot;\n\nJNIEXPORT jstring JNICALL\n\nJava_io_adjoe_programmatic_TokenGenerator_nativeGenerateSessionToken(\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0JNIEnv *env,\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0jobject \/* this *\/,\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0jstring userId,\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0jlong timestamp\n\n) {\n\n\u00a0\u00a0\u00a0\u00a0const char *nativeUserId = env-&gt;GetStringUTFChars(userId, nullptr);\n\n\u00a0\u00a0\u00a0\u00a0std::string token = generateSessionToken(nativeUserId, timestamp);\n\n\u00a0\u00a0\u00a0\u00a0env-&gt;ReleaseStringUTFChars(userId, nativeUserId);\n\n\u00a0\u00a0\u00a0\u00a0return env-&gt;NewStringUTF(token.c_str());\n\n}<\/code><\/pre>\n\t<\/div>\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>:<\/p><p class=\"wp-block-paragraph\">The Java_io_adjoe_programmatic_TokenGenerator_nativeGenerateSessionToken function name is derived from the corresponding Kotlin package\/class\/method name. Make sure it matches exactly with your Kotlin implementation.<\/p><p class=\"wp-block-paragraph\">Once this is complete, your Kotlin code can securely generate session tokens using the native implementation, which will be compiled into a shared library (.so file) and included in your module output.\u00a0<\/p><p class=\"wp-block-paragraph\">This native library acts as a black box, performing the required operations without exposing the underlying code, and the nativeGenerateSessionToken function will be used as a normal Kotlin function.<\/p>\n\n\t<div class=\"code-block code-block--acf code-block--has-language\" data-block=\"code-block\">\n\t\t\t\t\t<div class=\"code-block__header\">\n\t\t\t\t<span class=\"code-block__language\">Kotlin<\/span>\n\t\t\t<\/div>\n\t\t\t\t<pre class=\"code-block__pre\"><code class=\"code-block__code language-kotlin\">val token = TokenGenerator.nativeGenerateSessionToken(&quot;user123&quot;, System.currentTimeMillis())<\/code><\/pre>\n\t<\/div>\n<h3 class=\"wp-block-heading\">4. Obfuscating the Native Library<\/h3><p class=\"wp-block-paragraph\">While native code offers a strong layer of protection, it\u2019s not entirely immune to reverse engineering, especially when determined attackers use tools like <a href=\"https:\/\/hex-rays.com\/ida-pro\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">IDA Pro<\/a> or <a href=\"https:\/\/ghidra-sre.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Ghidra<\/a> to analyze the compiled .so binaries.\u00a0<\/p><p class=\"wp-block-paragraph\">One additional step to further harden your native code against tampering and analysis is to apply <strong>Symbol Obfuscation<\/strong> on your native code.\u00a0<\/p><p class=\"wp-block-paragraph\">This involves stripping or renaming the function and variable names that are embedded in the compiled binary, which are otherwise accessible to reverse engineers using disassemblers or decompilers.<\/p><h2 class=\"wp-block-heading\">Key Considerations and Challenges<\/h2><p class=\"wp-block-paragraph\">While native code enhances security, it comes with challenges that developers should address.<\/p><ol class=\"wp-block-list\"><li><strong>Debugging Complexity:<\/strong> Native code can be harder to debug compared to Java\/Kotlin. Using tools like Android Studio\u2019s native debugger helps streamline the process.<\/li><li><strong>Cross-Platform Variability:<\/strong> Variations in processor architecture (ARM, x86) require building native libraries for each target platform.<\/li><li><strong>Performance Overhead:<\/strong> Although native code is efficient, improper use (e.g., frequent JNI calls) can introduce performance bottlenecks.<\/li><\/ol><p class=\"wp-block-paragraph\">With these trade-offs in mind, it\u2019s recommended to always evaluate whether the added security justifies the extra maintenance effort.<\/p><h2 class=\"wp-block-heading\">Best Practices for Using Native Code<\/h2><p class=\"wp-block-paragraph\">Implementing native code involves more details than what can be covered in this article. If you\u2019re considering implementing native code in your Android projects, review <a href=\"https:\/\/developer.android.com\/ndk\/guides\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Android guides<\/a> and follow these best practices to enhance security and performance.<\/p><ul class=\"wp-block-list\"><li><strong>Minimize JNI Calls:<\/strong> Reduce the number of calls between Java and native code to avoid unnecessary overhead.<\/li><li><strong>Use Secure Communication:<\/strong> Encrypt data exchanged between the app and native layer to prevent interception.<\/li><li><strong>Stay Updated:<\/strong> Use the latest NDK version to leverage improved tools and security features.<\/li><li><strong>Combine with Other Protections:<\/strong> Pair native code with additional security measures like obfuscation, code signing, secure API, and other <a href=\"https:\/\/developer.android.com\/privacy-and-security\/security-tips\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">mobile app security best practices<\/a>.<\/li><\/ul><p class=\"wp-block-paragraph\">Moreover, note that Android 15 introduced support for 16 KB memory page sizes. You can review the <a href=\"https:\/\/developer.android.com\/guide\/practices\/page-sizes\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Android documentation<\/a> for more details and guidance on adding the support in your native build.<\/p><h2 class=\"wp-block-heading\">adjoe Insights: Native Code on Android for Mobile Application Security\u00a0<\/h2><p class=\"wp-block-paragraph\">Protecting sensitive application logic on Android is a critical aspect of mobile app security. Modern reverse engineering tools can easily bypass basic obfuscation techniques, making them inefficient on their own.<\/p><p class=\"wp-block-paragraph\">To truly build secure mobile applications and strengthen your overall security posture, it\u2019s important to combine native code with well-considered design, advanced obfuscation strategies, and robust runtime integrity checks.<\/p><p class=\"wp-block-paragraph\">At adjoe, we\u2019ve successfully implemented these security strategies into our SDK.\u00a0The results? Strong protection for key components of our logic without sacrificing performance.\u00a0<\/p><p class=\"wp-block-paragraph\">If you\u2019re developing an Android SDK or app with sensitive components, we highly recommend integrating native code for security-critical logic. It significantly increases the difficulty for attackers and enables cross-platform reuse, giving your app a real edge.\u00a0<\/p><p class=\"wp-block-paragraph\">Need expert insights on building great products and mobile app security? The <a href=\"https:\/\/adjoe.io\/company\/engineer-blog\/\" target=\"_blank\" rel=\"noreferrer noopener\">adjoe engineer\u2019s blog<\/a> has got you covered.<\/p>","protected":false},"excerpt":{"rendered":"<p>A cutting-edge mobil&#8230;<\/p>\n","protected":false},"author":3,"featured_media":2953,"template":"","engineer_blog_category":[78],"class_list":["post-2828","engineer_blog","type-engineer_blog","status-publish","has-post-thumbnail","hentry","engineer_blog_category-android"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mobile App Security: How Native Code Protects Sensitive Logic in Android - adjoe | Making every interaction the start of lasting value<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/\" \/>\n<meta property=\"og:locale\" content=\"zh_TW\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mobile App Security: How Native Code Protects Sensitive Logic in Android - adjoe | Making every interaction the start of lasting value\" \/>\n<meta property=\"og:description\" content=\"A cutting-edge mobil...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/\" \/>\n<meta property=\"og:site_name\" content=\"adjoe | Making every interaction the start of lasting value\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/people\/Adjoe\/100076450974671\/\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-12T22:56:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/adjoe.io\/wp-content\/uploads\/2025\/04\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/\",\"url\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/\",\"name\":\"Mobile App Security: How Native Code Protects Sensitive Logic in Android - adjoe | Making every interaction the start of lasting value\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/adjoe.io\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp\",\"datePublished\":\"2025-04-28T10:42:12+00:00\",\"dateModified\":\"2026-11-12T22:56:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/#breadcrumb\"},\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/#primaryimage\",\"url\":\"https:\\\/\\\/adjoe.io\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp\",\"contentUrl\":\"https:\\\/\\\/adjoe.io\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp\",\"width\":1920,\"height\":1080,\"caption\":\"Mobile App Security: How Native Code Protects Sensitive Logic in Android\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/company\\\/engineer-blog\\\/mobile-app-security-android-native-code\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mobile App Security: How Native Code Protects Sensitive Logic in Android\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/#website\",\"url\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/\",\"name\":\"adjoe | Making every interaction the start of lasting value\",\"description\":\"adjoe is a global tech company powering user acquisition, monetization and audience engagement across the app economy \u2013 making every interaction the start of lasting value.\",\"publisher\":{\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-TW\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/#organization\",\"name\":\"adjoe | Making every interaction the start of lasting value\",\"url\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/adjoe.io\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/OG-img-1200x630-1.jpg\",\"contentUrl\":\"https:\\\/\\\/adjoe.io\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/OG-img-1200x630-1.jpg\",\"width\":1200,\"height\":630,\"caption\":\"adjoe | Making every interaction the start of lasting value\"},\"image\":{\"@id\":\"https:\\\/\\\/adjoe.io\\\/zh-tw\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/people\\\/Adjoe\\\/100076450974671\\\/\",\"https:\\\/\\\/www.instagram.com\\\/adjoe.io\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/adjoe\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mobile App Security: How Native Code Protects Sensitive Logic in Android - adjoe | Making every interaction the start of lasting value","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/","og_locale":"zh_TW","og_type":"article","og_title":"Mobile App Security: How Native Code Protects Sensitive Logic in Android - adjoe | Making every interaction the start of lasting value","og_description":"A cutting-edge mobil...","og_url":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/","og_site_name":"adjoe | Making every interaction the start of lasting value","article_publisher":"https:\/\/www.facebook.com\/people\/Adjoe\/100076450974671\/","article_modified_time":"2026-11-12T22:56:04+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/adjoe.io\/wp-content\/uploads\/2025\/04\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp","type":"image\/webp"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/","url":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/","name":"Mobile App Security: How Native Code Protects Sensitive Logic in Android - adjoe | Making every interaction the start of lasting value","isPartOf":{"@id":"https:\/\/adjoe.io\/zh-tw\/#website"},"primaryImageOfPage":{"@id":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/#primaryimage"},"image":{"@id":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/#primaryimage"},"thumbnailUrl":"https:\/\/adjoe.io\/wp-content\/uploads\/2025\/04\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp","datePublished":"2025-04-28T10:42:12+00:00","dateModified":"2026-11-12T22:56:04+00:00","breadcrumb":{"@id":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/#breadcrumb"},"inLanguage":"zh-TW","potentialAction":[{"@type":"ReadAction","target":["https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/"]}]},{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/#primaryimage","url":"https:\/\/adjoe.io\/wp-content\/uploads\/2025\/04\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp","contentUrl":"https:\/\/adjoe.io\/wp-content\/uploads\/2025\/04\/Protecting-Sensitive-Logic-in-Android-Leveraging-Native-Code-for-Security.webp","width":1920,"height":1080,"caption":"Mobile App Security: How Native Code Protects Sensitive Logic in Android"},{"@type":"BreadcrumbList","@id":"https:\/\/adjoe.io\/zh-tw\/company\/engineer-blog\/mobile-app-security-android-native-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/adjoe.io\/zh-tw\/"},{"@type":"ListItem","position":2,"name":"Mobile App Security: How Native Code Protects Sensitive Logic in Android"}]},{"@type":"WebSite","@id":"https:\/\/adjoe.io\/zh-tw\/#website","url":"https:\/\/adjoe.io\/zh-tw\/","name":"adjoe | Making every interaction the start of lasting value","description":"adjoe is a global tech company powering user acquisition, monetization and audience engagement across the app economy \u2013 making every interaction the start of lasting value.","publisher":{"@id":"https:\/\/adjoe.io\/zh-tw\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/adjoe.io\/zh-tw\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-TW"},{"@type":"Organization","@id":"https:\/\/adjoe.io\/zh-tw\/#organization","name":"adjoe | Making every interaction the start of lasting value","url":"https:\/\/adjoe.io\/zh-tw\/","logo":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/adjoe.io\/zh-tw\/#\/schema\/logo\/image\/","url":"https:\/\/adjoe.io\/wp-content\/uploads\/2026\/11\/OG-img-1200x630-1.jpg","contentUrl":"https:\/\/adjoe.io\/wp-content\/uploads\/2026\/11\/OG-img-1200x630-1.jpg","width":1200,"height":630,"caption":"adjoe | Making every interaction the start of lasting value"},"image":{"@id":"https:\/\/adjoe.io\/zh-tw\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/people\/Adjoe\/100076450974671\/","https:\/\/www.instagram.com\/adjoe.io\/","https:\/\/www.linkedin.com\/company\/adjoe"]}]}},"_links":{"self":[{"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/engineer_blog\/2828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/engineer_blog"}],"about":[{"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/types\/engineer_blog"}],"author":[{"embeddable":true,"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":2,"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/engineer_blog\/2828\/revisions"}],"predecessor-version":[{"id":3099,"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/engineer_blog\/2828\/revisions\/3099"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/media\/2953"}],"wp:attachment":[{"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/media?parent=2828"}],"wp:term":[{"taxonomy":"engineer_blog_category","embeddable":true,"href":"https:\/\/adjoe.io\/zh-tw\/wp-json\/wp\/v2\/engineer_blog_category?post=2828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}