Delete the so library during Android gradle packaging APK process

In android plug-in development, it is necessary to delete specific so libraries in the process of packaging the host APK

At this time, you need to configure in gradle:

afterEvaluate {
    print("deleteSoLibrary task start")
    def customer = tasks.findByName("deleteSoLibrary") // 自定义的 Task
    def merge = tasks.findByName("mergeReleaseNativeLibs") // gradle Task:收集项目所有的 native 库
    def strip = tasks.findByName("stripReleaseDebugSymbols")
    if (merge != null) {
        customer.mustRunAfter(merge)
        strip.dependsOn(customer)
    }
}

task(deleteSoLibrary) {}.doLast {
    println("deleteSoLibrary insert")
    println(getRootProject().findAll())

    def file = new File("${projectDir}/build/intermediates/merged_native_libs/release/out/lib")
    if (file.exists()) {
        file.listFiles().each { jiagou ->
            if (jiagou.isDirectory()) {
                jiagou.listFiles().each { target ->
                    // 可对文件名进行检索,以删除特定的so文件
                    target.delete()
                    println("delete ${jiagou.name} ${target.name}")
                }
            }
        }
    } else {
        println("There is no so library")
    }
}

The above sample code is to delete all so libraries during the process of creating the Release package. If you need to delete a specific so library, you can make a corresponding judgment in the search file


The principle is to insert a custom Task to operate the so library after gradle completes a Task (mergeReleaseNativeLibs: collect all the native libraries of the project).

Extension: After executing mergeReleaseNativeLibs separately, all so libraries can be found in the build directory

Guess you like

Origin blog.csdn.net/weixin_47592544/article/details/129622580