android studio
下载安装cmake 和 ndk
先用javac -h 编译出 .h的头文件
新建CMakeLists.txt 文件
# cmake 最低版本需求
cmake_minimum_required(VERSION 3.13)
# 工程名称
project(cmake_study1)
// find_library(VAR name path)查找到指定的预编译库,并将它的路径存储在变量中。
// 默认的搜索路径为cmake 包含的系统库,因此如果是NDK的公共库只需要指定库的name即可。
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
add_libary,find_libart() set_target_properties,target_link_libary,link_directories, link_libary
set path="cmake文件夹"
cmake.exe -Hc:\user\Android-SDK@3.8.7.81902_20230704\UniPlugin-Hello-AS\xxx\src\main\jni -Bc:\user\Android-SDK@3.8.7.81902_20230704\UniPlugin-Hello-AS\xxx\.externalNativeBuild\cmake\release\armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-22 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=c:\user\Android-SDK@3.8.7.81902_20230704\UniPlugin-Hello-AS\xxx\build\intermediates\cmake\release\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Release -DANDROID_NDK=D:\document\AndroidSDK\ndk\16.1.4479499 -DCMAKE_TOOLCHAIN_FILE=D:\document\AndroidSDK\ndk\16.1.4479499\build\cmake\android.toolchain.cmake -G Ninja -DCMAKE_CXX_FLAGS="-llog" -DCMAKE_MAKE_PROGRAM=D:\document\AndroidSDK\cmake\3.6.4111459\bin\ninja.exe
其实就是 cmake 命令语法
-Hpath/to/cmakelists/folder
-Bpath/to/generated/ninja/project/debug/ABI
-DANDROID_ABI=ABI // For example, arm64-v8a
-DANDROID_PLATFORM=platform-version-string // For example, android-16
-DANDROID_NDK=android-sdk/ndk/ndk-version
-DCMAKE_TOOLCHAIN_FILE=android-sdk/ndk/ndk-version/build/cmake/android.toolchain.cmake
-G Ninja
成功后找到 build.ninja
直接在文件夹运行 ninja
最后在安卓libs 里面新建 armeabi-v7a armeabi 文件夹,将so文件复制进去
模块build.gradle 里面加 dependencies { implementation fileTree(include: ['*.jar', '*.so'], dir: 'libs') }
在要引用的java文件内加 (不要so文件名的开头lib 和 so后缀) System.loadLibrary("native-lib");