Android uses Gradle script to delete jar packages, resources and configurations in AndroidManifest.xml file

Today, a channel has a special requirement. First, connect to their parent package SDK. After the parent package SDK is tuned and logged in, some jar packages of the parent package, resources and files under assets, and configurations in AndroidManifest.xml All activities, services, meta-data, etc. are deleted.

So I used the Gradle script command to execute the delete command during packaging. The specific code is as follows:

Then start writing commands at the bottom of build.gradle to perform the deletion task:

1. First define a variable, which is a switch, to facilitate our daily testing, and then some deletion code

//Delete the resources of the channel under the assets file at the request of the channel, redundant jar packages, and all configuration files under the application node in AndroidManifest project.afterEvaluate { //Switch to 
execute 
    the delete command 
    def isParentApk = false 
    if (isParentApk) { 
        //Traverse and delete assets/sdk folder and sub-files 
        deleteFile(project.file("src/main/assets/sdk")) 
        //Delete the parent package resource that is required to be deleted by the channel under the assets folder 
        deleteExists(project.file("src/main/assets /poolsdk.xml")) 
        deleteExists(project.file("src/main/assets/sdk.zip")) 
        deleteExists(project.file("src/main/assets/sdkconf.ini")) 

        //Delete under libs The channel requires the deletion of the parent package resource 
        deleteExists(project.file("libs/alipaySdk-20161222.jar")) 
        deleteExists(project.file("libs/applog-release.jar"))
        deleteExists(project.file("libs/GDTActionSDK.min.1.3.0.jar")) 
        deleteExists(project.file("libs/HuoshuSDK.jar")) 
        deleteExists(project.file("libs/libammsdk.jar") ) 
        deleteExists(project.file("libs/PlayMad-Conversion-Tracking-SDK.jar")) 
        deleteExists(project.file("libs/support-v4.jar")) 
        deleteExists(project.file("libs/trackingIOsdk. jar")) 
        //Delete all Activity, service and other configurations in the AndroidManifest.xml file 
        def xmlFile = project.file("src/main/AndroidManifest.xml") 
        def manifest = new XmlParser().parse(xmlFile) 
        def nodes = manifest.application[0].'*'.findAll { 
            //Select the node to delete
            (it.name() == 'meta-data' || it.name() == 'activity' || it.name() == 'service' || it.name() == 'receiver' || it.name() == 'uses-library')
        }
        for (int i = 0; i < nodes.size(); i++) {
            Node parentNode = nodes[i]
            manifest.children().each {
                it.remove(parentNode)
            }
        }
        def writer = new PrintWriter(xmlFile, "UTF8")
        writer.write('<?xml version="1.0" encoding="UTF-8"?>\n')
        new XmlNodePrinter(writer).print(manifest)
    }
}

def deleteExists(File file) {
    if (file != null && file.exists())
        file.delete()
}

def deleteFile(File file) {
    if (file.isDirectory()) {
        File[] files = file.listFiles()
        for (int i = 0; i < files.length; i++) {
            deleteFile(files[i])
        }
        file.delete()
    } else if (file.exists()) {
        file.delete()
    }
}

After executing the above code, the resources you need to delete can be deleted.

Guess you like

Origin blog.csdn.net/zhao8856234/article/details/100083531#comments_28217232