A method and device for plugin loading in a host

By building host and plug-in class loaders, the classes between the host and plug-in are loaded, and the plug-in component declaration code is embedded in the host, and the plug-in component resources are dynamically modified, which solves the problem of host and plug-in class loading isolation, and realizes non-invasive plug-in loading and efficient code reuse.

CN112083968BActive Publication Date: 2025-06-03TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202010877570.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-08-27
Publication Date
2025-06-03
Estimated Expiration
2040-08-27

AI Technical Summary

Technical Problem

In the prior art, the class loading between the host and the plug-in has problems of isolation or one-way dependence, and it is impossible to support the plug-in and the host to find each other's classes. The plug-in component loading usually requires modifying the plug-in source code, which is highly invasive and affecting code reuse and porting.

Method used

By building a host class loader and a plug-in class loader, integrating the first class loader and the second class loader, realizing the mutual loading of classes between the host and the plug-in, and embed the component declaration code of the plug-in in the host executable file, dynamically modify the resources of the plug-in component object, and realizing non-invasive plug-in loading.

Benefits of technology

It realizes mutual access between the host and the plug-in, without intrusion and modification of the plug-in source code, reduces the size of the host package, and improves the normal operation of the plug-in components and code reusability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112083968B_ABST
    Figure CN112083968B_ABST
Patent Text Reader

Abstract

This application relates to the field of computer technology, and in particular, to a method and device for loading plugins in a host. When running an application program, the host executable file of the application program is started. If the resources corresponding to the currently executed function are located in the plugin executable file, then when triggering the component startup program of the operating system corresponding to the application program, based on the second-class loader and the component declaration code of the plugin, it is determined that the component declaration corresponding to the currently executed function is found, and a component object is created; the component corresponding to the component object in the plugin executable file is modified so that the resources of the corresponding component in the plugin executable file are assigned to the component object; the component object is run to execute the function implemented by the resources of the corresponding component in the plugin executable file, so as to realize the mutual access of classes between the host and the plugin, and there is no need to intrusively modify the plugin source code, thereby achieving the purpose of dynamically loading the plugin in the host.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method and device for loading a plug-in in a host. Background Art

[0002] At present, applications can be divided into hosts and plug-ins. Users only need to install the host Android installation package (AndroidPackage, Apk). The operation of the plug-in depends on the operation of the host program. The plug-in Apk can be dynamically loaded at runtime to realize the corresponding functions of the plug-in. However, in the related technology, the class loading between the host and the plug-in is either isolated from each other or can only be one-way dependent. For the loading of plug-in components, a method of pre-embedded shell components and linking the shell components with the plug-in components is usually adopted. This requires modifying the code of the startup component, invading the component source code in the plug-in, and modifying the related logic, which is not conducive to code reuse and transplantation. Summary of the invention

[0003] The embodiment of the present application provides a method and device for loading a plug-in in a host, so as to realize mutual access between the host and the plug-in classes, and there is no need to invade and modify the plug-in source code, thereby realizing the purpose of dynamically loading the plug-in in the host.

[0004] The specific technical solutions provided by the embodiments of this application are as follows:

[0005] An embodiment of the present application provides a method for loading a plug-in in a host, including:

[0006] When running an application, start the host executable file of the application, wherein the application includes a host executable file and a plug-in executable file, the host executable file at least packages a host class loader, a plug-in class loader and a component declaration code of the plug-in, and the structure of the host class loader integrates a first class loader for loading the host class and a second class loader for loading the plug-in class;

[0007] If the resource corresponding to the currently executed function is located in the plug-in executable file, then when the component startup program of the operating system corresponding to the application is triggered, based on the second class loader and the component declaration code of the plug-in, it is determined to find the component declaration corresponding to the currently executed function, and a component object is created;

[0008] Modify the component corresponding to the component object in the plug-in executable file so that the resource of the corresponding component in the plug-in executable file is assigned to the component object;

[0009] The component object is run to execute the functions implemented by the resources of the corresponding component in the plug-in executable file.

[0010] Another embodiment of the present application provides a plugin loading device in a host, including:

[0011] A startup module, configured to start the host executable file of the application when the application is running. Wherein, the application includes a host executable file and a plugin executable file, and at least a host class loader, a plugin class loader, and component declaration code of the plugin are packaged in the host executable file. In the structure of the host class loader, a first class loader for loading the classes of the host and a second class loader for loading the classes of the plugin are integrated;

[0012] A creation module, configured to, if the resources corresponding to the currently executed function are located in the plugin executable file, when triggering the component startup program of the operating system corresponding to the application, determine and find the component declaration corresponding to the currently executed function based on the second class loader and the component declaration code of the plugin, and create a component object;

[0013] A modification module, configured to modify the component corresponding to the component object in the plugin executable file, so that the resources of the corresponding component in the plugin executable file are assigned to the component object;

[0014] An operation module, configured to operate the component object to execute the function implemented by the resources of the corresponding component in the plugin executable file.

[0015] Another embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the steps of any of the above-mentioned plugin loading methods in the host are implemented.

[0016] Another embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above-mentioned plugin loading methods in the host are implemented.

[0017] In an embodiment of the present application, a class loader is constructed to enable the host and the plug-in to load classes to each other. The host executable file is packaged with at least the host class loader, the plug-in class loader and the component declaration code of the plug-in. The program source code of the plug-in does not need to be packaged into the host executable file, so as to reduce the size of the host executable file for the purpose of integrating the plug-in function in the host, and the declaration code of the component of the plug-in is built into the host. Then, when the application is run, the host executable file is started. If the resource corresponding to the currently executed function is located in the plug-in executable file, when the component startup program of the operating system corresponding to the application is triggered, based on the second class loader and the component declaration code of the plug-in, it is determined to find the currently executed function. The component declaration corresponding to the function is generated, and a component object is created, and the component corresponding to the component object in the plug-in executable file is dynamically modified, so that the resources of the corresponding component in the plug-in executable file are assigned to the component object, and then the component object is run, that is, the function implemented by the resources of the corresponding component in the plug-in executable file can be executed. In this way, since the constructed host class loader can load the plug-in class, and the component declaration code of the plug-in is pre-embedded in the host executable file, when the component of the plug-in is triggered at runtime, the component object can be supported and created normally, and when the component object of the plug-in is created, the component of the plug-in is dynamically modified, so that running the component can execute the corresponding function, realize non-intrusive plug-in and make the component of the plug-in run normally. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1 It is a schematic diagram of the plug-in technology principle in the related technology;

[0019] Figure 2 It is a schematic diagram of the principle of virtualization technology in related technologies;

[0020] Figure 3 A schematic diagram of an application architecture provided in an embodiment of the present application;

[0021] Figure 4 This is a flow chart of a method for loading a plug-in in a host in an embodiment of the present application;

[0022] Figure 5 This is a schematic diagram of the structure of the plug-in class loader in an embodiment of the present application;

[0023] Figure 6 This is a schematic diagram of the structure of the host class loader in an embodiment of the present application;

[0024] Figure 7 This is a logical schematic diagram of the compilation scheme of the plug-in loading method in the host in the embodiment of the present application;

[0025] Figure 8 This is a logical schematic diagram of the runtime scheme of the plug-in loading method in the host in the embodiment of the present application;

[0026] Figure 9 This is a schematic structural diagram of a plug-in loading device in a host in an embodiment of the present application;

[0027] Figure 10 The following shows a schematic structural diagram of an electronic device in an embodiment of the present application. Specific embodiments

[0028] Next, the technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.

[0029] For the convenience of understanding the embodiments of the present application, several concepts will be briefly introduced below:

[0030] Android installation package (Android Package, Apk): Compile the project of the Android SDK into an installation program file in the Apk format. The Apk file is actually in the zip format, but the suffix name is modified to Apk. It can be installed by directly transferring the Apk file to an Android emulator or an Android terminal for execution. After decompressing with UnZip, Dex files can be seen. Dex is short for DalvikVM executes, that is, the Android Dalvik execution program, which is not the bytecode of Java ME but Dalvik bytecode.

[0031] Software Development Kit (SDK): Usually, it is a collection of development tools for software engineers to develop application software for specific software packages, software frameworks, hardware platforms, operating systems, etc.

[0032] Host: Also known as the application (Application, APP) host. For the convenience of development and to improve the release efficiency of the application program, usually the application program can be divided into a host and a plug-in. The plug-in can be dynamically loaded based on the host. The host represents the basic running environment provided by the Apk file constructed by the APP, distinguishing the code contained in the single Apk file itself from other executable codes dynamically loaded during operation.

[0033] Plug-in: Also known as a dynamic plug-in or dynamic file, it refers to an independent file containing a part of the executable code on the Android platform, which can be an Apk file, a Dex file, a Zip file, etc. Its operation requires the host to provide a set of operating mechanisms, and there may be constraints on the implementation of the code in the plug-in file.

[0034] Components: An Android application consists of some scattered but related components, which are bound together through a project and are the cornerstone of an Android application. For example, in the embodiments of this application, the application mainly takes the Android application as an example. Then the components refer to the four major components of the Android platform: Activity, Service, Content Provider, and Broadcast Receiver. All components need to be registered before they can be used.

[0035] Among them, Activity is the window for an Android program to interact with users. It is the most basic type in the Android building blocks. It needs to maintain the states of various interfaces, do a lot of persistent things, properly manage the life cycle, and some jump logics; Service serves Activity in the background, encapsulates a complete functional logic implementation, accepts upper-layer instructions, and completes relevant tasks; Content Provider is the way for Android to provide access to third-party application data. It can derive a Content Provider class to provide data externally. It can perform selection and sorting like a database, shielding the storage details of internal data and providing a unified interface model externally, greatly simplifying upper-layer applications and providing a more convenient way for data integration; Broadcast Receiver accepts one or more intents as trigger events, receives relevant messages, does some simple processing, and converts them into a notification, unifying the event broadcast model of Android.

[0036] Class: It is a collection of a group of related attributes and behaviors. For example, Java is an object-oriented language, and all Java code is composed of classes one by one.

[0037] Class loader: For classes in a Java program, from the writing of the source code (.java text file) to the final running, it will go through compilation (outputting bytecode), loading, verification, preparation, resolution, initialization, and finally these processes of use. And the tool responsible for loading, verifying, preparing, resolving, initializing the compiled bytecode products, and finally providing the class to the program for use is called a class loader (for example, ClassLoader and its subclasses). For example, in the embodiments of this application, a new host class loader and a plugin class loader are respectively constructed to enable mutual access to classes between the host and the plugin.

[0038] Parent Delegation Model: Refers to the behavior of class loaders in the Java language when loading classes, that is, each class loader has a parent member variable (which is also a class loader object). Whenever a class needs to be loaded, the class loader will first try to load the class from the parent. Only when the parent fails to load the class will it try to load the class from the bytecode file associated with itself. This behavior is the default class loading behavior in the Java language. In practical applications, the parent of a certain class loader may be reassigned by the program, thus modifying this default class loading behavior.

[0039] compileOnly Dependency: A form of code dependency provided by the Android build tool Gradle. When specifying a compileOnly dependency, the dependent module is only visible during compilation, and its code will not be output to the final compilation product, that is, it will not participate in the final Apk packaging.

[0040] Implement Dependency: Represents a direct dependency relationship, and the dependent module can be packaged and output together with the dependant.

[0041] In practice, Android platform plugins are dynamically delivered. The goal is to control the increase in the size of the host package without affecting the functionality. Currently, the technical solutions adopted for this purpose are divided into two categories: pluginization and virtualization. Among them, for example, refer to Figure 1 As shown, it is a schematic diagram of the principle of pluginization technology in related technologies. The pluginization technology divides the complete App project into two categories: the host and the plugin. Through a custom class loader, the loading of the plugin is completed, and by modifying the code of the Android components in the plugin to make them look the same as the original Android components, the dynamic operation of the Android components in the plugin is realized; for example, refer to Figure 2 As shown, it is a schematic diagram of the principle of virtualization technology in related technologies. The virtualization technology, following the idea of simulating the Android system, completes the complete loading and running of an independent Apk file. This type of technology needs to simulate the entire Android system and provide a complete system running environment for the target Apk. Therefore, the implementation of this type of technology is quite complex and heavy. And in order to enable the Android components in the target Apk to run completely, there are a large number of system application program interface (API) hook operations in virtualization.

[0042] It can be seen that the main problems faced by the dynamic delivery and loading scheme of plugins are: class loading problems and the running problems of Android components in the plugin.

[0043] However, the plug-in technology or virtualization technology in the related art uses class loaders that are either isolated from each other or can only be one-way dependent, and cannot support the requirement that the plug-in and the host can search for classes with each other. In actual scenarios, it is a common requirement that the host needs to use the plug-in's class and the plug-in needs to use the host's class. If one party does not support loading the other party's class, it will cause the host to be unable to rely on the plug-in or the plug-in cannot rely on the host's constraint, which ultimately leads to the plug-in SDK module being unable to be dynamic. In addition, for the loading and running problems of components in the plug-in, a method is usually adopted to embed shell components and connect the shell components with the plug-in components so that the running effect of the shell components is consistent with the plug-in components. However, this method, since the plug-in components are not real Android components, and the Android system is deceived by the shell package, not only does it need to modify the code of the startup component, but in order to realize that the components can run in this way, a large number of system hooks (virtualization classes) must be performed or the source code of the components in the plug-in must be invaded to modify the key logic so that it can be loaded by wire, which is not conducive to code reuse and transplantation, and also reduces the efficiency of application development.

[0044] Therefore, in response to the above problems, an embodiment of the present application provides a method for loading plug-ins in a host, which optimizes the structure of the class loader, that is, modifies the host class loader and the plug-in class loader to enable the host and the plug-in to load classes with each other, and packages the component declaration code of the plug-in in the host executable file, that is, pre-buries the component of the plug-in in the host, and then when running the application, starts the host executable file. If the resource corresponding to the currently executed function is located in the plug-in executable file and the component of the plug-in is triggered, then when the component startup program of the operating system is triggered, based on the second class loader in the host class loader and the component declaration code of the plug-in, it is determined to find the component declaration corresponding to the currently executed function, and create a component object, and dynamically modify the component corresponding to the component object in the plug-in executable file to make the plug-in executable The resources of the corresponding components in the line file are assigned to the component object, and then the component object is run, that is, the functions implemented by the resources of the corresponding components in the plug-in executable file can be executed. In this way, by optimizing the structure of the class loader, it is finally possible to realize that the plug-in can load the host's class, and the host can load the plug-in's class, and pre-embed the component declaration in the plug-in into the host, and pre-embed it with real component information, so that the components of these plug-ins behave the same as when they are natively accessed, and are real Android components. At run time, you only need to modify the resources of the plug-in components through reflection, and there is no need to invade and modify the plug-in source code, that is, it can support the running of the plug-in components, so that the components in the plug-in can run completely in the form of real Android components to realize the corresponding functions of the plug-in components, facilitate development, and improve reusability and portability.

[0045] See also Figure 3As shown in the figure, it is a schematic diagram of an application architecture provided in an embodiment of the present application, including a terminal 100 and a server 200.

[0046] The terminal 100 can be a smart phone, a tablet computer, a notebook computer, a desktop computer, a smart speaker, a smart watch, etc., but is not limited thereto. In the embodiment of the present application, the terminal 100 can be a terminal for users or a terminal for developers.

[0047] For example, if the terminal 100 is a terminal for developers, the developers can compile the host and the plug-in on the terminal 100, and can also run the compiled host executable file to test the effect of dynamically loading the plug-in in the host. Then, after the development is completed, the compiled host executable file and the plug-in executable file can be uploaded to the server 200, and ordinary users can download the host executable file and the plug-in executable file of the application program from the server 200.

[0048] For another example, if the terminal 100 is a terminal for users, after the user (i.e., the user) downloads the host executable file and the plug-in executable file of the application program from the server 200, the user can run the application program, that is, can start the host executable file, and dynamically load and run the plug-in in the host to execute the functions implemented by the plug-in.

[0049] The server 200 is the background server of the terminal 100 and can provide various network services for the terminal 100. For different application programs, the server 200 can be regarded as the corresponding background server. The server 200 can provide the host executable file and the plug-in executable file of different application programs for the terminal 100 of the user, and can dynamically distribute different plug-ins to realize the update of the application program.

[0050] Among them, the server 200 can be an independent physical server, or a server cluster or a distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, Content Delivery Network (CDN), and big data and artificial intelligence platforms.

[0051] The terminal 100 and the server 200 can be directly or indirectly connected through wired or wireless communication methods, which are not limited in this application. For example Figure 1 it takes the terminal 100 and the server 200 as an example to be connected through the Internet to realize communication with each other.

[0052] Optionally, the above-mentioned Internet uses standard communication technologies, protocols, or a combination of both. The Internet is usually the Internet, but it can also be any network, including but not limited to any combination of a local area network (LAN), a metropolitan area network (MAN), a wide area network (WAN), a mobile, wired or wireless network, a private network, or a virtual private network. In some embodiments, technologies and / or formats including Hyper Text Mark-up Language (HTML), Extensible Markup Language (XML), etc. are used to represent the data exchanged through the network. In addition, conventional encryption technologies such as Secure Socket Layer (SSL), Transport Layer Security (TLS), Virtual Private Network (VPN), Internet Protocol Security (IPsec), etc. can be used to encrypt all or some of the links. In other embodiments, customized and / or dedicated data communication technologies can also be used to replace or supplement the above data communication technologies.

[0053] It should be noted that the plug-in loading method in the host in the embodiments of the present application is mainly executed by the terminal 100. The embodiments of the present application are described by taking this method applied to the terminal 100 as an example. For example, the terminal 100 downloads and obtains the host executable file and the plug-in executable file of a certain application from the server 200. When the user clicks to run the application on the terminal 100, the host executable file can be started, and when the host executable file is running, the classes and components in the plug-in can be called and loaded to run the plug-in to execute the functions corresponding to the plug-in.

[0054] It is worth noting that the application architecture diagram in the embodiments of the present application is for more clearly illustrating the technical solutions in the embodiments of the present application, and does not constitute a limitation on the technical solutions provided in the embodiments of the present application. And in the embodiments of the present application, Android applications are mainly used as examples. However, for other application architectures and applications, the technical solutions provided in the embodiments of the present application are equally applicable to similar problems. In each embodiment of the present application, the plug-in loading method in the host is applied to Figure 3 the application architecture shown for illustrative purposes.

[0055] Based on the above embodiments, refer to Figure 4 As shown, it is a flowchart of the plug-in loading method in the host in the embodiments of the present application. Specifically, the method includes:

[0056] Step 400: When running the application, start the host executable file of the application. The application includes a host executable file and a plug-in executable file, and at least a host class loader, a plug-in class loader, and component declaration code of the plug-in are packaged in the host executable file.

[0057] Among them, the structure of the host class loader integrates a first class loader for loading the classes of the host and a second class loader for loading the classes of the plug-in.

[0058] In the embodiment of the present application, when compiling and generating the host executable file, the packaged host executable file at least includes a host class loader, component declaration code of the plug-in, and also includes the code project of the host, a plug-in class loader, etc. The executable file is, for example, an Apk file.

[0059] Among them, the application is not limited and can be any application that can be installed and run in the terminal operating system. The application includes a host executable file and a plug-in executable file. The user only needs to install and run the host executable file on the terminal. The plug-in executable file is imperceptible to the user, but it will also be sent to the user terminal, so as to realize that when running the host executable file, classes or components of the plug-in can be dynamically loaded in the host. For example, the user clicks on the application icon in the terminal and runs the application, that is, starts the host executable file of the application.

[0060] Step 410: If the resources corresponding to the currently executed function are located in the plug-in executable file, when triggering the component startup program of the operating system corresponding to the application, based on the second class loader and the component declaration code of the plug-in, determine and find the component declaration corresponding to the currently executed function, and create a component object.

[0061] For example, an application is divided based on functions, and different functions are implemented by different plug-ins. When running the host executable file, the host class loader and the plug-in class loader are initialized, and the component starter proxy is initialized. The host executable file is executed in sequence. If triggering the startup of a component in the plug-in executable file, that is, the resources corresponding to the actual function are located in the plug-in executable file, then a component object of the operating system can be created by calling the component startup program of the operating system and the second class loader in the host class loader for loading the classes of the plug-in.

[0062] Among them, the operating system is the running environment of the application, for example, the Android system, which is not limited in the embodiment of the present application.

[0063] In practice, the operation of components depends on the operating system, and components need to be registered before they can be used, that is, registered to the operating system. In the embodiments of the present application, when triggering the startup of a component in a plugin, when the operating system component startup program creates a component object, the host class loader needs to provide implementation support. This is because when running the host executable file, if the component is not in the host executable file or cannot be found, directly creating a component object will result in an error and the application cannot continue to run. However, through the host class loader, classes in the plugin executable file can be accessed and loaded, that is, the corresponding component can be found. Therefore, an error can be avoided when creating an object.

[0064] Step 420: Modify the component corresponding to the component object in the plugin executable file, so that the resources of the corresponding component in the plugin executable file are assigned to the component object.

[0065] In the embodiments of the present application, after creating a component object, the component object points to the resources in the host executable file. In practice, the real resources of this component object are located in the plugin executable file. In order to enable the components in the plugin to run normally, in the embodiments of the present application, the resources and context objects of the components in the plugin are modified through reflection, so that the resources of the corresponding components in the plugin executable file are assigned to the component object.

[0066] Generally in Java, according to the name of a class, all information of the class can be obtained through the reflection mechanism. The reflection mechanism can realize dynamic object creation and compilation, obtain various contents of the class, making the code more flexible. That is to say, in the running state, for any class, its attributes and methods can be known, and for any object, any of its methods and attributes can be called. This function of dynamically obtaining information and dynamically calling the methods of an object can be called the reflection of Java. In this way, based on the principle of the reflection mechanism, in the embodiments of the present application, it is possible to modify the component source code of the plugin without intrusion, but through reflection modification at runtime to realize the operation of the components of the plugin.

[0067] Specifically, when executing step 420, it includes: through the reflection mechanism, based on the second type of class loader in the host class loader, load the component corresponding to the component object from the plugin executable file, and assign the resources of the corresponding component in the plugin executable file to the component object.

[0068] Step 430: Run the component object to execute the functions implemented by the resources of the corresponding component in the plugin executable file.

[0069] In this way, when the component is run, the functions in the corresponding plug-in executable file can be executed. For example, when running a certain application, an advertisement playing function needs to be executed, and this advertisement playing function is implemented by the component of a certain plug-in. In the host executable file, when running to the component of the plug-in, a component object is created, and the component in the plug-in is reflected and modified, so that the assignment of the created component object points to the resources of the component in the plug-in, and thus the dynamic operation of the component of the plug-in can be realized.

[0070] Furthermore, when starting the host executable file, if the resources corresponding to the currently executed function are located in the host executable file, that is, the triggered component is in the host, there is no need to modify. After triggering the component startup program of the operating system and creating a component object, through reflection modification, the resource assignment of the component object points to the host executable file. Therefore, after creation, the component can be directly run.

[0071] Furthermore, in the structure of the host class loader constructed in the embodiments of the present application, not only the first class loader for loading the classes of the host is integrated, but also the second class loader for loading the classes of the plug-in is included. In the structure of the plug-in class loader, the first class loader for loading the classes of the host and the second class loader for loading the classes of the plug-in are integrated. In this way, by constructing a special structure of the class loader, the class loaders between the plug-in and the host can be connected, the class search problem in the process of dynamically running the plug-in can be solved, and the problem that the classes between the host and the plug-in need to depend on each other can be solved, realizing that the host and the plug-in can load classes from each other without the deadlock of circular loading.

[0072] Furthermore, during the process of running the host executable file, through the host class loader and the plug-in class loader, when executing a certain class, it can be found and loaded, ensuring the normal operation of the program. For ordinary classes that are not components, specifically, a possible implementation manner is provided in the embodiments of the present application. For the currently triggered class, based on the host class loader, the class is searched from the host executable file through the first class loader. If not found, it is searched from the plug-in executable file through the second class loader. After determining that it is found, the class in the plug-in executable file is loaded, and the function corresponding to the class is executed.

[0073] In this way, through the host class loader, the classes of the host can be preferentially loaded, and then the classes of the plug-in can be loaded, so as to achieve the purpose of loading the classes of the plug-in in the host.

[0074] In addition, the purpose of loading the classes of the host in the plug-in can also be achieved. A possible implementation manner is provided in the embodiments of the present application. When loading the classes in the plug-in executable file or executing the functions implemented by the resources of the corresponding components in the plug-in executable file, if the classes in the host executable file are triggered to be executed, the plug-in is searched from the host executable file through the plug-in class loader, and after determining that it is found, the classes in the host executable file are loaded.

[0075] In this way, the structure of the plug-in class loader in the embodiments of the present application may further include a first class loader for loading the classes of the host, so as to meet the requirement that the plug-in needs to use the classes of the host. Thus, based on the plug-in class loader and the host class loader, mutual access to classes between the host and the plug-in is realized without the deadlock of circular loading.

[0076] Based on the above embodiments, it can be seen that in order to realize the mutual access of classes between the host and the plug-in without intrusively modifying the source code of the plug-in when the plug-in is dynamically delivered, the embodiments of the present application mainly pre-construct class loaders and embed plug-in components in the host during the compilation period, and then the purpose can be realized during the running period. The implementation method during the compilation period in the embodiments of the present application will be described below. Specifically, for the compilation period, a possible implementation method is provided in the embodiments of the present application:

[0077] S1. During compilation, obtain the program source code of the plug-in, the component declaration code of the plug-in, and the code project of the host.

[0078] The program source code of the plug-in is any SDK code that needs to be dynamically delivered.

[0079] S2. Compile the program source code of the plug-in to obtain the plug-in executable file of the plug-in.

[0080] The specific compilation method is not limited. In the embodiments of the present application, it is only necessary to pack the plug-in SDK program source code as it is into the plug-in executable file, that is, the plug-in Apk file, without packing it into the host Apk file, reducing the size of the host Apk file.

[0081] S3. During compilation, based on the dependency relationship between the program source codes of the host and the plug-in, compile and generate the declaration code of the classes in the program source code of the plug-in that needs to be called in the code project of the host, and package the code project of the host compiled based on the program source code of the plug-in, as well as the component declaration code of the plug-in, the host class loader, and the plug-in class loader to obtain the host executable file of the host.

[0082] In the embodiments of the present application, the dependency relationship between the code project of the host and the program source code of the plugin is defined as a compile-only dependency, that is, a compileOnly dependency. Without affecting the function, the program source code of the plugin only participates in compilation and is not packaged and output. And the dependency relationship between the code project of the host and the component declaration code of the plugin is defined as a direct dependency, that is, an implement dependency. Also, the dependency relationship between the code project of the host and the class loaders (including the host class loader and the plugin class loader) is also an implement dependency. In this way, when compiling the code project of the host, it depends on the program source code of the plugin during the compilation period for code writing. The life code of the classes in the program source code of the plugin that needs to be called is compiled and generated in the code project of the host. For the component declaration code of the plugin, the host class loader, and the plugin class loader, they are packaged and output together with the code project of the host to obtain the final host executable file.

[0083] It can be seen that the program source code of the dynamically loaded plugin is not included in the host Apk file. When the host executes the logic related to the plugin and cannot load the corresponding class, the program will crash. Therefore, in the embodiments of the present application, a new class loader is constructed. Specifically: the parent delegation model is adopted to construct the host class loader and the plugin class loader respectively.

[0084] The host class loader and the plugin class loader in the embodiments of the present application are specifically introduced below.

[0085] In the embodiments of the present application, for the Java language, the Java platform class loader must satisfy the parent delegation model. Therefore, the class loading action is always preferentially delegated to the parent class loader. Therefore, the host class loader constructed in the embodiments of the present application satisfies the purpose of preferentially loading the classes of the host and then loading the classes of the dynamic files.

[0086] 1) The structure of the plugin class loader is as follows: the operating system class loader of the operating system is the parent class loader of the second group of class loaders. The second group of class loaders includes the copied first class loader and the second class loader.

[0087] For example, refer to Figure 5 As shown, it is a schematic diagram of the structure of the plugin class loader in the embodiments of the present application. As Figure 5As shown, the BootClassLoader is the operating system class loader, which is used to load the software development kit (Java Development Kit, JDK) classes and system common classes of the Java language. The first type of class loader is the HostClassLoader, and the second type of class loader is the PluginClassLoader. An independent class loader, that is, the second group of class loaders, is created to load plugins, and an object copied from the first type of class loader used to load the classes of the host, that is, the CopyHostClassLoader, is held, so that the plugin class loader can load the classes of the plugins and also the classes of the host.

[0088] 2) The structure of the host class loader is as follows: The operating system class loader of the operating system is the parent class loader of the first group of class loaders. The first group of class loaders is the parent class loader of the first type of class loader. The first group of class loaders contains the copied first type of class loader and the second type of class loader.

[0089] For example, refer to Figure 6 As shown, it is a schematic diagram of the structure of the host class loader in the embodiment of the present application. An object copied from the original host class loader (i.e., the first type of class loader) of the host is created, and a new class loader is created, which holds the copied first type of class loader. And the class loading process of this newly created class loader is modified to add the logic of preferentially loading host classes and loading from the plugin class loader when the host classes cannot be loaded. At the same time, this class loader is set as the parent class loader of the first type of class loader through reflection, so that the host class loader can load the classes of the plugins.

[0090] In the embodiments of the present application, by constructing a new host class loader and a plugin class loader, it is possible to load classes between the host and the plugin mutually, and the component declarations of the plugin are built into the host executable file. All the declarations of the components in the plugin are pre-embedded into the host with real component information. Then, when running the application, the host executable file of the application is started. If the resources corresponding to the currently executed function are located in the plugin executable file, based on the host class loader, the component startup program of the operating system corresponding to the application is triggered to create a component object, and the component corresponding to the component object in the plugin executable file is modified so that the resources of the corresponding component in the plugin executable file are assigned to the component object, and then the component object is run, that is, the function implemented by the resources of the corresponding component in the plugin executable file is executed. In this way, during the running period, when the component object in the plugin is created, the plugin component is modified through reflection, such as modifying the method of the key member variables of the component of the plugin, modifying the resources and context object of the component of the plugin, without intrusion, and the dynamic operation of the plugin component is realized in a lightweight manner. Furthermore, the logic code of the plugin can be dynamically distributed without being released with the host, and there is no need to integrate the plugin into the host executable file. Finally, the purpose that the host can integrate the plugin with a very small increase in the host package size is achieved.

[0091] Based on the above embodiments, the following uses a specific application scenario for illustration. In the embodiments of the present application, the plugin loading method in the host can be divided into two aspects: the compilation period and the running period, which will be described separately below:

[0092] 1. Compilation period. Refer to Figure 7 As shown, it is the logical schematic diagram of the compilation scheme of the plugin loading method in the host in the embodiments of the present application.

[0093] As Figure 7 shown, the compilation scheme in the embodiments of the present application can include several modules to realize a non-intrusive and lightweight plugin SDK dynamic distribution method, which can specifically include a dynamic framework, a host project, the program source code of the plugin, the component declaration code of the plugin, a plugin dynamic compilation project, etc.

[0094] 1) Dynamic framework: The core module for realizing the dynamicization of the plugin SDK, mainly including: a class loader processing module, a plugin download module, and a plugin loading and running module, which are mainly responsible for the download and running of the plugin.

[0095] 2) Program source code of the plugin: The plugin code to be dynamically distributed, which is not limited in the embodiments of the present application. For example, it can be a mini-program running platform SDK for realizing the running of mini-programs, or it can also be an advertising SDK for realizing the advertising playback ability, etc.

[0096] 3) Component declaration code of the plugin: It includes plugin component definitions and resource definitions. The definitions of components such as Activity, Content Provider, Receiver, and Service involved in the plugin need to be declared in advance and provided for direct use by the host. Otherwise, they will be considered illegal components by the Android system during runtime.

[0097] 4) Host project: It is the code project of the host Apk, directly depending on the dynamic framework module to achieve the dynamic loading ability; depending on the program source code of the plugin during compilation for convenient code writing; directly depending on the component declaration code of the plugin to incorporate the component declaration code within the plugin, and then compiling and outputting the host Apk file, that is, the host executable file.

[0098] 5) Plugin dynamic compilation project: Responsible for compiling the program source code of the plugin and outputting the Apk file, outputting the dynamic product, that is, the plugin executable file.

[0099] In this way, in the embodiment of the present application, during compilation, the program source code of the plugin is directly compiled into the plugin executable file without being packaged into the host executable file, which can reduce the increase in the size of the host package caused by introducing the plugin ability. And to ensure that the plugin can be normally loaded and run in the host, a new structure of the host class loader and the plugin class loader is constructed to enable mutual loading of classes between the host and the plugin. Additionally, considering that the number of components in the plugin SDK is usually limited and will not be easily modified or added, the component definitions of the plugin are pre-embedded in the host executable file. Adopting this component pre-embedding method can solve the inspection constraints of the operating system on components and ensure the normal operation of the plugin components.

[0100] II. Runtime.

[0101] 1) When running the application, start the host executable file. When currently triggering the execution of a non-component class, based on the host class loader, first search in the host executable file through the first class loader. If not found, then search in the plugin executable file through the second class loader. After determining that it is found, load the class in the plugin executable file and execute the function corresponding to the class.

[0102] And during the execution of the class or component in the plugin executable file, if a class in the host is triggered, the class can also be loaded from the host executable file through the plugin class loader.

[0103] In this way, it can be achieved that the host class loader can load the classes of the plugin, and at the same time, the plugin class loader can also load the classes of the host.

[0104] 2) In the embodiments of the present application, the resource objects of the components in the plug-in can also be modified by reflection so that the components of the plug-in can run normally. Refer to Figure 8 As shown in

[0105] Step 800: Start the host executable file of the application.

[0106] For example, when the user clicks the icon of an application on the terminal, it triggers the running of the application and starts the host executable file.

[0107] Step 801: Start the attachBaseContext() method of the application.

[0108] Step 802: Initialize the class loader.

[0109] In the embodiments of the present application, the class loader includes a host class loader and a plug-in class loader.

[0110] Step 803: Initialize the component launcher proxy.

[0111] Step 804: Execute the program flow of the host.

[0112] That is, start the host executable file and sequentially execute the program flow of the host executable file.

[0113] Step 805: Trigger the start of the components of the plug-in.

[0114] That is, the resources corresponding to the currently executed function are located in the plug-in executable file, and the components in the plug-in are triggered to start.

[0115] Step 806: Trigger the component startup program of the operating system.

[0116] For example, if the operating system is an Android system, the Android system component startup logic runs.

[0117] Step 807: Create a component object.

[0118] In the embodiments of the present application, when calling the component startup program of the Android system to create a component object, the class loader needs to provide implementation support, and the component can be loaded in the host executable file, and the declaration code of the component can be determined, that is, it can be ensured that there will be no errors when creating the component object, and the component object can be created normally.

[0119] Step 808: Modify the component corresponding to the component object in the plug-in executable file.

[0120] For example, by modifying the resources and context objects of the corresponding components in the plugin through reflection, the resources of the corresponding components in the plugin executable file are assigned to the component objects, and the normal operation of the components in the plugin can be completed without invading the source code of the components in the plugin.

[0121] Step 809: Run the component.

[0122] That is, the components of the plugin can run normally to execute the functions implemented by the resources of the components.

[0123] In this way, in the embodiment of the present application, without invading and modifying the source code of the components of the plugin, the components of the plugin can run normally, achieving the purpose of lightweight dynamic distribution of the plugin.

[0124] In addition, in the embodiment of the present application, the plugin loading method in the host can be applied to various scenarios such as reducing the size of the host package of Android platform applications, dynamically accessing and distributing some independent ability plugin modules, etc. In this way, different function plugins can be dynamically loaded in the host of the application program. The plugin can be developed by a third party without limitation, which is convenient for development and reuse. By dynamically distributing the plugin, the application program can be continuously updated.

[0125] Based on the same inventive concept, in the embodiment of the present application, a plugin loading device in the host is further provided. The plugin loading device in the host can be, for example, the terminal in the foregoing embodiment. The plugin loading device in the host can be a hardware structure, a software module, or a combination of a hardware structure and a software module. Based on the above embodiment, refer to Figure 9 As shown, a plugin loading device in the host in the embodiment of the present application specifically includes:

[0126] A startup module 90, configured to start the host executable file of the application program when the application program is running. The application program includes a host executable file and a plugin executable file. At least a host class loader, a plugin class loader, and component declaration codes of the plugin are packaged in the host executable file. In the structure of the host class loader, a first class loader for loading the classes of the host and a second class loader for loading the classes of the plugin are integrated;

[0127] A creation module 91, configured to, if the resources corresponding to the currently executed function are located in the plugin executable file, when triggering the component startup program of the operating system corresponding to the application program, determine and find the component declaration corresponding to the currently executed function based on the second class loader and the component declaration codes of the plugin, and create a component object;

[0128] A modification module 92, configured to modify the component corresponding to the component object in the plugin executable file, so that the resources of the corresponding component in the plugin executable file are assigned to the component object;

[0129] The running module 93 is used to run component objects to execute the functions implemented by the resources of the corresponding components in the plug-in executable file.

[0130] Optionally, it further includes a first loading module 94 for:

[0131] For the currently triggered class to be executed, based on the host class loader, search for the class from the host executable file through the first class loader. If not found, search for it from the plug-in executable file through the second class loader. After determining that it is found, load the class in the plug-in executable file and execute the function corresponding to the class.

[0132] Optionally, at least a plug-in class loader is also packaged in the host executable file. The structure of the plug-in class loader integrates a first class loader for loading the classes of the host and a second class loader for loading the classes of the plug-in. Then it further includes:

[0133] The second loading module 95 is used to load the classes in the plug-in executable file or when executing the functions implemented by the resources of the corresponding components in the plug-in executable file, if it triggers the execution of the classes in the host executable file, search for them from the host executable file through the plug-in class loader, and after determining that they are found, load the classes in the host executable file.

[0134] Optionally, when modifying the components corresponding to the component objects in the plug-in executable file so that the resources of the corresponding components in the plug-in executable file are assigned to the component objects, the modification module 92 is specifically used for:

[0135] Through the reflection mechanism, based on the second class loader in the host class loader, load the components corresponding to the component objects from the plug-in executable file, and assign the resources of the corresponding components in the plug-in executable file to the component objects.

[0136] Optionally, it further includes a compilation module 96 for:

[0137] At compilation time, obtain the program source code of the plug-in, the component declaration code of the plug-in, and the code project of the host;

[0138] Compile the program source code of the plug-in to obtain the plug-in executable file of the plug-in;

[0139] And at compilation time, based on the dependency relationship between the program source codes of the host and the plug-in, compile and generate the declaration code of the classes in the program source code of the plug-in that need to be called in the code project of the host, and package the code project of the host compiled based on the program source code of the plug-in, as well as the component declaration code of the plug-in, the host class loader, and the plug-in class loader to obtain the host executable file of the host.

[0140] Optionally, the compilation module 96 is further used for:

[0141] Adopt the parent delegation model to construct the host class loader and the plug-in class loader respectively;

[0142] Among them, the structure of the host class loader is as follows: The operating system class loader of the operating system is the parent class loader of the first group of class loaders. The first group of class loaders is the parent class loader of the first class loader. The first group of class loaders contains the replicated first class loader and the second class loader;

[0143] The structure of the plug-in class loader is as follows: The operating system class loader of the operating system is the parent class loader of the second group of class loaders. The second group of class loaders contains the replicated first class loader and the second class loader.

[0144] Based on the above embodiments, refer to Figure 10 which shows the structural schematic diagram of the electronic device in the embodiments of the present application.

[0145] The embodiments of the present application provide an electronic device. This electronic device can be the terminal in the foregoing embodiments. This electronic device may include a processor 1010 (Center Processing Unit, CPU), a memory 1020, an input device 1030, an output device 1040, etc.

[0146] The memory 1020 may include a read-only memory (ROM) and a random access memory (RAM), and provide the program instructions and data stored in the memory 1020 to the processor 1010. In the embodiments of the present application, the memory 1020 may be used to store the program of any host plug-in loading method in the embodiments of the present application.

[0147] The processor 1010 is used to execute any host plug-in loading method in the embodiments of the present application by calling the program instructions stored in the memory 1020.

[0148] Based on the above embodiments, in the embodiments of the present application, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, it implements the host plug-in loading method in any of the above method embodiments.

[0149] Those of ordinary skill in the art can understand that all or part of the steps to implement the above method embodiments can be completed by hardware related to program instructions. The foregoing program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps including the above method embodiments; and the foregoing storage medium includes: various media such as removable storage devices, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), magnetic disks, or optical discs that can store program codes.

[0150] Alternatively, if the above-mentioned integrated units of the present invention are implemented in the form of software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiments of the present invention essentially or the part that contributes to the prior art can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the methods described in the various embodiments of the present invention. The foregoing storage medium includes: various media that can store program codes, such as removable storage devices, ROM, RAM, magnetic disks, or optical discs.

Claims

1. A method for loading plugins in a host, characterized in that, it includes: When running an application, start the host executable file of the application. Among them, the application includes a host executable file and a plugin executable file. At least the host class loader, the plugin class loader, and the component declaration code of the plugin are packaged in the host executable file. In the structure of the host class loader, a first group of class loaders and a first class loader for loading the classes of the host are integrated; in the first group of class loaders, there are included: the replicated first class loader and a second class loader for loading the classes of the plugin; If the resources corresponding to the currently executed function are located in the plugin executable file, then when triggering the component startup program of the operating system corresponding to the application, based on the second class loader and the component declaration code of the plugin, determine and find the component declaration corresponding to the currently executed function, and create a component object; Modify the component corresponding to the component object in the plugin executable file, so that the resources of the corresponding component in the plugin executable file are assigned to the component object; Run the component object to execute the function implemented by the resources of the corresponding component in the plugin executable file.

2. The method according to claim 1, characterized in that, it further includes: For the currently triggered class to be executed, based on the host class loader, use the first class loader to search for the class from the host executable file. If not found, use the second class loader to search from the plugin executable file. After determining and finding it, load the class in the plugin executable file and execute the function corresponding to the class.

3. The method according to claim 2, characterized in that, In the structure of the plugin class loader, a first class loader for loading the classes of the host and a second class loader for loading the classes of the plugin are integrated, then it further includes: When loading the classes in the plugin executable file or executing the function implemented by the resources of the corresponding component in the plugin executable file, if the class in the host executable file is triggered to be executed, search for it from the host executable file through the plugin class loader, and after determining and finding it, load the class in the host executable file.

4. The method according to claim 1, characterized in that, Modifying the component corresponding to the component object in the plugin executable file, so that the resources of the corresponding component in the plugin executable file are assigned to the component object, specifically includes: Through the reflection mechanism, based on the second class loader in the host class loader, load the component corresponding to the component object from the plugin executable file, and assign the resources of the corresponding component in the plugin executable file to the component object.

5. The method according to claim 1, characterized in that, it further includes: During compilation, obtain the program source code of the plugin, the component declaration code of the plugin, and the code project of the host; Compile the program source code of the plugin to obtain the plugin executable file of the plugin; And during compilation, based on the dependency relationship between the source code of the host and the plug-in, the declaration code of the classes in the source code of the program that needs to call the plug-in is compiled and generated in the code project of the host. Then, the code project of the host after being compiled based on the source code of the plug-in, as well as the component declaration code of the plug-in, the host class loader, and the plug-in class loader are packaged to obtain the host executable file of the host.

6. The method according to any one of claims 1-5, characterized in that, further comprising: Adopting the parent delegation model to respectively construct the host class loader and the plug-in class loader; Wherein, the structure of the host class loader is: the operating system class loader of the operating system is the parent class loader of the first group of class loaders, and the first group of class loaders is the parent class loader of the first class loader; The structure of the plug-in class loader is: the operating system class loader of the operating system is the parent class loader of the second group of class loaders, and the second group of class loaders includes the copied first class loader and the second class loader.

7. A plug-in loading device in a host, characterized in that, comprising: A startup module for starting the host executable file of the application when the application is running. Wherein, the application includes a host executable file and a plug-in executable file. The host executable file is at least packaged with a host class loader, a plug-in class loader, and the component declaration code of the plug-in. The structure of the host class loader integrates: a first group of class loaders and a first class loader for loading the classes of the host; the first group of class loaders includes: the copied first class loader and a second class loader for loading the classes of the plug-in; A creation module for, if the resources corresponding to the currently executed function are located in the plug-in executable file, when triggering the component startup program of the operating system corresponding to the application, based on the second class loader and the component declaration code of the plug-in, determining and finding the component declaration corresponding to the currently executed function, and creating a component object; A modification module for modifying the component corresponding to the component object in the plug-in executable file, so that the resources of the corresponding component in the plug-in executable file are assigned to the component object; An operation module for operating the component object to execute the function implemented by the resources of the corresponding component in the plug-in executable file.

8. The device according to claim 7, characterized in that, further comprising a first loading module for: For the currently triggered class to be executed, based on the host class loader, searching for the class from the host executable file through the first class loader. If not found, searching for it from the plug-in executable file through the second class loader. After determining that it is found, loading the class in the plug-in executable file and executing the function corresponding to the class.

9. The device according to claim 8, characterized in that, The structure of the plug-in class loader integrates a first class loader for loading the classes of the host and a second class loader for loading the classes of the plug-in, then further comprising: The second loading module is used to load the classes in the plugin executable file or execute the functions implemented by the resources of the corresponding components in the plugin executable file. When the execution of the classes in the host executable file is triggered, it searches for the classes in the host executable file through the plugin class loader. After determining that the classes are found, it loads the classes in the host executable file.

10. The device according to any one of claims 7-9, characterized in that it further includes a compilation module for: constructing the host class loader and the plugin class loader respectively using the parent delegation model; wherein the structure of the host class loader is: the operating system class loader of the operating system is the parent class loader of the first group of class loaders, and the first group of class loaders is the parent class loader of the first class loader; the structure of the plugin class loader is: the operating system class loader of the operating system is the parent class loader of the second group of class loaders, and the second group of class loaders contains the copied first class loader and the second class loader.

11. An electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that when the processor executes the program, it implements the steps of the method according to any one of claims 1-6.

12. A computer-readable storage medium, on which a computer program is stored, characterized in that: when the computer program is executed by the processor, it implements the steps of the method according to any one of claims 1-6.

Citation Information

Patent Citations

  • User-defined class loading method and apparatus

    CN105045642A

  • Class loading method and class loader

    CN106933611A