通常情况下,应用会根据不同的设备尺寸,准备不同的资源文件,以不同的资源修饰符进行区分。
例如,不同尺寸的图片将分别放入到drawable-mdpi、drawable-hdpi等文件夹。
然而,对于一个特定的设备而言,它的尺寸是固定的,即APK中大量的资源文件,设备可能是不需要使用的。
为了解决这个问题,Android可以针对设备定制APK,即利用不同的资源文件编译出mdpi APK、hdpi APK等。
这就是Apk Splits(APK分包)机制。
具体来说,就是在APK对应的build gradle文件中添加分包信息,例如:
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "stark.a.is.zhang.beatbox"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//按分辨率进行分包
splits {
density {
enable true
exclude "ldpi", "tvdpi", "xxxhdpi"
compatibleScreens 'small', 'normal', 'large', 'xlarge'
}
}
}
目前可以根据屏幕分辨率和ABI进行分包,定义如下:
基于屏幕分辨率:
android {
...
splits {
density {
enable true
exclude "ldpi", "tvdpi", "xxxhdpi"
compatibleScreens 'small', 'normal', 'large', 'xlarge'
}
}
..........
enable: enables the density split mechanism
exclude: By default all densities are included, you can remove some densities.
include: indicate which densities to be included
reset(): reset the list of densities to be included to an empty string (this allows, in conjunctions with include, to indicate which one to use rather than which ones to ignore)
compatibleScreens: indicates a list of compatible screens.
Note that this will also always generate a universal APK with all the densities.
基于ABI:
android {
...
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a', 'mips'
universalApk true
}
}
}
enable: enables the ABIs split mechanism
exclude: By default all ABIs are included, you can remove some ABIs.
include: indicate which ABIs to be included
reset(): reset the list of ABIs to be included to an empty string (this allows, in conjunctions with include, to indicate which one to use rather than which ones to ignore)
universalApk: indicates whether to package a universal version (with all ABIs) or not. Default is false.
详细请参考:http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
此外,Android中定义了mipmap目录。
APK分包时,mipmap资源会包含在所有编译出的APK文件中。