导入AOSP编译的framework后App崩溃的问题求助
问题解决:AOSP自定义framework依赖导致的兼容性错误
问题场景
作为AOSP新手开发者,修改AOSP代码后以sdk_car_x86_64-userdebug为目标编译,执行命令:
m framework -j32 m framework-minus-apex -j32
编译完成后从路径~/path_to/aosp/out/target/product/emulator_car64_x86_64/obj/JAVA_LIBRARIES/framework-sdkextensions.com.android.sdkext_intermediates取出javalib.jar,放入Android应用的app/libs目录,并在app/build.gradle添加依赖:
dependencies { implementation(files("libs/javalib.jar")) ... }
通过以下命令启动模拟器:
source build/envsetup.sh lunch sdk_car_x86_64-userdebug emulator
运行应用时出现两类错误:
错误1(第一个应用)
FATAL EXCEPTION: pool-2-thread-1 Process: com.example.myapp, PID: 2889 java.lang.IncompatibleClassChangeError: Class 'android.app.PendingIntent$$ExternalSyntheticLambda0' does not implement interface 'java.util.concurrent.Executor' in call to 'void java.util.concurrent.Executor.execute(java.lang.Runnable)' (declaration of 'androidx.profileinstaller.DeviceProfileWriter' appears in /data/app/~~ZGDppP5xMSrCMkinJtMDMQ==/com.example.myapp-asn8RdWTnUGeZXOezq_-iA==/base.apk!classes14.dex) at androidx.profileinstaller.DeviceProfileWriter.result(DeviceProfileWriter.java:87) at androidx.profileinstaller.DeviceProfileWriter.deviceAllowsProfileInstallerAotWrites(DeviceProfileWriter.java:140) at androidx.profileinstaller.ProfileInstaller.transcodeAndWrite(ProfileInstaller.java:440) at androidx.profileinstaller.ProfileInstaller.writeProfile(ProfileInstaller.java:575) at androidx.profileinstaller.ProfileInstaller.writeProfile(ProfileInstaller.java:515) at androidx.profileinstaller.ProfileInstaller.writeProfile(ProfileInstaller.java:479) at androidx.profileinstaller.ProfileInstallerInitializer.lambda$writeInBackground$2(ProfileInstallerInitializer.java:145) at androidx.profileinstaller.ProfileInstallerInitializer$$ExternalSyntheticLambda2.run(D8$$SyntheticClass:0)
错误2(第二个简单应用)
FATAL EXCEPTION: pool-2-thread-1 Process: com.example.dummyproject, PID: 3761 java.lang.NoSuchMethodError: No direct method <init>()V in class Landroid/app/PendingIntent$$ExternalSyntheticLambda0; or its super classes (declaration of 'android.app.PendingIntent$$ExternalSyntheticLambda0' appears in /system/framework/framework.jar) at androidx.profileinstaller.ProfileInstaller.writeProfile(ProfileInstaller.java:479) at androidx.profileinstaller.ProfileInstallerInitializer.lambda$writeInBackground$2(ProfileInstallerInitializer.java:145) at androidx.profileinstaller.ProfileInstallerInitializer$$ExternalSyntheticLambda2.run(D8$$SyntheticClass:0)
错误原因
这些错误本质是应用依赖的自定义framework jar与系统运行时的framework类不匹配:
PendingIntent$$ExternalSyntheticLambda0是D8/R8编译生成的合成类,自定义framework jar中的类结构,和模拟器系统framework.jar里的对应类结构不一致。- 直接导入AOSP编译的中间产物
javalib.jar,会导致应用编译时使用jar内的类定义,但运行时调用系统中的类,引发类结构、方法不匹配的冲突。
解决方法
1. 避免直接导入AOSP中间jar包
AOSP生成的javalib.jar是未完整处理的中间产物,不适合直接作为应用依赖。正确思路:
- 若修改是新增SDK接口,需通过AOSP SDK扩展机制暴露接口给应用。
- 若仅测试自定义framework功能,应将应用放入AOSP源码树作为系统应用编译,或使用AOSP对应版本的
android.jar编译。
2. 保证编译与运行时类一致性
方式一:将应用作为AOSP系统应用编译
- 将应用源码放入AOSP的
packages/apps/目录,编写Android.bp或Android.mk配置文件,随AOSP一起编译。这样应用编译和运行时使用的framework类完全一致。
方式二:使用AOSP生成的android.jar
- 从AOSP输出目录获取对应目标的
android.jar,路径通常为out/target/product/emulator_car64_x86_64/framework/android.jar。 - 在应用
build.gradle中替换默认SDK依赖为该自定义jar:
用android { compileSdkVersion 33 // 匹配你的AOSP版本SDK等级 buildToolsVersion "33.0.2" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } dependencies { compileOnly files("libs/android.jar") // 用compileOnly避免打包进APK // 其他依赖... } }compileOnly确保应用运行时调用系统framework类,编译时仅用自定义jar获取类定义。
3. 临时禁用ProfileInstaller(快速测试用)
若仅需临时验证应用功能,可先禁用Androidx的ProfileInstaller绕过错误:
- 在应用
AndroidManifest.xml中添加:
此方法仅为临时 workaround,无法解决根本的类不一致问题。<meta-data android:name="androidx.profileinstaller.ENABLED" android:value="false" />
4. 完整编译AOSP
之前仅编译framework和framework-minus-apex可能导致系统镜像与产物不一致,建议执行完整编译:
make -j32
确保系统镜像与所有中间产物完全同步后,再重新生成所需资源。
内容的提问来源于stack exchange,提问作者Zappy.Mans
相关产品推荐
相关产品推荐

