“Day 2 of my participation in the 2022 First Revision Challenge. For details: 2022 First Revision Challenge”

preface

As an Android developer, we all know that android is based on the Linux operating system and will eventually interact with physical devices, so it will involve native development. The NDK (Native Development Kit) is a set of tools that enable you to use C and C++ code in Android applications and provides a number of platform libraries that can be used to manage Native activities and access physical device components.

Environment set up

Download the NDK tool

Generate the first.so library

1. Create a library for Java class loading

public class JniUtil {
  static {
    System.loadLibrary("test");
  }
  public native String getStringJNI(a);
}
Copy the code

2. Run the Terminal command to automatically generate the header file

(Make sure you have JDK installed)

  • Go to the Java directory CD app/ SRC /main/ Java

  • Generate header file javah-jni-encoding UTF-8 Package name + class name

Results:

Generated header file:

3. Create a Jni file in the app directory and copy the generated. H file to the Jni directory

  • Then create a.c file

4. Create and edit Android.mk, application. mk

  • Android.mk
#Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := test / / the library
LOCAL_SRC_FILES :=demo.c // Use the.c file
include $(BUILD_SHARED_LIBRARY)
Copy the code
  • Application.mk
APP_PLATFORM := android-16
APP_ABI := all
Copy the code

5. In the Terminal and app directory, enter ndK-build to compile

Generate graph:

Off-topic: Nanjing is really beautiful in spring, with all kinds of flowers blooming one after another