Android first boot time long optimized pre-compilation extract Odex

Source: Internet
Author: User
<span id="Label3"></p>Tips! The application is installed in two cases: first when the system is first booted, and the second: after the system Starts. This blog post is based on the first installation scenario. In the First-time scenario, the system will translate all the APK in/system/app,/system/priv-app,/data/app directories to the local machine code, as well as the/system/ The APK or JAR file under the framework directory, and the external jar referenced by these apk, for Dex bytecode to local machine code Translation. This ensures that, in addition to the application, system services that are developed using Java in the system will also be uniformly translated from Dex bytecode to local machine Code. For more information, please visit Lao Luo's blog Android Art runtime to seamlessly replace the Dalvik virtual machine process Analysis.<br>one, JVM, DVM, Art Virtual machine Understanding <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <ul> <li> The JVM virtual machine is running Java bytecode: </li> </ul> Java->java bytecode (class)->java bytecode (jar) note! Java Virtual machine based on the stack, the stack-based machine must use instructions to load and manipulate the data on the stack, the required instructions are relatively more. <br> <ul> <li> Dalvik virtual machine interprets execution of Dex bytecode: </li> </ul> Java->java bytecode (class)->dalvik bytecode (dex) note: relative to jvm, The Dalvik is based on a register and is optimized and allows a limited amount of memory to run concurrently with multiple virtual machine instances, each Dalvik application executed as a separate Linxu process. If there are many classes in an application, the compilation will generate a lot of class files, and there will be a lot of redundant information between the class files, and the Dex format file consolidates all the contents of the Classs file into one file, which reduces the overall file occupancy, io operations, and also improves the class lookup Speed. In addition, the DEX format file adds new opcode support, and the file structure is relatively concise, using equal-length instructions to improve parsing speed. And the Dex file will maximize the size of the read-only structure to increase the speed of data sharing between Processes. <br> <ul> <li> The local machine code executed by the art virtual machine: </li> </ul> Java->java bytecode (class)->dalvik bytecode (dex)->optimized Android Runtime machine code (oat) note: The AOT (ahead-of-time) compilation used by art, when the application is first installed, the bytecode is pre-compiled to be stored locally, meaning it is compiled before the program Runs. While Dalvik is a typical JIT (just_in_time), in this mode, when the application <strong> every </strong> run, the bytecode needs to be converted to the machine code to execute the real-time compiler, that is, when the program Runs. As a result, when the app is running, the art mode eliminates the process of interpreting bytecode relative to dalvik, and reduces the memory footprint to improve the efficiency of the app. <br> </blockquote> </blockquote><span style="font-family:微软雅黑; orphans:2; widows:2; font-size:14px"><span style="font-family: Microsoft Jacob black; orphans:2; widows:2; font-size:14px"> Ii. Odex </span> </span> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px">     From the previous section we know that when compiling a packaged apk, the Java class is compiled into one or more bytecode files (. Class) and converted to a Dex (Dalvik Executable) file through the DX tool class File.     typically, The Android app we see is actually a compressed file with an. apk suffix. We can unzip the APK with the compression tool, and the extracted content has a file named Classes.dex. Then the system needs to extract it from the APK and save it in the Data/app directory the first time we Boot.     If you are currently running under a Dalvik virtual machine, Dalvik will perform a "translation" of classes.dex, and the "translation" process is the function dexopt of the daemon Installd to optimize the Dex Bytecode. In fact, the Odex file is generated by the Dex file, and the final Odex file is saved in the Phone's VM cache directory Data/dalvik-cache (note!). The Odex file generated here is still in the name of dex, as in the format: [email protected]@[email protected] @classes. dex).     If you are currently running in art mode,    art will also invoke the/system/bin/dexopt tool to translate DEX bytecode into local machine code at the time of the first entry into the system and save it in data/ Under the Dalvik-cache.     So it's important to note that either the Dex bytecode is optimized or the Dex bytecode is translated into the local machine code, The result is saved in a Odex file of the same name, But the former corresponds to a dey file (which indicates that this is an optimized dex), which corresponds to an oat file (actually a custom elf file that contains local machine instructions). In this way, any code that references the Odex file through an absolute path does not need to be modified.     Since the application is installed on the first boot of the system, it will greatly increase the time of the first boot of the system if the preset apk is More. From the previous description, since either DVM or art, the optimization results for Dex are saved in a Odex file of the same name, then if we take these two processes in the ROM compile time preprocessing extract Odex file will greatly optimize the system first boot Time. </blockquote><br>third, pre-compiled Extract Odex <blockquote style="font-family:微软雅黑; orphans:2; widows:2; font-size:14px; margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="font-family:微软雅黑; orphans:2; widows:2; font-size:14px; margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> defined in boardconfig.mk: with_dexpreopt: = True. After you open this macro, both the source code and the Source-free preset apk precompiled will extract the Odex File. </blockquote>however, It is important to note that after opening the with_dexpreopt macro, the pre-compilation Odex will add a certain amount of space, preset too many apk, will lead to system.img too large, but Compiled.    This can be done by deleting the Dex file in the apk, resizing the system.img size limit, or skipping some apk odex extracts at Precompilation. For example, skipping the HelloWorld application extraction method is as Follows: </blockquote> </blockquote> <blockquote style="font-family:微软雅黑; orphans:2; widows:2; font-size:14px; margin:0px 0px 0px 40px; border:none; padding:0px"> Add code for Red tags in directory \build\core\dex_preopt_odex_install.mk: <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <span style="color:#E30000">i<em>feq ($ (local_module),<strong>helloworld</strong>)</em> </span> </blockquote> </blockquote> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <em><span style="color:#E30000">local_dex_preopt:=</span></em> </blockquote> </blockquote> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <em><span style="color:#E30000">endif</span></em> </blockquote> </blockquote> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <span style="color:#5898FF"><em>build_odex:=</em></span> </blockquote> </blockquote> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <span style="color:#5898FF"><em>installed_odex:=</em></span> </blockquote> </blockquote> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <span style="color:#5898FF"><em>....</em></span> </blockquote> </blockquote> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <blockquote style="margin:0px 0px 0px 40px; border:none; padding:0px"> <strong><span style="color:#E30000">HelloWorld</span></strong> can be replaced with the local_module name of the apk that needs to skip extracting odex, such as Settings. </blockquote> </blockquote> </blockquote><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android first boot time long optimized pre-compilation extract Odex</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.