← Back to Kotlin/anko

How to Deploy & Use Kotlin/anko

Anko Deployment and Usage Guide

⚠️ Deprecation Warning: Anko is deprecated and obsolete. JetBrains no longer maintains this project. Consider migrating to Jetpack Compose for UI layouts and standard Kotlin coroutines/Android APIs for other functionality.

Prerequisites

  • JDK: Java 8 or higher
  • Android SDK: API levels 15, 19, 21, 23, 25, 27, or 28 (depending on target)
  • Kotlin: 1.3+ (for coroutines support)
  • Build Tool: Gradle 4.0+
  • IDE: Android Studio 3.0+ (with Kotlin plugin)
  • Git: For cloning the repository

Installation

Using Anko in Your Project

Add the Anko version to your project-level build.gradle:

ext.anko_version = '0.10.8'

Add dependencies to your app-level build.gradle:

Full Anko suite:

dependencies {
    implementation "org.jetbrains.anko:anko:$anko_version"
}

Modular dependencies (recommended):

dependencies {
    // Anko Commons (Intents, Dialogs, Logging)
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    
    // Anko Layouts (DSL for views)
    implementation "org.jetbrains.anko:anko-sdk25:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
    
    // Anko SQLite
    implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
    
    // Anko Coroutines
    implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
}

Building from Source

Clone the repository:

git clone https://github.com/Kotlin/anko.git
cd anko

Sync Gradle:

./gradlew build

Configuration

SDK Version Selection

Choose the appropriate SDK artifact based on your minSdkVersion:

minSdkVersionArtifact
15anko-sdk15 / anko-sdk15-coroutines
19anko-sdk19 / anko-sdk19-coroutines
21anko-sdk21 / anko-sdk21-coroutines
23anko-sdk23 / anko-sdk23-coroutines
25anko-sdk25 / anko-sdk25-coroutines
27anko-sdk27 / anko-sdk27-coroutines
28anko-sdk28 / anko-sdk28-coroutines

Coroutines Configuration

Ensure you have kotlinx.coroutines dependency for coroutine support:

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
}

ProGuard Rules

If using ProGuard/R8, add:

-keep class org.jetbrains.anko.** { *; }
-dontwarn org.jetbrains.anko.**

Build & Run

Building the Library

Generate all SDK variants:

./gradlew :anko:library:generate

Build specific module:

./gradlew :anko:library:generated:sdk25:build

Run tests:

./gradlew test

Using Anko Layouts in Development

Enable the Anko Support Plugin in Android Studio:

  1. Install "Anko Support" plugin from JetBrains repository
  2. Restart Android Studio
  3. Preview DSL layouts in the design tab

Sample Implementation

Create a layout in Kotlin:

import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        verticalLayout {
            padding = dip(16)
            
            val name = editText {
                hint = "Name"
            }
            
            button("Say Hello") {
                onClick {
                    toast("Hello, ${name.text}!")
                }
            }
        }
    }
}

Deployment

Publishing to Local Maven

./gradlew publishToMavenLocal

Deploying Apps Using Anko

Since Anko is deprecated, consider these migration paths for production apps:

  1. Replace Anko Layouts with Jetpack Compose:

    // Instead of Anko DSL
    setContent {
        Column {
            TextField(value = name, onValueChange = { name = it })
            Button(onClick = { /* action */ }) {
                Text("Say Hello")
            }
        }
    }
    
  2. Replace Anko Commons with Android KTX:

    implementation "androidx.core:core-ktx:1.9.0"
    
  3. Replace Anko SQLite with Room:

    implementation "androidx.room:room-runtime:2.5.0"
    

Legacy Bintray Artifacts

Previous versions were hosted on Bintray (now sunset). Use Maven Central or JitPack for legacy versions:

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

Troubleshooting

"anko is deprecated" Warnings

Issue: IDE shows deprecation warnings. Solution: This is expected. Migrate to Jetpack Compose for new projects or suppress with @Suppress("DEPRECATION").

Coroutine Context Exceptions

Issue: IllegalStateException: Module with the Main dispatcher had failed to initialize. Solution: Ensure you include kotlinx-coroutines-android:

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0"

SDK Version Mismatch

Issue: NoSuchMethodError or ClassNotFoundException for View methods. Solution: Match your Anko SDK artifact to your compileSdkVersion. If using SDK 28, use anko-sdk28, not anko-sdk25.

Generated Code Not Found

Issue: Cannot find ListenersWithCoroutines classes. Solution: Ensure you import the correct package:

import org.jetbrains.anko.sdk25.coroutines.* // or your SDK version

Gradle Sync Failures

Issue: Cannot resolve org.jetbrains.anko:anko:0.10.8. Solution: Add JCenter as a fallback repository (though deprecated):

repositories {
    jcenter() // Legacy repository
    mavenCentral()
}

Memory Leaks with Coroutines

Issue: Activities leaked in coroutine listeners. Solution: Use asReference() wrapper (if available in your version) or ensure proper coroutine scope cancellation in onDestroy():

private val job = Job()
override fun onDestroy() {
    job.cancel()
    super.onDestroy()
}