Filtering resources from the Play Services monolith to make your APK smaller

后端 未结 3 1208
花落未央
花落未央 2021-01-31 11:28

A lot has been written about the monolithic nature of the Google Play Services and why it should be split into more libraries. For now the workaround to keep your APK small is t

3条回答
  •  无人及你
    2021-01-31 11:48

    After some research I found the following sub-optimal solution. I had to list all the not needed resources manually (luckily patterns are applicable) and make sure also to delete all files where they are referenced. Below is an example which makes my Wear app APK from 1.5 MB into 300kb and the APK is working normally without issues.

    I had to create my own task stripResources and hook it between standard Android plug-in tasks: mergeReleaseResources and processReleaseResources.

    task stripResources << {
        println "Custom resource stripping in: $buildDir"
        delete fileTree(dir: "$buildDir", include: "**/layout/confirmation_activity_layout.xml")
        delete fileTree(dir: "$buildDir", include: "**/layout/watch_card_content.xml")
        delete fileTree(dir: "$buildDir", include: "**/common_signin*.png")
        delete fileTree(dir: "$buildDir", include: "**/drawable/common_signin*.xml")
        delete fileTree(dir: "$buildDir", include: "**/generic_confirmation*.png")
        delete fileTree(dir: "$buildDir", include: "**/drawable/confirmation_*.xml")
        delete fileTree(dir: "$buildDir", include: "**/drawable/card_background.xml")
        delete fileTree(dir: "$buildDir", include: "**/card_frame*.png")
        delete fileTree(dir: "$buildDir", include: "**/go_to*.png")
        delete fileTree(dir: "$buildDir", include: "**/drawable/go_to_*.xml")
        delete fileTree(dir: "$buildDir", include: "**/ic_plusone*.png")
        delete fileTree(dir: "$buildDir", include: "**/powered_by_google*.png")
        // if you only have English you can teh following to filter out some GPS texts wich also take few hundreds of kb
        // delete fileTree(dir: "$buildDir", include: "**/values-*/values.xml")
    }
    
        tasks.whenTaskAdded { task ->
            if (task.name == 'processReleaseManifest') {
                task.dependsOn stripResources
            }
        }
    

    You can do a similar task for regular Android APK.

提交回复
热议问题