Android uses gradle to print So library address

Time:2022-5-6

reference resources:[Android development] how to quickly know which library a so comes from

Recently, I’m doing package volume optimization. I want to find each Which third-party library does the so file come from, so it is convenient to exclude it. Finally found the following method:

Under app, build Add the following code to the gradle file:

//Print out all File address of so Library
tasks.whenTaskAdded { task ->
    //Note: if there are multiple flavors, change to the form of 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"
        }
    }
}

Note: task In the judgment condition of name, if there are multiple flavors, it will be changed to the form of merge “flavor” debugnativelibs. For example, if my flavor is “prod”, it will be changed to “mergeproddebugnativelibs”.

Then execute the gradle command:

./gradlew assemble

You can see the following output in the compilation log:

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.10908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\arm64-v8a\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.10908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\armeabi-v7a\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.10908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\x86\libgifimage.so
find so file: D:\Gradle_Repo\caches\transforms-2\files-2.10908405aa37ee091b2a987a24aa9d0\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 -------------------

For example: libgifimage So, which appears in the following directory:

find so file: D:\Gradle_Repo\caches\transforms-2\files-2.10908405aa37ee091b2a987a24aa9d0\jetified-animated-gif-2.0.0\jni\arm64-v8a\libgifimage.so

It shows that it was introduced by jetified-animated-gif-2.0.0.

Recommended Today

Crash tracking journey of IOS development (I)

Preface: Recently, I encountered a crash blood disaster caused by crash in daily development. In a release in early May, the crash rate of the app developed by the author was directly increased from one thousand to nearly two thousand. At that time, the project leader just needed to report the relevant situation of project […]