Android Studio与CMake

2017-08-01  本文已影响1241人  daking

概述

工具

创建支持C/C++的新项目

  1. 在向导的Configure your new project部分,选中Include C++ Support复选框。

    导入C++支持
  2. 在向导的Customize C++ Support部分,可自定义C++支持。

    自定义C++支持-w380

    C++ Standard:使用哪种C++标准,默认为Toolchain Default,即使用默认的CMake设置。

    Exceptions Support:启用对C++异常处理的支持。Android Studio会将-fexceptions标志添加到CMake的gradle配置中。

    Runtime Type Information Support:支持RTTI。Android Studio会将-frtti标志添加到CMake的gradle配置中。

  3. 创建项目成功后,可看到项目/app下有一个CMakeLists.txt。它是CMake构建脚本,指定C/C++代码文件的编译。

  4. 同时可看到项目/app/build.gradle中有与CMake相关的gradle配置。

    android {
        defaultConfig {
            externalNativeBuild {
                cmake {
                    cppFlags "-frtti -fexceptions"
                }
            }
        }
        
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    

CMake构建脚本

指定CMake的版本号

# Sets the minimum version of CMake required to build your native library.
cmake_minimum_required(VERSION 3.4.1)

创建原生库

add_library( # Sets the name of the library.
             nativecode

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/api.c
             src/main/cpp/entrance.c )
add_library( app-glue
             STATIC
             ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )

指定头文件的路径

# Specifies a path to native header files.
include_directories(src/main/cpp/include/)

添加NDK API

# 此处为引用Android的日志支持库log,并将其路径存为log-lib变量。
find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

关联库

# Links your native library against one or more other native libraries.
target_link_libraries( native-lib app-glue ${log-lib} )

添加其他预构建库

add_library( imported-lib
             SHARED
             IMPORTED )
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.
                       src/main/cpp/ext/imported-lib/${ANDROID_ABI}/libimported-lib.so )
include_directories( src/main/cpp/ext/imported-lib/include/ )
target_link_libraries( native-lib app-glue ${log-lib} imported-lib )

Gradle配置

Gradle与原生库关联

android {
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

指定可选配置

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                // 传递可选参数给CMake
                arguments "-DANDROID_PLATFORM=android-8", "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
                // 为C编译器设置可选标志
                cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2"
                // 设置标志来支持C++编译器的格式化宏常量。
                cppFlags "-frtti -fexceptions", "-D__STDC_FORMAT_MACROS"
            }
        }
    }
}
android {
    productFlavors {
        demo {
            externalNativeBuild {
                cmake {
                    targets "native-lib-demo"
                }
            }
        }
    }
}

指定ABI

android {
    defaultConfig {
        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'x86'
        }
    }
}
android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                abiFilters 'armeabi', 'armeabi-v7a', 'x86'
            }
        }
    }
}

CMake的单元测试

@RunWith(AndroidJUnit4.class)
public class NDKUtilTest {
    
    @Test
    public void test() {
        String libName = NDKUtil.getName();
        Log.e("NDKUtil", libName);
    }
}

我的博客

上一篇 下一篇

猜你喜欢

热点阅读