android studio introduces class jar package

In our development process, we will inevitably use other people's code, which includes referencing jar packages and referencing third-party engineering projects. For some specific projects of some companies, the modification of class.jar may be involved. The following simply records the relevant operations involving class.jar modification in the project:

The project I recorded referenced third-party projects and also modified class.jar. Here only the process of introducing modifications to third-party class.jar is recorded

Import the class.jar to be replaced into the libs directory of the project Module

Modify the Scope type of the Dependencies of the jar package in the Module to Provided. As 2.0 is Provided, but 3.0 is compileOnly, just pay attention

Add the following code to the project build.gradle

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs.add('-Xbootclasspath/p:autoLinkBT\\libs\\classes.jar')
        options.compilerArgs.add('-Xbootclasspath/p:autoLinkBTModel_X55\\libs\\classes.jar')
    }
}

The last step is to move the line whose type is jdk in the orderEntry in the app.iml file to the last line

The line marked should be at the top of the orderEntry category. After moving to the bottom, the program will actively search for the content in the class.jar package, otherwise it will find the class in the android.jar package. But there is still a problem with this, that is, after each synchronization, this file will be restored again. Is there a good skin? !

As the saying goes, if there is a problem, it is used to solve it. The following is the solution. After each update, we force it to return to the bottom

Add the following paragraph to the build.gradle of the module

Below is the code that can be reproduced as usual, so I can’t work hard for you, maybe including myself in the future

preBuild {
    doLast {
        def imlFile = file(project.name + ".iml")
        println 'Change ' + project.name + '.iml order'
        try {
            def parsedXml = (new XmlParser()).parse(imlFile)
            def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
            parsedXml.component[1].remove(jdkNode)
            def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
            new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
            groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
        } catch (FileNotFoundException e) {
            // nop, iml not found
        }
    }
}

The record is here, reviewing the past to learn the new.

Guess you like

Origin blog.csdn.net/hehota/article/details/83746421