android studio 引入class jar包

我们开发过程中,不可避免的会用到他人的代码,这其中有引用jar包和引用第三方工程项目。而对于一些公司的一些特定项目,可能会涉及到class.jar的修改。下面简单记录下工程中涉及class.jar修改情况下的相关操作:

我所记录的项目有引用第三方项目,同时也有class.jar修改。此处只记录第三方class.jar引入修改的过程

将要替换的class.jar引入项目Module的libs目录下

修改Module中jar包的Dependencies的Scope类型为Provided。as 2.0为Provided,但是3.0为compileOnly,注意就好

在项目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')
    }
}

最后一步,将app.iml文件中的orderEntry中的type为jdk的那一行移动到最后一行

标记这一行本来应该是在orderEntry这一类的最上面的,移动到下面后,程序运行才会主动去找class.jar包中的内容,否则会找android.jar包中的类。但是这样还有一个问题,就是每次同步后,这个文件会再次还原,好皮有没有?!

俗话话,有问题就是用来解决的,下面是解决方法,每次更新后,我们强制让它回到最下面

在module的build.gradle中加入下面这段即可

下面还是照例给出可复制的代码,不能辛苦各位,可能包括以后的自己

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
        }
    }
}

记录到此,温故而知新。

猜你喜欢

转载自blog.csdn.net/hehota/article/details/83746421