Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@cayozin

Android Flavor wrapper object

When using multiple flavors in a single Android project, a wrapper such as below is a useful utility when checking the current flavor (e.g. when assigning different features to different flavor).

It is not recommended to check the BuildConfig.Flavor string manually outside of this wrapper, due to potential errors in the string equality. Instead, we only need to ensure the "Name" enum in this wrapper is correct, to ensure correctness throughout the project.

The example here assumes two flavors in the build.gradle: "euro" and "japan"

object Flavor {

    // NOTE: must match flavor/dimension names given in build.gradle
    enum class Name {
        euro,
        asia
    }

    val current: Name 
    get() {
        Name.values().firstOrNull { it.name.equals(BuildConfig.FLAVOR) }?.let{ name->
            return name
        } ?: run {
            throw Exception("Unknown flavor: " + BuildConfig.FLAVOR)
        }
    }

    val isEuro: Boolean = current.equals(Name.euro)
    val isAsia: Boolean = current.equals(Name.asia)
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?