Giter Site home page Giter Site logo

antoniolg / kotlin-for-android-developers Goto Github PK

View Code? Open in Web Editor NEW
2.7K 137.0 610.0 505 KB

Companion App for the book

Home Page: http://antonioleiva.com/kotlin-android-developers/

License: Apache License 2.0

Kotlin 100.00%
kotlin kotlin-android android-development book

kotlin-for-android-developers's Introduction

Kotlin for Android Developers (the book)

This is the code you can use to follow the book.

https://antonioleiva.com/kotlin-android-developers-book/

Are you tired of using an ancient, inexpressive and unsafe language to develop your Android apps? Do you cry out loud every time you see a Null Pointer Exception in your bug tracker? Then Kotlin is your solution! A language specifically created for Java developers, easy to learn, expressive, null safe and really intuitive. Your productivity will boost and your apps will become more robust. Learn Kotlin the easy way by example and discover the tricks that will make coding easier.

And now, it's officially supported by Google!

Kotlin for Android Developers cover

About the book

In this book, I'll be creating an Android app from ground using Kotlin as the main language. The idea is to learn the language by example, instead of following a typical structure. I'll be stopping to explain the most interesting concepts and ideas about Kotlin, comparing it with Java 7. This way, you can see what the differences are and which parts of the language will help you speed up your work.

This book is not meant to be a language reference, but a tool for Android developers to learn Kotlin and be able to continue with their own projects by themselves. I'll be solving many of the typical problems we have to face in our daily lives by making use of the language expressiveness and some other really interesting tools and libraries.

The book is very practical, so it is recommended to follow the examples and the code in front of a computer and try everything it's suggested. You could, however, take a first read to get a broad idea and then dive into practice.

Versions

As I update the book, I need to push -f this repository with the new changes, so that it matches with the new text.

That means that if you are reading an old version of the book, main branches won't be aligned with your text.

To make things easier, I'll keep track of those versions in separates branches, which will be linked from here:

License

Copyright 2019 Antonio Leiva

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

kotlin-for-android-developers's People

Contributors

antoniolg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlin-for-android-developers's Issues

zip code vs city id in chapter-19 branch

In the chapter-19 branch there is a mix-up between zip code and city id. The code compiles and runs because the database is not used. Nonetheless I believe there is an error. Here is what I think needs to change:

In ForecastDataMapper.convertFromDataModel the first parameter in the ForecastList constructor should be "city.id" instead of "zipCode". Additionally, the "zipCode" parameter should be removed from convertFromDataModel so that it takes only a ForecastResult.

Once this change is made the overridden function in RequestForecastCommand must be changed so the "zipCode" parameter is removed from the parameters passed to convertFromDataModel.

This will fix the issue, but I would also recommend changing the name of the "id" parameter on the ForecastList constructor to "cityId" to make it clearer what id this is.

todayTimeSpan() does not account for time zone, but OpenWeatherMap API does

This becomes a problem when the date of your time zone differs from the date of GMT time.

  1. ForecastByZipCodeRequest obtains a weekly forecast starting with today in your current time zone.
  2. ForecastDb.requestForecastByZipCode(zipCode, todayTimeSpan()) filters out the first row of DayForecastTable because its column DATE precedes todayTimeSpan().
  3. ForecastProvider.requestByZipCode(zipCode, days) never returns a non-null value.

I am in America/New York (EDT GMT -04:00 DST) time zone. Here is a real example when I run the app:

  • System.currentTimeMillis() is 1464056054644, or May 23, 2016 at 10:14:14 PM EDT GMT-4:00 DST
  • todayTimeSpan() is 1464048000000, or May 23, 2016 at 8:00:00 PM EDT GMT-4:00 DST
  • Date values for returned by OpenWeatherMap for seven-day forecast are: {1464001200000, 1464087600000, 1464174000000, 1464260400000, 1464346800000, 1464433200000, 1464519600000}
  • Database only returns {1464087600000, 1464174000000, 1464260400000, 1464346800000, 1464433200000, 1464519600000}

Chapter 8 Code example doesn't run as described

Running the code from Chapter 8 branch, the toast never actually occurs. This is the error log I see in Logcat:

11-19 16:57:50.053 5203-5203/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-19 16:57:50.064 5203-5203/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-19 16:57:50.275 5203-5203/com.antonioleiva.weatherapp I/InstantRun: starting instant run server: is main process
11-19 16:57:50.462 5203-5229/com.antonioleiva.weatherapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-19 16:57:50.489 5203-5231/com.antonioleiva.weatherapp D/OpenGLRenderer: HWUI GL Pipeline

                                                                       [ 11-19 16:57:50.541  1902: 2245 W/         ]
                                                                       Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 

11-19 16:57:50.545 5203-5229/com.antonioleiva.weatherapp W/System.err: java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/forecast/daily?APPID=15646a06818f61f7b8d7823ca833e1ce&q=94043&mode=json&units=metric&cnt=7
11-19 16:57:50.546 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
11-19 16:57:50.551 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.net.URL.openStream(URL.java:1058)
11-19 16:57:50.553 5203-5229/com.antonioleiva.weatherapp W/System.err: at kotlin.io.TextStreamsKt.readBytes(ReadWrite.kt:144)
11-19 16:57:50.562 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.antonioleiva.weatherapp.data.Request.run(Request.kt:9)
11-19 16:57:50.563 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.antonioleiva.weatherapp.ui.activities.MainActivity$onCreate$1.invoke(MainActivity.kt:39)
11-19 16:57:50.563 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.antonioleiva.weatherapp.ui.activities.MainActivity$onCreate$1.invoke(MainActivity.kt:15)
11-19 16:57:50.563 5203-5229/com.antonioleiva.weatherapp W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt:140)
11-19 16:57:50.569 5203-5229/com.antonioleiva.weatherapp W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Unknown Source:0)
11-19 16:57:50.570 5203-5229/com.antonioleiva.weatherapp W/System.err: at org.jetbrains.anko.AsyncKt$sam$Callable$761a5578.call(Unknown Source:2)
11-19 16:57:50.570 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11-19 16:57:50.571 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
11-19 16:57:50.572 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
11-19 16:57:50.573 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
11-19 16:57:50.574 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.lang.Thread.run(Thread.java:764)
11-19 16:57:51.029 5203-5231/com.antonioleiva.weatherapp I/OpenGLRenderer: Initialized EGL, version 1.4
11-19 16:57:51.029 5203-5231/com.antonioleiva.weatherapp D/OpenGLRenderer: Swap behavior 1
11-19 16:57:51.030 5203-5231/com.antonioleiva.weatherapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
11-19 16:57:51.030 5203-5231/com.antonioleiva.weatherapp D/OpenGLRenderer: Swap behavior 0
11-19 16:57:51.032 5203-5231/com.antonioleiva.weatherapp D/EGL_emulation: eglCreateContext: 0xa73850c0: maj 2 min 0 rcv 2
11-19 16:57:51.035 5203-5231/com.antonioleiva.weatherapp D/EGL_emulation: eglMakeCurrent: 0xa73850c0: ver 2 0 (tinfo 0xa73832a0)

                                                                      [ 11-19 16:57:51.038  1902: 1911 W/         ]
                                                                      Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 

11-19 16:57:51.063 5203-5231/com.antonioleiva.weatherapp D/EGL_emulation: eglMakeCurrent: 0xa73850c0: ver 2 0 (tinfo 0xa73832a0)

ServerClasses, gson and null properties

I have a doubt about the data classes in ServerClasses.tk.
This classes are popullated using GSon. When Gson creates the classes it will use a kind of empty constructor and it will fill the properties, therefore, there might be values that are null (in case the api changes for example). Shouldn't we put default values ensure no null properties?

Botttom of page 19

import kotlinx.android.synthetic.main.activity_main.*

I could not find where this was automatically to the activity

RecyclerView: No adapter attached; skipping layout

@antoniolg
I followed your book step by step till chapter 15, and I compiled it on my Nexus 5.
It is an empty screen still.

Here is my build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
androidExtensions {
experimental = true
}

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'

defaultConfig {
    applicationId "com.example.user.weatherkotlin"
    minSdkVersion 17
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.code.gson:gson:2.8.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.anko:anko:0.10.5'
implementation 'org.jetbrains.anko:anko-common:0.10.5'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Database Error

Hello, I got a database error and the screen is empty when I first run this app.
Log‘s here : E/SQLiteLog: (1) near "org": syntax error

Book Errata: 7.3 Extension functions

Paragraph 2, Sentence 1 of 7.3 Extension functions

For instance, we can create a toast function that doesn't the context as parameter, which could ...

Should it be like this:
For instance, we can create a toast function that doesn't need the context as parameter...

(Inserted "need")

errors importing in androdiStudio3 preview2

importing this demo app in the last canary Android Studio, I found some errors during the sync.

please modify the code of "app" like:

    compile "org.jetbrains.anko:anko:0.10.1"
    compile "org.jetbrains.anko:anko-common:0.10.1"
    compile "org.jetbrains.anko:anko-sqlite:0.10.1n"
    compile "org.jetbrains.anko:anko-coroutines:0.10.1"

this is enounght to fix the errors of sync

regards
MaX

Book Errata: 7.2 Start using Anko

The following code sample does not work anymore:
val forecastList: RecyclerView = find(R.id.forecast_list)

Use this instead:
val forecastList = find<RecyclerView>(R.id.forecast_list)

Screen Is Empty After Fetching Weather Information

Okay. So, I started with the tutorial and everything has been going on fine not until I got to chapter 10 trying to implement the Asyn.

 doAsync {
        val result = RequestForecastCommand("94043").execute()
        Log.e(javaClass.simpleName, result.toString()) <-- It doesn't get to this place at all
        uiThread() {
            Log.e(TAG, "I am here")
            Log.e(javaClass.simpleName, result.toString())
            forcast_list.adapter = ForcastListAdapter(result)
        }
    }

Then, I debugged further and checked the ForecastRequest.kt file

 class ForecastRequest(val zipCode: String) {

companion object {
    private val APP_ID = "15646a06818f61f7b8d7823ca833e1ce"
    private val URL = "http://api.openweathermap.org/data/2.5/" +
            "forecast/daily?mode=json&units=metric&cnt=7"
    private val COMPLETE_URL = "$URL&APPID=$APP_ID&q="
}

fun execute(): ForecastResult {
    val forcastJsonStr = URL(COMPLETE_URL + zipCode).readText()
    Log.e(javaClass.simpleName, forcastJsonStr)<-- This printed out a result
    return Gson().fromJson(forcastJsonStr, ForecastResult::class.java)
}
}

I don't know why it's not throwing any error or any bug though. And should it be a Gson conversion error, at least I'm supposed to get an error right?

failed to run from master branch

I am facing a problem running the app from 'master' , gradle task is failing with these error

screen shot 2017-10-04 at 12 39 20 am

I have searched with this error on StackOverFlow . Most of the thread came with solution adding kotlin android plugin , which is already added . Am I missing something @antoniolg

crash:null check

Crash:
SettingPage
Clear the default zipCode and then exit the current page

Create SqlLite table failed when running the app

When I run the app, an error was occurred by create database table DayForecastTable. This is the error:
android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS DayForecast(id INTEGER PRIMARY KEY org.jetbrains.anko.db.SqlTypeModifierImpl@a5038ce, date INTEGER, description TEXT, high INTEGER, low INTEGER, iconUrl TEXT, cityId INTEGER);
Why the AUTOINCREMENT cannot be recognized. My anko version is 0.10.1

Page 43 URL Error

May be an error with android studio but there was no mention of which import was needed here.

Chapter 19 does not compile

I have been following along in the book. When I added the code for Chapter 19, the DatabaseExtensions.kt file will not compile. To ensure that I didn't type something wrong, I got a zip of the Chapter 19 commit and got the same error:

  • Error:(8, 19) Object must be declared abstract or implement abstract member public abstract fun parseRow(columns: Map<String, Any?>): T defined in org.jetbrains.anko.db.MapRowParser
  • Error:(9, 13) 'parseRow' overrides nothing
  • Error:(13, 18) Object must be declared abstract or implement abstract member public abstract fun parseRow(columns: Map<String, Any?>): T defined in org.jetbrains.anko.db.MapRowParser
  • Error:(14, 13) 'parseRow' overrides nothing

I'm using buildVersonSdk 25.0.0, targetSdkVersion 25, ext.support_version 25.1.0, ext.kotlin_version 1.0.6, ext_anko_version 0.9 in Android Studio. Everything had worked up to the transition from earlier chapters to chapter 19. Since I'm new to Kotlin, I don't have a clue what is wrong or how to fix it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.