Skip to content

Instantly share code, notes, and snippets.

@esafirm
Last active June 10, 2023 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esafirm/9b3032ff166c2c9dfa08fc3b162ae779 to your computer and use it in GitHub Desktop.
Save esafirm/9b3032ff166c2c9dfa08fc3b162ae779 to your computer and use it in GitHub Desktop.

Multi Flavor App

Using BuildConfig

if (BuildConfig.Theme == "red") {
	RedTheme { ... }
}else {
	GreenTheme { ... }
}

Using flavour source set

This is under src/red

// AppTheme.kt
@Composable
fun AppTheme(content: @Composable () -> Unit) {
	RedTheme(content)
}

This is under src/green

// AppTheme.kt
@Composable
fun AppTheme(content: @Composable () -> Unit) {
	GreenTheme(content)
}

Using flavour soruce set, but for mode

First, you need to define the dependency in build.gradle.kts

"cameraImplementation"(project(":image-source-camera")
"fileSystemImplementation"(project(":image-source-fs")

In the image-source-api module

interface ImageSource {
	...
}

In the image-source-camera module

class CameraImageSource : ImageSource {...}

in the image-source-fs module

class FileSystemImageSource : ImageSource {...}

In the app module, source set src/camera

@Module
@InstallIn(AppScope.class)
abstract class CameraImageSourceModule {
	abstract fun bindCameraImageSource(source: CameraImageSource): ImageSource
}

In the app module, source set src/filesystem

@Module
@InstallIn(AppScope.class)
abstract class CameraImageSourceModule {
	abstract fun bindCameraImageSource(source: FileSystemImageSource): ImageSource
}

The usage in other modules. We don't need to declare anything related to flavor since it's all configured via the host (application module)

class ImagePicker @Inject constructor(
	private val imageSource: ImageSource
) {
	...
}

Using Hilt

Since we're using hilt (same goes with Anvil) we don't exactly need to define the Dagger module in the custom source set. We can easily define the module in each image source module (image-source-camera and image-source-fs's src/main)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment