Android之Apk瘦身


要实现Apk瘦身,我们首先要了解apk是怎样产生的,瘦身是什么,为什么要瘦身,有什么辅助工具来分析,以及怎样瘦身。

apk是怎样产生的

可以参考Android之Ant打包流程

瘦身是什么

瘦身主要是减少apk大小

为什么要瘦身

主要有三方面

1.减少推广费用,要推广自已的App,必然要找推广平台,但是推广平台是按App的大小来收费,这就要求apk尽可能的小

2.用于体验度,一般来说,用户想要软件尽可能的小,但实现更多的功能,相比两款app,功能相同,用户可能更青睐app体积小的

3.优化

辅助工具

 nimbledroid

怎样瘦身

minifyEnabled开启

* minifyEnabled默认debug模式下没开启,需要设置开启

* 相当于proguard:混淆、删除无用类和无用方法、代码压缩

* proguard使用会遇到很多问题:默认配置(sdk/proguard-android.txt),修改工程配置(app/proguard-rules.txt)

* 规则:第三方包不需要混淆(已混淆过)、v4、v7包不需要、webview桥梁类公共方法不需要

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, these files are no longer used. Newer
# versions are distributed with the plugin and unpacked at build time. Files in this directory are
# no longer maintained.
#别使用大小写的类名

-dontusemixedcaseclassnames
#
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*

-keep public class com.google.vending.licensing.ILicensingService
-dontnote com.google.vending.licensing.ILicensingService

-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

shrinkResources开启

 删除无用资源,如果工程中有相关的图片,但是没用到,就不会将相关的资源打包进apk中

         buildTypes {
                debug {
                    shrinkResources true
                }
            }

resConfigs过滤语言

Android 开发经常会依赖各种外部开源代码库,比如Android Support Library, Google Play Services, Facebook SDK 等等。但是这些库里面并不是所有的资源文件你都会用到。Google Play Services 里面会有一些为其他语种提供翻译,而你的app又不需要这个语种的翻译。

过滤掉不必要的语言,如果项目中只支持中文和英文,配制如下

defaultConfig {
        resConfigs "zh","en"
    }

tinyPng有损压缩png图片

tinypng官网

非透明图片使用jpeg优先

对于非透明的大图片,jpg将会比png的大小有显著的优势,在启动页,活动页等之类的大图展示区采用jpg将是很好的选择

图片格式

删除so库:保留arm、x86

如果有多个so库,可以只保留arm,x86的so文件,算得上是一个折衷方案

armeabi:针对普通的或旧的arm v5 cpu

armeabi-v7:库会对图形渲染有很大的改进,如果没有这方面的要求,可以精简。针多有浮点运算或高级扩展功能的arm v7 cpu

arm64-v8a:64位ARM设备

去除support包资源删除,进行图片替换

删除drawable-ldrtl:是layout-direction-right-to-left的缩写,意味着布局方式从右到左,主要是为了适配阿拉伯语言,是API17,即Android4.2上新出的功能

替换v4,v7 v13中无用图片资源,如使用1*1像素图生,替换v4,v7,v13中的一些图片资源

使用webp新格式图片

从Android 4.0+开始原生支持,但是不支持包含透明度,直到Android 4.2.1+才支持显示含透明度的webp,使用的时候要特别注意

webp的探寻之路

删除除三方arr库中使用的大图

通过v4包的DrawableCompat,通过着色方案完成selector效果

public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
		final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
		DrawableCompat.setTintList(wrappedDrawable, colors);
		return wrappedDrawable;
	}

转自http://www.iblogstreet.com/2016/11/07/android-apk-shrink.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值