Android 性能优化 -- Apk 瘦身 ( Shell 脚本实现 png jpg gif 转 WebP )

公司项目Apk

Andorid Studio  bulid -> Analyze Apk :

可见资源文件占了>80%的容量。

 

自己写了个Shell 脚本 ,查看 有多少种 各多少张 图片资源

#!/bin/sh  
function getImages(){
 
  echo $1"\n"
  
  for file in $1/*

    do

    if test -f $file 

    then

	if [ "${file##*.}"x = "png"x ];then
     	   
	  	 arrPng=(${arrPng[*]} $file)
		 #echo $file"				PNG Nums = "${#arrPng[@]}
	fi

	if [ "${file##*.}"x = "gif"x ];then
     	   
	 	 arrGif=(${arrGif[*]} $file)
		 #echo $file"				GIF Nums = "${#arrGif[@]}
		
        fi

	if [ "${file##*.}"x = "jpg"x ] || [ "${file##*.}"x = "jpeg"x ];then
     	   
	 	 arrJpg=(${arrJpg[*]} $file)
		 #echo $file"				JPG/JPEG Nums = "${#arrJpg[@]}
		
        fi

    else

        getImages $file
    fi

    done

}
getImages /Users/eminem/Desktop/AndroidApp/libratone_android/app/src/main/res   
echo "PNG Nums = "${#arrPng[@]}"\n""GIF Nums = "${#arrGif[@]}"\n""JPG/JPEG Nums = "${#arrJpg[@]}"\n"
#echo ${arrPng[@]}

 

1.清理不用的 资源

  •    Android studio  Analyze  lint 

 

 

  • Gradle 中 shrinkResources

 

android {

...

buildTypes {

release {

    minifyEnabled true

    shrinkResources true

}

}

 

清除无用的资源后,再打包看看效果:

 

包体积  整体  缩减了  20M !!!

 

2. 所有png / gif / jpg / jpeg 转为webp

 

A new image format for the Web

https://developers.google.com/speed/webp/download

open .bash_profile

export PATH=${PATH}:/libwebp-1.0.3-mac-10.14/bin: 

将WebP的 命令加入环境变量

完整代码如下: (遍历 res/ 下所有的文件夹和文件   PNG  JPG GIF  转换成 WebP  ,并删除源文件,保留.9的PNG图片)

#!/bin/sh  
function getImages(){
 
  echo $1"\n"
  
  for file in $1/*

    do

    if test -f $file 

    then

 	b=${file##*/}

    	c=${b%.*}

  	if [[ $c == *\.9 ]];then 

 	 	arr9=(${arr9[*]} $file)
		continue
    	fi

	if [ "${file##*.}"x = "png"x ];then
     	   
	  	 arrPng=(${arrPng[*]} $file)

		 cwebp -q 75 $file -o ${file%.*}.webp

		 rm $file

		 #echo $file"				PNG Nums = "${#arrPng[@]}
	fi

	if [ "${file##*.}"x = "gif"x ];then
     	   
	 	 arrGif=(${arrGif[*]} $file)

		 gif2webp $file -o ${file%.*}.webp -q 75 -lossy -m 6

		 rm $file
		
		 #echo $file"				GIF Nums = "${#arrGif[@]}
		
        fi

	if [ "${file##*.}"x = "jpg"x ] || [ "${file##*.}"x = "jpeg"x ];then
     	   
	 	 arrJpg=(${arrJpg[*]} $file)
 		 cwebp -q 75 $file -o ${file%.*}.webp
		 rm $file
		 #echo $file"				JPG/JPEG Nums = "${#arrJpg[@]}
		
        fi

    else

        getImages $file
    fi

    done

}
getImages /Users/eminem/Desktop/AndroidApp/libratone_android/app/src/main/res   

echo "PNG          Nums = "${#arrPng[@]}"\n""GIF          Nums = "${#arrGif[@]}"\n""JPG/JPEG     Nums = "${#arrJpg[@]}"\n"".9           Nums = "${#arr9[@]}"\n"  


 

 

所有图片资源转为WebP后,再打包看看效果:

 

牛逼了   直接到 38M  ,干掉了 小60M  !!!

 

PS:

1.gradle shrinkResources true  因为兼容性原因 可能会报错  请手动 Lint 清除无用文件

2.Lint去除无用资源  自己要慎重   我这里更多是去除了 无用资源   ,至于 字符串  xml  大家可以考虑留下备用 

3.Fresco  支持  webp  图片和动画

implementation 'com.facebook.fresco:animated-webp:2.0.0'
implementation 'com.facebook.fresco:webpsupport:2.0.0'
implementation 'com.facebook.fresco:fresco:2.0.0'

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值