Android NDK compiles LED dynamic library so and uses so

Article directory

Table of contents

Article directory

basic information

My AS basic information

gradle plugin version

hardware information

basic knowledge

externalNativeBuild

specific steps

Download the NDK library

Add JNI related C language code and mk file

Configure the ndk path in local.properties

gradle configuration

Configure externalNativeBuild

Package dynamic library

 Reference third-party so library

 abnormal problem

 Final effect display


basic information

My AS basic information

Android Studio Dolphin | 2021.3.1 Patch 1
Build #AI-213.7172.25.2113.9123335, built on September 30, 2022
Runtime version: 11.0.13+0-b1751.21-8125866 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 3048M
Cores: 12
Registry:
    external.system.auto.import.disabled=true
    ide.text.editor.with.preview.show.floating.toolbar=false
    ide.windowSystem.autoShowProcessPopup=true
    ide.tooltip.initialDelay=695

Non-Bundled Plugins:
    org.intellij.plugins.markdown (213.6777.22)
    com.alibaba.p3c.xenoamess (2.1.1.5x-SNAPSHOT)
    color.scheme.GapStyle (4.1)
    GsonFormatPlus (1.6.1)

gradle plugin version

hardware information

Model: JY61

  1. The product defaults to the baud rate of the serial communication interface at 9600
  2. Power supply: DC12V ± 10%
  3. Mainly used for LED lights flashing and always on

basic knowledge

externalNativeBuild

build.gradleThere are two places inside that will be used externalNativeBuild, one defaultConfiginside and one defaultConfigoutside.

The cmake in the externalNativeBuild outside the defaultConfig specifies the path of CMakeList.txt or the path of Android.mk (src/main/jni/Android.mk is used in this chapter). The
cmake in the externalNativeBuild in the defaultConfig mainly fills in CMake command parameters. That is, the parameters in arguments are finally converted into an executable CMake command.

specific steps

Download the NDK library

Enter SDK in Settings to find the picture below, click to download the specified ndk package.

Add JNI related C language code and mk file

Create the jni directory in app/src/main as shown below ( not going into details here )

Configure the ndk path in local.properties

ndk.dir=E\:\\AndroidStudio\\sdk\\ndk\\21.4.7075529

gradle configuration

Corresponding to the specified version, the versions must be the same

android {
    ndkVersion "21.4.7075529"
}

Configure the output dynamic library file name and structure

defaultConfig {
    ...
    ndk {
            //输出指定三种abi体系结构下的so库。
            moduleName "libserial_port"
            abiFilters  "armeabi-v7a", "x86"
        }
}

Configure externalNativeBuild

This is very important. Only by adding it can the dynamic library be packaged normally.

externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
}

Package dynamic library

Click Make Project, the first compilation will generate the corresponding dynamic library in app\build\intermediates\merged_native_libs.

 

 Reference third-party so library

 Copy the dynamic library folder and files to the libs directory

Add libs configuration to app's build.gradle

android {
....
sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
}
....

The code references the dynamic library: System.loadLibrary("serial_port"); //Very important

The last is to package the apk

abnormal problem

After referencing the dynamic library, the packaged apk reports an error:

Execution failed for task':app:mergeApp_onlineReleaseNativeLibs'.

 2 files found with path 'lib/x86/libserial_port.so' from inputs:
      - D:\Demo\app\build\intermediates\merged_jni_libs\app_onlineRelease\out
      - D:\Demo\app\build\intermediates\ndkBuild\app_onlineRelease\obj\local
     If you are using jniLibs and CMake IMPORTED targets, see

 The solution is to comment out the following code to package normally

 Final effect display

Guess you like

Origin blog.csdn.net/piyangbo/article/details/129126972