Android Studio cmake和jni的一些坑

    xiaoxiao2021-03-25  62


    1.路径

    修改前CMakeLists.txt动态库配置部分

    add_library( avcodec-57 SHARED IMPORTED ) set_target_properties( avcodec-57 PROPERTIES IMPORTED_LOCATION libs/armeabi/libavcodec-57.so )

    执行后出错

    Error while executing 'D:\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build E:\MyApplication2\app\.externalNativeBuild\cmake\debug\armeabi --target native-lib} ninja: error: 'libs/armeabi/libavcodec-57.so', needed by '../../../../build/intermediates/cmake/debug/obj/armeabi/libnative-lib.so', missing and no known rule to make it

    在提示信息可以看出编译时的路径为

    E:\MyApplication2\app\.externalNativeBuild\cmake\debug\armeabi

    因此在CMakeList.txt中增加一个配置,即CMakeList.txt所在的目录

    set(CURRENT_DIR ${CMAKE_SOURCE_DIR}) //在Gradle Console输出信息 //CMAKE_SOURCE_DIR: E:/MyApplication2/app message("CURRENT_DIR:" ${CMAKE_SOURCE_DIR}) add_library( avcodec-57 SHARED IMPORTED ) set_target_properties( avcodec-57 PROPERTIES IMPORTED_LOCATION ${CURRENT_DIR}/libs/armeabi/libavcodec-57.so )

    2.使用cpp开发时的extern “C”

    //file native-lib.cpp #include <jni.h> #include <string> //用extern "C"将C库的头文件和代码用{}括起来 extern "C" { //ffmpeg使用c开发,所以头文件要用extern "C"声明 #include "libavcodec/avcodec.h" jstring Java_com_jesure_myapplication_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { char info[10000] = {0}; sprintf(info, "%s\n", avcodec_configuration()); //cpp中的JNIEnv是一级指针,在c中是二级指针 //在c中下面语句写为 return (*env)->NewStringUTF(info); return env->NewStringUTF(info); } }
    转载请注明原文地址: https://ju.6miu.com/read-34701.html

    最新回复(0)