android {
...
defaultConfig {...}
buildTypes {...}
productFlavors {
// When building a variant that uses this flavor, the following configurations// override those in the defaultConfig block.
dev {
// To avoid using legacy multidex, set minSdkVersion to 21 or higher.
minSdkVersion 21
versionNameSuffix "-dev"
applicationIdSuffix '.dev'
}
prod {
// If you've configured the defaultConfig block for the release version of// your app, you can leave this block empty and Gradle uses configurations in// the defaultConfig block instead. You still need to create this flavor.// Otherwise, all variants use the "dev" flavor configurations.
}
}
}
android {
...
productFlavors {
dev {
...
// The following configuration limits the "dev" flavor to using// English stringresources and xxhdpi screen-density resources.
resConfigs "en", "xxhdpi"
}
...
}
}
上面的配置将会限制dev 变体只使用 english string 资源和 xxhdpi 屏幕密度资源。
int MILLIS_IN_MINUTE = 1000 * 60int minutesSinceEpoch = System.currentTimeMillis() / MILLIS_IN_MINUTE
android {
...
defaultConfig {
// Making either of these two values dynamic in the defaultConfig will// require a full APK build and reinstallation because the AndroidManifest.xml// must be updated (which is not supported by Instant Run).
versionCode 1
versionName "1.0"
...
}
// The defaultConfig values above are fixed, so your incremental builds don't// need to rebuild the manifest (and therefore the whole APK, slowing build times).// But for release builds, it's okay. So the following script iterates through// all the known variants, finds those that are "release" build types, and// changes those properties to something dynamic.
applicationVariants.all { variant ->
if (variant.buildType.name == "release") {
variant.mergedFlavor.versionCode = minutesSinceEpoch;
variant.mergedFlavor.versionName = minutesSinceEpoch + "-" + variant.flavorName;
}
}
}
为了让Gradle能够确切的知道该如何构建你的APP,在每次构建之前,构建系统配置工程的所有modules和其他依赖(即使你只想构建或者测试一个modules),这使得大型的多module 工程的构建速度变得很慢。告诉Gradle仅仅配置你想要构建的Modules,用如下步骤使 on demand 配置可用
(1) 在菜单栏上选择 File -> Settings(如果是Mac上 ,选择 Android Studio ->Preferences)
android {
...
dexOptions {
preDexLibraries true
maxProcessCount 8// Instead of setting the heap size for the DEX process, increase Gradle's// heap size to enable dex-in-process. To learm more, read the next section.// javaMaxHeapSize "2048m"
}
}
WebP是一种图片文件格式,它提供了像JPEG一样的有损压缩和像PNG一样的透明支持,但是同时它的压缩质量比JPEG或者PNG任何一个都更好,减小Image文件的大小,而不用在构建时做压缩,因此它能提高构建速度,尤其是你的APP使用了大量的图片资源。但是有一点,在解压WebP格式的图片的时候,你的设备的CPU使用将小幅度增加。 用Android Studio 可以很方便的转WebP格式,详情请看convert your images to WebP.