【JNI】如何使用AndroidStudio-Cmake

随着gradle的更新,对jni的支持越来越完善,大部分Android开发对于jni及C/C++的开发比较头疼,因为没有语法提示或者方法类名索引等。
原本我的几个项目,都是在jni目录定义的android.mk和application.mk,由于老版本的gradle对“-std=c++11”库支持不够,所以虽然能够编译,但是没有语法高亮索引等,十分不爽,gradle有出gradle-experimental版本是,对C++编译库的支持增多了,但当时嫌麻烦,最近在AS3.0和Gradle 4.1的使用中意外发现新创建工程对Native库的支持,所以学习下,有不足之处欢迎一起交流。

参考文档

Cmake使用条件:

  1. 首先检查自己的 Android Studio2.2以上及Android Plugin for Gradle 版本 2.2.0 或更高版本
  2. Android Studio目前默认的构建工具是Cmake
  3. 在 build.properties里添加android.useDeprecatedNdk = true
  4. NDK工具
  5. CMake工具,可以在Android Studio中选择下载
  6. LLDB:一种调试程序,可以在Android Studio中选择下载, 使用它来调试原生代码。

ndk_tools.png

Cmake是什么

“CMake”即“cross platform make”,跨平台安装工具,可以通过简单的命令语句来执行构建,编译,生成的操作,他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。构建的组态档为CMakeLists.txt文件,CMake 支持 in-place 建构(二进档和源代码在同一个目录树中)和 out-of-place 建构(二进档在别的目录里),因此可以很容易从同一个源代码目录树中建构出多个二进档。CMake 也支持静态与动态程式库的建构。

新的Cmake使用

######有兴趣的同学可以去研究AS对C++库的支持

  • 使用CMake有2种情况,

    • 一直是创建项目,新版AS创建流程中可以自己去选择ndk-build和cmake构建2个版本,很简单,自己去尝试
    • 第二种是原有的native项目添加支持,接下来详细介绍这种
      • 添加ndk-build支持时,只需要在gradle Android 目录下添加

           externalNativeBuild {
              ndkBuild {
                  path 'src/main/jni/Android.mk'  你的android.mk目录,application.mk放在同目录即可被关联
              }
          }
        
      • 添加cmake支持,同理,在gradle Android 指定CMakeLists.txt的目录

           externalNativeBuild {
              cmake {
                  path "CMakeLists.txt"
              }
          } 
        
  • CMake在gradle的配置简介

      android {
        defaultConfig {    
          // This block is different from the one you use to link Gradle
          // to your CMake or ndk-build script.
          externalNativeBuild {
            // For ndk-build, instead use ndkBuild {}
            cmake {
              // Cmake可选参数
              arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
      
              // 选择C编译的标记,选一个即可
              cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2"
      
              // C ++宏常量格式
              cppFlags "-D__STDC_FORMAT_MACROS"
            }
          }
      	 ndk {
            // 指定编译的ABI,选择你项目需要的abi即可
            abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a',
                         'arm64-v8a'
          }
        }
      
        buildTypes {...}
      	//分包打包,可指定不同的编译版本
        productFlavors {   
          one {
            externalNativeBuild {
              cmake {
                targets "native-lib-1"
              }
            }
          }
          two {     
            externalNativeBuild {
              cmake {        
                targets "native-lib-2"
              }
            }
          }
        }
          // 你的cmake地址
          externalNativeBuild {
      		cmake {
      			path "CMakeLists.txt"
      		}
          } 
      }
    

Cmake语法介绍

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)

# 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.

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 
			 
			 # 可以使用${ANDROID_NDK}替代符指定路径
			 ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c 
			 )
			 

# 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.
# 引用 NDK 库
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 )
		  
			  
# 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} )
					   
#添加其他预构建库	您需要使用 IMPORTED 标志告知 CMake 您只希望将库导入到项目中 然后,您需要使用 set_target_properties() 命令指定库的路径	   
add_library( imported-lib
             SHARED
             IMPORTED )		

#某些库为特定的 CPU 架构(或应用二进制接口 (ABI))提供了单独的软件包,
#并将其组织到单独的目录中。此方法既有助于库充分利用特定的 CPU 架构,又能让您仅使用所需的库版本。
#要向 CMake 构建脚本中添加库的多个 ABI 版本,而不必为库的每个版本编写多个命令,
#您可以使用 ANDROID_ABI 路径变量。此变量使用 NDK 支持的一组默认 ABI,
#或者您手动配置 Gradle 而让其使用的一组经过筛选的 ABI。例如:
add_library(...)
set_target_properties( # Specifies the target library.
                       imported-lib

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so )		

# add_library() 向您的 CMake 构建脚本添加源文件或库时,
# Android Studio 还会在您同步项目后在 Project 视图下显示关联的标头文件。
#不过,为了确保 CMake 可以在编译时定位您的标头文件,
#您需要将 include_directories() 命令添加到 CMake 构建脚本中并指定标头的路径
include_directories(src/main/cpp/include/)

#要将预构建库关联到您自己的原生库,请将其添加到 CMake 构建脚本的 target_link_libraries() 命令中:
target_link_libraries( native-lib imported-lib app-glue ${log-lib} )					   

猜你喜欢

转载自blog.csdn.net/sslinp/article/details/83412272
今日推荐