android & kotlin - the code awakens #01

47
Android & Kotlin The code awakens #01

Upload: omar-miatello

Post on 15-Apr-2017

84 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Android & Kotlin - The code awakens #01

Android & KotlinThe code awakens #01

Page 2: Android & Kotlin - The code awakens #01

Omar MiatelloMember of GDG Milano (Italy)Android Developer @ Satispay

Personal profilegoogle.com/+OmarMiatelloGoogle+ Community: Kotlin for Androidgoo.gl/mUKF1w

Google Presentation#01 goo.gl/0jHLmE#02 goo.gl/h3uG8M#03 goo.gl/hnwvqu

Google Photo#01 goo.gl/photos/cKP9L6zqZcxDRGzQ8#02 goo.gl/photos/sXdpkbihCi5xAAnx7#03 goo.gl/photos/P6kGhLE8yrWYnhAW6

Page 3: Android & Kotlin - The code awakens #01

What is Kotlin?

Statically typed programming language for the JVM, Android and the browser.(http://kotlinlang.org/)

Why Kotlin?

Concise: drastically reduce the amount of boilerplate code you need to write.

Safe: avoid entire classes of errors such as null pointer exceptions.

Interoperable: leverage existing frameworks and libraries of the JVM with 100% Java Interoperability.

and more... http://kotlinlang.org/docs/reference/comparison-to-java.html

Page 4: Android & Kotlin - The code awakens #01

Kotlin vs Java - Part 1Property

String templatesLambdas

Lazy properties

Page 5: Android & Kotlin - The code awakens #01

vspublic class MyKotlinClass { val a: Int = 1}

public class MyJavaClass { private final int a = 1;

public int getA() { return a; }}

#1 Kotlin - Properties: val, varhttp://kotlinlang.org/docs/reference/properties.html

Page 6: Android & Kotlin - The code awakens #01

public class MyKotlinClass { val a: Int = 1 var b: Int = 1}

public class MyJavaClass { private final int a = 1; private int b = 1;

public int getA() { return a; }

public int getB() { return b; }

public void setB(int b) { this.b = b; }}

#1 Kotlin - Properties: val, varhttp://kotlinlang.org/docs/reference/properties.html

vs

Page 7: Android & Kotlin - The code awakens #01

public class MyKotlinClass { val a: Int = 1 var b: Int = 1 val c = 1 var d = 1}

public class MyJavaClass { private final int a = 1; private int b = 1; private final int c = 1; private int d = 1;

public int getA() { return a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public int getD() { return d; } public void setD(int d) { this.d = d; }}

#1 Kotlin - Properties: val, varhttp://kotlinlang.org/docs/reference/properties.html

vs

Page 8: Android & Kotlin - The code awakens #01

class MyKotlinClass { val name = "Omar" val surname = "Miatello"}

class MyJavaClass { final String getName() { return "Omar"; }

final String getSurname() { return "Miatello"; }}

#2 Kotlin - String templateshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates

vs

Page 9: Android & Kotlin - The code awakens #01

class MyKotlinClass { val name = "Omar" val surname = "Miatello" val example = "My name is $name $surname"}

class MyJavaClass { final String getName() { return "Omar"; }

final String getSurname() { return "Miatello"; }

final String getExample() { return String.format("My name is %s %s", getName(), getSurname()); }}

#2 Kotlin - String templateshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates

vs

Page 10: Android & Kotlin - The code awakens #01

class MyKotlinClass { val name = "Omar" val surname = "Miatello" val example = "My name is $name $surname"}

class MyJavaClass { final String getName() { return "Omar"; }

final String getSurname() { return "Miatello"; }

final String getExample() { return String.format("My name is %s %s", getName(), getSurname()); }}

#2 Kotlin - String templateshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates

vs

Page 11: Android & Kotlin - The code awakens #01

class MyActivity : Activity() {

fun example() { val view = findViewById(R.id.button) view.setOnClickListener {

} }

}

class MyActivity extends Activity {

void example() { View view = findViewById(R.id.button); view.setOnClickListener(

); }

}

#3 Kotlin - Lambdashttp://kotlinlang.org/docs/reference/coding-conventions.html#lambdas

vs

Page 12: Android & Kotlin - The code awakens #01

class MyActivity : Activity() {

fun example() { val view = findViewById(R.id.button) view.setOnClickListener { Log.d("TAG", "Item clicked!") } }

}

class MyActivity extends Activity {

void example() { View view = findViewById(R.id.button); view.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "Item clicked!"); } } ); }

}

#3 Kotlin - Lambdashttp://kotlinlang.org/docs/reference/coding-conventions.html#lambdas

vs

Page 13: Android & Kotlin - The code awakens #01

class MyActivity : Activity() {

fun example() { val view = findViewById(R.id.button) view.setOnClickListener { Log.d("TAG", "Item clicked!") } }

}

class MyActivity extends Activity {

void example() { View view = findViewById(R.id.button); view.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "Item clicked!"); } } ); }

}

#3 Kotlin - Lambdashttp://kotlinlang.org/docs/reference/coding-conventions.html#lambdas

vs

Page 14: Android & Kotlin - The code awakens #01

class MyJavaClass { class MyItem { }

MyItem item;}

#4 Kotlin - Delegated Properties: lazy (as example)http://kotlinlang.org/docs/reference/delegated-properties.html

vs

Page 15: Android & Kotlin - The code awakens #01

class MyJavaClass { class MyItem { }

MyItem item;

final MyItem getItem() { if (item == null) { item = new MyItem(); } return item; }}

#4 Kotlin - Delegated Properties: lazy (as example)http://kotlinlang.org/docs/reference/delegated-properties.html

vs

Page 16: Android & Kotlin - The code awakens #01

class MyItem

class MyKotlinClass { val item by lazy { MyItem() }}

// Simplified: in Kotlin is synchronized

class MyJavaClass { class MyItem { }

MyItem item;

final MyItem getItem() { if (item == null) { item = new MyItem(); } return item; }}

#4 Kotlin - Delegated Properties: lazy (as example)http://kotlinlang.org/docs/reference/delegated-properties.html

vs

Page 17: Android & Kotlin - The code awakens #01

Install Kotlin in Android StudioPart 1 / 2

Page 18: Android & Kotlin - The code awakens #01

1. Open “Preferences”

Install Kotlin in Android Studio

Page 19: Android & Kotlin - The code awakens #01

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

Install Kotlin in Android Studio

Page 20: Android & Kotlin - The code awakens #01

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

Install Kotlin in Android Studio

Page 21: Android & Kotlin - The code awakens #01

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

Install Kotlin in Android Studio

Page 22: Android & Kotlin - The code awakens #01

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

5. Create a new “Kotlin class”

Install Kotlin in Android Studio

Page 23: Android & Kotlin - The code awakens #01

Android ExampleFrom Java to Kotlin! (step1)

https://github.com/jacklt/KotlinExample/tree/java-version

Page 24: Android & Kotlin - The code awakens #01

dummy/HeroItem.javapublic class HeroItem { public final String id; public final String content; public final String details;

public HeroItem(String id, String content, String details) { this.id = id; this.content = content; this.details = details; }

@Override public String toString() { return content; }}

Page 25: Android & Kotlin - The code awakens #01

dummy/HeroItem.javapublic class HeroItem { public final String id; public final String content; public final String details;

public HeroItem(String id, String content, String details) { this.id = id; this.content = content; this.details = details; }

@Override public String toString() { return content; }}

“Convert Java File to Kotlin File”

CTRL + ALT + SHIFT + K(or CMD + ALT + SHIFT + K)

Page 26: Android & Kotlin - The code awakens #01

dummy/HeroItem.ktclass HeroItem(val id: String, val content: String, val details: String) { override fun toString(): String { return content }}

Page 27: Android & Kotlin - The code awakens #01

dummy/HeroItem.kt (optimized)

data class HeroItem(val id: String, val content: String, val details: String)

Page 28: Android & Kotlin - The code awakens #01

dummy/Items.kt (... a new hope)

data class HeroItem(val id: String, val content: String, val details: String)

data class VillanItem(val name: String, val power: String)

data class SpacecraftItem(val armaments: String, val defenses: String)

data class TinyItem(val name: String)

// ...

Page 29: Android & Kotlin - The code awakens #01

dummy/HeroAdapter.java (before HeroItem Conversion)

public class HeroAdapter extends RecyclerView.Adapter<HeroAdapter.ViewHolder> { // ... @Override public void onBindViewHolder(final ViewHolder holder, int position) { HeroItem item = mValues.get(position); holder.item = item; holder.idView.setText(item.id); holder.contentView.setText(item.content);

// ... }

// ...}

Page 30: Android & Kotlin - The code awakens #01

dummy/HeroAdapter.java (after HeroItem Conversion)

public class HeroAdapter extends RecyclerView.Adapter<HeroAdapter.ViewHolder> { // ... @Override public void onBindViewHolder(final ViewHolder holder, int position) { HeroItem item = mValues.get(position); holder.item = item; holder.idView.setText(item.getId()); holder.contentView.setText(item.getContent());

// ... }

// ...}

Page 31: Android & Kotlin - The code awakens #01

Install Kotlin in Android StudioPart 2 / 2

Page 32: Android & Kotlin - The code awakens #01

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

5. Create a new “Kotlin class”

6. Choose from menu “Tools” > “Kotlin” > “Configure Kotlin in Project”

Install Kotlin in Android Studio

Page 33: Android & Kotlin - The code awakens #01

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

5. Create a new “Kotlin class”

6. Choose from menu “Tools” > “Kotlin” > “Configure Kotlin in Project”

7. Ready for Kotlin! :)

dependencies { // other dependencies ... compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}buildscript { ext.kotlin_version = '1.0.0-beta-2423' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }}repositories { mavenCentral()}

Install Kotlin in Android Studio

Page 34: Android & Kotlin - The code awakens #01

Android ExampleFrom Java to Kotlin! (step2)

HeroAdapter

Page 35: Android & Kotlin - The code awakens #01

dummy/HeroAdapter.javapublic class HeroAdapter extends RecyclerView.Adapter<HeroAdapter.ViewHolder> {

private final List<HeroItem> mValues; private final HeroOnClickListener mListener;

public HeroAdapter(List<HeroItem> items, HeroOnClickListener listener) { mValues = items; mListener = listener; }

@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hero, parent, false); return new ViewHolder(view); }

@Override public void onBindViewHolder(final ViewHolder holder, int position) { HeroItem item = mValues.get(position);

“Convert Java File to Kotlin File”

CTRL + ALT + SHIFT + K(or CMD + ALT + SHIFT + K)

Page 36: Android & Kotlin - The code awakens #01

dummy/HeroAdapter.kt (need manual fix)// ...inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val idView: TextView val contentView: TextView var item: HeroItem

init { idView = view.findViewById(R.id.id) as TextView contentView = view.findViewById(R.id.content) as TextView }}// ...

Page 37: Android & Kotlin - The code awakens #01

dummy/HeroAdapter.kt (fixed!)// ...inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val idView: TextView val contentView: TextView var item: HeroItem? = null

init { idView = view.findViewById(R.id.id) as TextView contentView = view.findViewById(R.id.content) as TextView }}// ...

Page 38: Android & Kotlin - The code awakens #01

dummy/HeroAdapter.kt (optimized)// OPTIMIZATION 1: In class constructorval mListener: HeroOnClickListener?// can be replaced withval mListener: Function1<HeroItem, Unit>?

// OPTIMIZATION 2: Method definitionoverride fun getItemCount(): Int { return mValues.size}// can be replaced withoverride fun getItemCount() = mValues.size

Page 39: Android & Kotlin - The code awakens #01

Android ExampleFrom Java to Kotlin! (step3)

MainActivitywith Kotlin Android Extension

Page 40: Android & Kotlin - The code awakens #01

build.gradle (add kotlin-android-extensions)

// ..

dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"}

// ..

Page 41: Android & Kotlin - The code awakens #01

build.gradle (add kotlin-android-extensions)

// ..

dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"}

// ..

Page 42: Android & Kotlin - The code awakens #01

MainActivity.javapublic class MainActivity extends AppCompatActivity {

private Toolbar toolbar; private FloatingActionButton fab; private RecyclerView recyclerView; private HeroAdapter adapter;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

“Convert Java File to Kotlin File”

CTRL + ALT + SHIFT + K(or CMD + ALT + SHIFT + K)

Page 43: Android & Kotlin - The code awakens #01

MainActivity.kt (optimized, add import)import kotlinx.android.synthetic.activity_main.*

Page 44: Android & Kotlin - The code awakens #01

MainActivity.kt (optimized, use lazy for adapter)import kotlinx.android.synthetic.activity_main.*

class MainActivity : AppCompatActivity() { private val adapter by lazy { HeroAdapter(HeroDummyContent.ITEMS) { Snackbar.make(fab, "Tap on: " + it, Snackbar.LENGTH_SHORT).show() } }

Page 45: Android & Kotlin - The code awakens #01

MainActivity.kt (optimized, remove all unused property)import kotlinx.android.synthetic.activity_main.*

class MainActivity : AppCompatActivity() { private val adapter by lazy { HeroAdapter(HeroDummyContent.ITEMS) { Snackbar.make(fab, "Tap on: " + it, Snackbar.LENGTH_SHORT).show() } }

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) fab.setOnClickListener { Snackbar.make(it, "...", Snackbar.LENGTH_LONG).setAction("Action", null).show() } recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = adapter }}

Page 46: Android & Kotlin - The code awakens #01

Questions?Developers playground - #EX1 - Add properties to HeroItem: name, gender (String), power (Int)- Show item in list like: “$name (Power: $power)”- Choose (and keep in memory) hero “onClick”- Fight with second “onClick” (show a Snackbar)

Hint: Add in MainActivity “selectedHero” property (in Kotlin, or use field in Java)

Start with: https://github.com/jacklt/KotlinExample/tree/java-version or https://github.com/jacklt/KotlinExample/tree/kotlin-version

Solution: https://github.com/jacklt/KotlinExample/tree/ex1

Page 47: Android & Kotlin - The code awakens #01

THANKS!Omar Miatello, Member of GDG Milano (Italy)

Android Developer @ Satispay