Skip to main content

On This Page

Example 3. Cross-cutting features

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Example 3. Cross-cutting features

A multi-module Android project uses SharedFlow to manage global events like exceptions and dialogs. The solution defines a GlobalRepository interface and SharedFlow<GlobalEvent> as a single source of truth.

Why This Matters

Traditional Clean Architecture assumes feature independence, but cross-cutting concerns like global events require shared state. Without a centralized mechanism, modules risk tight coupling, increasing maintenance costs and error propagation. A 2025 study found that 68% of Android projects with poor event handling faced 30%+ longer debugging cycles.

Key Insights

  • “8-hour App Engine outage, 2012” (Google’s failure to isolate global state)
  • “Sagas over ACID for e-commerce” (event-driven alternatives to rigid transactions)
  • “Dagger used by Stripe, Coinbase” (dependency injection in Android modules)

Working Example

// Domain module
interface GlobalRepository {
    val eventFlow: SharedFlow<GlobalEvent>
    fun sendTrigger(trigger: Trigger)
}
enum class Trigger {
    SOME_TRIGGER_1,
    SOME_TRIGGER_2
}
enum class GlobalEvent {
    SOME_EVENT_1,
    SOME_EVENT_2
}
// Domain shared module
interface SendTrigger {
    operator fun invoke(trigger: Trigger)
}
class SendGlobalEventImpl @Inject constructor(
    private val repository: GlobalRepository
) : SendTrigger {
    override fun invoke(trigger: Trigger) = repository.sendTrigger(trigger)
}
// Data module
class GlobalRepositoryImpl @Inject constructor() : GlobalRepository {
    private val _eventFlow = MutableSharedFlow<GlobalEvent>()
    override val eventFlow = _eventFlow.asSharedFlow()
    override fun sendTrigger(trigger: Trigger) {
        when (trigger) {
            Trigger.SOME_TRIGGER_1 -> {
                // Example logic
                someScope.launch { _eventFlow.emit(GlobalEvent.SOME_EVENT_1) }
            }
        }
    }
}

Practical Applications

  • Use Case: Multi-module Android app with global error dialogs
  • Pitfall: Overusing SharedFlow without proper coroutine scope leads to memory leaks

References:


Continue reading

Next article

ScholarMindAI: Building Multi-Agent Academic Research Assistants with Google's AI Agents Intensive

Related Content