欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Android 生成so库 并调用

Android 生成so库 并调用

2025/5/11 21:27:46 来源:https://blog.csdn.net/xige1995/article/details/142132229  浏览:    关键词:Android 生成so库 并调用

第一步:创建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.cppsrc/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 libraryandroidlog)

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.cppsrc/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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextView tv = binding.sampleText;tv.setText(NativeImpl.ICC_interface_power());}}

最后运行看结果

上面的内容生成so库及给java代码调用就完成了

下面是怎样是这个so库给其他应用使用  如下

然后拷贝.so文件到 新创建的项目中的libs文件下 

 在build.gradle 里面android节点下增加

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

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com