Android APK优化压缩

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/yin13753884368/article/details/100714677

###Android APK优化压缩

欢迎扫码加入QQ群

QQ群

1.使用SVG图片

  • 新建 vector Asset

    使用svg图片

  • 在app的build.gradle配置

    compile ‘com.android.support:appcompat-v7:23.2.0’

  • 兼容5.0以下

    在gradle defaultConfig下配置

      defaultConfig {
      	//将SVG指定生成维度5.0以下
      	vectorDrawables.generatedDensities('xhdpi','xxhdpi')
      	//5.0以上
      	vectorDrawables.useSupportLibrary = true 
      } 
    

2.在xml中使用AppCompatImageView代替ImageView

  • 在AppCompatImageView中使用app:srcCompat代替android:src

    使用AppCompatImageView

3.xml中使用tint着色器

4.保留指定语言资源

在gradle defaultConfig下配置

defaultConfig {
	resConfigs('zk-rCN')        
}

5.动态库打包 ndk

在gradle defaultConfig下配置

defaultConfig {
	ndk {
		//配置so库架构 armeabi真机 x86模拟器
		abiFilters "armeabi", "armeabi-v7a", "x86_64"
	}        
}

6.移除无用资源

物理删除:
点击AS上的Analyze菜单按钮,选择Run Inspection by Name 
会出现一个弹窗,输入unused resources

避免物理删除:
gradle中设置

buildTypes {
	release {
		//混淆
		minifyEnabled true
		//移除无用的resource文件
		shrinkResources true
		//Zipalign优化
		zipAlignEnabled true
		proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
		signingConfig signingConfigs.config
	}
}

自定义保留资源
xml中使用keep, tools:keep="@drawable/ic_launcher_foreground" 

7.webp格式转换

8.资源混淆和7zip压缩对齐

https://github.com/shwenzhang/AndResGuard

在app的build.gradle配置

apply plugin: 'AndResGuard'
andResGuard {
    mappingFile = file("./resource_mapping.txt")
    use7zip = true
    useSign = true
    keepRoot = false
    mergeDuplicatedRes = true
    // add <yourpackagename>.R.drawable.icon into whitelist.
    // because the launcher will get the icon with his name
    whiteList = [//https://docs.fabric.io/android/crashlytics/build-tools.html
                 "R.string.com.crashlytics.*",
                 "R.id.*",]
    compressFilePattern = ["*.png",
                           "*.jpg",
                           "*.jpeg",
                           "*.gif",
                           "resources.arsc"]
    sevenzip {
        artifact = "com.tencent.mm:SevenZip:${ANDRESGUARD_SEVENZIP_VERSION}"
    }
}

在项目的build.gradle配置

dependencies {
    classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.17'
}

微信公众号 -->> 他晓 (欢迎加入)

公众号

猜你喜欢

转载自blog.csdn.net/yin13753884368/article/details/100714677