↓ 快轉到主要內容

Kotlin Incremental Compilation

·548 字·3 分鐘·
作者
Andreas Kung

Kotlin incremental compilation in gradle allows gradle to recompile only changed and related files.

Enable kotlin incremental compilation
#

In gradle.properties

kotlin.build.report.output=file
kotlin.incremental.useClasspathSnapshot=true
kotlin.incremental=true
kotlin.incremental.java=true

Run the project and check report file in build/reports/kotlin-build/

Build attributes:
  REBUILD_REASON: Unknown Gradle changes

Total time for Kotlin tasks: 1,924.09 s (93.8 % of all tasks time)
Time      |% of Kotlin time|Task
1,924.09 s|100.0 %         |:app:compileDevelopDebugKotlin

Task ':app:compileDevelopDebugKotlin' finished in 1,924.09 s
Compilation log for task ':app:compileDevelopDebugKotlin':
  Use module detection: true
  Non-incremental compilation will be performed: UNKNOWN_CHANGES_IN_GRADLE_INPUTS
  Cleaning outputs on rebuild
    Deleting contents of directory '/Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug'
    Deleting contents of directory '/Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/kotlin/compileDevelopDebugKotlin/cach
eable'
    Deleting contents of directory '/Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/kotlin/compileDevelopDebugKotlin/loca
l-state'
  Non-incremental compilation will be performed: UNKNOWN_CHANGES_IN_GRADLE_INPUTS

The project’s app module was completly rebuild because something changed in our gradle scripts.

Modify some source file BlowfishApp.kt and build again, the report become

Build attributes:
  REBUILD_REASON: Incremental compilation is not enabled

Total time for Kotlin tasks: 1,125.33 s (93.9 % of all tasks time)
Time      |% of Kotlin time|Task
1,118.77 s|99.4 %          |:app:compileDevelopDebugKotlin
6.56 s    |0.6 %           |:app:kaptGenerateStubsDevelopDebugKotlin

But also in the report file says Performing incremental compilation

  Performing incremental compilation
  Deleting /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug/com/blowfish/member/BlowfishApp$When
Mappings.class on clearing cache for /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/BlowfishApp.kt
  Deleting /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug/com/blowfish/member/BlowfishApp$blowfishBroadcastReceiver.class on clearing cache for /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/BlowfishApp.kt
  Deleting /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug/com/blowfish/member/BlowfishApp$onCreate$1.class on clearing cache for /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/BlowfishApp.kt
  Deleting /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug/com/blowfish/member/BlowfishApp$spec
ial$$inlined$inject$default$1.class on clearing cache for /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/BlowfishApp.kt
  Deleting /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug/com/blowfish/member/BlowfishApp$onCreate$4.class on clearing cache for /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/BlowfishApp.kt
  Deleting /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/build/tmp/kotlin-classes/developDebug/com/blowfish/member/BlowfishApp$init
AutoTrackerDCS$1.class on clearing cache for /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/BlowfishApp.kt

Reasons incremental compilation not working
#

Unknown Gradle changes
#

Gradle changes includes:

  • add/remove dependency library, change library version
  • change version name, version code of an Android project.
  • Any other file changes of gradle, or files included by gradle by apply(from = "../config/config.gradle.kts")
  • gradle.properties changes.
  • Change of gradlew arguments, such as run with ./gradlew --debug

Changes above will also invalidate gradle build caches

Remove of .java file
#

 REBUILD_REASON:
    Incremental compilation is not enabled(1)
    Untracked Java file is removed(2)

 Could not get changed for untracked removed java file /Users/blowfish/Desktop/git/blowfish/blowfish-android-member/app/src/main/java/com/blowfish/member/util/Point.java
  Non-incremental compilation will be performed: JAVA_CHANGE_UNTRACKED_FILE_IS_REMOVED

Incremental compilation is not enabled
#

People discuss about this, but no further information.

From kotlin source code, incremental compilation flag is from config isIncremental.

More gradle build info
#

If run gradlew with --debug, you get more information about whether build is incremental or not.

./gradlew --debug  app:assembleDevelopDebug |tee app.8.1-2.log

Check log file

$ grep -i "incremental" app.8.1-2.log|less

...
[DEBUG] [org.gradle.api.Task] [KOTLIN] Set task ':app:compileDevelopDebugKotlin'.incremental=true
[DEBUG] [org.gradle.api.Task] [KOTLIN] Set task ':app:compileDevelopDebugKotlin'.incremental=false
[DEBUG] [org.gradle.api.Task] [KOTLIN] Set task ':app:compileDevelopDebugKotlin'.incremental=true
...

[INFO] [org.gradle.internal.execution.steps.ResolveInputChangesStep] The input changes require a full rebuild for incremental task ':app:compileDevelopDebugKotlin'.

At first .incremental config is set to true, but if you see something like The input changes require a full rebuild, that task will not run incrementally.

We could not identify the root cause, because only the :app module in project has this problem, other modules could build incrementally.

This occurs after upgrading gradle from 7.6 to 8.2. After revert gradle back to latest 7.x and a fully rebuild. We could build :app module incrementally again.

Unconfirmed
#

  • :app modules contains aboud 2000 source files
  • The use of both kapt (dagger, autovalue, reductor) and ksp (epoxy)

TODO
#

  • Decouple dependency in :app module, separate to different gradle modules, hopefully they can build in parallel.

  • Check kotlin build report on jenkins,if any changes cause incremental build to fail, we would notice at first time.


comments powered by Disqus