A solution for dynamically loading encrypted plugins in the Android ART virtual machine
By using reflection to call makePathElements in an Android ART virtual machine to load the plug-in and dynamically replace the encrypted Dex file, the problem of loading Dex files after Android 4.4 is solved, and code protection and operation simplification is achieved.
Patent Information
- Application Number
- CN201710680509.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2017-08-10
- Publication Date
- 2025-06-13
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
After Android 4.4, the solution to directly loading Dex files in the Dalvik virtual machine is no longer applicable. A new method is needed to load Dex files to run the plug-in. At the same time, it is necessary to prevent Dex files from being decompiled and avoid code leakage.
Call makePathElements in Java files by reflecting to load the plug-in, and dynamically replace the original file in the APK with the encrypted Dex file, thereby achieving code protection and simplifying operations.
It effectively prevents Dex files from being decompiled, protects code, reduces operation complexity, and improves adaptability.
Smart Images

Figure CN109388919B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet communication technologies, and particularly to a solution for dynamically loading an encrypted plug-in in an Android ART virtual machine. Background Art
[0002] After Android 4.4, a new Android runtime environment ART (Android runtime) began to appear to replace the previous Dalvik runtime environment. The mechanism of ART is different from that of Dalvik. Under Dalvik, every time an application runs, bytecode needs to be converted into machine code through an immediate compiler, which will slow down the running efficiency of the application. In the ART environment, when the application is first installed, the bytecode will be pre-compiled into machine code to make it a real native application, that is, ahead-of-time compilation (AOT), eliminating the need to convert code in real time in the Dalvik mode. In this way, the startup and execution of the application will become faster.
[0003] The solution of directly loading Dex files in the Dalvik virtual machine is no longer applicable to mobile phones after Android 4.4. Therefore, a new method is needed to load Dex files to run plug-ins. Summary of the Invention
[0004] To overcome the deficiencies of the prior art, the purpose of the present invention is to provide a method for loading an encrypted Dex file that can overcome the above problems or at least partially solve the above problems, prevent the Dex file from being decompiled, and overcome or partially solve the increased complexity caused by directly calling DexFile in art.
[0005] The present invention adopts a scheme of reflecting and calling makePathElements in a Java file to load a plug-in and dynamically replacing the original file in the APK with an encrypted Dex file, so as to achieve the purposes of protecting code, simple implementation, and strong adaptability.
[0006] The method of the present invention is as follows:
[0007] 1. Call DexPathList in the Android art environment to load the apk file of the plug-in, obtain the resource file in the apk file, and load the corresponding Dex file; preferably, for the apk file as described above, the Dex file therein is an encrypted Dex file.
[0008] 2. Reflectively call makePathElements in DexPathList under the Android ART environment to convert the Dex file to be loaded into a dexElements object; preferably, for the Dex file to be loaded as described above, the called one is a decrypted File file; preferably, for the method of dynamically loading Dex as described above, the upper-layer dexElements object is the Element object in the DexPathList file dalvik / system / DexPathList of the application framework layer; replace the dexElements object in the existing DexPathList object with the converted dexElements object through reflection.
[0009] The present invention also provides a device for dynamically loading an encrypted Dex file in the Android ART environment, and the device includes;
[0010] 1. Dex file encryption and decryption module, used to decrypt the encrypted Dex file, complete the decryption of the Dex file to be decrypted, and obtain a dynamically loadable Dex file after decryption; preferably, the decryption module as described above should be placed in the main program and protected by means such as reinforcement or obfuscation to prevent the decryption method from being obtained by means such as decompilation.
[0011] 2. System Dex file loading module, used to create a DexClassLoader object and load the Dex file in the APK file at the specified path; preferably, for the Dex file in the APK file loaded by the system Dex file loading module as described above, it is a Dex file that has been encrypted and replaced; preferably, for the system Dex file loading module as described above, the DexClassLoader object is the DexClassLoader file dalvik / system / DexClassLoader of the application framework layer.
[0012] 3. System loading function reflection module, used to reflectively call the makePathElements function in the DexPathList file under the ART environment to obtain the converted Dex file. Used to reflectively obtain the dexElements object in the DexPathList file under the ART environment and replace it with the converted Dex file.
[0013] Beneficial effects brought by the technical solution of the present invention:
[0014] The present invention can encrypt the Dex files in the plugin APK, which can effectively prevent decompilation, code leakage, and theft of work results. And only by calling the upper layer of Android, that is, the JAVA layer, the operation can be completed, without the need to operate the underlying layer, that is, the native framework layer and the Native layer, like the prior art, which can effectively avoid increasing complexity. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0016] Figure 1 It is a schematic flow chart of dynamically loading an encrypted plugin in an Android ART virtual machine according to the present invention;
[0017] Figure 2 It is a schematic overall flow chart of the present invention; DETAILED DESCRIPTION OF THE EMBODIMENTS
[0018] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.
[0019] Combined with the attached Figure 1 The implementation process steps are as follows:
[0020] Step 1: Obtain the encrypted Dex file through downloading or other means and store it in the mobile phone memory;
[0021] Step 2: Obtain the plugin APK file through downloading or other means and store it in the mobile phone memory;
[0022] Step 3: Use the Dex file encryption and decryption module to decrypt the Dex file already in the mobile phone memory to obtain the corresponding File file object, and wait for dynamic replacement after step 4 starts loading;
[0023] Step 4: Call the system's own DexClassLoader to load the plugin APK obtained in step 2 and obtain the DexClassLoader object, where the Dex file in the APK file is an encrypted file;
[0024] Step 5: Obtain a File object for the decrypted Dex file in Step 3, and then reflectively call the makePathElements function in DexPathList to obtain an Elements object;
[0025] Step 6: Replace the dexElements object in the DexPathList object in the DexClassLoader object obtained in Step 4 with the object obtained in Step 5;
[0026] Step 7: Load the plugin normally.
[0027] Combined with the attached Figure 2 The implementation process steps are as follows:
[0028] Step 1: Normally develop an Android project;
[0029] Step 2: Export the Android project developed in Step 1 as an APK file;
[0030] Step 3: Obtain the Dex file from the APK for encryption, and replace the Dex file in the APK with the encrypted Dex file;
[0031] Step 4: Save the encrypted Dex file obtained in Step 3 separately;
[0032] Step 5: The main program respectively obtains the APK obtained in Step 2 and the encrypted APK saved in Step 4, where the APK in Step 2 has its Dex file replaced in Step 3;
[0033] Step 6: Run the plugin normally.
[0034] The above has introduced in detail a solution for dynamically loading an encrypted plugin in an Android ART virtual machine provided by an embodiment of the present invention. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A solution for dynamically loading encrypted plugins in the Android ART virtual machine. This method realizes the one-way file transfer function of the system, supports the outer end to download files from an external network file server, and at the same time the inner end supports uploading files to an internal network file server; It includes the following steps: Step 1: Obtain the encrypted Dex file through the download method and store it in the mobile phone memory; Step 2: Obtain the plugin's APK file through the download method and store it in the mobile phone memory; Step 3: Use the Dex file encryption and decryption module to decrypt the Dex file already in the mobile phone memory to obtain the corresponding File file object, and wait for dynamic replacement after Step 4 starts loading; Step 4: Call the system's own DexClassLoader to load the plugin APK obtained in Step 2 and obtain the DexClassLoader object, where the Dex file in the APK file is an encrypted file; Step 5: Obtain the File file object from the decrypted Dex file in Step 3, and then reflectively call the makePathElements function in DexPathList to obtain the Elements object; Step 6: Replace the dexElements object in the DexPathList object in the DexClassLoader object obtained in Step 4 with the object obtained in Step 5; Step 7: Normally load the plugin.
Citation Information
Patent Citations
Dynamic Android class loading method
CN103970563A
Method, device and mobile terminal for loading plugin
CN104731625A
Method and device for dynamically loading dex in android art environment
CN106648755A
Mobile terminal and application restoration method
CN106951280A