Android之Apk瘦身

阅读: 评论:0

Android之Apk瘦身

Android之Apk瘦身


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

apk是怎样产生的

可以参考Android之Ant打包流程

瘦身是什么

瘦身主要是减少apk大小

为什么要瘦身

主要有三方面

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

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

3.优化

辅助工具

 nimbledroid

怎样瘦身

minifyEnabled开启

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

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

* proguard使用会遇到很多问题:默认配置(),修改工程配置()

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

# This is a configuration file for ProGuard.
# .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
# &#" file instead of this one from your
# project.properties file.-keepattributes *Annotation*-keep public le.vending.licensing.ILicensingService
-le.vending.licensing.ILicensingService-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService# For native methods, see .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;}

转自.html

本文发布于:2024-02-02 07:08:42,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170682892342176.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:瘦身   Android   Apk
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23