最近在做包体积优化,想要查找各个 .so 文件是来源于哪个第三方库的,方便排除。终于找到如下方法:
在 app 下 build.gradle 文件中追加以下代码:
// 打印出所有 .so 库的文件地址
tasks.whenTaskAdded { task ->
// 注意:如果是有多个 flavor,则改成 merge“Flavor”DebugNativeLibs 的形式
if (task.name=='mergeDebugNativeLibs') {
task.doFirst {
println("------------------- find so files start -------------------")
println("------------------- find so files start -------------------")
println("------------------- find so files start -------------------")
it.inputs.files.each { file ->
printDir(new File(file.absolutePath))
}
println("------------------- find so files end -------------------")
println("------------------- find so files end -------------------")
println("------------------- find so files end -------------------")
}
}
}
def printDir(File file) {
if (file != null) {
if (file.isDirectory()) {
file.listFiles().each {
printDir(it)
}
} else if (file.absolutePath.endsWith(".so")) {
println "find so file: $file.absolutePath"
}
}
}
注意:task.name 的判断条件那里,如果是有多个 flavor,则改成 merge『Flavor』DebugNativeLibs 的形式,比如我的 flavor 是 『Prod』,则改成 “mergeProdDebugNativeLibs”。
然后执行 gradle 命令:
./gradlew assemble
可以在编译日志中看到类似如下的输出:
Task :app:mergeDebugNativeLibs
------------------- find so files start -------------------
------------------- find so files start -------------------
------------------- find so files start -------------------
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\290908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\arm64-v8a\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\290908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\armeabi-v7a\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\290908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\x86\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\290908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\x86_64\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\b2adcba2625cf2da1029c207c31ac6ca\jetified-webpsupport-2.0.0\jni\arm64-v8a\libstatic-webp.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\b2adcba2625cf2da1029c207c31ac6ca\jetified-webpsupport-2.0.0\jni\armeabi-v7a\libstatic-webp.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\b2adcba2625cf2da1029c207c31ac6ca\jetified-webpsupport-2.0.0\jni\x86\libstatic-webp.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\b2adcba2625cf2da1029c207c31ac6ca\jetified-webpsupport-2.0.0\jni\x86_64\libstatic-webp.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\da48f63f1984c1712902b6f0ec8a5e47\jetified-klog-2.2.10-gradle-564\jni\arm64-v8a\libyylog.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\da48f63f1984c1712902b6f0ec8a5e47\jetified-klog-2.2.10-gradle-564\jni\armeabi-v7a\libyylog.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\aa7e29876f055d50ea8fd93c07422010\jetified-flowimagesdk-3.1.2\jni\arm64-v8a\libflowimagesdk.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\aa7e29876f055d50ea8fd93c07422010\jetified-flowimagesdk-3.1.2\jni\armeabi\libflowimagesdk.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\aa7e29876f055d50ea8fd93c07422010\jetified-flowimagesdk-3.1.2\jni\armeabi-v7a\libflowimagesdk.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\aa7e29876f055d50ea8fd93c07422010\jetified-flowimagesdk-3.1.2\jni\x86\libflowimagesdk.so
...
------------------- find so files end -------------------
------------------- find so files end -------------------
------------------- find so files end -------------------
例如:libgifimage.so,它出现在以下目录:
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.1\290908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\arm64-v8a\libgifimage.so
就说明它是由 jetified-animated-gif-2.0.0 引入的。