Building a Kotlin project - Part 1

770 阅读2分钟
原文链接: cirorizzo.net
Part 1
本文已经翻译成中文《【译】创建一个基于 Kotlin 的 Android 项目(上集)》,欢迎参加「掘金翻译计划」,翻译优质的技术文章。

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 thecatapi.com/ API we can retrieve several funny cat images
查看图片

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 查看图片

Create a new project 查看图片

Select Target Android Device 查看图片

Add an activity 查看图片

Customize the Activity 查看图片

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

查看图片

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)

查看图片

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


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.

查看图片

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