Amol Pawar

Bundles in libs.versions.toml

Say Goodbye to Repetition: How Bundles in libs.versions.toml Simplify Android Projects

If you’re tired of repeating the same dependencies across different modules in your Android project, you’re not alone. Managing dependencies manually is error-prone, messy, and not scalable. Fortunately, Bundles in libs.versions.toml offer a clean and modern solution that saves time and reduces duplication. Let’s break it down, step by step, in a simple way.

What Is libs.versions.toml?

Starting with Gradle 7 and Android Gradle Plugin 7+, Google introduced Version Catalogs — a new way to centralize and manage dependencies. Instead of scattering dependency strings across multiple build.gradle files, you can now define everything in a single place: libs.versions.toml.

This TOML (Tom’s Obvious Minimal Language) file lives in your project’s gradle folder and acts as your master dependency list.

Here’s what a basic libs.versions.toml file looks like:

Kotlin
[versions]
kotlin = "1.9.0"
coroutines = "1.7.1"

[libraries]
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }

That’s great — but what if you’re using the same group of libraries in every module? Writing them out repeatedly is a waste of time. That’s where Bundles in libs.versions.toml come to the rescue.

What Are Bundles?

Bundles are a feature of version catalogs that let you group related dependencies under a single name. Think of them like playlists for your libraries. Instead of referencing each dependency one by one, you just call the bundle, and you’re done.

Why Use Bundles?

  • Clean, organized code
  • No repeated dependencies
  • Easy updates across modules
  • Better modularization

How to Create a Bundle in libs.versions.toml

Let’s say you’re using multiple Jetpack Compose libraries across several modules. Without bundles, you’d need to add each one like this:

Kotlin
implementation(libs.compose.ui)
implementation(libs.compose.material)
implementation(libs.compose.tooling)

With Bundles in libs.versions.toml, you can simplify it like this:

Step 1: Define the Libraries

Kotlin
[versions]
compose = "1.5.0"

[libraries]
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-material = { module = "androidx.compose.material:material", version.ref = "compose" }
compose-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }

Step 2: Create a Bundle

Kotlin
[bundles]
compose = ["compose-ui", "compose-material", "compose-tooling"]

How to Use Bundles in build.gradle.kts

In your module’s build.gradle.kts file, just add:

Kotlin
implementation(libs.bundles.compose)

That one-liner brings in all the Compose dependencies you need. Clean, right?

Real-World Use Case: Networking Stack

Let’s say you always use Retrofit, Moshi, and OkHttp in your data modules. Define a bundle like this:

Kotlin
[versions]
retrofit = "2.9.0"
moshi = "1.13.0"
okhttp = "4.10.0"

[libraries]
retrofit-core = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
moshi-core = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
okhttp-core = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }

[bundles]
networking = ["retrofit-core", "moshi-core", "okhttp-core"]

Then in your module:

Kotlin
implementation(libs.bundles.networking)

You’ve just replaced three lines with one — and centralized version control in the process.

Common Mistakes to Avoid

  • Wrong syntax: The bundle array must reference exact keys from the [libraries] block.
  • Missing versions: Always define versions under [versions] and refer using version.ref.
  • Not reusing bundles: If two modules share the same libraries, don’t duplicate — bundle them.

Why Bundles in libs.versions.toml Matter for Android Developers

Bundles in libs.versions.toml are more than just a convenience—they’re a best practice. They improve your project structure, reduce maintenance overhead, and make scaling a breeze. Whether you’re working solo or on a large team, bundling dependencies is the smart way to manage complexity.

If you’re building modular Android apps (and let’s face it, who isn’t in 2025?), adopting bundles is a no-brainer.

Conclusion

The old way of managing dependencies is clunky and outdated. With Bundles in libs.versions.toml, you can streamline your workflow, stay DRY (Don’t Repeat Yourself), and future-proof your project.

Say goodbye to repetitive implementation lines and hello to clean, maintainable build scripts.

Start bundling today — and give your Android project the structure it deserves.

Proto DataStore

Proto DataStore in Android: How to Store Complex Objects with Protocol Buffers

Managing data on Android has evolved significantly over the years. From SharedPreferences to Room, we’ve seen the full spectrum. But when it comes to storing structured, complex data in a lightweight and efficient way, Proto DataStore steps in as a game-changer.

In this blog, we’ll walk through Proto DataStore, how it works under the hood, and how to use it with Protocol Buffers to store complex objects. We’ll also look at how it stacks up against the older SharedPreferences and why it’s the better modern choice.

Let’s break it down step by step.

What is Proto DataStore?

Proto DataStore is a Jetpack library from Google that helps you store typed objects persistently using Protocol Buffers (protobuf), a fast and efficient serialization format.

It’s:

  • Type-safe
  • Asynchronous
  • Corruption-resistant
  • Better than SharedPreferences

Unlike Preferences DataStore, which stores data in key-value pairs (similar to SharedPreferences), Proto DataStore is ideal for storing structured data models.

Why Use Proto DataStore?

Here’s why developers love Proto DataStore:

  • Strong typing — Your data models are generated and compiled, reducing runtime errors.
  • Speed — Protocol Buffers are faster and more compact than JSON or XML.
  • Safe and robust — Built-in corruption handling and data migration support.
  • Asynchronous API — Uses Kotlin coroutines and Flow, keeping your UI smooth.

Store Complex Objects with Proto DataStore

Let’s go hands-on. Suppose you want to save a user profile with fields like name, email, age, and preferences.

Step 1: Add the Dependencies

Add these to your build.gradle (app-level):

Kotlin
dependencies {
    implementation "androidx.datastore:datastore:1.1.0"
    implementation "androidx.datastore:datastore-core:1.1.0"
    implementation "com.google.protobuf:protobuf-javalite:3.25.1"
}

In your build.gradle (project-level), enable Protobuf:

Kotlin
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.25.1"
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { }
            }
        }
    }
}

Also apply plugins at the top:

Kotlin
plugins {
    id 'com.google.protobuf' version '0.9.4'
    id 'kotlin-kapt'
}

Step 2: Define Your .proto File

Create a file named user.proto inside src/main/proto/:

Kotlin
syntax = "proto3";

option java_package = "com.softaai.datastore";
option java_multiple_files = true;

message UserProfile {
  string name = 1;
  string email = 2;
  int32 age = 3;
  bool isDarkMode = 4;
}

This defines a structured data model for the user profile.

Step 3: Create the Serializer

Create a Kotlin class that implements Serializer<UserProfile>:

Kotlin
object UserProfileSerializer : Serializer<UserProfile> {
    override val defaultValue: UserProfile = UserProfile.getDefaultInstance()

    override suspend fun readFrom(input: InputStream): UserProfile {
        return UserProfile.parseFrom(input)
    }

    override suspend fun writeTo(t: UserProfile, output: OutputStream) {
        t.writeTo(output)
    }
}

This handles how the data is read and written to disk using protobuf.

Step 4: Initialize the Proto DataStore

Create a DataStore instance in your repository or a singleton:

Kotlin
val Context.userProfileDataStore: DataStore<UserProfile> by dataStore(
    fileName = "user_profile.pb",
    serializer = UserProfileSerializer
)

Now you can access this instance using context.userProfileDataStore.

Step 5: Read and Write Data

Here’s how you read the stored profile using Kotlin Flow:

Kotlin
val userProfileFlow: Flow<UserProfile> = context.userProfileDataStore.data

To update the profile:

Kotlin
suspend fun updateUserProfile(context: Context) {
    context.userProfileDataStore.updateData { currentProfile ->
        currentProfile.toBuilder()
            .setName("Amol Pawar")
            .setEmail("[email protected]")
            .setAge(28)
            .setIsDarkMode(true)
            .build()
    }
}

Easy, clean, and fully type-safe.

Bonus: Handling Corruption and Migration

Handle Corruption Gracefully

You can customize the corruption handler if needed:

Kotlin
val Context.safeUserProfileStore: DataStore<UserProfile> by dataStore(
    fileName = "user_profile.pb",
    serializer = UserProfileSerializer,
    corruptionHandler = ReplaceFileCorruptionHandler {
        UserProfile.getDefaultInstance()
    }
)

Migrate from SharedPreferences

If you’re switching from SharedPreferences:

Kotlin
val Context.migratedUserProfileStore: DataStore<UserProfile> by dataStore(
    fileName = "user_profile.pb",
    serializer = UserProfileSerializer,
    produceMigrations = { context ->
        listOf(SharedPreferencesMigration(context, "old_prefs_name"))
    }
)

When to Use Proto DataStore

Use Proto DataStore when:

  • You need to persist complex, structured data.
  • You care about performance and file size.
  • You want a modern, coroutine-based data solution.

Avoid it for relational data (instead use Room) or for simple flags (Preferences DataStore may suffice).

Conclusion

Proto DataStore is the future-forward way to store structured data in Android apps. With Protocol Buffers at its core, it combines speed, safety, and type-safety into one clean package.

Whether you’re building a user profile system, app settings, or configuration storage, Proto DataStore helps you stay efficient and future-ready.

TL;DR

Q: What is Proto DataStore in Android?
 A: Proto DataStore is a modern Jetpack library that uses Protocol Buffers to store structured, type-safe data asynchronously and persistently.

Q: How do I store complex objects using Proto DataStore?
 A: Define a .proto schema, set up a serializer, initialize the DataStore, and read/write using Flow and coroutines.

Q: Why is Proto DataStore better than SharedPreferences?
 A: It’s type-safe, faster, handles corruption, and integrates with Kotlin coroutines.

Jetpack DataStore in Android

Mastering Jetpack DataStore in Android: The Modern Replacement for SharedPreferences

If you’re still using SharedPreferences in your Android app, it’s time to move forward. Google introduced Jetpack DataStore as a modern, efficient, and fully asynchronous solution for storing key-value pairs and typed objects. In this blog, we’ll break down what Jetpack DataStore is, why it’s better than SharedPreferences, and how you can use it effectively in your Android projects.

What Is Jetpack DataStore?

Jetpack DataStore is part of Android Jetpack and is designed to store small amounts of data. It comes in two flavors:

  • Preferences DataStore — stores key-value pairs, similar to SharedPreferences.
  • Proto DataStore — stores typed objects using Protocol Buffers.

Unlike SharedPreferences, Jetpack DataStore is built on Kotlin coroutines and Flow, making it asynchronous and safe from potential ANRs (Application Not Responding errors).

Why Replace SharedPreferences?

SharedPreferences has been around for a long time but comes with some baggage:

  • Synchronous API — can block the main thread.
  • Lacks error handling — fails silently.
  • Not type-safe — you can run into ClassCastExceptions easily.

Jetpack DataStore solves all of these with:

  • Coroutine support for non-blocking IO.
  • Strong typing with Proto DataStore.
  • Built-in error handling.
  • Better consistency and reliability.

Setting Up Jetpack DataStore

To start using Jetpack DataStore, first add the required dependencies to your build.gradle:

Kotlin
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.datastore:datastore-core:1.0.0"

For Proto DataStore:

Kotlin
implementation "androidx.datastore:datastore:1.0.0"
implementation "com.google.protobuf:protobuf-javalite:3.14.0"

Also, don’t forget to apply the protobuf plugin if using Proto:

Kotlin
id 'com.google.protobuf' version '0.8.12'

Using Preferences DataStore

Step 1: Create the DataStore instance

Jetpack DataStore is designed to be singleton-scoped. The recommended way is to create it as an extension property on Context:

Kotlin
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "user_prefs")

Here, preferencesDataStore creates a singleton DataStore instance. This ensures you have a single DataStore instance per file, avoiding memory leaks and data corruption.

Step 2: Define keys

Kotlin
val USER_NAME = stringPreferencesKey("user_name")
val IS_LOGGED_IN = booleanPreferencesKey("is_logged_in")

stringPreferencesKey and booleanPreferencesKey help define the keys.

Step 3: Write data

To write data, use the edit function, which is fully asynchronous and safe to call from any thread:

Kotlin
suspend fun saveUserData(context: Context, name: String, isLoggedIn: Boolean) {
    context.dataStore.edit { preferences ->
        preferences[USER_NAME] = name
        preferences[IS_LOGGED_IN] = isLoggedIn
    }
}

Here, edit suspends while the data is being written, ensuring no UI thread blocking.

Step 4: Read data

To read data, use Kotlin Flows, which emit updates whenever the data changes:

Kotlin
val userNameFlow: Flow<String> = context.dataStore.data
    .map { preferences ->
        preferences[USER_NAME] ?: ""
    }

Here, data is accessed reactively using Kotlin Flow, returns a Flow<String> that emits the username whenever it changes. You can collect this Flow in a coroutine or observe it in Jetpack Compose.

Real-World Use Case: User Login State

Let’s say you want to keep track of whether a user is logged in. Here’s how you do it:

Save login state:

Kotlin
suspend fun setLoginState(context: Context, isLoggedIn: Boolean) {
    context.dataStore.edit { prefs ->
        prefs[IS_LOGGED_IN] = isLoggedIn
    }
}

Observe login state:

Kotlin
val loginState: Flow<Boolean> = context.dataStore.data
    .map { prefs -> prefs[IS_LOGGED_IN] ?: false }

This setup lets your app reactively respond to changes in the login state, such as redirecting users to the login screen or the home screen.

Migrating from SharedPreferences

Jetpack DataStore makes migration easy with SharedPreferencesMigration:

Kotlin
import androidx.datastore.preferences.SharedPreferencesMigration

val Context.dataStore by preferencesDataStore(
    name = USER_PREFERENCES_NAME,
    produceMigrations = { context ->
        listOf(SharedPreferencesMigration(context, USER_PREFERENCES_NAME))
    }
)
  • Migration runs automatically before any DataStore access.
  • Once migrated, stop using the old SharedPreferences to avoid data inconsistency.

Using Proto DataStore (Typed Data)

Proto DataStore requires you to define a .proto schema file.

Step 1: Define the Proto schema

user_prefs.proto

Kotlin
syntax = "proto3";

option java_package = "com.softaai.sitless";
option java_multiple_files = true;

message UserPreferences {
  string user_name = 1;
  bool is_logged_in = 2;
}

Step 2: Create the serializer

Kotlin
object UserPreferencesSerializer : Serializer<UserPreferences> {
    override val defaultValue: UserPreferences = UserPreferences.getDefaultInstance()

    override suspend fun readFrom(input: InputStream): UserPreferences {
        return UserPreferences.parseFrom(input)
    }

    override suspend fun writeTo(t: UserPreferences, output: OutputStream) {
        t.writeTo(output)
    }
}

Step 3: Initialize Proto DataStore

Kotlin
val Context.userPreferencesStore: DataStore<UserPreferences> by dataStore(
    fileName = "user_prefs.pb",
    serializer = UserPreferencesSerializer
)

Step 4: Update and read data

Kotlin
suspend fun updateUser(context: Context, name: String, isLoggedIn: Boolean) {
    context.userPreferencesStore.updateData { prefs ->
        prefs.toBuilder()
            .setUserName(name)
            .setIsLoggedIn(isLoggedIn)
            .build()
    }
}

val userNameFlow = context.userPreferencesStore.data
    .map { it.userName }

Best Practices

  • Use Proto DataStore when your data model is complex or needs strong typing.
  • Use Preferences DataStore for simple key-value storage.
  • Always handle exceptions using catch when collecting flows.
  • Avoid main-thread operations; DataStore is built for background execution.

Conclusion

Jetpack DataStore is not just a replacement for SharedPreferences; it’s an upgrade in every sense. With better performance, safety, and modern API design, it’s the future of local data storage in Android.

If you’re building a new Android app or refactoring an old one, now’s the perfect time to switch. By embracing Jetpack DataStore, you’re not only writing cleaner and safer code, but also aligning with best practices endorsed by Google.

Use-Site

What Happens If You Don’t Specify a Use-Site Target in Kotlin?

In Kotlin, annotations can target multiple elements of a declaration — such as a field, getter, or constructor parameter. When you apply an annotation without explicitly specifying a use-site target (e.g., @MyAnnotation instead of @field:MyAnnotation), Kotlin tries to infer the most appropriate placement.

This default behavior often works well — but in some cases, especially when interoperating with Java frameworks, it can produce unexpected results. Let’s dive into how it works, and what’s changing with Kotlin 2.2.0.

Default Target Inference in Kotlin (Before 2.2.0)

If the annotation supports multiple targets (defined via its @Target declaration), Kotlin infers where to apply the annotation based on context. This is especially relevant for primary constructor properties.

Kotlin
annotation class MyAnnotation
class User(
    @MyAnnotation val name: String
)

In this case, Kotlin might apply @MyAnnotation to the constructor parameter, property, or field—depending on what @MyAnnotation allows.

Approximate Priority Order:

When multiple targets are applicable, Kotlin historically followed a rough order of priority:

  1. param – Constructor parameter
  2. property – The Kotlin property itself
  3. field – The backing field generated in bytecode

But this is not a strict rule — the behavior varies by context and Kotlin version.

Interop with Java Frameworks: Why Target Matters

Kotlin properties can generate several elements in Java bytecode:

  • A backing field
  • A getter method (and setter for var)
  • A constructor parameter (for primary constructor properties)

Java frameworks (like Jackson, Spring, Hibernate) often look for annotations in specific places — typically on the field or getter. If Kotlin places the annotation somewhere else (e.g., the property), the framework might not recognize it.

Kotlin
class User(
    @JsonProperty("username") val name: String
)

If @JsonProperty is placed on the property instead of the field, Jackson may not detect it correctly. The fix is to use an explicit target:

Kotlin
class User(
    @field:JsonProperty("username") val name: String
)

Kotlin 2.2.0: Refined Defaulting with -Xannotation-default-target

Kotlin 2.2.0 introduces a new experimental compiler flag:

Kotlin
-Xannotation-default-target=param-property

When enabled, this flag enforces a consistent and more predictable defaulting strategy, especially suited for Java interop.

New Priority Order:

  1. param – If valid, apply to the constructor parameter
  2. property – If param isn’t valid, and property is
  3. field – If neither param nor property is valid, but field is
  4. Error — If none of these are allowed, compilation fails

This makes annotation behavior more intuitive, especially when integrating with Java-based tools and frameworks.

The @all: Meta-Target (Experimental)

Kotlin 2.2.0 also introduces the experimental @all: use-site target, which applies an annotation to all applicable parts of a property:

  • param (constructor parameter)
  • property (Kotlin-level property)
  • field (backing field)
  • get (getter)
  • set (setter, if var)

Example:

Kotlin
@all:MyAnnotation<br>var name: String = ""

This is equivalent to writing:

Kotlin
@param:MyAnnotation
@property:MyAnnotation
@field:MyAnnotation
@get:MyAnnotation
@set:MyAnnotation

Only the targets supported in the annotation’s @Target list will be applied.

Best Practices

Here’s how to work with Kotlin annotations effectively:

ScenarioRecommendation
Using annotations with Java frameworksUse explicit use-site targets (@field:, @get:)
Want consistent defaultingEnable -Xannotation-default-target=param-property
Want broad annotation coverageUse @all: (if supported by the annotation)
Unsure where an annotation is being appliedUse the Kotlin compiler flag -Xemit-jvm-type-annotations and inspect bytecode or decompiled Java

Conclusion

While Kotlin’s inferred annotation targets are convenient, they don’t always align with Java’s expectations. Starting with Kotlin 2.2.0, you get more control and predictability with:

  • Explicit use-site targets
  • A refined defaulting flag (-Xannotation-default-target)
  • The @all: meta-target for multi-component coverage

By understanding and controlling annotation placement, you’ll avoid hidden bugs and ensure smooth Kotlin–Java interop.

how to apply annotations in Kotlin

How to Apply Annotations in Kotlin: Best Practices & Examples

Annotations are a powerful feature in Kotlin that let you add metadata to your code. Whether you’re working with frameworks like Spring, Dagger, or Jetpack Compose, or building your own tools, knowing how to apply annotations in Kotlin can drastically improve your code’s readability, structure, and behavior.

In this guide, we’ll walk through everything step by step, using real examples to show how annotations work in Kotlin. You’ll see how to use them effectively, with clean code and clear explanations along the way..

What Are Annotations in Kotlin?

Annotations are like sticky notes for the compiler. They don’t directly change the logic of your code but tell tools (like compilers, IDEs, and libraries) how to handle certain elements.

If you use @JvmStatic, Kotlin will generate a static method that Java can call without needing to create an object. It helps bridge Kotlin and Java more smoothly.?

Kotlin
object Utils {
    @JvmStatic
    fun printMessage(msg: String) {
        println(msg)
    }
}

This makes printMessage() callable from Java without creating an instance of Utils.

How to Apply Annotations in Kotlin

To apply an annotation in Kotlin, you use the @ symbol followed by the annotation’s name at the beginning of the declaration you want to annotate. You can apply annotations to functions, classes, and other code elements. Let’s see some examples:

Here’s an example using the JUnit framework, where a test method is marked with the @Test annotation:

Kotlin
import org.junit.*

class MyTest {
    @Test
    fun testTrue() {
        Assert.assertTrue(true)
    }
}

In Kotlin, annotations can have parameters. Let’s take a look at the @Deprecated annotation as a more interesting example. It has a replaceWith parameter, which allows you to provide a replacement pattern to facilitate a smooth transition to a new version of the API. The following code demonstrates the usage of annotation arguments, including a deprecation message and a replacement pattern:

Kotlin
@Deprecated("Use removeAt(index) instead.", ReplaceWith("removeAt(index)"))
fun remove(index: Int) { ... }

In this case, when someone uses the remove function in their code, the IDE will not only show a suggestion to use removeAt instead, but it will also offer a quick fix to automatically replace the remove function with removeAt. This makes it easier to update your code and follow the recommended practices.

Annotations in Kotlin can have arguments of specific types, such as primitive types, strings, enums, class references, other annotation classes, and arrays of these types. The syntax for specifying annotation arguments is slightly different from Java:

To specify a class as an annotation argument, use the ::class syntax:

When you want to specify a class as an argument for an annotation, you can use the ::class syntax.

Kotlin
@MyAnnotation(MyClass::class)

In this case, let’s say you have a custom annotation called @MyAnnotation, and you want to pass a class called MyClass as an argument to that annotation. In this case, you can use the ::class syntax like this: @MyAnnotation(MyClass::class).

By using ::class, you are referring to the class itself as an object. It allows you to pass the class reference as an argument to the annotation, indicating which class the annotation is associated with.

To specify another annotation as an argument, don’t use the @ character before the annotation name:

when specifying an annotation as an argument for another annotation, you don’t need to use the “@” symbol before the annotation name.

Kotlin
@Deprecated(replaceWith = ReplaceWith("removeAt(index)"))
fun remove(index: Int) { ... }

In the above example, the @Deprecated annotation. It allows you to provide a replacement pattern using the ReplaceWith annotation. In this case, you simply specify the ReplaceWith annotation without the “@” symbol when using it as an argument for @Deprecated .

By omitting the “@” symbol, you indicate that the argument is another annotation.

To specify an array as an argument, use the arrayOf function:

if you want to specify an array as an argument for an annotation, you can use the arrayOf function.

For example, let’s say you have an annotation called @RequestMapping with a parameter called path, and you want to pass an array of strings ["/foo", "/bar"] as the value for that parameter. In this case, you can use the arrayOf function like this:

Kotlin
@RequestMapping(path = arrayOf("/foo", "/bar"))

However, if the annotation class is declared in Java, you don’t need to use the arrayOf function. In Java, the parameter named value in the annotation is automatically converted to a vararg parameter if necessary. This means you can directly provide the values without using the arrayOf function.

To use a property as an annotation argument, you need to mark it with a const modifier:

In Kotlin, annotation arguments need to be known at compile time, which means you cannot refer to arbitrary properties as arguments. However, you can use the const modifier to mark a property as a compile-time constant, allowing you to use it as an annotation argument.

To use a property as an annotation argument, follow these steps:

  1. Declare the property using the const modifier at the top level of a file or inside an object.
  2. Initialize the property with a value of a primitive type or a String.

Here’s an example using JUnit’s @Test annotation that specifies a timeout for a test:

Kotlin
const val TEST_TIMEOUT = 100L

@Test(timeout = TEST_TIMEOUT)
fun testMethod() {
    // Test code goes here
}

In this example, TEST_TIMEOUT is declared as a const property with a value of 100L. The timeout parameter of the @Test annotation is then set to the value of TEST_TIMEOUT. This allows you to specify the timeout value as a constant that can be reused and easily changed if needed.

Remember that properties marked with const need to be declared at the top level of a file or inside an object, and they must be initialized with values of primitive types or String. Using regular properties without the const modifier will result in a compilation error with the message “Only ‘const val’ can be used in constant expressions.”

Best Practices for Using Annotations

Using annotations the right way keeps your Kotlin code clean and powerful. Here are some tips:

1. Use Target and Retention Wisely

  • @Target specifies where your annotation can be applied: classes, functions, properties, etc.
  • @Retention controls how long the annotation is kept: source code only, compiled classes, or runtime.
Kotlin
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class LogExecutionTime

Use RUNTIME if your annotation will be read by reflection.

2. Keep Annotations Lightweight

Avoid stuffing annotations with too many parameters. Use defaults whenever possible to reduce clutter.

Kotlin
annotation class Audit(val user: String = "system")

3. Document Custom Annotations

Treat annotations like part of your public API. Always include comments and KDoc.

Kotlin
/**
 * Indicates that the method execution time should be logged.
 */
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class LogExecutionTime

Example: Logging Execution Time

Let’s say you want to log how long your functions take to execute. You can create a custom annotation and use reflection to handle it.

Kotlin
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class LogExecutionTime

class Worker {
    @LogExecutionTime
    fun doWork() {
        Thread.sleep(1000)
        println("Work done!")
    }
}

Now add logic to read the annotation:

Kotlin
fun runWithLogging(obj: Any) {
    obj::class.members.forEach { member ->
        if (member.annotations.any { it is LogExecutionTime }) {
            val start = System.currentTimeMillis()
            (member as? KFunction<*>)?.call(obj)
            val end = System.currentTimeMillis()
            println("Execution time: ${end - start} ms")
        }
    }
}

fun main() {
    val worker = Worker()
    runWithLogging(worker)
}

This will automatically time any function marked with @LogExecutionTime. Clean and effective.

Advanced Use Case: Dependency Injection with Dagger

In Dagger or Hilt, annotations are essential. You don’t write much logic yourself; instead, annotations do the work.

Kotlin
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Provides
    fun provideApiService(): ApiService {
        return Retrofit.Builder()
            .baseUrl("https://api.softaai.com")
            .build()
            .create(ApiService::class.java)
    }
}

Here, @Module, @Provides, and @InstallIn drive the dependency injection system. Once you learn how to apply annotations in Kotlin, libraries like Dagger become far less intimidating.

Conclusion

Annotations in Kotlin are more than decoration — they’re metadata with a purpose. Whether you’re customizing behavior, interfacing with Java, or using advanced frameworks, knowing how to apply annotations in Kotlin gives you a real edge.

Quick Recap:

  • Use annotations to add metadata.
  • Apply built-in annotations to boost interoperability and performance.
  • Create your own annotations for clean, reusable logic.
  • Follow best practices: target, retention, defaults, and documentation.

With the right approach, annotations make your Kotlin code smarter, cleaner, and easier to scale.

Kotlin Use-Site Target Annotations

Kotlin Use-Site Target Annotations Explained with Real-World Examples

Kotlin has quickly become a developer favorite for its expressiveness and safety features. One powerful but often overlooked feature is Kotlin Use-Site Target Annotations. While annotations in Kotlin are commonly used, specifying a use-site target adds precision to how these annotations behave. Whether you’re building Android apps or backend services, understanding this feature can help...

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here
@Target in Kotlin

Everything About @Target in Kotlin: What It Is, Why It Matters, and How to Use It

Kotlin is known for being expressive, concise, and fully interoperable with Java. But when working with annotations in Kotlin, especially when defining your own, you might encounter something called @Target. If you’re wondering what @Target in Kotlin is, why it matters, and how to use it effectively—this guide is for you.

Let’s break it down, step-by-step.

What Is @Target in Kotlin?

In Kotlin, @Target is a meta-annotation. That means it’s an annotation used to annotate other annotations. It specifies where your custom annotation can be applied in the code.

For example, can your annotation be used on a class? A function? A property? That’s what @Target defines.

Kotlin uses the AnnotationTarget enum to list all possible valid locations.

Basic Syntax:

Kotlin
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class MyAnnotation

In this example, MyAnnotation can only be used on classes and functions.

Why @Target in Kotlin Matters

Without @Target, your custom annotation could be misused. Kotlin wouldn’t know where it should or shouldn’t be applied. That could lead to:

  • Confusing code
  • Compilation warnings or errors
  • Unintended behavior, especially when interoperating with Java

By clearly defining usage points, you make your code more maintainable, readable, and safe.

Understanding AnnotationTarget Options

Kotlin gives you a range of options for @Target. Here are the most common ones:

Different options for @Target

You can use more than one if needed:

Kotlin
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR)
annotation class AuditLog

This lets you use @AuditLog on multiple types of declarations.

Example: Creating a Custom Annotation

Let’s say you’re building a system where you want to mark certain functions as “experimental”.

Step 1: Define the Annotation

Kotlin
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class ExperimentalFeature(val message: String = "This is experimental")
  • @Target(AnnotationTarget.FUNCTION): Only allows this annotation on functions.
  • @Retention(RUNTIME): Keeps the annotation at runtime (optional but useful).

Step 2: Use the Annotation

Kotlin
@ExperimentalFeature("Might be unstable in production")
fun newAlgorithm() {
    println("Running experimental algorithm...")
}

Step 3: Read the Annotation at Runtime (Optional)

Kotlin
fun checkExperimentalAnnotations() {
    val method = ::newAlgorithm
    val annotation = method.annotations.find { it is ExperimentalFeature } as? ExperimentalFeature

    if (annotation != null) {
        println("Warning: ${annotation.message}")
    }
}

This prints:

Kotlin
Warning: Might be unstable in production

Tips for Using @Target in Kotlin

  1. Be specific: The more targeted your annotation, the less chance of misuse.
  2. Use multiple targets wisely: Don’t overgeneralize.
  3. Pair with @Retention: Decide whether your annotation should be available at runtime, compile time, or source level.
  4. Think Java Interop: If you’re interoperating with Java, know that @Target in Kotlin maps to @Target in Java too.

Conclusion

@Target in Kotlin is more than just a syntactic detail. It controls how annotations behave, where they’re valid, and how your tools (including the compiler and IDE) handle them.

If you’re building libraries, frameworks, or just want clean annotation usage, understanding @Target in Kotlin is essential. With the right @Target settings, your custom annotations stay safe, purposeful, and powerful.

from OOP to AOP

From OOP to AOP: How Aspect-Oriented Programming Enhances Your Codebase

Object-Oriented Programming (OOP) has been the backbone of software development for decades. It gave us a way to model real-world entities, encapsulate behavior, and promote reuse through inheritance and polymorphism.

But as codebases grow, some problems start slipping through the cracks. Enter Aspect-Oriented Programming (AOP). If you’ve ever found yourself copying the same logging, security checks, or error handling into multiple places, AOP might be what your codebase needs.

In this post, we’ll walk you through the transition from OOP to AOP, showing how AOP can declutter your logic, improve maintainability, and make cross-cutting concerns a breeze.

The Limits of OOP

Let’s say you have a service class:

Java
public class PaymentService {
    public void processPayment() {
        System.out.println("Checking user permissions...");
        System.out.println("Logging payment attempt...");
        // Core payment logic
        System.out.println("Processing payment...");
        System.out.println("Sending notification...");
    }
}

Looks okay? Maybe. But what happens when multiple services require permission checks, logging, and notifications? You start repeating code.

This kind of duplication violates the DRY (Don’t Repeat Yourself) principle and tangles business logic with infrastructural concerns. This is where OOP starts to fall short.

What Is AOP?

Aspect-Oriented Programming is a programming paradigm that allows you to separate cross-cutting concerns from core business logic.

Think of it like this: OOP organizes code around objects, AOP organizes code around aspects.

An aspect is a module that encapsulates a concern that cuts across multiple classes — like logging, security, or transactions.

From OOP to AOP: The Transition

Here’s what our earlier PaymentService might look like with AOP in action (using Spring AOP as an example):

Java
public class PaymentService {
    public void processPayment() {
        // Just the core logic
        System.out.println("Processing payment...");
    }
}

Now the cross-cutting logic lives in an aspect:

Java
@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.softaai.PaymentService.processPayment(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Logging payment attempt...");
    }

    @Before("execution(* com.softaai.PaymentService.processPayment(..))")
    public void checkPermissions(JoinPoint joinPoint) {
        System.out.println("Checking user permissions...");
    } 

    @After("execution(* com.softaai.PaymentService.processPayment(..))")
    public void sendNotification(JoinPoint joinPoint) {
        System.out.println("Sending notification...");
    }
}

Here,

  • @Aspect: Marks the class as an aspect.
  • @Before and @After: Define when to apply the advice.
  • JoinPoint: Carries metadata about the method being intercepted.

This keeps your business logic laser-focused and your concerns decoupled.

Why Move From OOP to AOP?

1. Cleaner Code

Business logic is free of noise. You focus on “what” a class does, not “how” to handle auxiliary tasks.

2. Reusability and Centralization

One aspect handles logging across the entire app. You don’t duplicate it in every class.

3. Maintainability

Changes in logging or security rules only require changes in one place.

4. Improved Testing

With concerns isolated, you can test core logic without worrying about side effects from logging or transactions.

Real-World Use Cases for AOP

  • Authentication & Authorization: Apply rules globally.
  • Logging: Record every action with minimal intrusion.
  • Performance Monitoring: Measure execution time of critical methods.
  • Error Handling: Catch and handle exceptions in one aspect.
  • Transaction Management: Wrap DB operations in transactions automatically.

Is AOP a Replacement for OOP?

Not at all. Think of it as an enhancement. AOP complements OOP by modularizing concerns that OOP struggles to cleanly separate.

Most modern frameworks like Spring (Java), PostSharp (.NET), and AspectJ support AOP, so the ecosystem is mature and battle-tested.

Best Practices When Using AOP

  • Don’t Overuse It: Not every piece of logic needs to be an aspect.
  • Be Transparent: Make sure team members understand the aspects being applied.
  • Use Descriptive Naming: So it’s easy to trace what each aspect does.
  • Log Wisely: Avoid logging sensitive data in aspects.

Conclusion

Making the leap from OOP to AOP isn’t about abandoning what works. It’s about recognizing when your code needs a little help separating concerns. AOP helps you write cleaner, more modular, and maintainable code.

If you’re tired of boilerplate and want your business logic to shine, exploring AOP might be your next best move.

Kotlin annotations

Kotlin Annotations 101: Learn the Basics in Under 10 Minutes

New to Kotlin and wondering what the @ symbol means? That symbol introduces Kotlin annotations — a simple yet powerful feature that adds useful metadata to your code, making it smarter, cleaner, and easier to manage.

This quick guide will show you what Kotlin annotations are, why they matter, and how to use them effectively. No complex jargon, just the essentials — all in under 10 minutes.

What Are Kotlin Annotations?

Annotations in Kotlin are a way to attach metadata to code elements such as classes, functions, properties, and parameters. Metadata is like extra information about the code that can be used by the compiler, libraries, or even runtime frameworks.

Think of Kotlin annotations as digital sticky notes. They’re not actual instructions for logic, but they tell tools how to treat your code.

Kotlin
@Deprecated("Use newFunction() instead", ReplaceWith("newFunction()"))
fun oldFunction() {
    println("This function is deprecated.")
}

Here,

  • @Deprecated tells both the developer and the compiler that oldFunction() shouldn’t be used.
  • ReplaceWith("newFunction()") offers a suggestion.

Pretty straightforward, right?

Why Use Kotlin Annotations?

Kotlin annotations let you:

  • Communicate intent clearly (e.g., deprecate functions)
  • Influence compiler behavior
  • Hook into frameworks like Android or Spring
  • Enable code generation tools

They’re also essential when working with Java interoperability, which is a key strength of Kotlin.

Built-in Kotlin Annotations You Should Know

1. @Deprecated

Used to mark something as outdated.

Kotlin
@Deprecated("Don't use this")
fun oldApi() {}

2. @JvmStatic

Useful when writing Kotlin code that will be used from Java.

Kotlin
companion object {
    @JvmStatic
    fun create() = MyClass()
}

This makes create() callable as a static method from Java.

3. @JvmOverloads

Generates Java-compatible overloads for functions with default parameters.

Kotlin
@JvmOverloads
fun greet(name: String = "World") {
    println("Hello, $name")
}

Java doesn’t support default arguments natively, so this helps bridge the gap.

4. @Target

Specifies where an annotation can be used (e.g., class, property, function).

Kotlin
@Target(AnnotationTarget.CLASS)
annotation class MyAnnotation

5. @Retention

Defines how long the annotation is kept: source, binary, or runtime.

Kotlin
@Retention(AnnotationRetention.RUNTIME)
annotation class Loggable

Runtime retention means reflection tools can access this annotation at runtime.

How to Create Your Own Kotlin Annotations

Creating custom Kotlin annotations is easy.

Kotlin
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class LogExecutionTime

This annotation can now be used on functions where you want to measure execution time.

Kotlin
@LogExecutionTime
fun performTask() {
    // Some logic here
}

Of course, this is just a tag. You’d need reflection or an AOP (Aspect-Oriented Programming) tool to act on it.

Kotlin Annotations in Android Development

In Android, Kotlin annotations are everywhere. A few you’ve probably seen:

  • @UiThread, @MainThread, @WorkerThread — indicate expected thread usage.
  • @Nullable and @NonNull — help with null safety, especially in Java interop.
  • @Parcelize — works with Kotlin’s Parcelize plugin to simplify Parcelable implementation.
Kotlin
@Parcelize
data class User(val name: String, val age: Int) : Parcelable

This eliminates boilerplate, making Android dev smoother.

Best Practices When Using Kotlin Annotations

  • Be intentional. Don’t slap annotations on everything. Know what they do.
  • Check retention policies. Source-retained annotations won’t be available at runtime.
  • Avoid clutter. Annotations should clarify, not complicate.
  • Test interop. If you’re writing code to be used in Java, test how annotations behave.

Conclusion

Kotlin annotations might seem like just extra syntax, but they play a powerful role in shaping how your code behaves, communicates, and integrates with other systems.

They reduce boilerplate, enforce contracts, and help the compiler help you.

Whether you’re building Android apps, writing libraries, or just learning the ropes, understanding Kotlin annotations will make you a stronger, more fluent Kotlin developer.

Unit Testing in Kotlin

How to Do Unit Testing in Kotlin Like a Google Engineer

Unit testing in Kotlin isn’t just about making sure your code works. It’s about writing tests that prove your code works, stays reliable over time, and catches bugs before they hit production. Google engineers treat testing as a core development skill, not an afterthought. And you can do the same.

In this guide, we’ll break down unit testing in Kotlin in a simple way. We’ll show you how to write clean, maintainable tests just like a pro. Whether you’re building Android apps or server-side Kotlin applications, this blog will give you the confidence to write bulletproof unit tests.

What is Unit Testing in Kotlin?

Unit testing is the process of testing individual units of code (like functions or classes) in isolation to ensure they work as expected. Unlike integration or UI tests, unit tests focus on your own logic, not external libraries or frameworks.

Unit Test is a piece of code that is not a part of your application. It can create and call all of your application’s public classes and methods… You want to verify whether application code works as you expect.

Why Google Engineers Prioritize Unit Testing

  • Fast feedback: Tests run in milliseconds. You catch bugs fast.
  • Safe refactoring: When you change code, tests confirm nothing breaks.
  • Confidence in deployment: You ship faster because you trust your code.
  • Documents behavior: Tests show how code is supposed to work.

Now let’s get to the fun part — how to actually do this in Kotlin.

Setting Up Unit Testing in Kotlin

Most Kotlin projects use JUnit as the test framework. Android Studio and IntelliJ IDEA make setup easy:

1. Add JUnit to your project dependencies (usually already included in Android projects). Use JUnit5 for unit testing in Kotlin. It’s modern, fast, and integrates well.

Kotlin
dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
    testImplementation("org.jetbrains.kotlin:kotlin-test:1.9.0")
}

test {
    useJUnitPlatform()
}

2. Create a test class for the code you want to test.

3. Write test methods using the @Test annotation.

Basic Unit Test in Kotlin

Let’s say you have a function that adds two numbers:

Kotlin
fun add(a: Int, b: Int): Int = a + b

Here’s how you write a test for it:

Kotlin
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class MathUtilsTest {
    @Test
    fun `add should return sum of two numbers`() {
        val result = add(3, 4)
        assertEquals(7, result)
    }
}

What’s happening here?

  • @Test marks the method as a test case.
  • assertEquals checks the expected and actual values.
  • The function name is written in backticks for clarity.

Best Practices for Unit Testing in Kotlin

Google engineers follow these principles to ensure effective unit testing in Kotlin:

1. Keep Tests Small and Focused

Each test should verify one behavior or scenario. This makes tests easy to read and maintain.

2. Use Immutable Test Data

Initialize objects as val and avoid mutating shared state between tests. This prevents flaky tests and makes debugging easier.

3. Leverage Kotlin Features

Kotlin’s concise syntax (like data classes and extension functions) makes tests more readable and expressive.

4. Test Lifecycle Annotations

  • @Before: Setup code before each test.
  • @After: Cleanup after each test.
  • @TestInstance(Lifecycle.PER_CLASS): Reuse test class instance for all tests (avoids static members).

5. Mock Dependencies

Use libraries like MockK or Mockito to replace dependencies with mock objects, so you only test your own code’s logic.

Testing with Mocks in Kotlin

Sometimes, your code depends on external systems (like APIs or databases). For true unit testing in Kotlin, you should mock those dependencies.

Use MockK — a Kotlin-first mocking library.

Add MockK to Your Project

Kotlin
dependencies {
    testImplementation("io.mockk:mockk:1.13.8")
}

Example with MockK

Kotlin
interface UserRepository {
    fun getUser(id: Int): String
}

class UserService(private val repository: UserRepository) {
    fun getUsername(id: Int): String = repository.getUser(id).uppercase()
}

class UserServiceTest {
    private val repository = mockk<UserRepository>()
    private val service = UserService(repository)
    @Test
    fun `getUsername returns uppercased username`() {
        every { repository.getUser(1) } returns "amol"
        val result = service.getUsername(1)
        assertEquals("AMOL", result)
    }
}

Key Points

  • mockk<UserRepository>() creates a mock object.
  • every { ... } returns ... defines behavior for the mock.
  • Test checks the result of the service method in isolation.

Testing Coroutines in Kotlin

Kotlin’s coroutines make asynchronous code easier, but they require special handling in tests.

Example: Testing a Coroutine Function

Suppose you have:

Kotlin
suspend fun fetchData(): String {
    delay(1000) // Simulate network call
    return "Data"
}

Test with runBlocking:

Kotlin
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.Assert.assertEquals

class DataFetchTest {
    @Test
    fun fetchDataReturnsCorrectValue() = runBlocking {
        val result = fetchData()
        assertEquals("Data", result)
    }
}

Tips:

  • Use runBlocking to execute suspending functions in tests.
  • For more advanced coroutine testing, use CoroutineTestRule and TestCoroutineDispatcher to control coroutine execution and skip delays.

Running and Maintaining Tests

  • Run tests frequently: Use your IDE or command line to run all tests after every change.
  • Fix failing tests immediately: Don’t ignore red tests.
  • Refactor tests as needed: Keep them clean and up-to-date as your code evolves.

Tips for Writing Great Unit Tests

  1. Name tests clearly: Describe what the test checks.
  2. Test one thing at a time: Keep tests focused.
  3. Keep tests fast: No real network/database.
  4. Avoid logic in tests: Use literal values.
  5. Use setup methods for repetitive boilerplate.

Common Mistakes to Avoid

  • Testing too much in one test
  • Using real APIs in unit tests
  • Not asserting outcomes
  • Ignoring failed tests
  • Skipping tests because “it works on my machine

Conclusion

Unit Testing in Kotlin isn’t just for Google engineers — it’s a superpower for every developer. By writing small, focused tests, leveraging Kotlin’s features, and using the right tools, you’ll catch bugs early and build robust applications with confidence. 

Start small, keep practicing, and soon unit testing will be second nature..!

error: Content is protected !!