Android 6.0 开启Pre-optimization出现ClassNotFoundException异常

开启Pre-optimization功能只需在板级文件BoardConfig.mk设置:WITH_DEXPREOPT := true
摘要由CSDN通过智能技术生成

http://blog.csdn.net/u013686019/article/details/77053455

1、何为Pre-optimization

Pre-optimization针对的是整个system.img镜像,好处是可以减少系统启动时间


2、配置Pre-optimization
开启该功能只需在板级文件BoardConfig.mk设置:
WITH_DEXPREOPT := true
更多配置参数及其详细内容见参考1


3、问题背景及描述
有一些定制的系统服务,所有服务代码统一放置在目录:
/framework/myservices
编译后生成jar包myservices.jar


然后在SystemServer.java(/framework/base/services/java/com/android/server)所在的Android.mk引用myservices.jar
LOCAL_JAVA_LIBRARIES += myservices
并在SystemServer.java中注册那些服务


由于系统core级别的jar需要提前加载,所以在环境变量BOOTCLASSPATH中增加myservices.jar
init.environ.rc
export BOOTCLASSPATH .....:/system/framework/myservices.jar



在不开启Pre-optimization(即WITH_DEXPREOPT := false)的情况下,系统可以正常运行;现在启用Pre-optimization,所用来源于myservices.jar的类都在系统启动过程中出现异常,类似:
E SystemServer: BOOT FAILURE starting MySecure Service
E SystemServer: java.lang.NoClassDefFoundError: Failed resolution of: Laa/bb/cc/MySecure;
E SystemServer: 	at com.android.server.SystemServer.startOtherServices(SystemServer.java:1034)
E SystemServer: 	at com.android.server.SystemServer.run(SystemServer.java:286)
E SystemServer: 	at com.android.server.SystemServer.main(SystemServer.java:178)
E SystemServer: 	at java.lang.reflect.Method.invoke(Native Method)
E SystemServer: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:772)
E SystemServer: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
E SystemServer: Caused by: java.lang.ClassNotFoundException: Didn't find class "aa.bb.cc.MySecure" on path: DexPathList[[zip file "/system/framework/services.jar", zip file "/system/framework/ethernet-service.jar", zip file "/system/framework/wifi-service.jar", zip file "/system/framework/pppoe-service.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E SystemServer: 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E SystemServer: 	at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E SystemServer: 	at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E SystemServer: 	... 6 more
E SystemServer: 	Suppressed: java.lang.ClassNotFoundException: Didn't find class "aa.bb.cc.MySecure" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E SystemServer: 		at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E SystemServer: 		at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E SystemServer: 		at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E SystemServer: 		... 7 more
E SystemServer: 		Suppressed: java.lang.ClassNotFoundException: aa.bb.cc.MySecure
E SystemServer: 			at java.lang.Class.classForName(Native Method)
E SystemServer: 			at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E SystemServer: 			at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E SystemServer: 			at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E SystemServer: 			... 8 more
E SystemServer: 		Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available


4、Pre-optimization开启前后编译差别
开启Pre-optimization后,每个模块会额外生成一个.odex文件,位于/system/framework/oat/arm/目录;
模块的一些信息被记录到boot.art和boot.oat文件中,位于/system/framework/arm/目录。
boot.art和boot.oat文件简介见参考2.


参考文章提到:boot.art里面使用的都是绝对地址。
绝对地址!
来,类比几个概念:
相对路径<--->绝对路径
相对地址<--->绝对地址
动态链接<--->静态链接
LOCAL_JAVA_LIBRARIES<--->LOCAL_STATIC_JAVA_LIBRARIES
这就不用解释了。
LOCAL_JAVA_LIBRARIES := myservices 改为>>>>LOCAL_STATIC_JAVA_LIBRARIES += myservices
就可以解决上述问题。


但,
但是,
但是啊!
如果有第三方APP调用myservices.jar的class,同样出现上述的错误;这时,不可能让APP也采用LOCAL_STATIC_JAVA_LIBRARIES的方式。

怎么办?

这时我们发现,以telephony-common.jar为例,那些引用telephony-common.jar的使用的LOCAL_JAVA_LIBRARIES而并没有出现问题,所以我们可以推测还有其他方式解决类似问题。由于本人没打算深入研究ART虚拟机,就没有进一步研究。解决方式就是在build/target/product/core_minimal.mk文件PRODUCT_BOOT_JARS的变量中添加myservices这就相当于把myservices.jar放到了一个公共的、众所周知的地方,自然不会出现找不到class的问题。



这时编译可能出现"unknown package name of class file"的错误,解决方式参见另一篇"Android更改源码后编译错误unknown package name of class file"


附录1:

Configuring ART

Introduction


This document is intended to discuss how to configure ART and its compilation options. Topics addressed here include configuration of pre-compilation of the system image, dex2oat compilation options at first boot (and post-OTA), and how to trade off system partition space, data partition space, and performance.

See ART and Dalvik, the Dalvik Executable format, and the remaining pages on source.android.com to work with ART. See Verifying App Behavior on the Android Runtime (ART) to ensure your apps work properly.

How ART works


ART is the new Android runtime for the Android 5.0 (Lollipop or L) release and beyond. Dalvik is no longer available.

Please note, this section merely summarizes ART’s configuration. For an in-depth description, see the Android runtime presentation conducted at Google I/O 2014.

ART uses ahead-of-time (AOT) compilation. This means that, at installation, dex code is compiled to native code in OAT files, which replace Dalvik’s odex files. This has several implications:

  • Performance is improved over Dalvik. There is also a commensurate improvement in power consumption measured in the lab.
  • There is no runtime code cache. The OAT files are mapped to memory (and are thus page-able). The RAM memory footprint for OAT files might seem larger in terms of Proportional Set Size (PSS, or the memory shared across processes divided evenly between the processes). But because they are pageable we have found the system impact is improved in terms of real memory pressure effects as the Dalvik JIT cache was not pageable.
  • Similar to preloaded classes in the zygote, ART attempts to pre-initialize a set of classes at compile time. This creates a ‘boot.art’ file that comprises an image of the compacted heap of pre-initialized classes and related objects. This file is mapped into memory upon zygote startup. While this consumes additional storage (typically 10MB), it speeds zygote startup and creates opportunities for the system to swap out some preloaded classes under memory pressure. This also contributes to improved low-RAM performance for ART, since in Dalvik much of this class information would have been stored in dirty pages in the linear alloc space.
  • Dex file compilation uses a tool called dex2oat and takes more time than dexopt. The increase in time varies, but 2-3x increases in compile time are not unusual. For example, apps that typically take a second to install using dexopt might take 2-3 seconds.
  • OAT files are larger than odex files if full compilation is enabled. We discuss options to mitigate this cost later in this document.

Compilation options


Dex file compilation takes more time than dexopt, which can be noticeable when all of a user’s apps must be compiled during first boot (after factory reset or after receiving an OTA). To reduce the amount of compilation needed, ART supports the option of pre-optimizing libraries and applications in the system partiti

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值