直接引用 out\target\common\obj\JAVA_LIBRARIES\framework_intermediates目录下的相关文件,该目录下的classes-jarjar.jar 包含系统中很多接口api
Android Studio 引入源码编译的jar包
1.拷贝classes-jarjar.jar到工程libs目录并重新命名为framework.jar,并右键单击jar包 Add as Library
2. 右键Module选择“Open Module Settomgs,进入Project Structure界面,把framework.jar移到最顶上,把Scope改为Compile Only,点击确定
3.在工程的根目录下打开build.gradle,并找到allprojects,加入如下代码
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xbootclasspath/p:app\\libs\\framework.jar')
}
}
4.在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 groovy.util.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
}
}
}
并在Module下面的build.gradle里找到defaultConfi加入multiDexEnabled = true,dependencies中添加implementation ‘com.android.support:multidex:1.0.0’,如下截图:
5.工程 clean下再次编译即可
注意:
Android 4.1.3可以通过修改文件以达到调整引用jar包的顺序如下: