android jni相关

Posted on 2018-08-02 09:42  yacbo  阅读(138)  评论(0编辑  收藏  举报

https://blog.csdn.net/zeqiao/article/details/77893167

一、在原有旧项目中引入

1.新建C/C++源码文件夹和文件

  main>>cpp 文件夹下加源码

2.编写CMakeLists.txt文件

# 设置构建本机库文件所需的CMake的最小版本
cmake_minimum_required(VERSION 3.4.1)
#添加自己写的C/C++源文件
add_library( demo-lib
             SHARED
             src/main/cpp/demo-lib.cpp )
#依赖NDK中的库
find_library( log-lib
              log )
#将目标库与NDK中的库进行连接
target_link_libraries( demo-lib
                       ${log-lib} )                   

3.配置build.gradle文件

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                // 默认是 “ cppFlags "" ”
                // 如果要修改 Customize C++ Support 部分,可在这里加入
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        ...
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    ...
}

注意点:①要在哪个类运用 JNI,就得加载相应的动态库.  ②快速生成代码:Alt+Enter  ③新建 C/C++ 源代码文件,要添加到 CMakeLists.txt 文件中


 CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

#设置生成的so动态库最后输出的路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

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 )

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

  

Copyright © 2024 yacbo
Powered by .NET 9.0 on Kubernetes