Skip to main content

Posts

Showing posts from November, 2015

Again: Supporting Android Typedefs in Kotlin

Before reading on, it is important to note that Android lint checks do not run on Kotlin code. However, it is possible to annotate Kotlin code such that lint can correctly check Java code against the annotated interfaces. There is an issue tracking lint support for Kotlin here . Typedef (Enumerated Annotations) Previously , I wrote about how to get a library written in Kotlin to interoperate with IntDef and StringDef. The method I outlined got the lint check working in a partial state. However, here is what you have to do to get the annotation to work properly. In Kotlin, declare the constants that are the values in the typedef. DatePicker.kt package com.example.datepicker class MyKotlinClass { companion object { const val YEAR = 1L const val MONTH = 2L const val WEEK = 3L const val DAY = 4L } } The actual typedef is must be declared in Java. DatePickedKind.java package com.example.datepicker import android.support.annot...

Easy SDK version handling with Anko

Anko is a library for Kotlin that implements a bunch of DSLs that significantly simplifies and cleans up your Android code. One of the small pieces it offers are some extension functions to selectively execute code based on the current configuration. It looks like this: configuration(screenSize = ScreenSize.LARGE, orientation = Orientation.LANDSCAPE) { // This code will be only executed // if the screen is large and its orientation is landscape } Since it was available I thought it’d be nice to use it to tidy up my code that targets different versions of the Android SDK. configuration(fromSdk = Build.VERSION_CODES.M) { // This code will be only executed // on sdk versions 23 and later } However, this has a problem because I want to target multiple version ranges of the SDK. Fortunately, it turns out the function returns null if the configuration check fails. With the power of Elvis ( ?: ) the null return can distinguish when the closure hasn’t been executed and ...

Kotlin, IntDef, and Android libraries

Lately I’ve been investing time into Kotlin. Tonight, I was particularly interested in its utility for Android libraries. IntDef Update : Actually, lint doesn’t quite interoperate properly with Kotlin. So, while the following gets IntDefs and the related support annotations working 90%, you have to do things a little different, which I’ve outlined here . First up is how to use the support library’s IntDef annotation in Kotlin. This is especially important for library development since it is considered best practice to avoid enum usage for Android development. I don’t really want to go into the arguments about this practice, this is just how to use it. As it turns out, this is pretty easy. In Kotlin, using IntDef looks like this; companion object { const val YEAR = 1L const val MONTH = 2L const val WEEK = 3L const val DAY = 4L } @IntDef(YEAR, MONTH, WEEK, DAY) annotation class TimePeriod and the same code in Java looks like this: public static final lon...