美文网首页
NDK开发-1环境配置

NDK开发-1环境配置

作者: proud2008 | 来源:发表于2019-12-24 16:46 被阅读0次

https://www.imooc.com/video/20907

在已有项目中开发ndk

应用的build.gradle文件修改


android {
      defaultConfig {
       //...
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }
    }
 
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"  //路径
       }
    }
}

defaultConfig cmake 解析

cppFlags 
arguments "-DANDROID_TOOLCHAIN=clang"
abiFilters "armeabi-v7a", "armeabi","x86"
cppFlags "-std=c++11 -frtti -fexceptions"
arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native/jni" // , "-DANDROID_ARM_NEON=TRUE"

新建项目默认的 CMakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

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).
             native-lib.cpp )

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

# 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} )

详解 CMakeLists.txt

1、message

set(var 1111) # 设置变量 第一个 变量名 第二个参数 变量值
message(WARNING ${var})  # ${变量名} 取出变量值  WARNING  打印级别
#[message 不打印的问题 https://www.jianshu.com/p/80482e90eb33](https://www.jianshu.com/p/80482e90eb33)

gradle 任务 externalNativeBuildDebug 可打印显示
image.png

在不修改CMakeLists.txt文件时再次执行是不会打印出message的,此时可以删除这两件文件夹


image.png
message(WARNING  log-lib ${log-lib})
log-lib 所指定的文件路径
D:/Android/android-ndk-r16b/platforms/android-21/arch-x86_64/usr/lib64/liblog.so

内置变量

message(WARNING 22 ${CMAKE_CURRENT_LIST_DIR})
message(WARNING "Dir=${CMAKE_CURRENT_LIST_DIR}")
CMAKE_CURRENT_LIST_DIR 当前CMakeLists.txt所在的文件目录 .../app/src/main/cpp
//可用于 include_directories("${CMAKE_CURRENT_LIST_DIR}")

file方法

查找文件

file(GLOB srcs *.cpp *.c)  # 查找当前目录下的.cpp .c  文件保存在srcs变量中,是绝对路径
file(GLOB srcs *.cpp *.c aa/*.cpp) #若有查找指定目录下的带路径 如aa/*.cpp
file(GLOB hdrs *.hpp *.h)
add_library(demo ${srcs } ${hdrs }) #查找到的文件用于library

aux_source_directory(. srcs) # 搜索当前目录下的所有.cpp文件存到变量srcs中,是aux_source_directory返回的是相对路径
aux_source_directory(aa srcs) # 搜索aa目录下的所有.cpp文件

find_package

cmake find_package说明

find_package(OpenCV)
message(WARNING  "OpenCV=${OpenCV_FOUND},${OpenCV_INCLUDE_DIRS}")
if(OpenCV_FOUND)
    message(WARNING ${OpenCV_INCLUDE_DIRS})
else()
    message(WARNING OpenCV not Found)
endif()

输出如下
debug|x86_64 :-- Found OpenCV: C:/Users/Administrator/Desktop/OpenCV-android-sdk (found version "4.2.0") found components:  opencv_calib3d opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs opencv_imgproc opencv_ml opencv_objdetect opencv_photo opencv_stitching opencv_video opencv_videoio 
debug|x86_64 :CMake Warning at CMakeLists.txt:22 (message):
debug|x86_64 :  OpenCV=1,C:/Users/Administrator/Desktop/OpenCV-android-sdk/sdk/native/jni/include
debug|x86_64 :CMake Warning at CMakeLists.txt:23 (message):
debug|x86_64 :  C:/Users/Administrator/Desktop/OpenCV-android-sdk/sdk/native/jni/include
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
#从OpenCV 中查找opencv_java库 
#REQUIRED表示如果没有找到,cmake会停止处理,并报告一个错误.
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
message(WARNING  "OpenCV=${OpenCV_FOUND},${OpenCV_INCLUDE_DIRS}")

find_library

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)

log-lib 变量名 log要查找的库

package 与library的区别

Python入门之面向对象module,library,package之间区别
package 中包含多个library

include_directories

参考include_directories和find_package

是用来提供找头文件路径的,打个比方,我现在想要#include"cv.h",但是这个cv.h的路径是/usr/local/include/opencv,那么我总不能在主函数头前写
#include “/usr/local/include/opencv/cv.h”吧,这个时候就用到include_directories了,它提供了一个搜索头文件暂时的根目录,即你可以在cmakelists中写上
include_directories(/usr/local/include)来让库文件搜索以/usr/local/include为基础,即在main函数前写上#include “opencv/cv.h"即可

include_directories(aa)
image.png

c++ 11标准支持

set( CMAKE_CXX_FLAGS "-std=c++11" )//添加c++ 11标准支持

基他方法

参考CMakeLists.txt 语法介绍与实例演练

相关文章

  • android ndk 文章汇总

    NDK开发-1环境配置NDK开发-2-数据交换NDK开发-3回调 返回java实例ndk开发-4 引用类型ndk开...

  • NDK开发之HelloWorld

    1.配置NDK开发环境(OS X系统) 1) 首先,我们的下载NDK,下载地址是:NDK开发工具。 2)下载完成后...

  • Android Studio NDK开发-环境配置

    Android Studio NDK开发-环境配置 NDK全称Native Development Kit。NDK...

  • Android gradle 命令行打包

    1.项目配置gradle环境 Mac Android开发环境变量的配置(java、sdk、ndk、gradle) ...

  • Android NDK开发之环境搭建

    工欲善其事,必先利其器。要想进行Android NDK开发首先我们载NDK的开发包,配置NDK开发环境,就像配置S...

  • JNI相关知识点

    1、Android NDK开发:JNI基础篇 2、Android Studio NDK环境配置及JNI使用方法 3...

  • 简单的As中NDK demo

    1.AS的NDK Android Studio1.4.x JNI开发基础-基本环境配置 NDK-JNI实战教程(一...

  • 用ndk-build编译NDK程序

    1. 配置Android NDK编译环境 1.1 配置NDK 首先下载NDK软件包,并解压: 设置NDK的环境变量...

  • Android(Java):搭建Android ndk开发环境

    目录 (?) [+] 配置NDK环境变量 用NDK来编译程序 在eclipse中集成cc开发环境 配置CC的编译器...

  • Hello NDK!

    NDK 开发变得越来越重要,接下来介绍一下 Android Studio 下 NDK 开发环境配置:功能需求:1....

网友评论

      本文标题:NDK开发-1环境配置

      本文链接:https://www.haomeiwen.com/subject/tysfoctx.html