大家都知道在eclipse上进行ndk开发光是编译C代码就很蛋疼,还好android studio的出现改变了现状,然而,在android studio 3.0以前进行ndk开发,也是各种配置总的来说用着很不爽,如今的android studio作了优化升级,不仅仅配置简化了,还直接引入cmake等功能,终于解放了双手。

一、android studio3.3.2版本演习

1、环境配置:

  • 导入ndk等工具:Tools->SDK Manager->SDK Tools:选中LLDB、CMake、NDK

  • 设置NDK安装路径:File->Project Structure->Android NDK location 选中默认路径

  • 检查NDK路径:local.properties里面的ndk.dir在sdk路径里面,默认ndk-bundle

2、创建NDK项目:

  • 创建native工程:

创建一个native c++工程:

有些时候可能会是下面这样的,这个时候只需要选中include c++ support就行了

  • native工程目录:原来android studio已经帮我们把所有的配置都设置好了,build.gradle里面的配置如下,这其实不需要我们更改。而cpp文件夹也已经帮我们创建了,里面除了有cpp文件之外,还有一个includes和CMakeLists.txt

3、CMakeLists.txt

该文件其实是一个cmake的脚步。除去注释部分,其实核心的内容并不是很多

# 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).
             src/main/cpp/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} )
  • cmake_minimum_required(VERSION 3.4.1):用来设置在编译本地库时我们需要的最小的cmake版本,AndroidStudio自动生成,我们几乎不需要自己管。
  • add_library:用来设置编译生成的本地库的名字为native-libSHARED表示编译生成的是动态链接库(这个概念前面已经提到过了),src/main/cpp/native-lib.cpp表示参与编译的文件的路径,这里面可以写多个文件的路径。如下将两个cpp文件都编译到动态链接库dpc-lib中,注意在java调用native方法之前需要加载的动态库跟这里的名字保持一致 static { System.loadLibrary("dpc-lib");}
     
  • find_library:是用来添加一些我们在编译我们的本地库的时候需要依赖的一些库,由于cmake已经知道系统库的路径,所以我们这里只是指定使用log库,然后给log库起别名为log-lib便于我们后面引用,此处的log库是我们后面调试时需要用来打log日志的库,是NDK为我们提供的。

target_link_libraries:是为了关联我们自己的库和一些第三方库或者系统库,这里把我们把自己的库native-lib库和log库关联起来。

4、includes

这是一个包含很多头文件的库,这些头文件定义了很多c++系统函数或者android 框架层下的一些函数,这样更加方便了我们的ndk开发

例如在android/log.h文件中提供了一些在c++里面打印日志的函数,在c++中我们可以试着利用这些方法来向logcat打印日子:

#include <jni.h>
#include <string>
//引入log.h头文件
#include <android/log.h>           
extern "C" JNIEXPORT jint JNICALL
Java_shen_com_myapplication_MainActivity_printfLog(JNIEnv* env,jobject thiz) 
{
    //参数1是LOG等级,参数二是TAG,参数三表示后面跟字符串
    __android_log_print(ANDROID_LOG_INFO, "SHEN", "%s", "Hello, World");
    return 0;
}

 

相关文章:

  • 2021-11-24
  • 2021-12-16
  • 2021-04-18
  • 2021-08-14
  • 2021-11-04
  • 2021-11-01
  • 2021-04-07
猜你喜欢
  • 2021-07-08
  • 2021-04-11
  • 2021-11-04
  • 2021-10-25
  • 2021-06-01
  • 2021-05-13
  • 2021-09-07
相关资源
相似解决方案