Android jni开发

推荐博客:http://blog.csdn.net/kgdwbb/article/details/72810251
jni:java和c互调
ndk:
环境配置,创建Android project 支持c++,不再多说,创建的时候可以看出来
创建成功后注意几个地方:
1、CMakeLists、2、jni文件夹、3、build.gradle、4、MainActivity中的两个地方

一、MainActivity中

System.loadLibrary(“native-lib”)
括号里面是你so库的名字,这个是在CMakeLists中配置的
第二个地方
private native string getString(){
}
这个方法native方法,编译又两个方式,一个是通过alt+enter弹出,通过cmake自动创建到nativi-lib.cpp中,一个是通过右键点击MainActivity,弹出NDK-->javah,这样会直接创建到src/main/jni目录中一个  包名.h文件,然后在编写.cpp文件

二、jni目录

这个里面存放的是通过javah编译出来的.h文件和你创建的.cpp文件

三、build.gradle

这个里面需要看两个地方
第一个地方:配置CMakeLists路径
externalNativeBuild {
    cmake {
        path 'src/main/jni/CMakeLists.txt'
    }
}
第二个地方
defaultConfig {
    。。。

    externalNativeBuild {
        cmake {
            //abiFilters 'arm64-v8a','armeabi', 'armeabi-v7a','mips','mips64','x86','x86_64'
            abiFilters 'x86','arm64-v8a'

            arguments '-DANDROID_TOOLCHAIN=clang',
                    '-DANDROID_STL=gnustl_static',
                    '-DANDROID_PLATFORM=android-9'
        }
    }
}

四、CMakeLists

关注三个点
1、cmake_minimum_required(VERSION 3.4.1)
设置最小版本
2、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/jni/com_example_bai_jnidemo_Util.cpp )
第一点:Sets the name of the library.
添加so库,这个里面native-lib这个就是自己创建的库的名字,当你进入app后需要System.loadLibrary(“native-lib”);这里填写自己库的名字;
第二点:Sets the library as a shared library.
这个有两个一个是STATIC一个是SHARED,分别交静态链接库和动态链接库
http://blog.csdn.net/qhairen/article/details/45644117


第三点:Provides a relative path to your source file(s).
这个是将自己要编译的c cpp 等文件添加到这里
3、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 )
这个是搜索三方的so库            
4、target_link_libraries( # Specifies the target library.
                       native-lib
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

猜你喜欢

转载自blog.csdn.net/androidwubo/article/details/79595557
今日推荐