# andresand/ads-kmp architecture

> Architecture diagram of the GitHub repository andresand/ads-kmp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andresand/ads-kmp
- Repository: https://github.com/andresand/ads-kmp

## Overview

##### Project Type and Purpose
- This is a Kotlin Multiplatform project that targets both Android and iOS platforms.
- The purpose is to integrate AdMob for serving ad banners in applications built using Compose Multiplatform, with shared business logic in “commonMain” and platform-specific implementations in “androidMain” and “iosMain.”
- The README clearly states the project’s goal of integrating AdMob, and the file tree confirms a separation of shared code (composeApp/commonMain) from platform-specific code (androidMain, iosMain) along with an additional iOS entry point in the iosApp directory, which contains Swift and Xcode project files.

##### Analyzing the File Structure and Architectural Choices
- The top-level directories split the project into multiple concerns:
- “composeApp” – contains the shared logic and UI definitions:
- “commonMain”: The shared code base has components like AdMobBanner and platform definitions.
- “androidMain”: Contains Android-specific code including AndroidManifest.xml and MainActivity.kt, which set up and start the Android application.
- “iosMain”: Contains iOS-specific code, providing similar functionality tailored for iOS (e.g. MainViewController.kt, AdMobBanner.ios.kt).
- “iosApp” – the native iOS wrapper containing Swift/SwiftUI code and configuration, providing the entry point from the iOS system.
- Gradle files (build.gradle.kts, gradle.properties, etc.) define build configurations and dependency management.
- The presence of dedicated directories for each platform and a shared code module indicates a modular, multi-target architecture optimized for code reusability.

##### Key Components
a. Main Components:
- Shared Module (composeApp/commonMain): Represents the core business logic and shared UI resources that include AdMob integration code common across platforms.
- Android Platform-specific Module (composeApp/androidMain): Contains Android-specific implementations such as MainActivity, Platform.android.kt, and Android resource files.
- iOS Platform-specific Module (composeApp/iosMain): Contains iOS-specific implementations including view controllers and platform-specific utility classes.
- iOS Application Wrapper (iosApp): Contains the native iOS application’s Swift code (AppDelegate, BannerAdView, ContentView, etc.), acting as the bootstrap for the iOS app.
- External AdMob Service: Represents the third-party AdMob integration used for displaying advertisements.
- Build and Configuration: Gradle and Xcode project configurations that orchestrate build and deployment processes.

b. Relationships and Interactions:
- The shared module (commonMain) acts as the base for ad-related logic and UI and is referenced by the platform-specific modules.
- Android-specific code (androidMain) and iOS-specific code (iosMain) implement platform adapters that wrap or extend the shared logic, adapting it to native behaviors.
- The iOS wrapper (iosApp) integrates with the iOS-specific module from composeApp as well as native SwiftUI code, ensuring the shared UI is displayed.
- Both platform modules communicate with the external AdMob service via the shared integration points defined in the common module.
- Build scripts coordinate the compilation and linking of the shared and platform-specific codebases.

##### Highlighting Architectural Patterns and Design Principles
- Modular Architecture: Clear separation of shared business logic from platform-specific implementations enhances code reuse.
- Adapter / Abstraction: Platform-specific adapters (Platform.android.kt and Platform.ios.kt) abstract the differences between Android and iOS implementations.
- Layered Architecture: The build configuration and dependency management (Gradle, Xcode) serve as infrastructure layers, while business and presentation logic are clearly segregated.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Build & Configuration Subgraph
    subgraph "Build & Configuration"
        Gradle["Gradle & Dependencies"]:::build
        Xcode["Xcode Project"]:::build
    end

    %% ComposeApp Modules Subgraph
    subgraph "ComposeApp Modules"
        %% Shared Module
        Shared["Shared Module (commonMain)"]:::shared

        %% Android-specific Module
        subgraph "Android-specific Module (androidMain)"
            A_Main["MainActivity.kt"]:::android
            A_Platform["Platform.android.kt"]:::android
            A_AdMob["AdMobBanner.android.kt"]:::android
        end

        %% iOS-specific Module
        subgraph "iOS-specific Module (iosMain)"
            I_MainController["MainViewController.kt"]:::ios
            I_Platform["Platform.ios.kt"]:::ios
            I_AdMob["AdMobBanner.ios.kt"]:::ios
        end
    end

    %% External AdMob Service Component
    AdMobService["External AdMob Service"]:::external

    %% iOS Application Wrapper Subgraph
    subgraph "iOS Application Wrapper (iosApp)"
        I_AppDelegate["AppDelegate.swift"]:::iosWrapper
        I_Banner["BannerAdView.swift"]:::iosWrapper
        I_Content["ContentView.swift"]:::iosWrapper
    end

    %% Relationships
    Gradle -->|"configures"| Shared
    Gradle -->|"builds"| A_Main
    Xcode -->|"builds"| I_AppDelegate

    Shared -->|"shared_logic"| A_Main
    Shared -->|"shared_logic"| I_MainController

    A_Main -->|"calls_sdk"| A_AdMob
    A_AdMob -->|"integrates_with"| AdMobService

    I_MainController -->|"adapts_logic"| I_AdMob
    I_AdMob -->|"integrates_with"| AdMobService

    I_MainController -->|"integrates"| I_AppDelegate
    I_Platform -->|"assists_in"| I_AppDelegate

    %% Styling Classes
    classDef shared fill:#B6E2D3,stroke:#333,stroke-width:2px;
    classDef android fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef ios fill:#D7BDE2,stroke:#333,stroke-width:2px;
    classDef iosWrapper fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef build fill:#D5D8DC,stroke:#333,stroke-width:2px;
    classDef external fill:#F5B7B1,stroke:#333,stroke-width:2px;

    %% Click Events for Component Mapping
    click Shared "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/commonMain/kotlin/com/sandovalandres/kmp/ads/AdMobBanner.kt"
    click A_Main "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/androidMain/kotlin/com/sandovalandres/kmp/ads/MainActivity.kt"
    click A_Platform "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/androidMain/kotlin/com/sandovalandres/kmp/ads/Platform.android.kt"
    click A_AdMob "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/androidMain/kotlin/com/sandovalandres/kmp/ads/AdMobBanner.android.kt"
    click I_MainController "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/iosMain/kotlin/com/sandovalandres/kmp/ads/MainViewController.kt"
    click I_Platform "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/iosMain/kotlin/com/sandovalandres/kmp/ads/Platform.ios.kt"
    click I_AdMob "https://github.com/andresand/ads-kmp/blob/main/composeApp/src/iosMain/kotlin/com/sandovalandres/kmp/ads/AdMobBanner.ios.kt"
    click I_AppDelegate "https://github.com/andresand/ads-kmp/blob/main/iosApp/iosApp/AppDelegate.swift"
    click I_Banner "https://github.com/andresand/ads-kmp/blob/main/iosApp/iosApp/BannerAdView.swift"
    click I_Content "https://github.com/andresand/ads-kmp/blob/main/iosApp/iosApp/ContentView.swift"
    click Gradle "https://github.com/andresand/ads-kmp/blob/main/build.gradle.kts"
    click Xcode "https://github.com/andresand/ads-kmp/blob/main/iosApp/iosApp.xcodeproj"
```
