大家好,欢迎来到IT知识分享网。
第一步:创建so库
第二步:看一下Native的项目结构
自动生成引用文件build.gradle(app)
externalNativeBuild { cmake { path file('src/main/cpp/CMakeLists.txt') version '3.22.1' } } //每个版本的开发工具不一样 我这个是自动生成的so库文件类型,尽量用新版ASK开发工具
CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("sodemo")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
native-lib.cpp
src/NativeImpl.cpp)
# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log)
native-lib.cpp
#include <jni.h> #include <string> #include "src/NativeImpl.h" /* 获取NativeImpl */ NativeImpl nativeImpl; NativeImpl *getNativeImpl() { return &nativeImpl; } extern "C" JNIEXPORT jstring JNICALL Java_com_geek_sodemo_NativeImpl_ICC_1interface_1power(JNIEnv *env, jclass clazz) { // TODO: implement ICC_interface_power() // TODO: implement getUserName() char *c = getNativeImpl()->ICC_interface_power(); return env->NewStringUTF(c); }
第三步:直接加上so库代码优化(不废话)
把native-lib.cpp分离出来创建 .h和.cpp文件 如下代码
NativeImpl.cpp
#include "NativeImpl.h" NativeImpl::NativeImpl() { } NativeImpl::~NativeImpl() { } char *NativeImpl::ICC_interface_power() { // 这里处理代码逻辑 return "卡槽上下电控制"; }
NativeImpl.h
#ifndef MYNATIVE_CLIENTIMPL_H #define MYNATIVE_CLIENTIMPL_H #include <vector> class NativeImpl { public: NativeImpl(); virtual ~NativeImpl(); virtual char* ICC_interface_power(); }; #endif //MYNATIVE_CLIENTIMPL_H
第四步:在CMakeLists.txt文件中增加cpp引入
add_library(${CMAKE_PROJECT_NAME} SHARED # List C/C++ source files with relative paths to this CMakeLists.txt. native-lib.cpp src/NativeImpl.cpp)
native-lib.cpp增加方法
#include <jni.h> #include <string> #include "src/NativeImpl.h" /* 获取NativeImpl */ NativeImpl nativeImpl; NativeImpl *getNativeImpl() { return &nativeImpl; } extern "C" JNIEXPORT jstring JNICALL Java_com_geek_sodemo_NativeImpl_ICC_1interface_1power(JNIEnv *env, jclass clazz) { // TODO: implement ICC_interface_power() // TODO: implement getUserName() char *c = getNativeImpl()->ICC_interface_power(); return env->NewStringUTF(c); }
第五步:so库使用优化NativeImpl创建在应用的目录结构下
public class NativeImpl { // 加载so库 static { System.loadLibrary("sodemo"); } / * 添加 native 方法 */ public static native String ICC_interface_power(); }
MainActivity.class中进行引用
public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // Example of a call to a native method TextView tv = binding.sampleText; tv.setText(NativeImpl.ICC_interface_power()); } }
最后运行看结果
上面的内容生成so库及给java代码调用就完成了
下面是怎样是这个so库给其他应用使用 如下
然后拷贝.so文件到 新创建的项目中的libs文件下
在build.gradle 里面android节点下增加
sourceSets { main { jniLibs.srcDirs = ['libs'] } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/136904.html