Q42 / Template.Android

MIT License
7 stars 0 forks source link

Template.Android

A template for creating Android projects at Q42.

Using this template

  1. Click the green "Use this template" button in the Github repo to create your own repository with this code template.
  2. Clone the new repo locally on your machine.
  3. Update dependencies and check whether everything still works. After that, create a PR to the main branch of this template. This helps both your project and this template to stay up to date.
  4. Run python ./scripts/rename-project.py or python3 ./scripts/rename-project.py from the project root, to change the project name and the package names. The script will ask for your new project name and update all references.
  5. Replace google-services.json with your own Firebase config file. The template Firebase project can be found here: Firebase project Google Services currently in use:
    • Crashlytics
  6. Setup your signing key secrets for usage in automated builds, . Steps are described in CI / Automated Builds
  7. You probably want to enable the self-hosted runner (our own, free and fast, mac-mini), as described in self hosted runner, when using Github Actions.
  8. Setup/adjust uploads to Firebase app distribution.
  9. Build your awesome project :-)
  10. Consider contributing to this template when you find something that could be improved.

Contributing

To contribute, simply create a PR and let developers from other projects approve and merge it. A note on changes:

Wishlist for changes / new features

Features

Only basic features that almost all projects use, were added in this template:

Project Setup

Signing Builds

If you need local signing of release builds, copy the upload keystore into the root of your project, and call it 'upload-keystore.jks'. Note that you probably don't need to use local signing. The CI build setup builds and signs your app for you.

CI / Automated Builds

This project uses Github Actions for CI. We have added these workflows for now:

Adding your own keystore's details on Github Actions

Run the below command to generate an encoded text representation of your keystore. If you don't have one, you can generate a new keystore.

openssl base64 < upload-keystore.jks | tr -d '\n' | tee keystore.base64.txt

The above command will generate a new file called keystore.base64.txt. Open the file, copy the contents and save it to your repo's github secrets in the variable KEYSTORE_BASE_64.

Also add these repository secrets in github, and store them in your projects 1Password vault as well:

Firebase app distribution

We use a firebase app github action ( in our github actions setup) to automatically upload release builds to firebase.

To change this to your own firebase project, you need to set two secrets:

Self-hosted runner

You can run the worlkflows on our self-hosted runner (a mac mini). This is:

In a private repo you can enable the use of our self-hosted runner using [self-hosted, macOS] as desribed in the .github/workflows/\*.yml files. If you need more help: the documentation is in Notion.

For security reasons, using the mac mini is not possible on a public repo (like this template repo).

Removing Github Actions

Using Bitrise or another CI tool instead? Then you can skip the above and delete this folder:

Setup decisions

Clean architecture

We use Clean Architecture, to a very large extend. Our setup:

Q42-CA

Clean Architecture layers

Clean Architecture Module Dependencies

Use cases

DTO Models

Data Transfer Models (DTO) are preferably generated from server source code/json/schemas and do not contain any changes compared to the server contract.

Model Entities

Model Entities live in the data layer and are the local storage objects.

Model mapping

Models map from XDTO to XEntity to X and then probably into a viewstate. Mapping is always done in the outside layer, before being transported to the inner layer. See the diagram for more info.

Core modules

Core modules are used for common logic that is shared between features and is not related to any feature or model. If your core-feature is 1-2 classes only and most modules will use it, putting it in Core:utils is simpler than creating a separate module. It is the most general module in the diagram, please keep it small.

Examples of logic that can live in core:utils:

Key architecture patterns and principles

Groovy instead of kotlin script build files

In January 2023: We wrote gradle files in KTS. But many things are cumbersome using KTS, like sharing gradle files between modules in a includeBuild: adds more steps to take per shared file. We rolled it back to Groovy, let's try again in a year?

Feature gradle files

To share gradle config over modules and make adding more modules as simple as possible, we have 2 base files:

Also, feature-specific gradle files are set up, like build.dep.compose.gradle. When a feature encompasses adding multiple dependencies and/or config and you might re-use it, please add a separate gradle file for it.

No non-android modules

We do not use non-android modules because we want to use Hilt across all modules. We prefer to clarity over faster compile time, for now.

Other options we considered and denied:

  1. Domain and data could have been kotlin modules if we used a Java inject to annotate injected constructors.
    • Pro: cleanliness, faster compile time.
    • Con: no HiltModules possible in domain and data; The app module will have to contain DataModule and DomainModule to wire interfaces to their implementations.
  2. We could have mixed dagger (non-android modules) with hilt (android modules). Con: Having two types of injection, depending on where you are, is confusing.
  3. We could have used dagger only. Con: Dagger is more complex and harder to grasp for new developers.

If you use this template to create a white label app, then you might be better off with option 1. Then you actually might want the wiring to be outside the domain and data modules.

Dependency injection

We use Hilt, because compared to Dagger, it's simpler to get into for new developers. It also reduces the amount of code you need to write for DI. Because we use Hilt, and do not want the domain layer to know the data layer, app must know all modules.

Version catalog

We use a version catalog (toml) file for versioning. This is the latest feature from Gradle currently. It can be shared over (included) projects.

ViewStateString

ViewStateStrings enables you to move String logic from the View to the ViewModel, especially plurals or replacement parameter logic.

Compose Previews

Use @PreviewLightDark to generate two previews from one function, one dark and one light. You probably also want to wrap your preview content with a PreviewAppTheme{..} so that your previews have the correct theme background.

BuildConfig

BuildConfig (or gradle build type configuration) is only allowed in the app module, for clarity and to avoid bugs . If you need the config in a different module, use dependency injection and our app/ConfigModule.

Libraries

Navigation

We use compose destinations for navigation because it's low risk, high gain:

We added extra logic to navigate from ViewModel . To enable this for your ViewModel:

Networking

We choose Retrofit over Ktor because we already use it in all of our projects and have a positive experience with it. Retrofit has many more features, but does not support Kotlin Multi Platform ( yet).

JSON parsing

We use Kotlinx.serialization for all json parsing because it is fast, modern and has IDE integration (which warns you when you forget to add @Serializable annotations). It also has multiplatform support, so we can use it in our KMP projects as well.

We use Napier because it's usage is close to Timber/Tolbaaken, but Napier supports KMM.

We use Crashlytics for crash reporting. Note that Google Analytics is not added. Google [recommends] (https://firebase.google.com/docs/crashlytics/get-started?platform=android#before-you-begin) to enable it for more insight such as breadcrumbs and crash-free percentage. If you don't want to use Google Analytics, you can remove it by simply removing the dependency.

Image loading

We did not include an image loading library is this template, because not every app might need it, but we do have a suggestion. Use Coil or Landscapist-Coil. Currently, Glide had memory issues with compose at HEMA.

Theming

Inspiration Material