Amplifying Flavor Customization with Kotlin Extensions: Crafting Tailored Experiences

<p>Kotlin stands out for its elegance, conciseness, and versatility. One of its most remarkable features is extensions, which empower developers to augment existing classes with new functionality without altering their source code.</p> <p>Today I will show how we can use the Kotlin extension for different flavors, in our Android application. Here is an example of a simple compose app showing as &ldquo;<strong>Android</strong>&rdquo; when running</p> <pre> import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import com.indexer.extenstionforeasy.ui.theme.ExtenstionForEasyTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ExtenstionForEasyTheme { // A surface container using the &#39;background&#39; color from the theme Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { Greeting(&quot;Android&quot;) } } } } } @Composable fun Greeting( name: String, modifier: Modifier = Modifier ) { Text( text = &quot;Hello $name!&quot;, modifier = modifier ) } @Preview(showBackground = true) @Composable fun GreetingPreview() { ExtenstionForEasyTheme { Greeting(&quot;Android&quot;) } }</pre> <p>Now I want to add flavor and based on the flavor I want to show &ldquo;<strong>Champagne</strong>&rdquo; and &ldquo;<strong>Melot</strong>&rdquo; so I added the flavor in the application gradle as below</p> <pre> plugins { id &#39;com.android.application&#39; id &#39;org.jetbrains.kotlin.android&#39; } android { namespace &#39;com.indexer.extenstionforeasy&#39; compileSdk 34 defaultConfig { applicationId &quot;com.indexer.extenstionforeasy&quot; minSdk 24 targetSdk 34 versionCode 1 versionName &quot;1.0&quot; testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot; vectorDrawables { useSupportLibrary true } } flavorDimensions &quot;wine&quot; productFlavors { champagne { dimension &quot;wine&quot; applicationIdSuffix &quot;.champagne&quot; } melot { dimension &quot;wine&quot; applicationIdSuffix &quot;.melot&quot; } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39; } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = &#39;1.8&#39; } sourceSets { champagne { java { srcDirs &#39;src/champange/java&#39; } } melot { java { srcDirs &#39;src/melot/java&#39; } } } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion &#39;1.3.2&#39; } packagingOptions { resources { excludes += &#39;/META-INF/{AL2.0,LGPL2.1}&#39; } } } dependencies { implementation &#39;androidx.core:core-ktx:1.10.1&#39; implementation platform(&#39;org.jetbrains.kotlin:kotlin-bom:1.8.0&#39;) implementation &#39;androidx.lifecycle:lifecycle-runtime-ktx:2.6.1&#39; implementation &#39;androidx.activity:activity-compose:1.7.2&#39; implementation platform(&#39;androidx.compose:compose-bom:2022.10.00&#39;) implementation &#39;androidx.compose.ui:ui&#39; implementation &#39;androidx.compose.ui:ui-graphics&#39; implementation &#39;androidx.compose.ui:ui-tooling-preview&#39; implementation &#39;androidx.compose.material3:material3&#39; testImplementation &#39;junit:junit:4.13.2&#39; androidTestImplementation &#39;androidx.test.ext:junit:1.1.5&#39; androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.5.1&#39; androidTestImplementation platform(&#39;androidx.compose:compose-bom:2022.10.00&#39;) androidTestImplementation &#39;androidx.compose.ui:ui-test-junit4&#39; debugImplementation &#39;androidx.compose.ui:ui-tooling&#39; debugImplementation &#39;androidx.compose.ui:ui-test-manifest&#39; }</pre> <p>Now we have two new flavors champagne and merlot as you in below.</p> <p>&nbsp;</p> <p>Flavoring allows for the creation of distinct versions of an app tailored to different scenarios, such as countries, features, or environments.</p> <p>While flavors bring numerous benefits, managing them without the aid of extensions can introduce challenges that impact code maintainability, readability, and overall development efficiency.</p> <p><a href="https://medium.com/arpalar-tech/amplifying-flavor-customization-with-kotlin-extensions-crafting-tailored-experiences-f113933e174e">Click Here</a></p>