Help us understand the problem. What is going on with this article?

Android Flavor wrapper object

More than 1 year has passed since last update.

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)
}
cayozin
iOS/Android development (Swift/C++/Java/Kotlin/XML/Javascript), backend (php/mySQL) and Machine Learning (Python/Scala)
ayudante
いつもユーザー中心で技術者とコンサルタントがとことん考え抜く それがアユダンテです
https://ayudante.jp/
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした