Skip to content

Kill Kotlin daemon before R8 to reduce peak memory on release builds#356

Merged
kaeawc merged 2 commits intomainfrom
kill-kotlin-daemon
Mar 4, 2026
Merged

Kill Kotlin daemon before R8 to reduce peak memory on release builds#356
kaeawc merged 2 commits intomainfrom
kill-kotlin-daemon

Conversation

@kaeawc
Copy link
Owner

@kaeawc kaeawc commented Mar 4, 2026

Summary

  • Implements the Kotlin daemon kill technique from Inaki's blog post directly in the build script
  • Scoped to release builds only, preventing the circular task dependency bug in the R8Booster plugin that breaks debug unit tests
  • Frees ~14-15% peak memory by releasing the Kotlin compile daemon process before R8 starts

Details

The R8Booster plugin (io.github.cdsap.r8booster) has a bug: it applies finalizedBy(killKotlinCompileDaemon) to all KotlinCompile tasks project-wide (including test tasks), and dependsOn(killKotlinCompileDaemon) to all JavaCompile tasks. This creates a circular dependency when running debug unit tests:

bundleDebugClassesToCompileJar → compileDebugJavaWithJavac → killKotlinCompileDaemon
    ↑ (finalizer of) compileDebugUnitTestKotlin → bundleDebugClassesToCompileJar ↑

The manual implementation scopes the wiring to release tasks only:

  • tasks.withType<KotlinCompile>().configureEach with a name guard for release (no test) tasks
  • tasks.matching { startsWith("minify") && endsWith("WithR8") } instead of tasks.named() since AGP 9.x registers these tasks lazily

Test Plan

  • assembleReleasekillKotlinCompileDaemon runs after compileReleaseKotlin, before minifyReleaseWithR8
  • testDebugUnitTest — no circular dependency error
  • spotlessCheck — formatting passes
  • tasks — configuration resolves cleanly

🤖 Generated with Claude Code

kaeawc and others added 2 commits March 4, 2026 10:49
Implements the technique from https://dev.to/cdsap/what-happens-when-you-kill-the-kotlin-daemon-before-r8-el7
directly in the build script rather than via the R8Booster plugin, which has
a bug where it applies task wiring to all variants (not just release), causing
a circular task dependency when running debug unit tests.

Scoping finalizedBy/dependsOn to release Kotlin compile and minify tasks avoids
the cycle while preserving the ~14-15% peak memory savings from releasing the
Kotlin daemon process before R8 starts.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@kaeawc kaeawc enabled auto-merge (squash) March 4, 2026 16:50
@kaeawc kaeawc merged commit 5a27e6c into main Mar 4, 2026
15 checks passed
@kaeawc kaeawc deleted the kill-kotlin-daemon branch March 4, 2026 16:53
@github-actions
Copy link

github-actions bot commented Mar 4, 2026

Diffuse output:

OLD: app-base-debug.apk (signature: V2)
NEW: app-debug.apk (signature: V2)

          │          compressed          │         uncompressed         
          ├───────────┬───────────┬──────┼───────────┬───────────┬──────
 APK      │ old       │ new       │ diff │ old       │ new       │ diff 
──────────┼───────────┼───────────┼──────┼───────────┼───────────┼──────
      dex │  17.8 MiB │  17.8 MiB │  0 B │  60.1 MiB │  60.1 MiB │  0 B 
     arsc │ 468.7 KiB │ 468.7 KiB │  0 B │ 468.6 KiB │ 468.6 KiB │  0 B 
 manifest │   1.9 KiB │   1.9 KiB │  0 B │   6.1 KiB │   6.1 KiB │  0 B 
      res │  56.5 KiB │  56.5 KiB │  0 B │  64.4 KiB │  64.4 KiB │  0 B 
   native │  37.1 KiB │  37.1 KiB │  0 B │  36.5 KiB │  36.5 KiB │  0 B 
    asset │       0 B │       0 B │  0 B │       0 B │       0 B │  0 B 
    other │    46 KiB │    46 KiB │  0 B │ 104.2 KiB │ 104.2 KiB │  0 B 
──────────┼───────────┼───────────┼──────┼───────────┼───────────┼──────
    total │  18.4 MiB │  18.4 MiB │  0 B │  60.8 MiB │  60.8 MiB │  0 B 

         │          raw           │           unique            
         ├────────┬────────┬──────┼────────┬────────┬───────────
 DEX     │ old    │ new    │ diff │ old    │ new    │ diff      
─────────┼────────┼────────┼──────┼────────┼────────┼───────────
   files │     14 │     14 │    0 │        │        │           
 strings │ 241498 │ 241498 │    0 │ 222331 │ 222331 │ 0 (+0 -0) 
   types │  32802 │  32802 │    0 │  30118 │  30118 │ 0 (+0 -0) 
 classes │  28549 │  28549 │    0 │  28549 │  28549 │ 0 (+0 -0) 
 methods │ 146696 │ 146696 │    0 │ 141575 │ 141575 │ 0 (+0 -0) 
  fields │  61016 │  61016 │    0 │  60139 │  60139 │ 0 (+0 -0) 

 ARSC    │ old │ new │ diff 
─────────┼─────┼─────┼──────
 configs │ 106 │ 106 │  0   
 entries │ 372 │ 372 │  0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant