Disclosure of Invention
In view of the above, the present application provides an optimization method, an apparatus, an electronic device and a storage medium for applying cold start, so as to solve the problem of slower cold start of the application in the prior art.
In order to achieve the above purpose, the present application provides the following technical solutions:
the first aspect of the application discloses an optimization method for cold start application, which comprises the following steps:
Constructing a first relation diagram and a second relation diagram based on the configuration file of the application, wherein the first relation diagram is used for representing class structure information corresponding to the components of the application configuration file;
Based on the first relation diagram and the second relation diagram, screening out the high-frequency class of the application according to the access frequency of the class, and packaging the high-frequency class into a main dex file;
Packaging non-high frequency classes into a secondary dex file, wherein the non-high frequency classes are classes except the high frequency classes in the application;
And when the application is started in a cold mode, loading the main dex file until the application is started in a cold mode, and loading the secondary dex file.
Optionally, in the above method, the constructing the first relationship graph and the second relationship graph based on the application configuration file includes:
Acquiring component information of a preset component based on the configuration file;
Analyzing a class structure in the preset component based on the component information, and establishing an index and a dependence of a class according to the class structure to generate the first relation diagram;
analyzing class references in the preset components based on the component information, and establishing indexes and dependencies of the classes according to the class references to generate the second relation diagram.
Optionally, in the above method, the screening the high-frequency class of the application according to the access frequency of the class based on the first relationship diagram and the second relationship diagram, and packing the high-frequency class into a main dex file includes:
traversing the first relation diagram and the second relation diagram respectively to acquire the access frequency of each class in the first relation diagram and the access frequency of each class in the second relation diagram;
placing each class in the first relation diagram into a first queue according to the order of the access frequency from high to low;
placing each class in the second relation diagram into a second queue according to the order of the access frequency from high to low;
And screening out classes with access frequency meeting preset conditions based on the first queue and the second queue, and packaging the screened classes into the main dex file.
Optionally, in the above method, the screening the class meeting the preset condition based on the first queue and the second queue, and packing the screened class into the master dex file includes:
performing depth traversal on the first queue, stopping the traversal and packing the traversed class in the first queue into the main dex file if the number of the traversed class is equal to a preset threshold value and the first queue does not complete the traversal, wherein the threshold value is the maximum number of methods which can be accommodated by the main dex file;
If the number of the methods for completing the traversed classes is smaller than the threshold value and the first queue is traversed, performing depth traversal on the second queue, and calculating to obtain a difference value between the threshold value and the number of the methods for completing the traversed classes of the first queue;
Stopping traversing if the number of the traversed classes in the second queue is equal to the difference value and the second queue does not complete traversing, and packaging the traversed classes in the first queue and the traversed classes in the second queue into the main dex file;
And if the number of the methods of the traversed classes of the second queue is smaller than the difference value and the second queue is traversed, stopping traversing, and packaging the traversed classes in the first queue and the traversed classes in the second queue into the main dex file.
The second aspect of the present application discloses an optimizing device for cold start application, comprising:
The system comprises a construction unit, a first relation graph and a second relation graph, wherein the construction unit is used for constructing a first relation graph and a second relation graph based on an application configuration file, the first relation graph is used for representing class structure information corresponding to a component of the application configuration file, and the second relation graph is used for representing class reference information corresponding to the component of the application configuration file;
The screening unit is used for screening the high-frequency class of the application according to the access frequency of the class based on the first relation diagram and the second relation diagram, and packaging the high-frequency class into a main dex file;
The packaging unit is used for packaging non-high-frequency classes into a secondary dex file, wherein the non-high-frequency classes are classes except the high-frequency classes in the application;
and the loading unit is used for loading the main dex file when the application is cold started until the application is cold started, and loading the secondary dex file.
Optionally, the above apparatus, the building unit includes:
The acquisition subunit is used for acquiring the component information of the preset component based on the configuration file;
the first generation subunit is used for analyzing the class structure in the preset component based on the component information, establishing class indexes and dependencies according to the class structure and generating the first relation diagram;
And the second generation subunit is used for analyzing the class references in the preset component based on the component information, establishing indexes and dependencies of the classes according to the class references, and generating the second relation graph.
Optionally, in the foregoing apparatus, the screening unit includes:
The first traversing subunit is used for traversing the first relation diagram and the second relation diagram respectively, and acquiring the access frequency of each class in the first relation diagram and the access frequency of each class in the second relation diagram;
A first queue construction subunit, configured to put each class in the first relationship diagram into a first queue according to an order from high to low of access frequency;
A second queue construction subunit, configured to put each class in the second relationship diagram into a second queue according to an order from high to low of access frequency;
And the screening subunit is used for screening out the classes with access frequency meeting preset conditions based on the first queue and the second queue, and packaging the screened classes into the main dex file.
Optionally, in the foregoing apparatus, the screening subunit includes:
the second traversing subunit is used for performing depth traversing on the first queue, stopping traversing and packing the traversed class in the first queue into the main dex file if the number of the traversed class is equal to a preset threshold value and the first queue does not complete traversing;
A third traversing subunit, configured to, if the number of methods for completing the traversed class is less than the threshold and the first queue has completed traversing, perform depth traversing on the second queue, and calculate to obtain a difference between the threshold and the number of methods for completing the traversed class of the first queue;
A fourth traversing subunit, configured to stop traversing if the number of methods of the traversed class in the second queue is equal to the difference value and the traversed class in the first queue and the traversed class in the second queue are not completed, and package the traversed class in the first queue and the traversed class in the second queue into the main dex file;
And a fifth traversing subunit, configured to stop traversing if the number of methods of the traversed class in the second queue is smaller than the difference value and the traversed class in the second queue is completed, and package the traversed class in the first queue and the traversed class in the second queue into the main dex file.
The third aspect of the present application discloses an electronic device, comprising:
One or more processors;
a storage device having one or more programs stored thereon;
The one or more programs, when executed by the one or more processors, cause the one or more processors to implement the method of any of the first aspects of the present application.
A fourth aspect of the application discloses a computer storage medium having stored thereon a computer program which, when executed by a processor, implements a method according to any of the first aspects of the application.
According to the technical scheme, in the optimization method for the application cold start, a first relation diagram and a second relation diagram are constructed based on the application configuration file, wherein the first relation diagram is used for representing class structure information corresponding to the components of the application configuration file, and the second relation diagram is used for representing class reference information corresponding to the components of the application configuration file. And then, based on the first relation diagram and the second relation diagram, the high-frequency class of the application is screened out according to the access frequency of the class, and the high-frequency class is packed into the main dex file. And packaging the non-high frequency class into a secondary dex file. And finally, loading the main dex file when the application is started in a cold mode, and sequentially loading other secondary dex files until the application is started in a cold mode. Therefore, by using the method of the application, the service scene commonly used by the user can be covered in the main dex file by packing the high-frequency class into the main dex file, and only the main dex file is loaded in the application starting stage, thereby reducing the loading time and accelerating the application cold starting speed. The problem of the application in prior art that the first cold start is slower is solved.
Detailed Description
The following description of the embodiments of the present application will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present application, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
In the present disclosure, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises an element.
Moreover, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions.
As known from the background technology, for a device with a low version of the android system, the adopted Java running environment is a Dalvik virtual machine. The biggest problem is that after the application is installed or updated, the application is started in a cold mode for the first time, the time is long, tens of seconds or even minutes are needed, a user needs to face a period of black screen to normally use the APP, and user experience is seriously affected.
In view of the above, the application provides an optimization method, an apparatus, an electronic device and a storage medium for applying cold start, so as to solve the problem of slower application cold start in the prior art.
The embodiment of the application discloses an optimization method for cold start application, which is shown in fig. 1 and specifically comprises the following steps:
s101, constructing a first relation diagram and a second relation diagram based on an application configuration file, wherein the first relation diagram is used for representing class structure information corresponding to a component of the application configuration file, and the second relation diagram is used for representing class reference information corresponding to the component of the application configuration file.
It should be noted that, firstly, an application configuration manifest file of an application is obtained, which includes information such as a name, version, authority, component, etc. of the application. And then constructing a first relation diagram and a second relation diagram based on the component information of the application configuration manifest file, wherein the first relation diagram is used for representing class structure information corresponding to the component of the application configuration manifest file, and the second relation diagram is used for representing class reference information corresponding to the component of the application configuration manifest file.
Alternatively, in another embodiment of the present application, an implementation manner of step S101, as shown in fig. 2, may include:
S201, acquiring component information of a preset component based on a configuration file.
The preset component comprises an Activity component, a service component, a Provider component and a Receiver component.
It should be noted that, based on the application's manifest configuration file, component information of four large components, i.e., an Activity component, a service component, a Provider component, and a Receiver component, is obtained.
The Activity component is the most basic and most commonly used component in Android, and provides a screen that a user can use to interact with to complete a task. Each Activity is implemented as a separate class and inherits from the Activity base class. The activities communicate with each other through the Intent.
The Service component is a solution for implementing program background running in Android, which is typically used to perform tasks that do not require interaction with users. Service has two states, started and Bound. The Started state is initiated by the other component, while the Bound state is Bound by the other component. Service is not dependent on the user interface in operation, and Service can still remain operational even if the program switches to the background.
The Receive component is used to process system broadcasts, such as detecting changes in the power of a cell phone. It may receive broadcast messages from a system or application and perform corresponding operations.
The Provider component is used to enable data sharing between different applications. Each application has its own data space and other applications can access and manipulate shared data.
S202, analyzing a class structure in a preset component based on component information, and establishing an index and a dependence of the class according to the class structure to generate a first relation diagram.
It should be noted that, traversing the four component entries related to the application configuration manifest file, analyzing the class structure in the preset component according to the parent class, expansion and static relation, and establishing the index and the dependence of the class according to the class structure, and mapping to generate a first relation diagram, as shown in fig. 3. The first relationship graph should have at least the following information in each node:
Signing characters to a dictionary table of Class, field and Method structures;
the Class structure comprises a parent Class structure list of all inheritance and realization;
the Class structure comprises a String constant list which is all referenced;
The Class structure contains a list of all referenced constant methods.
S203, analyzing class references in the preset components based on the component information, and establishing indexes and dependencies of the classes according to the class references to generate a second relation diagram.
It should be noted that, based on the four component entries related to the application configuration manifest file, only the class references in the preset components are resolved, and the class indexes and dependencies are established according to the class references, so as to generate a second relationship diagram, as shown in fig. 4. Each node in the second relationship graph has at least the following information:
Signing characters to a dictionary table of Class, field and Method structures;
The Class structure contains a list of all the referenced types, which are PRIMITIVETYPE and Class.
S102, based on the first relation diagram and the second relation diagram, the applied high-frequency class is screened out according to the access frequency of the class, and the high-frequency class is packed into the main dex file.
Based on the first relation diagram and the second relation diagram, the high-frequency class of the application, namely the class with higher access frequency, is screened out according to the access frequency of the class. And packaging the screened high-frequency class into a main dex file. The dex file is an executable file format specific to the Android system and is mainly used for bearing core instructions and data of application programs. is not only a core component of the Android application program, but also carries key instructions and data during program running. The dex file provides powerful support for efficient operation of the Android application program through the unique file format and the optimization technology. Typically, an application will contain multiple dex files, such as class1. Dex (primary dex), class2. Dex, class3. Dex, class4. Dex, where the first dex file is the primary dex file and class2. Dex, class3. Dex, class4. Dex, etc. are the secondary dex files. Wherein, there is only one primary dex file, and there may be one or more secondary dex files.
Alternatively, in another embodiment of the present application, an implementation manner of step S102, as shown in fig. 5, may include:
s501, traversing the first relation diagram and the second relation diagram respectively, and obtaining the access frequency of each class in the first relation diagram and the access frequency of each class in the second relation diagram.
It should be noted that, the first relationship graph and the second relationship graph are traversed according to the breadth-first algorithm, so as to obtain the access frequency of each class in the first relationship graph and the access frequency of each class in the second relationship graph. For example, the four-component Activity category includes the class activity_1 to activity_n and the Service category, and the class service_1 to service_n, the similar Provider, the class provider_1 to provider_n and the Receiver category, and the class receiver_1 to receiver_n respectively obtain the access frequency of each class.
S502, each class in the first relation diagram is put into a first queue according to the order of the access frequency from high to low.
It should be noted that, a first queue is constructed, and each class in the first relation diagram is put into the first queue according to the order of the access frequency from high to low. In this queue, the higher the access frequency, the higher the rank, for example as shown in FIG. 6.
S503, placing each class in the second relation diagram into a second queue according to the order of the access frequency from high to low.
It should be noted that, a second queue is constructed, and each class in the second relation diagram is put into the second queue according to the order of the access frequency from high to low. In this queue, the higher the access frequency, the higher the ranking.
S504, screening out classes with access frequency meeting preset conditions based on the first queue and the second queue, and packaging the screened classes into a main dex file.
It should be noted that, traversing the first queue and the second queue, screening out the classes with k names before the access frequency, and packing the screened classes into the main dex file. Wherein k can be set according to practical situations, such as the maximum number of methods which can be accommodated by the main dex file.
Alternatively, in another embodiment of the present application, an implementation of step S504 may include;
And performing depth traversal on the first queue, stopping the traversal if the number of the traversed classes is equal to a preset threshold value and the first queue does not complete the traversal, and packaging the traversed classes in the first queue into a main dex file, wherein the threshold value is the maximum number of methods which can be accommodated by the main dex file.
If the number of the methods for completing the traversed classes is smaller than the threshold value and the first queue has completed the traversed, performing depth traversing on the second queue, and calculating to obtain a difference value between the threshold value and the number of the methods for completing the traversed classes of the first queue.
If the number of the methods of the traversed classes of the second queue is equal to the difference value and the second queue does not complete the traversing, stopping the traversing, and packing the traversed classes in the first queue and the traversed classes in the second queue into a main dex file.
If the number of the methods of the traversed classes of the second queue is smaller than the difference value and the second queue is traversed, stopping traversing, and packaging the traversed classes in the first queue and the traversed classes in the second queue into a main dex file.
It should be noted that, the first queue needs to be traversed from one level to n levels, if the number of the traversed classes is equal to a preset threshold and the first queue does not complete the traversal, the traversal is stopped, the remaining classes are not traversed any more, and the traversed classes in the first queue are packed into the main dex file. Wherein, the threshold is changed to set the maximum number of methods which can be accommodated by the main dex file. If the number of the methods for completing the traversed classes is smaller than the threshold value and the first queue has completed the traversed, continuing to conduct deep traversing on the second queue, and calculating to obtain a difference value between the threshold value and the number of the methods for completing the traversed classes of the first queue. If the number of the methods of the class traversed by the second queue is equal to the difference value and the second queue does not complete the traversal, stopping the traversal of the rest classes, and packing the traversed classes in the first queue and the traversed classes in the second queue into a main dex file. If the number of the methods of the traversed class of the second queue is smaller than the difference value and the second queue has completed traversing, stopping traversing and packing the traversed class in the first queue and the traversed class in the second queue into a main dex file.
S103, packaging the non-high frequency class into a secondary dex file, wherein the non-high frequency class is a class except the high frequency class in the application.
It should be noted that, when the high frequency class is packed into the primary dex file, the remaining non-high frequency class is packed into the secondary dex file. The non-high frequency class is a class except the high frequency class in the application, and the secondary dex file is a dex file except the main dex file, for example, class2. Dex, class3. Dex, class4. Dex, and the like.
S104, loading the main dex file when the application is started in a cold mode until the application is started in a cold mode, and loading the secondary dex file.
When the application cold start is performed, only the main dex file is loaded, and the secondary dex file is not loaded until the application cold start is completed, and then the secondary dex file is loaded. Because the high-frequency class is packed into the main dex file, the service scene commonly used by a user can be covered in the main dex file during cold start, and only the main dex file is loaded in the application start stage, so that the loading time is reduced, and the application cold start speed is increased.
In the optimization method for the application cold start, a first relation diagram and a second relation diagram are constructed based on an application configuration file, wherein the first relation diagram is used for representing class structure information corresponding to a component of the application configuration file, and the second relation diagram is used for representing class reference information corresponding to the component of the application configuration file. And then, based on the first relation diagram and the second relation diagram, the high-frequency class of the application is screened out according to the access frequency of the first cold start scene class counted by the online version, and the high-frequency class is packed into the main dex file. And packaging the non-high frequency class into a secondary dex file. And finally, when the application is started in a cold way, only the main dex file is needed to be loaded until the application is started in a cold way, and then the secondary dex file is loaded. Therefore, by using the method of the application, the service scene commonly used by the user can be covered in the main dex file by packing the high-frequency class into the main dex file, and only the main dex file is loaded in the application starting stage, thereby reducing the loading time and accelerating the application cold starting speed. The problem of the prior art that the application cold start is slower is solved.
Another embodiment of the present application further provides an optimizing apparatus for cold start application, as shown in fig. 7, including:
The construction unit 701 is configured to construct a first relationship diagram and a second relationship diagram based on the configuration file of the application, where the first relationship diagram is used to characterize class structure information corresponding to the component of the application configuration file, and the second relationship diagram is used to characterize class reference information corresponding to the component of the application configuration file.
And a screening unit 702, configured to screen the applied high-frequency class according to the access frequency of the class based on the first relationship diagram and the second relationship diagram, and package the high-frequency class into the main dex file.
And a packing unit 703, configured to pack the non-high frequency class into the secondary dex file, where the non-high frequency class is a class other than the high frequency class in the application.
And a loading unit 704, configured to load the primary dex file when the application is cold started, until the application is cold started, and load the secondary dex file.
In this embodiment, the specific execution of the constructing unit 701, the filtering unit 702, the packaging unit 703 and the loading unit 704 can be referred to in the embodiment of the method corresponding to fig. 1, and will not be described herein again.
In the optimizing device for application cold start, a construction unit 701 firstly constructs a first relation diagram and a second relation diagram based on an application configuration file, wherein the first relation diagram is used for representing class structure information corresponding to a component of the application configuration file, and the second relation diagram is used for representing class reference information corresponding to the component of the application configuration file. Then, the screening unit 702 screens out the applied high-frequency class according to the access frequency of the first cold start scene class counted by the online version based on the first relation graph and the second relation graph, and packages the high-frequency class into the main dex file. The packing unit 703 packs the non-high frequency class into a secondary dex file. Finally, the loading unit 704 loads the main dex file when the application is cold started, until the application is cold started, and loads the secondary dex file. Therefore, by using the method of the application, the service scene commonly used by the user can be covered in the main dex file by packing the high-frequency class into the main dex file, and only the main dex file is loaded in the application starting stage, thereby reducing the loading time and accelerating the application cold starting speed. The problem of the prior art that the application cold start is slower is solved.
Alternatively, in another embodiment of the present application, an implementation manner of the above-mentioned construction unit 701 may include:
the device comprises an acquisition subunit, a configuration file acquisition subunit, a storage subunit and a storage subunit, wherein the acquisition subunit is used for acquiring component information of a preset component based on the configuration file, and the preset component comprises an Activity component, a service component, a Provider component and a Receiver component.
The first generation subunit is used for analyzing the class structure in the preset component based on the component information, establishing the index and the dependence of the class according to the class structure, and generating a first relation diagram.
The second generating subunit is used for analyzing the class references in the preset component based on the component information, establishing indexes and dependencies of the classes according to the class references, and generating a second relation graph.
In this embodiment, the specific execution process of the acquiring subunit, the first generating subunit, and the second generating subunit may refer to the content corresponding to the embodiment of fig. 2, and will not be described herein again.
Alternatively, in another embodiment of the present application, an implementation manner of the above screening unit 702 may include:
The first traversing subunit is configured to traverse the first relationship graph and the second relationship graph respectively, and obtain an access frequency of each class in the first relationship graph and an access frequency of each class in the second relationship graph.
And the first queue construction subunit is used for placing each class in the first relation diagram into the first queue according to the sequence from high to low of the first cold start scene access frequency.
And the second queue construction subunit is used for placing each class in the second relation diagram into the second queue according to the sequence from high to low of the first cold start scene access frequency.
And the screening subunit is used for screening out the classes with access frequencies meeting preset conditions based on the first queue and the second queue, and packaging the screened classes into a main dex file.
In this embodiment, the specific execution process of the first traversing subunit, the first queue constructing subunit, the second queue constructing subunit, and the screening subunit may refer to the content corresponding to the embodiment of fig. 5, and will not be described herein again.
Optionally, in another embodiment of the present application, an implementation manner of the above screening subunit may include:
And the second traversing subunit is used for carrying out deep traversing on the first queue, stopping traversing and packing the traversed class in the first queue into a main dex file if the number of the traversed class methods is equal to a preset threshold value and the first queue does not complete traversing, wherein the threshold value is the maximum number of the traversed methods which can be contained in the main dex file.
And the third traversing subunit is used for performing deep traversing on the second queue if the number of the methods for completing the traversed classes is smaller than the threshold value and the first queue has completed traversing, and calculating to obtain a difference value between the threshold value and the number of the methods for completing the traversed classes of the first queue.
And the fourth traversing subunit is used for stopping traversing if the number of the methods of the traversed class in the second queue is equal to the difference value and the second queue does not complete traversing, and packaging the traversed class in the first queue and the traversed class in the second queue into a main dex file.
And the fifth traversing subunit is used for stopping traversing if the number of the methods of the traversed class of the second queue is smaller than the difference value and the second queue has completed traversing, and packaging the traversed class in the first queue and the traversed class in the second queue into a main dex file.
In this embodiment, the specific execution process of the second traversal subunit, the third traversal subunit, the fourth traversal subunit, and the fifth traversal subunit may refer to the content corresponding to the embodiment of the method described above, which is not described herein again.
Another embodiment of the present application further provides an electronic device, as shown in fig. 8, specifically including:
one or more processors 801.
A storage device 802 on which one or more programs are stored.
The one or more programs, when executed by the one or more processors 801, cause the one or more processors 801 to implement the methods of any of the embodiments described above.
Another embodiment of the present application also provides a computer storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements a method according to any of the above embodiments.
In this specification, each embodiment is described in a progressive manner, and identical and similar parts of each embodiment are all referred to each other, and each embodiment mainly describes differences from other embodiments. In particular, for a system or system embodiment, since it is substantially similar to a method embodiment, the description is relatively simple, with reference to the description of the method embodiment being made in part. The systems and system embodiments described above are merely illustrative, wherein the elements illustrated as separate elements may or may not be physically separate, and the elements shown as elements may or may not be physical elements, may be located in one place, or may be distributed over a plurality of network elements. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art will understand and implement the present invention without undue burden.
Those of skill would further appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both, and that the various illustrative elements and steps are described above generally in terms of functionality in order to clearly illustrate the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present invention.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the application. Thus, the present application is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.