Current location - Health Preservation Learning Network - Slimming men and women - How to dynamically load the so file of android and how to compress the apk size?
How to dynamically load the so file of android and how to compress the apk size?
Hello, I'm glad to answer your question.

1. Toolkit introduction (project address:/liyuming1978/nativelibcompression)

The Android compression toolset provides an extremely simple method to store so files with higher compression ratio than the original Android Zip (later versions can also support the compression of dynamically loaded jar packages and game resource files), and also provides a method to update and download compressed files in the in-app network, so that the application can store part of SO in the cloud and reduce the size of the application.

Compression principle: The compression tool will use LZMA algorithm to compress all so into the assert directory, and the application will be decompressed into the private directory of the application when it is first started.

Second, the composition of the tool set

Toolset is an installer, so it is recommended to install it in the default path. If installed under program files, some exceptions may be caused by the problem that win7 may have read and write permissions.

After installation, you can see four directories, all of which contain source code.

The four directories after installation are as follows

Among them, ApkLibComrepss is the source code of java command line program. In the bin subdirectory of this directory, you can find ApkCompress.jar, which can be used to convert ordinary apk files into compressed apk files.

CompressDemo is a sample code, you can refer to this code to understand how to integrate the compressed SDK.

DecRawso is a compressed SDK. Your development project needs to refer to this SDK and make some source code modifications to integrate the compression function.

RawsoCreator is a conversion tool under windows, which is generally unnecessary and only used for debugging and secondary development of compressed SDK.

Thirdly, how to integrate the compressed SDK?

Open Compression Demo, and we will take this project as an example to illustrate how to integrate compression SDK.

1. First, we need to introduce the DecRawso project.

Then you need to call DecRawso. NewInstance is placed at the initial location of the project. In this demonstration project, this method is called in MainActivity.java's OnCreate, which creates a unique decompression instance. Note: This method is asynchronous, so you can pass in a message that the handler accepts asynchronous decoding. If the parameter showProgress=true is passed in at the same time, a progress dialog box will be generated in the SDK to block the main process. Decrawso.newinstance (mcontext, null, false) is not recommended. In this way, no messages are accepted and there is no progress dialog box. Decompression will be completed automatically in the background, and will be blocked when the first load so is applied until the background decompression is completed. So if the blocking time is too long, it may cause the application to be unresponsive.

3. Method of modifying the load so file: change all System.loadlibrary(***) to? System.load(DecRawso。 GetInstance()。 GetPath(" * * * ");

For the new version, this step can be omitted, and the sdk will modify the libaray loading path of the system. In general, there will be no problem with system upgrade (the code is not standardized, and it is unlikely to modify the new code with android upgrade). If it is convenient, we still use the system. Load (declawso. getinstance()。 getpath ("* *))。

After these simple steps, the compressed SDK has been integrated into the project.

Fourthly, how to compress and publish APK?

Use ApkCompress.jar to compress and publish APK. This tool is a command line tool. The general usage of this command is: run compressapk.jar-a c:/my/test.apk-k c:/key * * # alias-x86 (you can also run java -jarComPressApk.jar).

-a followed by apk pathname, which may not be a complete path.

The -k is followed by the signature file [key storepass alias], and the key may not be a complete path name (if there is no written name, the default is CERT).

-x86 means that x86 library files need to be stored in the cloud, followed by /cloudrawso_x86.

After the command is executed, test _ Compressialign. The apk will be generated. This apk is a compressed apk.

Verb (abbreviation of verb) development mode and compression mode

In order to facilitate development, during the development process (after modifying the source code to support compression), it can be decompressed and apk can run normally. The compressed package SDK will automatically determine whether there is a compressed package. If there is no compressed package, the loaded path will be restored to the default path of android. Therefore, the most convenient development is to integrate the code first, develop as before (without compression), and compress apk when releasing.

Mixed call of intransitive verb X86 and ARM library

In the process of implementation and development, there may be some third-party libraries without x86 version. Usually ISV will not put arm's third-party libraries in x86 directory, and there will be a shortage of libraries in actual operation. In the case of insufficient library, the compressed SDK will automatically decompress the compressed package of arm on x86 devices to avoid the phenomenon of insufficient library. (Only the missing library is really loaded, and inconsistent library files are not necessarily missing libraries. )

But obviously, this will lead to inefficiency. If the libraries of the third-party so and x86 don't refer to each other at all (that is, these libraries are all called by JNI in the java layer, not in the native layer), then the third-party libraries of arm can be copied to the x86 directory, so there will be no shortage of libraries. Of course, this situation will lead to redundant copies of the arm library, which will make the compressed package bigger in the previous zip compression case, but in the new LZMA compression case, the size of the library will not increase at all, because LZMA compression can compress several related files as much as possible because the dictionary is relatively large. If the files are identical, the compression of LZMA will be basically the same as that of a single file.

If you are satisfied, please click on the right to adopt the answer. If you have any questions, please click Ask.

I hope my answer is helpful to you, and I hope it will be adopted!

~ O(∩_∩)O~