Mobile Architecture

person shubham sharmafolder_openissuesaccess_time April 26, 2024

What is NKD?

The NDK, or Native Development Kit, is a set of tools provided by Android for developers to write and implement performance-critical parts of their applications using native code languages like C and C++. This is particularly useful for tasks that require high performance, such as graphics-intensive applications, games, or algorithms that benefit from low-level system access.

The NDK allows developers to create native libraries containing the code written in C/C++ and then integrate these libraries into their Android applications using Java Native Interface (JNI) bindings. This approach can offer advantages in terms of performance, memory management, and accessing platform-specific features, although it often requires more expertise in low-level programming compared to using only Java or Kotlin.

Overall, the NDK serves as a bridge between the Android platform’s Java/Kotlin-based development environment and the capabilities of native code languages for developers who need to optimize performance or leverage existing C/C++ libraries in their Android apps.

So when we write code in C and C++ for Android after compile (Make build APK) it excuse by JRE or C/C++ complier?

When you write code in C or C++ for Android using the NDK, the process involves compiling the native code using a C/C++ compiler, not the Java Runtime Environment (JRE). Here’s an overview of how the compilation process works:

  1. Write Native Code: You write the performance-critical parts of your Android application using C or C++.
  2. Compile Native Code: Using the Android NDK tools (like ndk-buildor CMake with Android Studio), you compile the native code into native libraries (.so files). This step involves using a C/C++ compiler (such as GCC or Clang) to generate machine code specific to the target Android architecture (like ARM or x86).
  3. Create Java Native Interface (JNI) Bindings: You create Java classes that act as a bridge between your Java/Kotlin code and the native code. These Java classes use JNI bindings to call functions from the native libraries.
  4. Integrate with Android App: You include the compiled native libraries (.so files) and the Java classes with JNI bindings into your Android app project. During the build process, these components are packaged together to create the final APK.
  5. Execution: When the Android app runs on a device, the Java/Kotlin code can invoke functions from the native libraries using JNI. The Android system loads and executes the native code through the runtime environment specific to the device’s architecture (not the JRE used for Java/Kotlin code).

So, in summary, the C/C++ code is compiled by a C/C++ compiler (not the JRE) into native libraries, which are then integrated into the Android app and executed by the Android system’s runtime environment corresponding to the device’s architecture.

What compiler does Android support to run APK

Android primarily supports two main compilers for compiling and running APKs: the Android Runtime (ART) and the Android NDK (Native Development Kit) compilers.

  1. Android Runtime (ART):
    • For Java and Kotlin code, Android apps are compiled into bytecode by the Java compiler (javac). This bytecode is then executed by the Android Runtime (ART) on the device. ART is the runtime environment responsible for running the bytecode and managing the application’s execution, including memory management, garbage collection, and more.
  2. Android NDK (Native Development Kit):
    • For native code written in C or C++ using the Android NDK, the code is compiled into native libraries (.so files) using a C/C++ compiler like GCC (GNU Compiler Collection) or Clang. These native libraries are then packaged into the APK along with the Java/Kotlin code. When the app runs on the device, the Android system’s runtime environment loads and executes these native libraries using the appropriate ABI (Application Binary Interface) specific to the device’s architecture (e.g., ARM, x86).

Both ART and the NDK compilers are integral parts of the Android development ecosystem, with ART handling Java/Kotlin bytecode execution and the NDK compilers managing compilation and execution of native C/C++ code. This combination allows developers to create apps with a mix of Java/Kotlin and native code for optimal performance and functionality.

What about flutter, When we write and build APK does it convert Dart code into .class file (java bytecode) or how does dart code runs

Flutter works differently from traditional Android development because it uses a different approach to compiling and running code.

  1. Dart to Native Code (Ahead-of-Time Compilation):
    • When you write code in Dart for a Flutter app, the Dart code is compiled ahead of time (AOT) into native code specific to the target platform (Android or iOS). This compilation process converts Dart code into machine code directly, bypassing the need for Java bytecode (.class files) or intermediate interpretation by a virtual machine.
    • AOT compilation is used for the release builds of Flutter apps. It results in faster startup times and improved performance compared to just-in-time (JIT) compilation.
  2. Flutter Engine:
    • The compiled native code (along with the Flutter framework) is bundled into the Flutter engine, which is a platform-specific library that handles rendering, input, and other platform interactions for Flutter apps.
    • When you build the APK for a Flutter app, the Flutter engine (compiled for Android) is included in the APK along with any assets, resources, and native code required for the app.
  3. Android Runtime:
    • When the Flutter app runs on an Android device, it interacts with the Android system primarily through the Flutter engine. The Flutter engine handles rendering UI elements, managing state, and executing platform-specific code through plugins.
    • The Android Runtime (ART) is not directly involved in executing Dart code in Flutter apps. Instead, the Flutter engine manages the execution of Dart code that has been compiled to native code.

In summary, Flutter compiles Dart code ahead of time into native code specific to the target platform (such as ARM for Android devices). The Flutter engine then handles the execution of this compiled native code, including UI rendering, state management, and platform interactions, without relying on Java bytecode or the Android Runtime (ART) for Dart code execution.

Which machine code?

When Dart code in a Flutter app is compiled ahead of time (AOT compilation), it is compiled into native machine code specific to the target platform architecture. For Android devices, which typically use ARM-based processors, the machine code generated would be ARM machine code.

Here’s a breakdown of the process:

  1. AOT Compilation: The Dart code in a Flutter app is compiled ahead of time. This means that the Dart code is translated into native machine code during the build process, rather than being interpreted or compiled on-the-fly during runtime.
  2. Target Platform Architecture: The machine code generated during AOT compilation is specific to the target platform’s architecture. For Android devices, which are commonly based on ARM architectures (such as ARMv7 or ARM64), the machine code generated would be ARM machine code.
  3. Optimization: During the AOT compilation process, optimizations are applied to the generated machine code to improve performance and reduce resource usage. These optimizations may include code size reduction, inline caching, and other techniques to enhance the efficiency of the compiled code.
  4. Flutter Engine Integration: The compiled native machine code, along with the Flutter framework and assets, is bundled into the Flutter engine. This engine is integrated into the Flutter app’s APK and is responsible for executing the compiled Dart code, handling UI rendering, managing state, and interacting with the platform.

In summary, Dart code in a Flutter app is compiled ahead of time into native machine code specific to the target platform’s architecture (such as ARM machine code for Android devices). This machine code is optimized and executed by the Flutter engine to power the app’s functionality and user interface.

is it possible to write code in c/c++ for iOS devices?

Yes, it’s possible to write code in C or C++ for iOS devices. In fact, C and C++ are widely used languages for iOS development, especially for performance-critical tasks, games, multimedia applications, and cross-platform development using frameworks like Flutter, Unity, or custom C/C++ libraries.

Here’s how you can write and integrate C/C++ code into an iOS app:

  1. Using Xcode:
    • Xcode, Apple’s integrated development environment (IDE), fully supports C and C++ development for iOS. You can create a new C/C++ source file within an Xcode project or import existing C/C++ code into your project.
  2. Objective-C++:
    • Objective-C++ is a language that combines Objective-C and C++. It allows you to mix Objective-C code (commonly used in iOS development) with C++ code seamlessly. This can be useful when you need to integrate C++ functionality into an existing Objective-C or Swift codebase.
  3. C/C++ Libraries:
    • You can include third-party C/C++ libraries in your iOS app project. This involves adding the library’s source code or precompiled binary (.a or .framework file) to your Xcode project, configuring build settings, and linking the library to your app.
  4. iOS SDK and APIs:
    • Apple’s iOS SDK provides APIs and frameworks that allow you to interact with system services, UI components, sensors, networking, and more using C, Objective-C, or Swift. You can use C or C++ alongside these APIs as needed.
  5. Cross-Platform Development:
    • If you’re using a cross-platform framework like Flutter or Unity for iOS app development, you can write core logic or performance-critical parts of your app in C/C++. These frameworks typically provide APIs or plugins for integrating C/C++ code into your app project.
  6. Performance and Optimization:
    • C and C++ are known for their performance and low-level system access, making them suitable for tasks such as graphics rendering, audio processing, mathematical computations, and game development on iOS devices.

Overall, writing code in C or C++ for iOS devices is not only possible but also common in scenarios where performance, system-level access, or integration with existing C/C++ libraries are required. Xcode provides the necessary tools and support for developing iOS apps using C, C++, Objective-C, and Swift.

warningComments are closed.