Skip to main content

111 posts tagged with "Android"

View All Tags

How to Implement the Decorator Pattern in Jetpack Compose

Published: · Last updated: · 3 min read
Don Peter
Cofounder and CTO, Appxiom

How to Implement the Decorator Pattern in Jetpack Compose

Jetpack Compose gives you an incredible amount of freedom when building Android UIs. You describe what the UI should look like, and Compose takes care of the rest. But even with this flexibility, there are moments where you want to add behavior or styling around a component - without rewriting it or making it harder to maintain.

That's where the Decorator Pattern fits in beautifully.

The decorator pattern allows you to wrap additional behavior or visual enhancements around an existing component without changing its core implementation. In Jetpack Compose, this aligns perfectly with composable functions and modifiers, letting you layer responsibilities in a clean, reusable, and scalable way.

How to Test Jetpack Compose UIs Using Espresso

Published: · Last updated: · 5 min read
Don Peter
Cofounder and CTO, Appxiom

UI bugs are sneaky. Everything looks fine on your device, animations feel smooth, and then - someone reports that a button doesn't respond, a screen doesn't load, or a critical flow breaks on a specific device. By the time you hear about it, the damage is already done.

This is where UI testing earns its keep.

With Jetpack Compose becoming the standard way to build Android UIs, testing strategies need to evolve as well. Espresso is still a powerful UI testing tool - but testing Compose-based UIs requires a slightly different mindset.

Let's walk through how to test Jetpack Compose UIs using Espresso, step by step, in a way that actually makes sense when you sit down to write tests.

Prerequisites

Before jumping into writing tests, make sure you have the basics in place:

  • An Android project using Jetpack Compose
  • Android Studio Arctic Fox or newer
  • Basic familiarity with:
    • Jetpack Compose
    • Espresso
    • JUnit
  • UI tests enabled in your project (androidTest source set)

If you already have a Compose screen running, you're good to go.

Setting Up Espresso for a Compose Project

Jetpack Compose doesn't replace Espresso - it complements it. Espresso still handles UI synchronization and assertions, while Compose provides its own testing APIs.

In your app module, make sure you have the required dependencies:

androidTestImplementation 'androidx.test.espresso:espresso-core:<version>'
androidTestImplementation 'androidx.test.ext:junit:<version>'

This setup allows Espresso and Compose Test APIs to work together seamlessly.

Writing Your First Espresso Test with Jetpack Compose

Let's put theory into practice and write a simple UI test. The goal here isn't to be fancy - it's to understand how Espresso and Jetpack Compose work together in a real test scenario.

We'll create a test that checks whether a button is visible on the screen and then performs a click on it.

Step 1: Create a UI test class

Start by creating a new Kotlin file inside your app's androidTest directory. You can name it something like ExampleEspressoTest.

This file will hold all your UI test logic.

Step 2: Import the required dependencies

You'll need imports from both Jetpack Compose testing and Espresso:

import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.*
import androidx.test.espresso.Espresso.*
import androidx.test.espresso.matcher.ViewMatchers.*
import org.junit.Rule
import org.junit.Test

These give you access to Compose test rules, UI matchers, and Espresso actions.

Step 3: Set up the Compose test rule

The test rule is what launches your Compose content in a controlled testing environment:

class ExampleEspressoTest {
@get:Rule
val composeTestRule = createComposeRule()
}

This rule tells the test runner how to render Compose UI before running assertions.

Step 4: Write your first test

Now for the actual test. We'll render a simple button and verify two things:

  1. The button is visible
  2. The button can be clicked
@Test
fun testButtonVisibilityAndClick() {
// Launch the Compose screen/activity
composeTestRule.setContent {
// Compose UI code here
Button(
onClick = { /* Button click action */ }
) {
Text("Click Me")
}
}

// Check if the button is displayed
onView(withText("Click Me")).check(matches(isDisplayed()))

// Perform a click action on the button
onView(withText("Click Me")).perform(click())
}

What's happening here:

  • setContent renders a Compose UI just for this test
  • Espresso verifies the button exists on screen
  • Espresso simulates a real user click

This might look simple - and that's the point. UI tests should clearly describe user behavior, not hide it behind complexity.

Step 5: Run the test

You can run the test directly from Android Studio or use the test runner to execute it as part of your test suite.

Once it passes, you've officially written and executed your first Espresso test for a Jetpack Compose UI.

From here, you can expand into testing state changes, navigation, error states, and full user flows.

Working with Matchers and Actions

Even when you're testing Jetpack Compose UI, Espresso's core ideas - matchers and actions - still apply. The difference is what you're interacting with. Instead of traditional View objects, you're now targeting Compose-based UI elements.

Matchers help Espresso find the UI element you care about, while actions define what you want to do with it - just like a real user would.

Commonly Used Matchers

Matchers are used to locate Compose components based on their properties:

  • withText("text") - Finds a composable that displays the given text.
  • isDisplayed() - Ensures the composable is currently visible on the screen.

These matchers make your tests readable and expressive, almost like describing what a user sees.

Commonly Used Actions

Actions simulate user interactions:

  • click() - Performs a tap on the matched Compose component.

When combined, matchers and actions let you write tests that read like user behavior:

"Find this button, make sure it's visible, then tap it."

This approach keeps your tests focused on what the user does, not on internal implementation details - which is exactly how good UI tests should behave.

Testing Jetpack Compose Components

When testing Compose components, you can use the onNode method to target specific components.

For example, to test a Button component:

onNode(hasText("Click Me")).performClick()

Verifying Assertions the Right Way

Assertions tell you whether your UI behaves as expected. For example:

  • isDisplayed(): Checks if the Compose component is currently visible on the screen.
  • hasText("text"): Checks if the Compose component contains the specified text.

Conclusion

Testing Jetpack Compose UI with Espresso isn't complicated - but it does require a shift in how you think about UI testing.

Compose simplifies UI structure.

Espresso ensures stability.

Assertions keep regressions in check.

Together, they help you ship UIs that behave correctly - not just in demos, but on real devices, under real conditions.

Because the best UI bug is the one your users never see.

Happy testing.

How to Use Vulkan for GPU Acceleration in Kotlin Android Apps

Published: · Last updated: · 5 min read
Robin Alex Panicker
Cofounder and CPO, Appxiom

Modern Android applications are expected to deliver smooth animations, rich visuals, and real-time graphical effects. However, executing heavy graphical operations on the CPU can quickly lead to performance bottlenecks, increased battery consumption, and poor user experience. For a broader look at tools that can elevate your Android development workflow, check out our guide on 10 Android libraries you really need.

Earlier, Android developers relied on RenderScript for GPU-accelerated workloads. With RenderScript now deprecated, Vulkan has emerged as the most powerful and future-ready alternative for high-performance graphics and compute operations on Android.

In this blog, we'll explore how to utilize GPU capabilities using Vulkan in Kotlin-based Android apps to efficiently handle intensive graphical workloads and unlock next-level performance.

Avoid Android App Crashes: Kotlin Best Practices

Published: · Last updated: · 6 min read
Andrea Sunny
Marketing Associate, Appxiom

You know that moment when you're rushing to book a cab, the payment is about to go through, and suddenly the app freezes? For a few seconds, you're stuck - did the payment go through or not? Do you retry? Do you close the app? That tiny moment of uncertainty is enough to frustrate most users. And more often than not, they don't come back.

That's exactly how silent damage begins in mobile apps. Not with big disasters—but with small, unexpected failures in moments that matter most. On Android, even one crash in a critical flow like login, checkout, or onboarding can quietly push users away, hurt your ratings, and impact revenue. While no app can ever be completely crash-proof, Kotlin gives you a strong safety net to reduce these risks long before users feel them.

Build Better, Ship Faster: 10 Android Libraries You Really Need

Published: · Last updated: · 7 min read
Sandra Rosa Antony
Software Engineer, Appxiom

Imagine building a house with your bare hands. Then, someone hands you a toolbox that automates half the work, ensures structural safety, and even paints the walls. That's what the right Android libraries feel like.

You don't just want to write code. You want to write clean, efficient, testable code that doesn't give you a migraine three months later. These 10 libraries? They're your survival kit.

Let's break them down together. I'll show you real examples, sprinkle in some numbers, and tell you exactly why each one deserves a spot in your next Android project. No fluff - just the stuff that actually helps.

How to Detect and Fix Android Memory Leaks Before They Crash Your App

Published: · Last updated: · 4 min read
Andrea Sunny
Marketing Associate, Appxiom

Have you ever dated someone who just… wouldn't let go?

You break up, move on, start fresh - and boom - they're still texting, still showing up in your life, refusing to be deleted.

That's your app with a memory leak.

It's holding on to screens, data, and objects long after it should've moved on. You've moved past the Activity, but it's still lingering in memory like a clingy ex who didn't get the memo.

The worst part? You might not even know it's happening.

But users will. They will feel it in the slowdowns, the crashes, the app that once felt smooth now feeling… emotionally unavailable.

And in Android, they're not just annoying. They're dangerous. They can slow down your app, cause freezes, and eventually - boom! A crash.

Let's dive into the most common memory leak scenarios in Android. I'll walk you through real-world examples, show you how to spot them, and most importantly, how to fix them.

How to Build an Offline-Capable Android App with Jetpack Compose and Kotlin

Published: · Last updated: · 6 min read
Robin Alex Panicker
Cofounder and CPO, Appxiom

The streak broke. So did the flow.

It wasn't that I forgot. I remembered, just a little too late.

Right before midnight, I opened the app to log my progress. But the screen just sat there, trying to connect. No internet. No log. No streak.

It sounds small, but if you've ever built a habit one day at a time, you know what a streak can mean. It's not just numbers. It's proof. And losing it? That stings.

That moment made one thing very clear: apps that help you grow should work with you, not against you, especially when the internet doesn't cooperate.

So let's build something better.

How to Avoid Memory Leaks in Jetpack Compose: Real Examples, Causes, and Fixes

Published: · Last updated: · 5 min read
Andrea Sunny
Marketing Associate, Appxiom

"Hey… why does this screen freeze every time I scroll too fast?"

That's what my QA pinged me at 11:30 AM on a perfectly normal Tuesday.

I brushed it off. "Probably a one-off," I thought.

But then the bug reports started trickling in:

  • "The app slows down after using it for a while."
  • "Navigation feels laggy."
  • "Sometimes it just… dies."

That's when the panic set in.

How to Use Gradle Flavors in Android to Build Multiple App Versions from One Codebase

Published: · Last updated: · 6 min read
Andrea Sunny
Marketing Associate, Appxiom

Ever wondered how big apps manage free vs paid versions, or white-label multiple client apps from a single Android project? The answer is Gradle Flavors.

Imagine you're building an app for a fitness startup. The client loves it. Then they say: "Can we also get a version for our premium users, with extra features and no ads? Oh, and one more version for our corporate partners?"

You smile, and quietly panic.

Do you:

  • Copy the codebase three times?
  • Manually toggle features before every build?
  • Cry?

Nope. You use Gradle Flavors.

How to manage UX when a bug occurs in your Android App

Published: · Last updated: · 5 min read
Andrea Sunny
Marketing Associate, Appxiom

You're out for dinner. Ambience? Perfect. Service? Spot on. Then your dish arrives. But something's off. The pasta's missing salt, or the steak's slightly overcooked. You raise your hand to get the waiter's attention. But before you can say a word, they smile and say, "The chef already noticed. A fresh plate is on its way."

It feels like magic. But really, it's just attention to detail before a complaint even happens.

That's the kind of experience your users expect from your app too. Silent problems fixed before they even realize something went wrong.

BUILDING OFFLINE-CAPABLE ANDROID APPS WITH KOTLIN AND JETPACK COMPOSE

Published: · Last updated: · 5 min read
Robin Alex Panicker
Cofounder and CPO, Appxiom

In today's mobile-first world, users expect apps to work seamlessly, even when there's no internet connection. This blog post will guide you through the process of building an offline-capable Android app using Kotlin and Jetpack Compose. We'll use a ToDo app as our example to illustrate key concepts and best practices.

Architecture Overview

Before diving into the code, let's outline the architecture we'll use:

  • UI Layer: Jetpack Compose for the user interface

  • ViewModel: To manage UI-related data and business logic

  • Repository: To abstract data sources and manage data flow

  • Local Database: Room for local data persistence

  • Remote Data Source: Retrofit for API calls (when online)

  • WorkManager: For background synchronization

Setting Up the Kotlin Project

First, ensure you have the necessary dependencies in your build.gradle file:

dependencies {
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.2")
implementation("androidx.compose.ui:ui:1.4.3")
implementation("androidx.compose.ui:ui-tooling-preview:1.4.3")
implementation("androidx.compose.material3:material3:1.1.1")

// Room
implementation("androidx.room:room-runtime:2.5.2")
implementation("androidx.room:room-ktx:2.5.2")
kapt("androidx.room:room-compiler:2.5.2")

// Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

// WorkManager
implementation("androidx.work:work-runtime-ktx:2.8.1")
}

Implementing the Local Database

We'll use Room to store ToDo items locally. First, define the entity:

@Entity(tableName = "todos")
data class ToDo(
@PrimaryKey val id: String = UUID.randomUUID().toString(),
val title: String,
val description: String,
val isCompleted: Boolean = false,
val lastModified: Long = System.currentTimeMillis()
)

Next, create the DAO (Data Access Object):

@Dao
interface ToDoDao {
@Query("SELECT * FROM todos")
fun getAllToDos(): Flow&lt;List&lt;ToDo&gt;&gt;

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertToDo(todo: ToDo)

@Update
suspend fun updateToDo(todo: ToDo)

@Delete
suspend fun deleteToDo(todo: ToDo)
}

Finally, set up the Room database:

@Database(entities = [ToDo::class], version = 1)
abstract class ToDoDatabase : RoomDatabase() {
abstract fun todoDao(): ToDoDao

companion object {
@Volatile
private var INSTANCE: ToDoDatabase? = null

fun getDatabase(context: Context): ToDoDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
ToDoDatabase::class.java,
"todo_database"
).build()
INSTANCE = instance
instance
}
}
}
}

Implementing the Repository

The repository will manage data operations and decide whether to fetch from the local database or the remote API:

class ToDoRepository(
private val todoDao: ToDoDao,
private val apiService: ApiService
) {

val allToDos: Flow&lt;List&lt;ToDo&gt;&gt; = todoDao.getAllToDos()

suspend fun refreshToDos() {
try {
val remoteToDos = apiService.getToDos()
todoDao.insertAll(remoteToDos)
} catch (e: Exception) {
// Handle network errors
}
}

suspend fun addToDo(todo: ToDo) {
todoDao.insertToDo(todo)
try {
apiService.addToDo(todo)
} catch (e: Exception) {
// Handle network errors, maybe queue for later sync
}
}

// Implement other CRUD operations similarly
}

Setting Up the ViewModel

The ViewModel will handle the UI logic and interact with the repository:

class ToDoViewModel(private val repository: ToDoRepository) : ViewModel() {
val todos = repository.allToDos.asLiveData()

fun addToDo(title: String, description: String) {
viewModelScope.launch {
val todo = ToDo(title = title, description = description)
repository.addToDo(todo)
}
}

fun refreshToDos() {
viewModelScope.launch {
repository.refreshToDos()
}
}
// Implement other operations
}

Creating the UI with Jetpack Compose

Now, let's create the UI for our ToDo app:

@Composable
fun ToDoScreen(viewModel: ToDoViewModel) {
val todos by viewModel.todos.collectAsState(initial = emptyList())
LazyColumn {
items(todos) { todo -&gt;
ToDoItem(todo)
}
item {
AddToDoButton(viewModel)
}
}
}

@Composable
fun ToDoItem(todo: ToDo) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = todo.isCompleted,
onCheckedChange = { /* Update todo */ }
)
Column(modifier = Modifier.weight(1f)) {
Text(text = todo.title, fontWeight = FontWeight.Bold)
Text(text = todo.description)
}
}
}
}

@Composable
fun AddToDoButton(viewModel: ToDoViewModel) {
var showDialog by remember { mutableStateOf(false) }
Button(onClick = { showDialog = true }) {
Text("Add ToDo")
}
if (showDialog) {
AddToDoDialog(
onDismiss = { showDialog = false },
onConfirm = { title, description -&gt;
viewModel.addToDo(title, description)
showDialog = false
}
)
}
}

@Composable
fun AddToDoDialog(onDismiss: () -&gt; Unit, onConfirm: (String, String) -&gt; Unit) {
// Implement dialog UI here
}

Implementing Background Sync with WorkManager

To ensure our app stays up-to-date even when it's not actively running, we can use WorkManager for background synchronization:

class SyncWorker(
context: Context,
params: WorkerParameters
) : CoroutineWorker(context, params) {
private val repository = ToDoRepository(
ToDoDatabase.getDatabase(context).todoDao(),
ApiService.create()
)
override suspend fun doWork(): Result {
return try {
repository.refreshToDos()
Result.success()
} catch (e: Exception) {
Result.retry()
}
}
}

Schedule the work in your application class or main activity:

class ToDoApplication : Application() {
override fun onCreate() {
super.onCreate()
setupPeriodicSync()
}

private fun setupPeriodicSync() {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val syncRequest = PeriodicWorkRequestBuilder&lt;SyncWorker&gt;(1, TimeUnit.HOURS)
.setConstraints(constraints)
.build()

WorkManager.getInstance(this).enqueueUniquePeriodicWork(
"ToDo_Sync",
ExistingPeriodicWorkPolicy.KEEP,
syncRequest
)
}
}

Handling Conflicts

When working offline, conflicts may arise when syncing data. Implement a conflict resolution strategy:

suspend fun syncToDo(todo: ToDo) {
try {
val remoteToDo = apiService.getToDo(todo.id)
if (remoteToDo.lastModified &gt; todo.lastModified) {
// Remote version is newer, update local
todoDao.insertToDo(remoteToDo)
} else {
// Local version is newer, update remote
apiService.updateToDo(todo)
}
} catch (e: Exception) {
// Handle network errors
}
}

Testing Offline Functionality

To ensure your app works offline:

  • Implement a network utility class to check connectivity.

  • Use this utility in your repository to decide whether to fetch from local or remote.

  • Write unit tests for your repository and ViewModel.

  • Perform UI tests with network on and off to verify behavior.

Conclusion

Building an offline-capable Android app requires careful consideration of data flow, synchronization, and conflict resolution. By using Room for local storage, Retrofit for API calls, and WorkManager for background sync, you can create a robust offline experience for your users.

Remember to handle edge cases, such as first-time app usage without internet, and always provide clear feedback to users about the sync status of their data.

Would you like me to explain or break down any part of this code?

GEMINI NANO FOR ANDROID: AN OFFLINE AI MODEL

Published: · Last updated: · 3 min read
Don Peter
Cofounder and CTO, Appxiom

What is Gemini Nano

Gemini Nano is an efficient AI model for on device tasks according to Google. With Gemini Nano, developers will be able to deliver rich generative AI experiences without requiring a network connection or moving data off-device. Gemini Nano is ideal for use cases where low latency, low cost, and privacy safeguards are paramount.

Architecture

To handle AI services, Android OS has introduced a new module called AICore. It is available in Android 14.

AICore, a brand new system service that simplifies using AI features in your apps. AICore takes care of everything behind the scenes, from managing AI models to ensuring their safe operation on your device. AICore lets your apps run powerful AI models like Gemini Nano directly on your device. This means faster processing compared to sending data to the cloud.

Gemini Nano and AICore Architecture - Image from Google

AICore prioritizes Privacy

Limited Connections:  AICore operates in a restricted environment, only interacting with essential system functions. This isolation helps prevent unauthorized access to your data by other other apps.

  • Secure Downloads: AICore doesn't directly access the internet. Instead, it relies on a separate, privacy-focused app (Private Compute Services) to download any required models. This ensures your information stays secure during the download process.

Getting Started with Gemini Nano

You will need Google AI Edge SDK for Android to work with Gemini Nano. This SDK provides APIs for interacting with the model and managing its functionalities.

Currently the SDK is available only for private review and is not available for public, but soon it will be.

Compatibility Check

Not all Android devices support Gemini Nano. Hence you need to run a compatibility check in your code.

fun isDeviceSupported(): Boolean {
val edgeManager = AiEdgeManager.getInstance(context)
return edgeManager.isModelSupported(GeminiNano.MODEL_NAME)
}

Accessing Gemini Nano

Once you've confirmed compatibility, you can obtain an instance of the GeminiNano class using the AIEdgeManager in AI Edge SDK:

val geminiNano = AiEdgeManager.getInstance(context).getAiModel(GeminiNano.MODEL_NAME) as GeminiNano

Example: Sentiment classification

Gemini Nano claims it is good in performing inference on data. Let's run a piece of text and infer the sentiment score.

val text = "I'm happy to meet you again."
val i = TextInput.Builder().setText(text).build()
val o = geminiNano.runInference(i) as TextClassificationOutput
val sentimentClassification = o.classification

Pretty straight forward. The model classifies the sentiment and stores in a variable.

Gemini Nano UseCases

Comming back to Gemini Nano, here are some of the use cases of the model that can be put to use by developer once it is out of private beta.

  • Get smarter summaries:  Gemini Nano can condense lengthy articles or reports into easy-to-understand summaries, saving you time.

  • Find answers fast:  Have questions? Ask away! Gemini Nano can analyze text and provide relevant answers.

  • Write with confidence:  Gemini Nano helps you polish your writing with features like grammar correction, proofreading, and writing suggestions. It can even generate smart replies based on the context.

  • Analyze emotions:  Understand the sentiment behind text.  Gemini Nano can detect the overall mood or feeling expressed in writing.

  • Built-in privacy:  Use powerful AI features without compromising your data.  Gemini Nano is designed with privacy in mind.

Already on Pixel:  Several Google Pixel apps, like Voice Recorder and Gboard, leverage the power of AICore to enhance your experience.

GRADLE FLAVORS: BUILDING MULTIPLE ANDROID APP VARIANTS WITH SINGLE CODEBASE

Published: · Last updated: · 4 min read
Don Peter
Cofounder and CTO, Appxiom

Gradle Flavors, also known as product flavors, allow developers to create multiple variants of their app within a single codebase. Each flavor can have its own unique configuration, resources, and dependencies, enabling customization based on factors such as branding, feature sets, or target audiences.

If you're developing free and paid versions of your app, adapting it for different languages or regions, or incorporating variations for testing purposes, Gradle Flavors offer unparalleled flexibility.

Why Use Gradle Flavors?

  • Customization: With Gradle Flavors, developers can easily tailor their app to suit specific user segments or market requirements. This level of customization fosters better user engagement and satisfaction.

  • Efficiency: Rather than maintaining separate codebases for different app variants, Gradle Flavors streamline the development process by centralizing code while allowing for variant-specific configurations. This results in reduced complexity and faster iteration cycles.

  • Consistency: By defining variant-specific resources and dependencies within the Gradle build script, developers ensure consistency across different app versions while minimizing the risk of errors or inconsistencies.

  • Market Segmentation: For businesses targeting diverse demographics or regions, Gradle Flavors facilitate the creation of specialized versions of the app tailored to each market segment's preferences and needs.

Free and Pro Versions

Let's illustrate the power of Gradle Flavors with the above scenario – creating free and pro versions of an app. Suppose you have an app called "WeatherApp" and want to offer both a free version with basic features and a paid pro version with additional functionalities.

android {
...
productFlavors {
free {
dimension "tier"
applicationId "com.example.weather.free"
versionCode 1
versionName "1.0"
// Define flavor-specific configurations
buildConfigField "boolean", "IS_PRO_VERSION", "false"
}
pro {
dimension "tier"
applicationId "com.example.weather.pro"
versionCode 1
versionName "1.0"
// Define flavor-specific configurations
buildConfigField "boolean", "IS_PRO_VERSION", "true"
}
}

buildTypes {
debug {
// Debug-specific configurations
...
}
release {
// Release-specific configurations
...
}
}
...
}

In this example, we define two product flavors: 'free' and 'pro', each with its own unique applicationID. We can then customize the behavior, features, and resources specific to each flavor, such as limiting certain features to the pro version or displaying different branding elements.

In the above example, we define two build types. "debug" and "release," each with its own configurations. These configurations might include signing configurations, proguard rules, or other build-specific settings.

With the flavors and build variants defined, Gradle will generate the following build variants:

  • freeDebug

  • freeRelease

  • proDebug

  • proRelease

Developers can then use these variants to build and test different versions of the app. For instance, they can build the "pro" release variant to generate a signed APK for distribution to users who have purchased the pro version of the app. Similarly, they can build the "free" debug variant to test new features or changes specific to the free version of the app.

Android Project Structure with Gradle Flavors

In the above example, the folder structure for the Android project would typically look like this:

- app
- src
- free
- java
- com
- example
- weather
- MainActivity.java
- ...
- res
- layout
- drawable
- values
- ...
- pro
- java
- com
- example
- weather
- MainActivity.java
- ...
- res
- layout
- drawable
- values
- ...
- main
- java
- com
- example
- weather
- MainActivity.java
- ...
- res
- layout
- drawable
- values
- ...

Here's a breakdown of the folder structure:

  • app: This is the main module of the Android project.

  • src: This directory contains the source code and resources for different build variants.

  • free: This directory contains the source code and resources specific to the "free" flavor.

java: Java source code files for the "free" flavor.

  • com.example.weather: Package directory.

  • MainActivity.java: Example activity class.

  • Other Java files specific to the "free" flavor.

  • res: Resource directory for the "free" flavor.

  • layout: XML layout files.

  • drawable: Image resources.

  • values: Resource files such as strings, colors, dimensions, etc.

  • Other resource directories specific to the "free" flavor.

  • pro: This directory contains the source code and resources specific to the "pro" flavor. The structure is similar to the "free" flavor but with resources and code specific to the "pro" variant.

  • main: This directory contains the main source code and resources shared among all flavors and build types. It serves as the base for all variants and contains code and resources common to both "free" and "pro" flavors.

By organizing the source code and resources in this way, Gradle can easily build different variants of the app by combining the contents of the "main" directory with the specific contents of each flavor directory (free and pro).

Leveraging Gradle flavors and build variants empowers Android developers to efficiently manage and customize multiple versions of their apps, catering to diverse user preferences and market requirements while maintaining codebase integrity and flexibility.

DATA STRUCTURES IN KOTLIN

Published: · Last updated: · 7 min read
Don Peter
Cofounder and CTO, Appxiom

Data structures play a crucial role in software development by enabling efficient storage and manipulation of data. Kotlin, a modern and expressive programming language, offers a variety of data structures to cater to different needs.

In this tutorial, we'll delve into the common data structures available in Kotlin, along with their use cases and implementation examples.

Major Data Structures in Kotlin are

  • Arrays

  • Lists

Immutable List

  • MutableList

  • ArrayList

  • LinkedList

  • Sets

  • Maps

  • Stacks and Queues

  • Trees (Binary Trees)

  • Graphs

Arrays

Arrays in Kotlin are collections of elements with a fixed size. While most languages allow only the same type of data to be stored in an array, Kotlin arrays can hold elements of any type.

// Creating an array of integers
val numbers = arrayOf(1, 2, 3, 4, 5)

// Accessing elements
val firstElement = numbers[0]

// Modifying elements
numbers[2] = 10

// Iterating through an array
for (number in numbers) {
println(number)
}

Arrays are suitable for scenarios requiring a fixed-size collection of elements accessed by index.

Lists

Lists in Kotlin are ordered collections that can grow or shrink in size dynamically.

Kotlin provides several types of lists, including List, MutableList, and specialized implementations like ArrayList and LinkedList.

Immutable List (List):

An immutable list cannot be modified after it is created. You can use the listOf() function to create an immutable list.

// Creating an immutable list of strings
val names: List&lt;String&gt; = listOf("Alice", "Bob", "Charlie")

// Accessing elements
println(names[0]) // Output: Alice

// Iterating through the list
for (name in names) {
println(name)
}

Mutable List

A mutable list allows you to add, remove, and modify elements after it is created. You can use the mutableListOf() function to create a mutable list.

// Creating a mutable list of integers
val numbers: MutableList&lt;Int&gt; = mutableListOf(1, 2, 3, 4, 5)

// Adding elements
numbers.add(6)

// Removing elements
numbers.removeAt(2)

// Modifying elements
numbers[0] = 10

// Iterating through the list
for (number in numbers) {
println(number)
}

ArrayList

An ArrayList is a resizable array implementation of the MutableList interface. It provides dynamic resizing and efficient random access. ArrayList in Kotlin is same as Java.

public actual typealias ArrayList&lt;E&gt; = java.util.ArrayList&lt;E&gt;
// Creating an ArrayList of doubles
val prices: ArrayList&lt;Double&gt; = arrayListOf(10.5, 20.3, 15.8)

// Adding elements
prices.add(25.0)

// Removing elements
prices.removeAt(1)

// Iterating through the ArrayList
for (price in prices) {
println(price)
}

LinkedList

A LinkedList is a doubly linked list implementation of the MutableList interface. It provides efficient insertions and deletions at both ends of the list.

// Creating a LinkedList of characters
val letters: LinkedList&lt;Char&gt; = LinkedList()
letters.add('A')
letters.add('B')
letters.add('C')

// Adding elements at the beginning and end
letters.addFirst('Z')
letters.addLast('D')

// Removing elements
letters.removeFirst()
letters.removeLast()

// Iterating through the LinkedList
for (letter in letters) {
println(letter)
}

These examples demonstrate different types of lists available in Kotlin and how to use them for various purposes. Depending on your requirements, you can choose the appropriate list implementation that suits your needs for immutability, mutability, random access, or efficient insertions and deletions.

Sets

Sets in Kotlin are collections of unique elements with no duplicates.

// Creating a set of integers
val uniqueNumbers = setOf(1, 2, 3, 3, 4, 5)

// Adding and removing elements
val mutableSet = mutableSetOf&lt;Int&gt;()
mutableSet.add(6)
mutableSet.remove(3)

// Checking membership
val containsFive = 5 in mutableSet

Sets are useful when you need to ensure uniqueness or perform set operations like union, intersection, and difference.

Maps

Maps in Kotlin are collections of key-value pairs where each key is unique.

// Creating a map of student grades
val studentGrades = mapOf("Alice" to 90, "Bob" to 85, "Charlie" to 92)

// Accessing values
val aliceGrade = studentGrades["Alice"]

// Modifying values (Not allowed for immutable maps)
val mutableGrades = studentGrades.toMutableMap()
mutableGrades["David"] = 88

// Iterating through a map
for ((name, grade) in studentGrades) {
println("$name: $grade")
}

Maps are ideal for situations requiring key-value associations and efficient lookups based on keys.

Stack and Queues

Stacks follow the Last-In-First-Out (LIFO) principle and support push and pop operations where as Queues adhere to the First-In-First-Out (FIFO) principle and support enqueue and dequeue operations.

ArrayDeque in Kotlin, introduced in version 1.4, is a mutable collection that functions as both a queue and a stack. It is provides efficient operations for adding and removing elements from both ends of the deque.

import kotlin.collections.ArrayDeque

val deque = ArrayDeque&lt;Int&gt;()

// Adding elements to the deque
deque.addFirst(1)
deque.addLast(2)

// Removing elements from the deque
val firstElement = deque.removeFirst()
val lastElement = deque.removeLast()

println("First Element: $firstElement") // Output: First Element: 1
println("Last Element: $lastElement") // Output: Last Element: 2

Trees (Binary Trees)

Binary trees consist of nodes where each node has at most two child nodes.

// Node structure for a binary tree
class BinaryTreeNode&lt;T&gt;(val value: T) {
var left: BinaryTreeNode&lt;T&gt;? = null
var right: BinaryTreeNode&lt;T&gt;? = null
}

// Creating a binary tree
val root = BinaryTreeNode(1)
root.left = BinaryTreeNode(2)
root.right = BinaryTreeNode(3)

//Reading the tree nodes.
fun &lt;T&gt; inOrderTraversal(node: BinaryTreeNode&lt;T&gt;?) {
if (node == null) return
inOrderTraversal(node.left)
println(node.value)
inOrderTraversal(node.right)
}

println("In-order traversal:")
inOrderTraversal(root)

Output:
In-order traversal:
2
1
3

Here we excute an In-order traversal. In-order traversal visits the left subtree, then the root, and finally the right subtree. Binary trees are useful for hierarchical data representation, searching, and sorting algorithms like binary search.

Graphs

Graphs are complex structures consisting of nodes and edges.

Graphs are essential for modelling relationships and solving problems like route finding and network analysis.

// Implementing an adjacency list for a graph
class Graph {
val adjacencyList = mutableMapOf&lt;Int, MutableList&lt;Int&gt;&gt;()

fun addEdge(from: Int, to: Int) {
adjacencyList.getOrPut(from) { mutableListOf() }.add(to)
}
}

// Creating a graph
val graph = Graph()
graph.addEdge(1, 2)
graph.addEdge(1, 3)
graph.addEdge(2, 4)

// Accessing adjacency list
val adjacencyList = graph.adjacencyList

// Reading all vertices and their adjacent vertices
for ((vertex, adjacentVertices) in adjacencyList) {
println("Vertex $vertex is connected to: $adjacentVertices")
}

// Reading adjacent vertices of a specific vertex
val vertexAdjacent = adjacencyList[1]
println("Adjacent vertices of vertex 1: $vertexAdjacent")

Output
Vertex 1 is connected to: [2, 3]
Vertex 2 is connected to: [4]
Adjacent vertices of vertex 1: [2, 3]

The Graph class represents a graph data structure using an adjacency list. In this implementation, the adjacency list is stored as a mutable map where the keys represent the vertices (nodes) of the graph, and the corresponding values are lists containing the adjacent vertices.

adjacencyList:

It's a mutable map where the keys are integers representing the vertices, and the values are mutable lists containing adjacent vertices.

addEdge:

The addEdge function adds an edge between two vertices in the graph. It takes two parameters: from (the source vertex) and to (the destination vertex).

If the from vertex already exists in the adjacency list, getOrPut retrieves its associated mutable list of adjacent vertices. If it doesn't exist, a new entry is created and to vertex is then added to the list of adjacent vertices for the from vertex.

In the above code snippet, the for-in loop prints each vertex along with its adjacent vertices.

The last println statement prints the adjacent vertices of vertex 1, which are [2, 3].

Conclusion

This blog post aims to provides a comprehensive overview of various data structures available in Kotlin, along with their use cases and implementation examples. Experiment with these data structures to enhance your understanding and proficiency in Kotlin programming.

INTEGRATING URL_LAUNCHER IN FLUTTER APPS

Published: · Last updated: · 4 min read
Don Peter
Cofounder and CTO, Appxiom

The mobile app development world has moved from fast to ‘impatiently fast’. One essential aspect of faster user interaction is the ability to navigate to external websites or open other apps directly from within your Flutter application.

This is where the url_launcher plugin for Flutter comes into play. This plugin allows you to open URLs in the default web browser of the device. It also allows for ​​opening URLs that launch other apps installed on the device such as emails or social media apps. 

Installing URL Launcher in Flutter

Installation can be done in a whiff by following the code given below: 

Terminal Command

flutter pub add url_launcher

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
url_launcher: x.y.z.

Supported URL Schemes

url_launcher supports various URL schemes. They are essentially prefixes or protocols that help define how a URL should be handled by Android, iOS or any operating system or apps in general. Common URL Schemes supported by url_launcher include HTTP, HTTPS, mailto, SMS, tel, App Schemes and Custom Schemes. 

Integrating url_launcher

When using the url_launcher package, you can open URLs with these schemes using the launch function. This package will delegate the URL handling to the underlying platform, ensuring compatibility with both Android and iOS. 

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class MyApp extends StatelessWidget {

@override Widget build(BuildContext context) {

&nbsp;&nbsp;&nbsp;&nbsp;return MaterialApp(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;home: Scaffold(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;appBar: AppBar(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title: Text('URL Launcher Example'),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: Center(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child: ElevatedButton(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onPressed: () {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_openURL("https://appxiom.com/");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child: Text('Open URL'),

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),

&nbsp;&nbsp;&nbsp;&nbsp;);

&nbsp;&nbsp;}

&nbsp;&nbsp;// Function to open a URL using url_launcher

&nbsp;&nbsp;void _openURL(String url) async {

&nbsp;&nbsp;&nbsp;&nbsp;if (await canLaunchUrl(url)) { //Checking if there is any app installed in the device to handle the url.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;await launch(url);

&nbsp;&nbsp;&nbsp;&nbsp;} else {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Handle error

&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;}

}

Configuring canLaunchUrl in iOS

Make sure to add the URL schemes passed to canLaunchUrl as LSApplicationQueriesSchemes entries in your info.plist file. Otherwise, it will return false.

&lt;key&gt;LSApplicationQueriesSchemes&lt;/key&gt;

&lt;array&gt;

&nbsp;&nbsp;&lt;string&gt;sms&lt;/string&gt;

&nbsp;&nbsp;&lt;string&gt;tel&lt;/string&gt;

&lt;/array&gt;

Configuring canLaunchUrl in Android

Add the URL schemes passed to canLaunchUrl as <queries> entries in your AndroidManifest.xml, otherwise, it will return false in most cases starting on Android 11 (API 30) or higher. 

&lt;!-- Provide required visibility configuration for API level 30 and above --&gt;

&lt;queries&gt;

&nbsp;&nbsp;&lt;!-- If your app checks for SMS support --&gt;

&nbsp;&nbsp;&lt;intent&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;action android:name="android.intent.action.VIEW" /&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;data android:scheme="sms" /&gt;

&nbsp;&nbsp;&lt;/intent&gt;

&nbsp;&nbsp;&lt;!-- If your app checks for call support --&gt;

&nbsp;&nbsp;&lt;intent&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;action android:name="android.intent.action.VIEW" /&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;data android:scheme="tel" /&gt;

&nbsp;&nbsp;&lt;/intent&gt;

&nbsp;&nbsp;&lt;!-- If your application checks for inAppBrowserView launch mode support --&gt;

&nbsp;&nbsp;&lt;intent&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;action android:name="android.support.customtabs.action.CustomTabsService" /&gt;

&nbsp;&nbsp;&lt;/intent&gt;

&lt;/queries&gt;

That’s it for now. For more information on  url_launcher with Flutter, check https://pub.dev/packages/url_launcher/