失效链接处理 |
Android java和C互相调用 PDF 下蝲
本站整理下蝲Q?nbsp;
链接Q?a target="_blank">https://pan.baidu.com/s/1k325UbaEulmweOgpErPJKQ
提取码:(x)c5ij
相关截图Q?/strong>
![]()
主要内容Q?/strong>
一QCMakeLists
英文文档Qhttps://cmake.org/documentation/
中文文Qhttps://www.zybuluo.com/khan-lau/note/254724
1Q\径配|,在buildGradle的android层目中进行配|例?
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
path配置cmake的引用\径,version配置cmake的版?/div>
2Q添加库Q在cmakeList中调用add_library函数
里面传入三个参数
W一个是要引入的库别名,W二个是库的cdQ是?rn)态库q是动态库。在Android中是不能引用?rn)态库的也是.dll后缀的库Q所以第二个参数只要是引入第三方?so库都是写“SHARED”Q第三个是通过什么样方式引入q来Q第三方的一般都是通过包含q来Q所以第三个参数基本也是固定的都是写“IMPORTED”?/div>
add_library后,p讄.so的详l\径了(jin)Q通过set_target_properties()函数来设|?该函C是要传入三参数来指定.so库的路径。第一个参数和add_library的第一个参CP不过q里的库别名要和add_library的库别名要一_(d)要不然在~译时会(x)报找不到库的错误。第二个参数是固定的Q都是写“ PROPERTIES IMPORTED_LOCATION”主要用来指定库的引入方式。都是通过本地引入。第三个是库的具体路径Q这个不能写错,如果写错?jin),~译时也同样?x)找不到库的。只要是引入W三方的库用add_libraryp使用set_target_propetiesq个l合Q所以它们是成对出现的?/div>
3Q编译我们自己在目中写的c/c++文g
调用方式和上面的add_libraryҎ(gu)cMQ前两个参数和上面传入的是一L(fng)Q最后一个参数写对应我们的c++的文件\径,可以写多个\径,如下:
add_library( # Sets the name of the library.
main-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
main-lib.cpp
runoobtest.cpp)
q里׃用set_target_propeties()
4,下面一个函数是find_library
q个Ҏ(gu)是用来寻扄l库的,如果有用用到pȝ库就要调用这个函?/div>
W一个参数是库的别名Q第二个参数表示该库在系lndk中的名字如下:
# 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)
5,下一个方法是target_link_libraries,依赖的库
如果是系l的库要用这个格?{库的名字}?/div>
如下:
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
target_link_libraries( # Specifies the target library.
main-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
?java调用层C代码程
W一U方式静(rn)态注?
1,首先加蝲׃n库,加蝲库一般是在在?rn)态代码块中进行:(x)
static {
System.loadLibrary("native-lib");
}
2Q在Java中声明nativeҎ(gu)
public native String stringFromJNI();
3Q实现原生方?/div>
#include <jni.h>
#include <string>
#include <android/log.h>
extern "C" JNIEXPORT jstring JNICALL
Java_com_test_jni_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
__android_log_print(ANDROID_LOG_ERROR,"TEST","HELLO ....... native lib");
return env->NewStringUTF(hello.c_str());
}
Z(jin)扑ֈc代码中对应的Ҏ(gu)Q需要要Java关键字开_(d)在方法前面加上包名,q且以下划线隔开。上面的stringFromJjni声明在了(jin)com.test.jniq个包名的MainActivity里面?/div>
q样在java代码里就可以调用stringFromJniq个Ҏ(gu)?/div>
W二U方?动态注?/div>
1Q和?rn)态注册一P先加载共享库
static {
System.loadLibrary("dynamic-lib");
}
2Q在java中声明nativeҎ(gu)
public native int sum(int x, int y);
public native String getNativeString();
3Q在c代码中中d回调函数JNI_OnLoadҎ(gu)QloadLibrary?x)执行该?gu)?/div>
然后在JNI_OnLoadҎ(gu)中调用方法RegisterNatives完成注册了(jin)如下:
JNIEXPORT int JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
|