anko README

The project's README from github.com/Kotlin/anko. As one markdown file: README.md.

obsolete JetBrains project Download GitHub license

Anko logo

:warning: Anko is deprecated. Please see this page for more information.


Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.

Anko consists of several parts:

Anko Commons

Anko Commons is a "toolbox" for Kotlin Android developer. The library contains a lot of helpers for Android SDK, including, but not limited to:

Anko Layouts (wiki)

Anko Layouts is a DSL for writing dynamic Android layouts. Here is a simple UI written with Anko DSL:

verticalLayout {
    val name = editText()
    button("Say Hello") {
        onClick { toast("Hello, ${name.text}!") }
    }
}

The code above creates a button inside a LinearLayout and attaches an OnClickListener to that button. Moreover, onClick accepts a suspend lambda, so you can write your asynchronous code right inside the listener!

Note that this is the complete layout code. No XML is required!

Anko has a DSL for ConstraintLayout since v0.10.4

Hello world

There is also a plugin for Android Studio that supports previewing Anko DSL layouts.

Anko SQLite (wiki)

Have you ever been tired of parsing SQLite query results using Android cursors? Anko SQLite provides lots of helpers to simplify working with SQLite databases.

For example, here is how you can fetch the list of users with a particular name:

fun getUsers(db: ManagedSQLiteOpenHelper): List<User> = db.use {
    db.select("Users")
            .whereSimple("family_name = ?", "John")
            .doExec()
            .parseList(UserParser)
}

Anko Coroutines (wiki)

Anko Coroutines is based on the kotlinx.coroutines library and provides:

Using Anko

Gradle-based project

Anko has a meta-dependency which plugs in all available features (including Commons, Layouts, SQLite) into your project at once:

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

Make sure that you have the $anko_version settled in your gradle file at the project level:

ext.anko_version='0.10.8'

If you only need some of the features, you can reference any of Anko's parts:

dependencies {
    // Anko Commons
    implementation "org.jetbrains.anko:anko-commons:$anko_version"

    // Anko Layouts
    implementation "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available
    implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"

    // Coroutine listeners for Anko Layouts
    implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"

    // Anko SQLite
    implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
}

There are also a number of artifacts for the Android support libraries:

dependencies {
    // Appcompat-v7 (only Anko Commons)
    implementation "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"

    // Appcompat-v7 (Anko Layouts)
    implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
    implementation "org.jetbrains.anko:anko-coroutines:$anko_version"

    // CardView-v7
    implementation "org.jetbrains.anko:anko-cardview-v7:$anko_version"

    // Design
    implementation "org.jetbrains.anko:anko-design:$anko_version"
    implementation "org.jetbrains.anko:anko-design-coroutines:$anko_version"

    // GridLayout-v7
    implementation "org.jetbrains.anko:anko-gridlayout-v7:$anko_version"

    // Percent
    implementation "org.jetbrains.anko:anko-percent:$anko_version"

    // RecyclerView-v7
    implementation "org.jetbrains.anko:anko-recyclerview-v7:$anko_version"
    implementation "org.jetbrains.anko:anko-recyclerview-v7-coroutines:$anko_version"

    // Support-v4 (only Anko Commons)
    implementation "org.jetbrains.anko:anko-support-v4-commons:$anko_version"

    // Support-v4 (Anko Layouts)
    implementation "org.jetbrains.anko:anko-support-v4:$anko_version"

    // ConstraintLayout
    implementation "org.jetbrains.anko:anko-constraint-layout:$anko_version"
}

There is an example project showing how to include Anko library into your Android Gradle project.

IntelliJ IDEA project

If your project is not based on Gradle, just attach the required JARs from the jcenter repository as the library dependencies and that's it.

Contributing

The best way to submit a patch is to send us a pull request. Before submitting the pull request, make sure all existing tests are passing, and add the new test if it is required.

If you want to add new functionality, please file a new proposal issue first to make sure that it is not in progress already. If you have any questions, feel free to create a question issue.

Instructions for building Anko are available in the Wiki.

anko (Kotlin/anko)

Searching Database…

15.8K ·GitHub
archived
这是什么?Anko 是一个 Kotlin 库,能让 Android 应用开发更快、更简单:它让你的代码保持整洁易读,并掩盖了 Android SDK 中为 Java 设计的那些难用的地方。
它是一个大而全的库,还是一组模块?是四个模块的组合:Anko Commons(intent、对话框、日志等方面的辅助工具)、Anko Layouts(用于动态布局的 DSL)、Anko SQLite(查询 DSL 和解析器集合)以及 Anko Coroutines(基于 kotlinx.coroutines 的实用工具)。
怎么把 Anko 加到我的 Android 应用里?在 Gradle 项目中添加元依赖即可——它会一次性接入所有部分(Commons、Layouts、SQLite),也可以只引用你需要的部分。非 Gradle 的 IntelliJ 项目可以把 jcenter 上的 JAR 作为库依赖附加进来。
除了普通的 Android 项目,使用它还需要别的吗?只需要一套 Kotlin Android 环境:Anko 是用 Kotlin 写的,它的 coroutines 模块基于 kotlinx.coroutines 库,而后者会作为常规依赖自动带上。
Anko 对新项目来说还是稳妥的选择吗?不算了——Anko 已被弃用,仓库也已归档,所以它更适合维护现有应用,而不适合开启新开发。README 里链接了一个页面,提供更多信息。
我能用 Kotlin 代替 XML 来写布局吗?可以——Anko Layouts 是一个用于 Android 动态布局的类型安全 DSL,README 还强调这段 DSL 代码就是完整的布局代码:不需要任何 XML。从 v0.10.4 起还加入了 ConstraintLayout 的 DSL。
写代码时能直观预览 Anko 布局吗?可以——有一个专门的 Android Studio 插件,支持预览 Anko DSL 布局。
在 SQLite 方面 Anko 提供了什么?Anko SQLite 提供了查询 DSL、解析器和辅助工具,让你不用手动解析 cursor 的查询结果——按某个名字取一个用户列表只需要几行代码。
它是怎么处理后台和异步代码的?像 onClick 这样的监听器可以接受 suspend lambda,所以异步代码可以直接写在里面。Anko Coroutines 还提供了 bg() 用于在公共线程池上运行代码,以及 asReference() 用来防止不可取消的协程泄漏所捕获的对象。
Creator
Kotlin
Size
14.9 MB
Stars
15.8K
Age
12y
Commits · 90 Days