Building a Kotlin project 1/2

Building a Kotlin project 1/2

Ciro Rizzo

Part 1

The best way to learn a new language is to use it in a real use case. That's way this new series of posts are focused on building a proper Android project using Kotlin.

Scenario

To cover as many scenarios as possible the project will require:

  • Accessing to the network
  • Retrieving data through out a REST API call
  • Deserialize data
  • Showing Images in a list

For this purpose why not have an app showing kittens? ;) Using the http://thecatapi.com/ API we can retrieve several funny cat images

Example

Dependencies

It looks like a very good chance to try out some very cool libraries like

Set Up the Project

Using Android Studio is extremely simple create a new project from scratch

Start a new Android Project

Start a new Android Project

Create a new project

Create a new project

Select Target Android Device

Select Target Android Device

Add an activity

Add an activity

Customize the Activity

Customize the Activity

Press on Finish, the new project from the chosen template will be created.

Done

This is the starting point of our Kitten App! However the code is still in Java, later we’re going to see how to convert it.

Defining Gradle Build Tool

The next step is to adjust the Build Tool and defining which libraries we're going to use for the project.

Before starting with this phase, have a look at what you need for an Android Kotlin project on this post

Open the Module App build.gradle (highlighted with a red rectangle in the picture)

Gradle

It's a very good practice collecting all the libraries' version and Android properties in separate scripts and accessing to them through the ext property object provided by Gradle.

The easiest way is to add at the beginning of the build.gradle file the following snippet

buildscript {
  ext.compileSdkVersion_ver = 23
  ext.buildToolsVersion_ver = '23.0.2'

  ext.minSdkVersion_ver = 21
  ext.targetSdkVersion_ver = 23
  ext.versionCode_ver = 1
  ext.versionName_ver = '1.0'

  ext.support_ver = '23.1.1'

  ext.kotlin_ver = '1.0.0'
  ext.anko_ver = '0.8.2'

  ext.glide_ver = '3.7.0'
  ext.retrofit_ver = '2.0.0-beta4'
  ext.rxjava_ver = '1.1.1'
  ext.rxandroid_ver = '1.1.0'

  ext.junit_ver = '4.12'

  repositories {
      mavenCentral()
  }

  dependencies {
      classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_ver"
  }
}

Then adding the Kotlin plugins as shown

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

Before adding the dependencies for the libraries we're going to use in the project starting to change all the version numbers of the script with the ext properties added early at the beginning of the file

android {
  compileSdkVersion "$compileSdkVersion_ver".toInteger()
  buildToolsVersion "$buildToolsVersion_ver"

  defaultConfig {
    applicationId "com.github.cirorizzo.kshows"
    minSdkVersion "$minSdkVersion_ver".toInteger()
    targetSdkVersion "$targetSdkVersion_ver".toInteger()
    versionCode "$versionCode_ver".toInteger()
    versionName "$versionName_ver"
}
...

One more change to the builTypes section

buildTypes {
    debug {
        buildConfigField("int", "MAX_IMAGES_PER_REQUEST", "10")
        debuggable true
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    release {
        buildConfigField("int", "MAX_IMAGES_PER_REQUEST", "500")
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

Next step is to declare the Libraries used in the project

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile "junit:junit:$junit_ver"

  compile "com.android.support:appcompat-v7:$support_ver"
  compile "com.android.support:cardview-v7:$support_ver"
  compile "com.android.support:recyclerview-v7:$support_ver"
  compile "com.github.bumptech.glide:glide:$glide_ver"

  compile "com.squareup.retrofit2:retrofit:$retrofit_ver"
  compile ("com.squareup.retrofit2:converter-simplexml:$retrofit_ver") {
    exclude module: 'xpp3'
    exclude group: 'stax'
}

  compile "io.reactivex:rxjava:$rxjava_ver"
  compile "io.reactivex:rxandroid:$rxandroid_ver"
  compile "com.squareup.retrofit2:adapter-rxjava:$retrofit_ver"

  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_ver"
  compile "org.jetbrains.anko:anko-common:$anko_ver"
}

Finally the build.gradle is ready to work with the project.

Just one more thing is to add the uses-permission to access to Internet, so add the following line to the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

And now we are ready to go to the next step

Designing Project Structure

Another good practice is to structure the project having different packages and folders for different group of Classes composing our project so we can structure our project as shown below.

Structure

Right Click on the Root Package com.github.cirorizzo.kshows and then New -> Package

Coding

The next post is on how to code the elements of the Kitten app

Translations

This post is also available in Chinese on gold.xitu.io A special thanks for the translation to the gold.xitu.io Team