Tối ưu Build Gradle
Build chậm là một trong những vấn đề phổ biến nhất khi phát triển Android. Bài viết này hướng dẫn các kỹ thuật để giảm thời gian build và cải thiện năng suất làm việc.
Tổng quan các kỹ thuật tối ưu
Phân tích Build Time
Trước khi tối ưu, cần hiểu build đang chậm ở đâu.
Build Scan
# Tạo build scan
./gradlew assembleDebug --scan
# Sau khi build xong, Gradle sẽ hiển thị URL để xem reportProfile Build
# Profile với báo cáo HTML
./gradlew assembleDebug --profile
# Report được tạo tại:
# build/reports/profile/profile-<timestamp>.htmlBuild Analyzer trong Android Studio
- Build app: Build → Make Project
- Mở Build → Build Analyzer
- Xem chi tiết các tasks chậm
Cấu hình gradle.properties
Cấu hình bộ nhớ
# gradle.properties
# Tăng heap size cho Gradle daemon
org.gradle.jvmargs=-Xmx4096m -XX:+HeapDumpOnOutOfMemoryError
# File encoding
-Dfile.encoding=UTF-8
# Tránh GC pauses dài
-XX:+UseParallelGCKhuyến nghị heap size:
- Dự án nhỏ: 2GB (
-Xmx2048m) - Dự án trung bình: 4GB (
-Xmx4096m) - Dự án lớn: 8GB (
-Xmx8192m)
⛔ Cảnh báo: Không set heap size quá cao nếu máy không đủ RAM. Để lại ít nhất 2GB cho hệ thống.
Parallel Execution
# Build các modules song song
org.gradle.parallel=true
# Số worker threads (mặc định = số CPU cores)
org.gradle.workers.max=4Build Caching
# Bật build cache (cache task outputs)
org.gradle.caching=true
# Configuration cache (cache configuration phase)
org.gradle.configuration-cache=trueConfiguration on Demand
# Chỉ configure modules cần thiết
org.gradle.configureondemand=trueVí dụ gradle.properties tối ưu hoàn chỉnh
# gradle.properties - Optimized for build performance
# === JVM Settings ===
org.gradle.jvmargs=-Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -XX:+UseParallelGC
# === Gradle Daemon ===
org.gradle.daemon=true
# === Parallel & Caching ===
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true
org.gradle.configureondemand=true
# === Android Specific ===
android.useAndroidX=true
android.nonTransitiveRClass=true
android.enableJetifier=false
# === Kotlin ===
kotlin.code.style=official
kotlin.incremental=trueTối ưu Dependencies
Tránh Dynamic Versions
// ❌ Dynamic version - slow & unpredictable
implementation("com.example:library:1.+")
implementation("com.example:library:latest.release")
// ✅ Fixed version - fast & predictable
implementation("com.example:library:1.2.3")Sử dụng implementation thay api
// ✅ implementation: Chỉ rebuild module hiện tại khi thay đổi
implementation(libs.retrofit)
// ⚠️ api: Rebuild tất cả dependent modules
api(libs.retrofit)Loại bỏ Dependencies không dùng
Kiểm tra dependencies không sử dụng với Dependency Analysis Plugin :
// settings.gradle.kts
plugins {
id("com.autonomousapps.dependency-analysis") version "1.32.0"
}./gradlew buildHealthSử dụng BOM cho compatible versions
// ✅ BOM đảm bảo versions compatible, tránh conflict resolution
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.material3)Tối ưu Annotation Processing
Sử dụng KSP thay KAPT
KSP nhanh hơn KAPT khoảng 2x:
// ❌ KAPT - chậm
plugins {
id("org.jetbrains.kotlin.kapt")
}
dependencies {
kapt(libs.room.compiler)
}
// ✅ KSP - nhanh
plugins {
alias(libs.plugins.ksp)
}
dependencies {
ksp(libs.room.compiler)
}Các thư viện đã hỗ trợ KSP
| Library | KSP Support |
|---|---|
| Room | ✅ Có |
| Hilt | ✅ Có |
| Moshi | ✅ Có |
| Glide | ✅ Có (glide-ksp) |
| Coil | Không cần |
Tối ưu Build Config
Disable BuildConfig nếu không dùng
android {
buildFeatures {
buildConfig = false // Disable nếu không dùng BuildConfig
}
}Disable không cần thiết
android {
buildFeatures {
aidl = false
renderScript = false
resValues = false
shaders = false
}
}Sử dụng Non-transitive R Classes
# gradle.properties
android.nonTransitiveRClass=trueĐiều này ngăn R class chứa resources từ dependencies, giảm compile time.
Tối ưu cho Debug Build
Disable Minification
android {
buildTypes {
debug {
isMinifyEnabled = false // Đã mặc định
isShrinkResources = false
}
}
}Sử dụng Pre-dex Libraries
Với Android Gradle Plugin 8.0+, pre-dexing được bật mặc định.
Chỉ build cho kiến trúc cần thiết
android {
defaultConfig {
// Chỉ build cho emulator khi debug
ndk {
abiFilters += listOf("x86_64", "arm64-v8a")
}
}
}Hoặc sử dụng split APKs:
android {
splits {
abi {
isEnable = true
reset()
include("x86_64", "arm64-v8a")
isUniversalApk = false
}
}
}Tối ưu Multi-module Project
Modularization đúng cách
Chia project thành các modules nhỏ giúp incremental build hiệu quả hơn:
app/ # App module
├── feature/
│ ├── home/ # Feature module
│ ├── profile/
│ └── settings/
├── core/
│ ├── network/ # Core module
│ ├── database/
│ └── common/
└── data/
└── repository/ # Data moduleSử dụng api cẩn thận
// ❌ Quá nhiều api làm chậm build
dependencies {
api(libs.retrofit)
api(libs.gson)
api(libs.okhttp)
}
// ✅ Chỉ api những gì cần expose
dependencies {
implementation(libs.retrofit)
implementation(libs.gson)
api(libs.okhttp.core) // Chỉ expose OkHttp nếu cần
}Tối ưu cho CI/CD
Gradle Cache
# GitHub Actions example
- name: Cache Gradle
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-Parallel execution
./gradlew assembleRelease --parallel --max-workers=4Skip không cần thiết
# Skip tests khi build release
./gradlew assembleRelease -x test -x lintGradle Daemon
# Disable daemon trên CI (mỗi build là fresh)
org.gradle.daemon=falseMonitoring Build Performance
Build Scan
Gradle Build Scan cung cấp insights chi tiết:
./gradlew build --scanSau đó truy cập URL được cung cấp để xem:
- Task execution timeline
- Dependency resolution
- Cache hits/misses
- Build warnings
Custom performance logging
// build.gradle.kts
gradle.taskGraph.afterTask { task ->
if (task.state.didWork) {
println("${task.path} took ${(task.state.endTime - task.state.startTime)}ms")
}
}Checklist tối ưu
Cấu hình cơ bản
- Tăng Gradle heap size phù hợp
- Bật parallel execution
- Bật build caching
- Bật configuration cache
Dependencies
- Sử dụng fixed versions (không dùng
+hoặclatest) - Ưu tiên
implementationthayapi - Loại bỏ dependencies không dùng
- Sử dụng BOM cho thư viện families
Annotation Processing
- Migrate từ KAPT sang KSP
- Sử dụng incremental annotation processing
Build Features
- Disable features không dùng (aidl, renderScript, etc.)
- Bật non-transitive R classes
Project Structure
- Modularize project hợp lý
- Tránh circular dependencies
📚 Tham khảo
Last updated on