赞
踩
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自编译framework.jar测试代码
float aaa = AdaptiveIconDrawable.MASK_SIZE;
int bbb = WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
Log.e(TAG, "onCreate: " + aaa + " " + bbb);//onCreate: 100.0 2019
}
}
在File >> Settings >> Build… >> Build Tools >> Gradle 中勾选generate *.iml,否则项目中将没有My_Application.app.main.iml这个文件
dependencies { compileOnly files("libs/framework.jar") } gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs.add("-Xbootclasspath/p:$rootProject.rootDir/app/libs/framework.jar") } } preBuild { doLast { //测试版本 Android Studio Giraffe | 2022.3.1 Patch 1 //需要先在File >> Settings >> Build... >> Build Tools >> Gradle 中勾选generate *.iml, //否则项目中将没有My_Application.app.main.iml这个文件 def imlFile = file("$rootProject.rootDir/.idea/modules/app/My_Application.app.main.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 30 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) { e.printStackTrace() } } }
至此,MainActivity中framework.jar的测试代码将不再报红
----------------------------我是分割线----------------------------
2023-11-15 更新
上次更新的是单独模块引用framework.jar的配置方法,今天带来的是全局所有模块引用framework.jar的配置方法
dependencies {
compileOnly files("../libs/framework.jar")
}
subprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs.add("-Xbootclasspath/p:$rootProject.rootDir/libs/framework.jar") } preBuild { doLast { def rootProjectName = rootProject.name.replace(" ", "_") def projectName = project.name.replace(" ", "_") def iml_path = "$rootProject.rootDir\\.idea\\modules\\" + projectName + "\\" + rootProjectName + "." + projectName + ".main.iml" def imlFile = file(iml_path) try { def parsedXml = (new XmlParser()).parse(imlFile) def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' } def sdkString = jdkNode.'@jdkName' parsedXml.component[1].remove(jdkNode) new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK']) groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile)) } catch (FileNotFoundException e) { e.printStackTrace() } } } } }
修改完成后,点击Build->Make Project使配置生效,framework.jar中隐藏api或者系统级api将可正常调用并且不会报红,测试结果如下图所示:
----------------------------我是分割线----------------------------
2024-01-20 更新
上述使用的framework.jar一定是从以下路径获得,否则可能无法在android studio中编译通过,并且正常运行,android 12 版本
out\target\common\obj\JAVA_LIBRARIES\framework-minus-apex_intermediates\classes.jar
如果你觉得这篇文章帮助到了你,欢迎打赏
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。