Skip to content

构建性能优化

源:Android 官方文档 - 优化构建速度

Android 项目的构建速度直接影响开发效率。本文介绍如何通过配置优化加快构建速度。

保持工具最新

使用最新版本的工具可获得性能改进和bug修复:

  • Android Gradle Plugin (AGP)
  • Gradle
  • Android Studio
  • Kotlin Plugin

检查更新:HelpCheck for Updates

使用 KSP 替代 kapt 推荐

KSP (Kotlin Symbol Processing) 比 kapt 快 2 倍:

kotlin
plugins {
    id("com.google.devtools.ksp") version "2.1.0-1.0.29"
}

dependencies {
    ksp("androidx.room:room-compiler:2.6.1")
}
groovy
plugins {
    id 'com.google.devtools.ksp' version '2.1.0-1.0.29'
}

dependencies {
    ksp 'androidx.room:room-compiler:2.6.1'
}

避免编译不必要的资源

在 debug 构建中仅编译当前测试设备的资源:

kotlin
android {
    buildTypes {
        getByName("debug") {
            // 仅编译指定密度的资源
            resourceConfigurations.addAll(listOf("xxhdpi"))
            
            // 仅编译指定语言的资源
            resourceConfigurations.addAll(listOf("en", "zh"))
        }
    }
}
groovy
android {
    buildTypes {
        debug {
            resConfigs 'xxhdpi'
            resConfigs 'en', 'zh'
        }
    }
}

使用静态构建配置值

动态版本号会阻止某些构建优化。在 debug 构建中使用静态值:

kotlin
android {
    buildTypes {
        getByName("debug") {
            // 使用静态值
            applicationIdSuffix = ".debug"
            versionNameSuffix = "-debug"
        }
        
        getByName("release") {
            // 仅 release 使用动态值
            versionCode = computeVersionCode()
            versionName = computeVersionName()
        }
    }
}
groovy
android {
    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-debug'
        }
        
        release {
            versionCode computeVersionCode()
            versionName computeVersionName()
        }
    }
}

使用静态依赖版本

避免使用动态版本号(如 +):

不推荐

kotlin
implementation("com.example:library:1.+")
implementation("com.example:library:latest.release")

推荐

kotlin
implementation("com.example:library:1.2.3")

或使用 Version Catalog 管理版本。

创建library模块

将功能拆分为 library 模块,享受增量编译:

project/
├── app/
├── core/             # 核心功能模块
├── feature-login/    # 登录功能模块
└── feature-profile/  # 个人资料功能模块

为自定义逻辑创建 tasks

将重复的自定义构建逻辑提取为可缓存的 task:

kotlin
tasks.register("customTask") {
    inputs.file("input.txt")
    outputs.file("output.txt")
    
    doLast {
        // 自定义逻辑
    }
}

图片优化

转换为 WebP

WebP 比 PNG 小 25-35%:

Android Studio:右键图片 → Convert to WebP

禁用 PNG 压缩

debug 构建中禁用 PNG 压缩:

kotlin
android {
    buildTypes {
        getByName("debug") {
            isCrunchPngs = false
        }
    }
}
groovy
android {
    buildTypes {
        debug {
            crunchPngs false
        }
    }
}

JVM 参数优化

增加堆内存

gradle.properties 中配置:

properties
# 增加 JVM 堆大小
org.gradle.jvmargs=-Xmx4g -XX:MaxMetaspaceSize=1g -XX:+HeapDumpOnOutOfMemoryError

# 启用 Gradle Daemon
org.gradle.daemon=true

# 启用配置缓存
org.gradle.configuration-cache=true

# 启用并行编译
org.gradle.parallel=true

# 启用构建缓存
org.gradle.caching=true

使用并行垃圾回收器

properties
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC

非传递性 R 类 AGP 8.0+

减少 R 类的大小:

gradle.properties 中:

properties
android.nonTransitiveRClass=true

非常量 R 类

允许更好的增量编译:

properties
android.nonFinalResIds=true

禁用 Jetifier

如果项目已完全迁移到 AndroidX:

properties
android.enableJetifier=false

配置缓存 Gradle 8.1+

显著提升构建速度。在 gradle.properties 中启用:

properties
org.gradle.configuration-cache=true

要求

  • Gradle 8.1+
  • AGP 8.0+
  • 所有插件兼容配置缓存

构建分析

使用 Build Analyzer

Android Studio 内置工具:ViewTool WindowsBuild Analyzer

显示:

  • 总构建时间
  • 各任务耗时
  • 插件耗时
  • 优化建议

使用 Gradle Profiler

bash
./gradlew assembleDebug --profile

报告位于:build/reports/profile/

最佳实践汇总

开发时优化

properties
# gradle.properties
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true

android.nonTransitiveRClass=true
android.nonFinalResIds=true
android.enableJetifier=false
kotlin
// build.gradle.kts (debug)
buildTypes {
    getByName("debug") {
        isCrunchPngs = false
        resourceConfigurations.addAll(listOf("xxhdpi", "en"))
    }
}

依赖管理

  • 使用 KSP 替代 kapt
  • 使用静态版本号
  • 使用 Version Catalog

模块化

  • 合理拆分 library 模块
  • 避免循环依赖

定期检查

  • 使用 Build Analyzer 识别瓶颈
  • 更新工具版本
  • 移除无用依赖