Ccache is a compiler cache. 能极大的提升 clean build 编译效率。
github 上有一个为 CMake 提供 Ccache 集成的项目:https://github.com/TheLartians/Ccache.cmake,可以拿过来直接用。
在 cpp 库根目录的 CMakeLists.txt 中添加Ccache.cmake
,如:
src/main/cpp/CMakeLists.txt1 2 3 4 5 6 7 8 9 10 11
| cmake_minimum_required(VERSION 3.11.0)
project(myapp)
include(FetchContent) FetchContent_Declare(Ccache.cmake GIT_REPOSITORY https://github.com/TheLartians/Ccache.cmake.git GIT_TAG origin/master ) FetchContent_MakeAvailable(Ccache.cmake)
|
注意:FetchContent 最低要求 CMake 3.11
下载安装 Ccache: https://ccache.dev/download.html ,或者使用 Homebrew 安装:brew install ccache
安装完毕后,在 Android 项目的 build.gradle
中记得加上参数开启次模块功能:-DUSE_CCACHE=ON
,并给Ccache 加上选项-DCCACHE_OPTIONS=CCACHE_CPP2=true;CCACHE_SLOPPINESS=time_macros,locale,file_stat_matches
:
build.gradle1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' }
android { namespace 'com.example.myapplication' compileSdk 33
defaultConfig { applicationId "com.example.myapplication" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0"
externalNativeBuild { cmake { arguments "-DUSE_CCACHE=ON", "-DCCACHE_OPTIONS=CCACHE_CPP2=true;CCACHE_SLOPPINESS=time_macros,locale,file_stat_matches" } } }
externalNativeBuild { cmake { path file('src/main/cpp/CMakeLists.txt') version '3.22.1'
} }
}
|
开启 ccache stats log,clean build,日志如下表示命中缓存:
# ~/MyApplication/app/src/main/cpp/native-lib.cpp
direct_cache_hit
local_storage_hit
local_storage_read_hit
local_storage_read_hit
ENJOY it.