Flutter 앱 빌드 시간 개선 - Gradle 캐싱 최적화

문제 상황

회사 Flutter 프로젝트의 안드로이드 빌드가 점점 느려져서 CI에서 5~6분씩 걸리고 있었다. 로컬에서도 clean build 시 비슷한 시간이 소요됐다.

원인 분석

./gradlew --profile 옵션으로 확인해보니 dependency resolution과 compilation에 대부분의 시간이 소요되고 있었다. Gradle 캐시가 제대로 동작하지 않는 것으로 보였다.

적용한 최적화

1. Gradle 버전 업데이트

android/gradle/wrapper/gradle-wrapper.properties에서 6.7에서 7.3.1로 올렸다.

2. build.gradle 설정 개선

android {
    buildTypes {
        release {
            shrinkResources false  // 개발 중에는 비활성화
        }
    }
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

3. gradle.properties 튜닝

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError
org.gradle.caching=true

4. CI 캐시 설정

GitHub Actions에서 Gradle 캐시를 추가했다.

- uses: actions/cache@v2
  with:
    path: |
      ~/.gradle/caches
      ~/.gradle/wrapper
    key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}

결과

  • 로컬 clean build: 5분 20초 → 2분 10초
  • CI 빌드: 6분 → 2분 30초 (캐시 히트 시 1분 20초)
  • 증분 빌드는 20초 내외로 유지

특히 CI에서 캐시가 제대로 동작하면서 PR 피드백 속도가 크게 개선됐다. JVM heap을 4GB로 늘린 것도 효과가 있었던 것 같다.

Flutter 앱 빌드 시간 개선 - Gradle 캐싱 최적화