Package structure analysis and comparison

Thin body before

After the thin body

Package file comparison

type Original size The current size Cut type
resource.arsc 1.9 M 855.9 KB Internationalized resource files, currently only zh is reserved
res/ 14.3 M 6.7 M Image to WebP
assets 42M 21.1 M Reduced FFmpeg x86 platform compilation files
lib 28M 6.6 M Reduce so files on x86

1. Image resource optimization

tinypng

  • Advantage: There is no compatibility problem
  • Disadvantages: complex compression needs to be uploaded to the web page download, do not support batch compression replacement

webp

  • Advantages: There is a batch add tool, CwebP, for batch replacement, and later consider adding webP compression to the automated packaging process

  • Disadvantages: compatibility issues, WebP format is supported from Android4.0 version; Versions between Android4.0 and Android4.2.1 do not support lossless compression and codec of WebP images in transparent format

Finally, after checking the distribution of users’ mobile phone models and versions, we decide to use WebP to optimize image resources. Of course, it is time-consuming and error-prone to convert images into WebP one by one through Android Studio. Google officially provides cWEBP tool, which can be compressed and replaced in batches with scripts. The specific syntax is as follows:

execCommand(“cwebp”, “-q”, “80”, in, “-o”, out)

80 after -q indicates that the compression ratio is 80% of the source file. In Indicates that the source file directory is replaced. Out after -o indicates the output path of the source file

Image batch replace with Webp

``golang``
package main

var fileRealPath string

func init() {
	fileRealPath = "/Users/aihuishou/Desktop/opt_rn/android"
	//fileRealPath = getCurrentDirectory()
}

func main() {
	getFileList(fileRealPath)
}

func getFileList(filep string) {
	err := filepath.Walk(filep, func(path string, f os.FileInfo, err error) error {
		if f == nil {
			return err
		}
		if f.IsDir() {
			return nil
		}

		png2webp(f.Name(), path)
		//deleteWebp(f.Name(),path)
		return nil
	})
	if err == nil {
		fmt.Println("Conversion completed"Func png2webp(name string, path string) {var isLauncher = strings.hasprefix (name,"ic_launcher")

	var isPng = strings.HasSuffix(name, ".png")

	var isJpg = strings.HasSuffix(name, ".jpg")

	var is9png = strings.HasSuffix(name, ".9.png") // Process PNG and PJG at the same time, excluding the application icon and.9 figureifisPng && ! isLauncher && ! is9png || isJpg { var out stringif isJpg {
			out = strings.TrimSuffix(path, ".jpg") + ".webp"
		}
		if isPng {
			out = strings.TrimSuffix(path, ".png") + ".webp"
		}

		cmdErr := execCommand("cwebp"."-q"."80", path, "-o", out)

		if cmdErr {
			println(path, "Replace with =======>", out)
			del := os.Remove(path)
			if del == nil {
				println(path, "Deletion succeeded!")}}}} // delete the webp file in the current directory func deleteWebp(name string, path string) {var isweb = strings.hassuffix (name,".webp")

	if isweb {
		del := os.Remove(path)
		if del == nil {
			println(path, "Deletion succeeded!"}}} // Run the cwebp conversion command funcexecCommand(commandName string, params ... string) bool { cmd := exec.Command(commandName, params...) Println(cmd.args) stdout, err := cmd.stdoutpipe ()iferr ! = nil { fmt.Println(err)return false} CMD.Start() reader := bufio.newreader (stdout) // Loop in real time to read a line from the output streamfor {
		line, err2 := reader.ReadString('\n')
		iferr2 ! = nil || io.EOF == err2 {break
		}
		fmt.Println(line)
	}

	cmd.Wait()
	return true
}

Copy the code

2, the main project thin

  • Main project deleted x86 platform support
  • The main project resConfigs is set to zh. Only Chinese language packages are supported
defaultConfig {
        resConfigs "zh"
        ndk {
            abiFilters "armeabi-v7a"}}Copy the code
  • Main project GIF image compression

According to the main project GIF image actual display size for appropriate compression


3, three party library thin

Because of the video splicing function in the project, ffMPEG-Android library file was added, resulting in a sharp increase in package volume. According to the current needs of the project, x86 platform support can be cancelled

  • Set the ABI platform to support armeabi-V7a only
  • Remove historical redundant code from the project

Afterword.

Different projects can choose different methods of volume compression according to different needs. The common APK compression techniques are summarized below

  • Redundant code and resource file cleanup

The optimized content is as follows:

Code cleanup (delete useless code in the project, when using third-party packages, try to use part of the source code to copy into the project)

Image resources clearing (in the project resolution requirements are not with high premise can only support XHDPI XXDPI XXXDPI can only support XXDPI XXXDPI two resolution)

  • Image size optimization

webp

tinypng

  • Code confusion
  • Resources to confuse

AndResGuard