AndroidStudio 引用 framework.jar

端木淇
2023-12-01

修改最外层的 build.gradle,如下:

buildscript {
    repositories {
        google()
        //jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"
    }

    gradle.projectsEvaluated {
        subprojects {
            tasks.withType(JavaCompile) {
                // 将 framework.jar 添加到 bootclasspath 是为了能够编译通过
                // 其中,该示例中 framework.jar 目录的位置为:
                // ├─ProjectRoot
                // │ └─libs
                // │    └─framework.jar
                def bootclasspath = '-Xbootclasspath/p:' + rootProject.files("libs/framework.jar").getAsPath()
                options.compilerArgs.add(bootclasspath)
                doFirst {
                    FileCollection fc = rootProject.files("libs/framework.jar") + options.bootstrapClasspath
                    options.setBootstrapClasspath(fc)
                }
            }
            preBuild {
                doLast {
                    // 别忘了在 dependencies 中添加依赖,如下:
                    // 使用 compileOnly 确保 framework.jar 只参与编译而不打包进 apk
                    // dependencies {
                    //    compileOnly files('../libs/framework.jar')
                    //}
                    // 修改 iml 文件是为了 AndroidStudio 编辑器中可以识别 framework.jar 中的的方法或符号
                    // 否则会报: Cannot resolve method 提示
                    // 不修改 iml 文件也没问题,只要将 framework.jar 加入 bootclasspath 即可编译通过
                    def idea = new File(getRootDir(), ".idea")
                    def dirFileList = new LinkedList<File>()
                    def imlFileList = new ArrayList<File>()
                    dirFileList.add(idea)
                    while (!dirFileList.isEmpty()) {
                        def dir = dirFileList.pop()
                        if (dir != null) {
                            def files = dir.listFiles()
                            if (files != null) {
                                for (File f : files) {
                                    if (f.isDirectory()) {
                                        dirFileList.add(f)
                                    } else if (f.name.endsWith(".iml")) {
                                        imlFileList.add(f)
                                    }
                                }
                            }
                        }
                    }
                    for (File imlFile : imlFileList) {
                        try {
                            def parsedXml = (new XmlParser()).parse(imlFile)
                            def component = parsedXml.component[1]
                            if (component == null) {
                                continue
                            }
                            def orderEntryArray = component.orderEntry
                            if (orderEntryArray == null) {
                                continue
                            }
                            def frameworkNode = orderEntryArray.find {
                                def name = it.'@name'
                                if (name != null && name.contains('framework')) {
                                    true
                                } else {
                                    false
                                }
                            }
                            if (frameworkNode == null) {
                                continue
                            }
                            System.out.println("reorder framework entry for: " + imlFile)
                            component.remove(frameworkNode)
                            def orderEntryList = new ArrayList<Object>()
                            // component.remove 之后,component.orderEntry 变化了,所以不能直接使用 orderEntryArray.iterator()
                            def iterator = component.orderEntry.iterator()
                            while (iterator.hasNext()) {
                                def entry = iterator.next()
                                orderEntryList.add(entry)
                                component.remove(entry)
                            }
                            component.append(frameworkNode)
                            def i = orderEntryList.iterator()
                            while (i.hasNext()) {
                                component.append(i.next())
                            }
                            //noinspection UnnecessaryQualifiedReference
                            groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
                        } catch (Exception e) {
                            // nop, iml not found
                            System.out.println("exception: " + e)
                        }
                    }
                }
            }
        }
    }
}

allprojects {
    repositories {
        google()
        //jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 类似资料: