1.从android源码中找到framework.jar(make -j8 framework-minus-apex)
out/soong/.intermediates/frameworks/base/framework-minus-apex/android_common/combined/framework-minus-apex.jar
将 framework-minus-apex.jar重命名为framework.jar
2.将framework.jar放入app的lib目录
3.在module的build.gradle里面加入依赖
compileOnly files('libs/framework.jar')
3 工程的目录下的build.gradle添加:
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { alias(libs.plugins.android.application) apply false } allprojects { /*配置自编译的framework.jar优先于SDK api*/ gradle.projectsEvaluated { tasks.withType(JavaCompile){ options.compilerArgs.add('-Xbootclasspath/p:app/libs/framework.jar') } tasks.withType(JavaCompile) { Set<File> fileSet = options.bootstrapClasspath.getFiles() List<File> newFileList = new ArrayList<>(); //相对位置,根据存放的位置修改路径 newFileList.add(new File('./app/libs/framework.jar')) newFileList.addAll(fileSet) options.bootstrapClasspath = files( newFileList.toArray() ) } } }
4 app的build.gradle添加
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
preBuild {
doLast {
def imlFile = file("../.idea/modules/app/MLinkServer.app.iml")
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
println "no iml found"
}
}
}
}
dependencies {
implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
compileOnly files('libs/framework.jar')
}
5 生成iml文件