Intro Mobile

75.72 - Taller de programación 2

¿Que vamos a ver?

  • Intro Android

  • Intro Kotlin

  • Ejemplo App Kotlin

Android

Insights

Insights

Versiones

Arquitectura

Kotlin

  • Desarrollado por el equipo de Jetbrains

  • Compatible con java

  • Bytecode corre en JVM

Algunas características...

Checked Exceptions

Coroutines

public final void throwSomething() {
   throw (Throwable)(new IOException());
}

// Compatibilidad con exceptiones
// chequeadas como en Java -> @Throws

@Throws(IOException::class)
fun throwSomething() {
   throw IOException("")
}
suspend fun fetchAndShowUser() {
	// fetch on IO thread
    val user = fetchUser() 
    // back on UI thread
    showUser(user) 
}

Data Classes

Extension Functions

// Kotlin
data class User(var name: String, var age: Int)

// Java
public class User {

    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
fun String.removeFirstLastChar(): String =  this.substring(1, this.length - 1)
fun main(args: Array<String>) {
    val myString= "Hello Everyone"
    val result = myString.removeFirstLastChar()
    println("First character is: $result")
}

Null Safety

Mutable & immutable variables

// Not null safe value
var a: String = "abc"
a = null // compilation error


// Null safe value
var b: String? = "abc"
b = null // ok
print(b)

// Safe Calls

val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
// Mutable
var mutable: Int = 1
mutable = 2 // Ok

// immutable
val immutable: Int = 1
immutable = 2 // Error

¿Porque Kotlin?

Contexto Android

Activity

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.toggletest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.toggletest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

</manifest>

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>

Testing

Android Studio

APK Bundle

Nativo vs WebApp

Made with Slides.com