android优化学习记录

1.svg

1.1 创建

	res -> new -> Vector assess 
							 Clip art 
							 local file(SVG,PSD) PSD不支持透明度和渐变

1.2.注意点

	svg需要做版本兼容 最低版本为5.0
	在app的build.gradle下的androd{
		defaultConfig{
			// 将svg图片生成 指定的png图片
			// VectorDrawables.generatedDensities("xhdpi","xxhdpi","xxxhdpi")
			// 使用support-v7兼容
			vectorDrawables.useSupportLibrary = true
		}
	}

1.3.使用

在布局文件中,eg 在imgageView下使用
app:srcCompat="@drawable/xxxx"

2.Tint 着色器 解决同张图片不同颜色

2.1 使用

在布局文件中,eg 在imgageView下使用
android:tint="@android:color/black"

3.资源打包设置

3.1 只保留指定和默认的资源

在app的build.gradle下的androd{
		defaultConfig{
			resConfigs('zh-rCN','ko') // 按需添加
		}
	}	

3.2 appcompat-v7包下会自动转成40个语言(国际化支持)

4.so库优化

4.1加载so库,在app的build.gradle下的

android{
	sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

4.2 指定so库,在app的build.gradle下的

android{
	defaultConfig{
		ndk{
			abiFilters "armeabi", "armeabi-v7a"
		}
	}
}

5.移除无用的资源 (不推荐,物理上的删除,资源要先备份)

5.1 工具栏 Refactor ->Remove Unused Resources

5.2 工具栏 Analyze -> Run Inspection By Name -> Unused Resources

6.开启代码压缩、混淆

android{
	buildTypes {
        release {
        	// 开启代码压缩
            minifyEnabled true
        }
	}
}

7.资源压缩

7.1 压缩资源

android{
	buildTypes {
        release {
        	// 开启资源压缩
            shrinkResources true
        }
	}
}

7.2 自定义要保留的资源

创建res/raw/keep.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2" />

tools:keep 属性中指定每个要保留的资源  
tools:discard 属性中指定每个要舍弃的资源  

可以参考:https://developer.android.google.cn/studio/build/shrink-code

8.webp

8.1 webp比png小25% 比jpeg小35%

8.2 使用

选中图片右键 -> convert to webP.... 

9.压缩对齐 资源混淆

猜你喜欢

转载自blog.csdn.net/androidWorkor/article/details/88894449
今日推荐