Guide to app architecture in Android

Guide to app architecture

  • Mobile app user experiences
    • Android app contains multiple app components, including activities, fragments, services, content providers and broadcast receivers.
    • Mobile devices are resource-constrained, so at any time, the operating system might kill some app processes to make room for new ones.
    • You shouldn't store or keep in memory any application data or state in your app components, and your app components shouldn't depend on each other.
  • Common architectural principles
    • It's important to define an architecture that allows the app to scale, increases the app's robustness, and makes the app easier to test.
    • You should design your app architecture for the following principles
      • Separation of concerns
        • UI-based classes should only contain logic that handles UI and operating system interactions.
      • Drive UI from data models
        • If you base your app architecture on data model classes, you make your app more testable and robust.
  • Recommended app architecture
    • The recommendations and best practices present in this page can be applied to a broad spectrum of apps to allow them to scale, improve quality and robustness, and make them easier to test. However, you should treat them as guidelines and adapt them to your requirements as needed.
    • UI Layer
      • The role of the UI layer is to display the application data on the screen.
      • UI Layer is made of two things
    • Domain Layer
      • The domain layer is an optional layer that sits between the UI and data layers.
      • The domain layer holds complex business logic or simple business logic that is reused by multiple ViewModels.
      • Classes in this layer are commonly called use cases or interactors.
      • Each use case should have responsibility over a single functionality.
    • Data Layer
      • The data layer of made of repositories that each can contain zero to many data sources.
      • Repository classes are responsible for the following tasks
        • Exposing data to the rest of the app
        • Centralizing changes to the data
        • Resolving conflicts between multiple data sources
        • Abstracting sources of data from the rest of the app.
        • Containing business logic.
      • Each data source class should have the responsibility of working with only one source of data, which can be a file, a network source, or a local database.
  • Manage dependencies between components
    • You can use either of the following design patterns
      • Dependency injection
        • Allows classes to define their dependencies without constructing them.
      • Service locator
        • This pattern provides a registry where classes can obtain their dependencies instead of constructing them.
    • We recommend following dependency injection patterns and using the Hilt library in Android apps.
  • General best practices
    • Don't store data in app components.
    • Reduce dependencies on Android classes.
    • Create well-defined boundaries of responsibility between various modules in your app.
    • Expose as little as possible from each module.
    • Focus on the unique core of your app so it stands our from other apps.
    • Consider how to make each part of your app testable in isolation.
    • Persist as much relevant and fresh data as possible.

Do's and Don'ts

  • Each use case should have responsibility over a single functionality.
  • Each data source class should have the responsibility of working with only one source of data, which can be a file, a network source, or a local database.
  • Don't store data in app components.
  • Reduce dependencies on Android classes.
  • Create well-defined boundaries of responsibility between various modules in your app.
  • Expose as little as possible from each module.
  • Focus on the unique core of your app so it stands our from other apps.
  • Consider how to make each part of your app testable in isolation.
  • Persist as much relevant and fresh data as possible.

References



Comments

Popular posts from this blog

SSLSocketFactory in Android

Architecture Components in Android

SQLite in Android