在androidstudio中通过compileOnly引用自编译的framework且配置jdk为1.8,就可以在代码中使用systemApi或hide api,能正常打包出apk,能正常运行。
activity内容
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int a = Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT;
Log.d("MainActivity", "onCreate: a:" + a);
}
}
app的build.gradle配置如下:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
// sourceCompatibility JavaVersion.VERSION_11
// targetCompatibility JavaVersion.VERSION_11
}
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
println "bootstrapClasspath is null: " + (options.bootstrapClasspath == null)
if (options.bootstrapClasspath != null) {
Set<File> fileSet = options.bootstrapClasspath.getFiles()
List<File> newFileList = new ArrayList<>();
newFileList.add(new File("libs/framework.jar"))
newFileList.addAll(fileSet)
options.bootstrapClasspath = files(newFileList.toArray())
}
}
}
}
但jdk1.8是很老的版本了,目前最新的是jdk24。在把compileOptions的jdk版本改成1.8之后项目就无法编译运行了,比如compileOptions改成如下,
compileOptions {
// sourceCompatibility JavaVersion.VERSION_1_8
// targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
app编译就报错:

请问这个怎么解?
说明:我是在androidstudio里开发系统app,要直接使用systemApi,另外我的代码以及部分引用的库使用了jdk1.8之后新特性。所以不要说降低jdk版本或是反射使用systemApi。
问题代码已上传github:https://github.com/haipinghuang/SystemApiDemo, 拉下来可直接运行