What is the difference between android and native android(Android NDK)?

Learn what is the difference between android and native android(android ndk)? with practical examples, diagrams, and best practices. Covers java, android development techniques with visual explanat...

Android vs. Native Android (NDK): Understanding the Core Differences

Hero image for What is the difference between android and native android(Android NDK)?

Explore the fundamental distinctions between standard Android development with Java/Kotlin and native Android development using the NDK, including performance, language choices, and use cases.

When developing applications for the Android platform, you primarily encounter two main approaches: standard Android development and native Android development. While both result in apps running on Android devices, they differ significantly in the programming languages used, the underlying execution environment, and their typical use cases. This article will demystify these differences, focusing on what 'native Android' truly means in the context of the Android Native Development Kit (NDK).

What is 'Android' Development?

Standard Android development, often simply referred to as 'Android development,' primarily uses Java or Kotlin as the programming languages. These languages compile into bytecode that runs on the Android Runtime (ART), which is a virtual machine. This approach offers a high level of abstraction, garbage collection, and a rich set of frameworks and libraries provided by the Android SDK (Software Development Kit). Most typical Android applications, from social media apps to utility tools, are built using this method.

What is 'Native Android' (Android NDK)?

Native Android development, facilitated by the Android Native Development Kit (NDK), involves writing parts of an Android application using native code languages like C or C++. This native code is then compiled directly into machine code that runs on the device's CPU, bypassing the Android Runtime (ART) for those specific components. The NDK allows developers to reuse existing C/C++ libraries, achieve higher performance for computationally intensive tasks, and gain lower-level hardware access.

flowchart TD
    A[Android App (Java/Kotlin)] --> B{Android Runtime (ART)}
    B --> C[Device CPU]
    D[Android App (Java/Kotlin)] --> E[JNI (Java Native Interface)]
    E --> F[Native Code (C/C++)]
    F --> G[Device CPU]
    subgraph Standard Android
        A
        B
        C
    end
    subgraph Native Android (NDK)
        D
        E
        F
        G
    end
    style Standard Android fill:#e0ffe0,stroke:#333,stroke-width:2px
    style Native Android (NDK) fill:#ffe0e0,stroke:#333,stroke-width:2px

Execution Flow: Standard Android vs. Native Android (NDK)

Key Differences and Use Cases

The choice between standard and native Android development hinges on specific project requirements. Here's a breakdown of their core differences and when to use each:

Hero image for What is the difference between android and native android(Android NDK)?

Comparison of Android Development Approaches

Integrating Native Code with JNI

When using the NDK, Java or Kotlin code interacts with C/C++ code through the Java Native Interface (JNI). JNI is a framework that allows Java code running in the ART to call native applications and libraries, and vice-versa. This bridge is essential for leveraging the performance benefits of native code while still building the majority of the application's UI and high-level logic in Java or Kotlin.

public class MainActivity extends AppCompatActivity {

    // Load the native library
    static {
        System.loadLibrary("my_native_lib");
    }

    // Declare native method
    public native String stringFromJNI();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI()); // Call the native method
    }
}

Example of calling a native method from Java in an Android application.

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapp_MainActivity_stringFromJNI(
        JNIEnv* env, jobject /* this */) {
    std::string hello = "Hello from C++ NDK!";
    return env->NewStringUTF(hello.c_str());
}

Corresponding C++ native code implementation for the stringFromJNI method.