Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Android NDK Programming: Master Class
Rating: 4.0 out of 5(6 ratings)
41 students

Android NDK Programming: Master Class

Android apps development use C/C++ code
Created byJames Jiang
Last updated 10/2024
English

What you'll learn

  • Android NDK programming
  • Write High performance Android APP
  • Android programming using C/C++
  • Build FFmpeg for Android from source code
  • Using FFmpeg in AndroidStudio project

Course content

6 sections18 lectures2h 32m total length
  • Introduction to NDK2:59

    Hello everyone. In this course you are going to learn about Android NDK development by using C/C++ code.

    This is a practical class about Android NDK programming. After learning this class, you will be able to write high performance program with C/C++ code for your Android Apps. And you can integrate existing C/C++ libraries into your Android Apps.

    This course has two prerequisites. Firstly, basic Java programming skills is required as most Android apps are developed in Java code. Secondly, basic C/C++ programming skills is recommended. It will be helpull if you hava some basic Android development experience.

    These references & resources are very userfull in NDK programming.

    All the source code will be discussed and developed in this course is presented in the two github url. Before we dive into NDK programming, let's learn some Basic Concepts about NDK and JNI.

    So what is NDK?

    The Android NDK is a set of tools allowing you to embed C or C++ (“native code”) into your Android apps. The ability to use native code in Android apps can be particularly useful to developers who wish to do one or more of the following:

    • Port their apps between platforms.

    • Reuse existing libraries, or provide their own libraries for reuse.

    • Increase performance in certain cases, particularly computationally intensive ones like games.

    Whait is JNI

    JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++). JNI is vendor-neutral, has support for loading code from dynamic shared libraries, and while cumbersome at times is reasonably efficient.

    In next lecture we will setup Android NDK devlop environment and create our fist NDK program. Thanks for watching.

  • Installation and Setup5:36

    In this lecture we are going to setup Android NDK develop environment and create our first NDK program.

    First you need to install Android Studio. You can download Android Studio in this website. After Android Studio is installed, you need to setup SDK and NDK. Let's open Android Studio, click SDK Manager, edit Android SDK Location, click next to install SDK and then click finish. Click SDK Tools, select Android SDK Build-Tools, select NDK and CMake, then click OK.

    Let's create a new project with C/C++ support.

    To create a new project with support for native code, the process is similar to creating any other Android Studio project, but with an additional step:

    1. In the Choose your project section of the wizard, select the Native C++ project type.

    2. Click Next.

    3. Complete all other fields in the next section of the wizard.

    4. Click Next.

    5. In the Customize C++ Support section of the wizard, you can customize your project with the C++ Standard field.

      • Use the drop-down list to select which standardization of C++ you want to use. Selecting Toolchain Default uses the default CMake setting.

    1. Click Finish.

    After Android Studio finishes creating your new project, open the Project pane from the left side of the IDE and select the Android view from the menuAndroid Studio adds the cpp group:

    The cpp group is where you can find all the native source files, headers, build scripts for CMake or ndk-build, and prebuilt libraries that are a part of your project. For new projects, Android Studio creates a sample C++ source file, native-lib.cpp, and places it in the src/main/cpp/ directory of your app module. This sample code provides a simple C++ function, stringFromJNI(), that returns the string "Hello from C++".

    Similar to how build.gradle files instruct Gradle how to build your app, CMake and ndk-build require a build script to know how to build your native library. For new projects, Android Studio creates a CMake build script,CMakeLists.txt, and places it in your module’s root directory.

    Let's build and run the sample app.

    When you click Run, Android Studio builds and launches an app that displays the text "Hello from C++" on your Android device or emulator.

    Now we have setup Android NDK develop environment. In next lecture, we will learn NDK program with android log.

  • Use NDK log4:46

    In this lecture we are going to learn Android NDK programming with log.

    __android_log_print is a very usefull function. It writes a formatted string to the log, with priority prio and tag tag. The useage of Logging is well documented in this URL. Let's try this function,

    We are going to create a native header file ndk_utils.h

    #ifndef NDK_DEMOS_NDK_UTILS_H #define NDK_DEMOS_NDK_UTILS_H #include <android/log.h> #define TAG "ndk_log" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__); #endif

    Let's add a Button in the layout of activity_main, set id to call_jni

    <Button android:id="@+id/call_jni" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="160dp" android:text="Call JNI" app:layout_constraintBottom_toBottomOf="parent" tools:layout_editor_absoluteX="-16dp" />

    In MainActivity, set click listener for call_jni button.

    TextView tv = binding.sampleText; binding.callJni.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String result = stringFromJNI(); tv.setText(result); } });

    Let's run the project, then click button. Sounds good.

    Let's use android log in native code.

    Let's open native-lib.cpp, android include "ndk_utils.h", call LOG_D

    #include <jni.h> #include <string> #include "ndk_utils.h" extern "C" JNIEXPORT jstring JNICALL Java_ndk_demo_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; LOG_D("Hello from native"); return env->NewStringUTF(hello.c_str()); }

    Let's run the app, open the logcat. click the button, we can see log string in logcat, and the string from native is displayed on the Andoid device.

    In next lecture we will write Java native method that use c codes. Thanks for watching.

Requirements

  • Android Programming
  • C/C++
  • Java

Description

This is a practical class about Android NDK programming. After learning this class, you will be able to write high performance program with C/C++ code for your Android Apps. And you can integrate existing C/C++ libraries into your Android Apps.

This course has two prerequisites. Firstly, basic Java programming skills is required as most Android apps are developed in Java code. Secondly, basic C/C++ programming skills is recommended. It will be helpull if you hava some basic Android development experience.

The Android NDK is a set of tools allowing you to embed C or C++ (“native code”) into your Android apps. The ability to use native code in Android apps can be particularly useful to developers who wish to do one or more of the following:

  • Port their apps between platforms.

  • Reuse existing libraries, or provide their own libraries for reuse.

  • Increase performance in certain cases, particularly computationally intensive ones like games.

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++). JNI is vendor-neutral, has support for loading code from dynamic shared libraries, and while cumbersome at times is reasonably efficient.

This lecture will teach you setup Android NDK develop environment and create your first NDK program.

FFMPEG is one of the most popular or powerful media processing library with multiple platform support and it is capable of doing Most of Media processing task. You will learn Compile/Build Latest ffmpeg for Android and Use it in Android Studio.

Who this course is for:

  • Beginner Android developers
  • Beginner C/C++ developers
  • Beginner FFmpeg developers