How to dynamically load Android so files, how to compress apk size

Source: Internet
Author: User

Android Compression Toolset Description document

I. Introduction to the Toolset (Project address: https://github.com/liyuming1978/NativeLibCompression)

The Android Compression toolset provides an extremely concise way to provide a much higher compression ratio of so files in a storage application than Android's original zip (later versions can also support compressing dynamically loaded jar packages, as well as game resource files), while providing an in-app network update to download compressed files. Enables applications to store parts of so in the cloud, reducing the size of the application.

Maximum Savings 50% Space!

Tested 158 terminals on the cloud platform, covering 2.3 to more than 4.4 system versions, 100% through

100% Pass the test!

8M file 1s Internal Decompression

Compression principle: The compression tool will compress all so using the LZMA algorithm to the Assert directory, and the application will be extracted to the application's private directory at the first boot time.

Second, the tool set composition

Toolset is a setup program, it is recommended to install under the default path, installed under Program files in the Win7 may have read and write permissions problems caused some exceptions

After installation, you can see 4 directories, this directory contains the source code.

The following four directories are installed:

Where APKLIBCOMREPSS is the source of the Java command-line program, in this directory of the bin subdirectory, you can find Apkcompress.jar, using this file can convert a normal apk file into a compressed APK file

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

DECRAWSO is a compressed SDK, your development project needs to refer to this SDK, and some source changes, in order to integrate the compression function

Rawsocreator is a conversion tool under Windows, which is generally not needed, and is used only when debugging and two times the development of the compression SDK.

Third, how to integrate the compression SDK

Open the Compressdemo, we use this project as an example to explain how to integrate the compression SDK

1. First need to introduce DECRAWSO engineering

2. You will then need to call decrawso.newinstancein the most initial place within your project. In this demo project, this method is called within Mainactivity.java's OnCreate, which creates a unique instance of the decompression. Note: This method is asynchronous, so you can pass in a message that handler accepts the completion of an asynchronous decoding, and if passed in the parameter showprogress=true,sdk, a progress dialog box is generated to block the main process. It is not recommended to use Decrawso.newinstance (Mcontext,null,false), this way does not accept any messages, and there is no progress dialog box, decompression will be done automatically in the background, and the first load so when the application blocked until the background decompression completed. So if the blocking time is too long, it may cause the application to become unresponsive.

3. How to modify the load so file: All system.loadlibrary (* * *) to system.load (Decrawso.getinstance (). GetPath ("* * *"));
New version, this step can be omitted, the SDK will modify the system's Libaray load path, generally, there will be no problem (informal code, small probability will be modified with the Android upgrade new code), if convenient, or the use of system.load ( Decrawso.getinstance (). GetPath ("* * *"))

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

Iv. How to compress the release apk

Use Apkcompress.jar compression to publish the APK. This tool is a command-line tool. The general use of this command is: Run Compressapk.jar-a c:/my/test.apk-k c:/key on the command line * # # alias-x86http://www.test.com (can also run JAVA–JARCOMPR Essapk.jar)

-A followed by the APK pathname, which may not be the full path

-K is followed by the signature file [key Storepasskeypass alias name], key can not be the full path name (name if not written, default is cert)

-x86 represents the need to store x86 library files in the cloud, followed by a link starting with HTTP//, and the actual storage location should be http://www.test.com/cloudrawso_x86

When the command finishes executing, test_compressalign.apk is generated. This apk is the compressed apk

V. Development mode and compression mode

In order to facilitate development, in the implementation of the development process (modified source support compression), can also not compress the so,apk can also run normally, the compression of the SDK will automatically determine whether there is a compressed package, if there is no compression package, the loaded path reverts to the Android default path. So the most convenient development is to first integrate the code, in the development process and the same as the original development (not compressed), the release of the time to compress the APK

Six, X86 and arm library mixed calls

In the implementation of the development process, there may be some third-party libraries do not have the x86 version, usually the ISV does not place arm's third-party library in the x86 directory, then in the actual operation will lead to the lack of library phenomenon occurs. In the absence of a library, the compressed SDK will automatically decompress the arm's compression package on the x86 device to avoid the lack of library phenomenon. (Only the missing library is really loaded, the library file is inconsistent and not necessarily the missing library)

But obviously this will lead to inefficient operation, if the third party so and x86 libraries do not have a mutual reference (that is, these libraries are Java layer using JNI calls, in the native layer does not call each other), you can copy arm's third-party library to the x86 directory, This way, there is no shortage of libraries. Of course, this situation will lead to an extra copy of the arm library, in the previous zip compression case, will make the compression package larger, but in the new LZMA compression, the library size will not increase at all, because LZMA compression because the dictionary is larger, can compress the associated files as much as possible, if the files are identical, The compression of the LZMA is basically consistent with a single file. Such as



Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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