背景
Android 开发中,我们都知道引入依赖是这样的。
dependencies {
implementation "com.arthenica:mobile-ffmpeg-full:4.3.1.LTS"
}
复制代码
那其中的 implementation
就被称之为 configuration 了。
而根据 官方文档 ,configruation 支持自定义。
用处
自定义 configruation 有什么用?
比如有这么一个场景,我需要在工程中添加一些 ffmpeg 的 so 文件用于 native 代码的 link。最简单的实现方法就是将编译好的动态库放到对应的文件夹中就好啦。
但是 so 文件放在项目中就需要随着版本控制,进入到 git 仓库中,这可能就不是个好主意了。
那还能怎么办呢?
- 4.0 agp 引入的 prefab build feature. (这一点暂不做讨论)
- 使用自定义的 configuration. 将下载后的 aar 解压后复制到相应的文件夹。
用法
对于上述 case,于是很容易就能写出下面的逻辑:
configurations {
nativeDeps
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
nativeDeps 'com.arthenica:mobile-ffmpeg-full:4.3.1.LTS'
}
// COPY prebuilt ffmpeg libs to cpp/lib/
afterEvaluate {
def libPath = projectDir.absolutePath + "/src/main/cpp/lib"
def tempPath = buildDir.absolutePath + "/nativeDeps/ffmpeg"
copy {
from(zipTree(configurations.nativeDeps.files.first()))
into(tempPath)
}
copy {
from(tempPath + "/jni/")
into(libPath)
}
}
复制代码
于是 gradle configuration 阶段时, mobile-ffmepg 就能通过自定义的 configration nativeDeps
下载下来,然后解压并复制到项目中的动态库的 link 目录啦。
其他用法
这个项目中 JakeWharton 就是通过自定义 configuration 来下载 R8 来将编译生成的 jar 进行混淆操作。(俺就是从这偷师的)
完
(逃