使用CMake编译NDK

Author Avatar
罗炜光 1月 02, 2017
  • 在其它设备中阅读本文章

新建Android项目

使用Android Studio2.2新建一个Android项目

在新建项目时勾选Include C++ Support

CMake支持

在Settings点击Appearance & Behavior -> System Settings -> Android SDK在右面板点击SDK Tools勾选CMake和LLDB,点击Apply即可

修改build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "luoweiguang.github.io.video"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        //选择要编译的CPU类型
        ndk {
            abiFilters 'x86', 'armeabi-v7a'
        }

        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    //设置CMakeLists.txt的路径,根路径是app
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    //选择so所放的目录。不填此项默认目录是在app/src/main/jniLibs
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'
}

修改CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

# 设置变量,方便底下使用
set(INC_DIR ${PROJECT_SOURCE_DIR}/src/main/cpp/include)
set(LINK_DIR ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})


# 添加so库对应的头文件目录
include_directories(${INC_DIR})

# 引入so库,IMPORT代表从第三方引入的意思
add_library( avcodec-57 SHARED IMPORTED)
# 设置编译的库文件存放的目录
set_target_properties( avcodec-57 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavcodec-57.so)

add_library( avfilter-6 SHARED IMPORTED)
set_target_properties( avfilter-6 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavfilter-6.so)

add_library( avformat-57 SHARED IMPORTED)
set_target_properties( avformat-57 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavformat-57.so)

add_library( avutil-55 SHARED IMPORTED)
set_target_properties( avutil-55 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavutil-55.so)

add_library( swresample-2 SHARED IMPORTED)
set_target_properties( swresample-2 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libswresample-2.so)

add_library( swscale-4 SHARED IMPORTED)
set_target_properties( swscale-4 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libswscale-4.so)


# 自己本地的代码所编译的库
add_library(
             # 库的名称
             native-lib
             SHARED
             # 编译文件的目录
             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 )

# 将其他库文件链接到native-lib上
target_link_libraries(
                       native-lib
                        avcodec-57
                        avfilter-6
                        avformat-57
                        avutil-55
                        swresample-2
                        swscale-4
                       ${log-lib})

注意事项

当调用的库为C代码写时,需要用

extern "C"
{
// #include <*>
//内容
}

其中{}也是必不可少的

例如:

#include <jni.h>
#include <string>

extern "C"
{
    #include <libavcodec/avcodec.h>

    JNIEXPORT jstring JNICALL
    Java_luoweiguang_github_io_video_MainActivity_getString(JNIEnv *env, jobject instance) {

        char info[10000] = { 0 };

        sprintf(info, "%s\n", avcodec_configuration());
        return env->NewStringUTF(info);
    }

}

参考资料

cmake 学习笔记(一)
Android Studio中的CmakeList NDK配置
最简单的基于FFmpeg的移动端例子:Android HelloWorld