新版本的Android Gradle plugin中,对于resource有了更加一步的管理,可以把unused resource移除,不仅是自己工程,并且library里面也可以没有用到的,也可以移除。
需要体验这个功能,build.gradle配置如下:
android {
... buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
为了使用shrinkResources,必须先开启minifyEnabled,依赖关系。
当你配置完毕,运行查看日志
...
:android:shrinkDebugResources
Removed unused resources: Binary resource data reduced from 2570KB to 1711KB: Removed 33%
Note: If necessary, you can disable resource shrinking by adding
android {
buildTypes {
debug {
shrinkResources false
}
}
}
:android:validateDebugSigning
...
如果需要查看更详细的具体哪些resource被移除了,可以在Gradle command输入一下命令:--info
$ ./gradlew clean assembleDebug --info | grep "Skipped unused resource"
Skipped unused resource res/anim/abc_fade_in.xml: 396 bytes
Skipped unused resource res/anim/abc_fade_out.xml: 396 bytes
Skipped unused resource res/anim/abc_slide_in_bottom.xml: 400 bytes
Skipped unused resource res/anim/abc_slide_in_top.xml: 400 bytes
Skipped unused resource res/anim/abc_slide_out_bottom.xml: 400 bytes
Skipped unused resource res/anim/abc_slide_out_top.xml: 400 bytes
Skipped unused resource res/color/rating_bar_label.xml: 472 bytes
Skipped unused resource res/drawable-xhdpi-v4/big.png: 866901 bytes
Skipped unused resource res/drawable-xhdpi-v4/ic_action_add_schedule.png: 282 bytes
Skipped unused resource res/drawable-xhdpi-v4/ic_action_remove_schedule.png: 368 bytes
Skipped unused resource res/drawable-xhdpi-v4/ic_livestream_pause.png: 1694 bytes
Skipped unused resource res/drawable-xhdpi-v4/ic_livestream_play.png: 2141 bytes
Skipped unused resource res/drawable-xhdpi-v4/ic_media_route_on_holo_light.png: 1594 bytes
Skipped unused resource res/drawable-xxhdpi-v4/actionbar_icon.png: 2002 bytes
Skipped unused resource res/drawable-xxhdpi-v4/ic_action_overflow.png: 330 bytes
Skipped unused resource res/drawable-xxhdpi-v4/ic_action_play_dark.png: 331 bytes
Skipped unused resource res/drawable/photo_banner_scrim.xml: 620 bytes
Skipped unused resource res/drawable/session_detail_photo_gradient.xml: 620 bytes
Skipped unused resource res/drawable/transparent_background_pattern.xml: 436 bytes
Skipped unused resource res/layout/activity_letterboxed_when_large.xml: 360 bytes
Skipped unused resource res/menu/sessions_context.xml: 1088 bytes
Skipped unused resource res/raw/keep.xml: 262 bytes
Skipped unused resource res/transition-v21/shared_element.xml: 1008 bytes
Skipped unused resource res/transition-v21/window_enter_exit.xml: 108 bytes
from:http://tools.android.com/tech-docs/new-build-system/resource-shrinking