Skip to content

Add variant compression to reduce generated Bazel targets for Android libraries#169

Closed
arunkumar9t2 wants to merge 4 commits intograb:masterfrom
arunkumar9t2:arun/grazel-variant-compression
Closed

Add variant compression to reduce generated Bazel targets for Android libraries#169
arunkumar9t2 wants to merge 4 commits intograb:masterfrom
arunkumar9t2:arun/grazel-variant-compression

Conversation

@arunkumar9t2
Copy link
Contributor

@arunkumar9t2 arunkumar9t2 commented Feb 2, 2026

Summary

Implements variant compression to reduce Bazel targets generated from Android projects with multiple flavors/build types. Equivalent variants are collapsed into fewer targets.

Data Flow

flowchart TB
    subgraph Analysis["Phase 1: Analysis (runs first)"]
        Task[AnalyzeVariantCompressionTask]
        Sorter[TopologicalSorter]
        Extractor[AndroidLibraryDataExtractor]
        Compressor[VariantCompressor]
        Cache[(VariantCompressionService<br/>in-memory cache)]
        
        Task --> Sorter
        Sorter -->|ordered projects| Extractor
        Extractor -->|variant data| Compressor
        Compressor -->|compression result| Cache
    end
    
    subgraph Generation["Phase 2: Generation (runs after)"]
        GenTask[GenerateBazelScriptsTask]
        Builder[ProjectBazelFileBuilder]
        TargetBuilder[AndroidLibraryTargetBuilder]
        Cache2[(VariantCompressionService)]
        Output[BUILD.bazel]
        
        GenTask --> Builder
        Builder --> TargetBuilder
        Cache2 -->|pre-computed targets| TargetBuilder
        TargetBuilder --> Output
    end
    
    Cache -.->|shared service| Cache2
Loading

How It Works

Phase 1 - Analysis:

  1. AnalyzeVariantCompressionTask runs first
  2. Projects processed in topological order (dependencies before dependents)
  3. For each project: extract variant data → compress → register result in service
  4. Results cached in VariantCompressionService (Gradle BuildService)

Phase 2 - Generation:

  1. GenerateBazelScriptsTask runs per project
  2. AndroidLibraryTargetBuilder queries the cache: service.get(project.path)
  3. Uses pre-computed compressionResult.targets instead of re-extracting
  4. Unit tests deduplicated by grouping variants by compressed suffix

Compression Logic:

  • Flavor compression: If freeDebugpaidDebug, emit single -debug target
  • Build-type compression: If -debug-release, emit single target (no suffix)
  • Dependency blocking: If dependency is expanded, dependent cannot compress

Key Files

Component File
Analysis task AnalyzeVariantCompressionTask.kt
Cache service VariantCompressionService.kt
Compression engine VariantCompressor.kt
Target builder AndroidLibraryTargetBuilder.kt
Task wiring TasksManager.kt
Topological sort TopologicalSorter.kt

Implements intelligent grouping of equivalent Android build variants
to reduce the number of generated Bazel targets.
Apply buildifier formatting: fix indentation and add trailing comma.
Changed bazel-build job to build only the demo-free-debug APK target instead of building all targets, reducing CI build time.
Reverted bazel-build to build all targets and bumped cache keys to invalidate existing caches.
@arunkumar9t2
Copy link
Contributor Author

The build is failing due to unrelated cache issue with crashlytics rules, will fix that separately

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.

2 participants