Showing posts with label Kotlin. Show all posts
Showing posts with label Kotlin. Show all posts

Wednesday, May 17, 2017

Google I/O 2017: Empowering developers to build the best experiences across platforms


By Jason Titus, Vice President, Developer Product Group


It's great to be in our backyard again for Google I/O to connect with developers
around the world. The 7,200 attendees at Shoreline Amphitheatre, millions of
viewers on the livestream, and
thousand of developers at local I/O Extended events across 80+
countries heard about our efforts to make the lives of developers easier --
allowing them to focus on the problems they're trying to solve by minimizing the
pain points of building a product.


Earlier this morning, our CEO Sundar Pichai talked
about our various billion-user platforms. Whether it's Android or Chrome or the
mobile Web, our success would not have been possible without the developer
community. And during our Developer
Keynote
, we covered our heavy investments in tools and services for
developers who build on our platforms every day.


We have a lot to cover over the next three days. Let's take a closer look at the
major developer news at I/O so far:



Platforms that connect developers to billions of users around the
world



  • Android
    O Developer Preview 2
    � Get a look at the next release of Android O focused
    on fluid experiences that make Android even more useful, and our efforts to
    optimize battery life, startup time, graphic rendering time, and stability.
    Early adopters can opt in to the Android O Beta Program at android.com/beta and run Android O now.

  • Project
    Treble
    � Last week, we also introduced a new Android framework designed to
    help reduce the time and effort it takes device makers to upgrade a phone to a
    new version of Android, starting with Android O.

  • Android
    Go
    � We're optimizing Android to run smoothly on entry-level devices,
    starting with the O release. We're also designing Google apps to use less
    memory, storage space, and mobile data, including apps such as YouTube Go,
    Chrome, and Gboard.

  • Kotlin
    � Android is officially supporting the Kotlin programming language, in addition
    to the Java language and C++. Kotlin is a brilliantly designed, mature,
    production-ready language that we believe will make Android development faster
    and more fun.

  • Android
    Studio 3.0 Canary
    � Our new preview includes three major features to
    accelerate development flow: a new suite of app performance profiling tools to
    quickly diagnose performance issues, support for the Kotlin programming
    language, and increased Gradle build speeds for large sized app projects.

  • Mobile Web � AMP and Progressive Web
    Apps
    (PWAs) are re-defining
    modern mobile web development. AMP gets content in front of users fast and PWAs
    deliver app-focused experiences that are reliable, fast and engaging. We're
    seeing success stories
    from all around the world - travel company Wego has rolled out a
    successful AMP based PWA and Forbes has seen user
    engagement double since launching a PWA. If you're wondering how good your
    current web experience is, you can use Lighthouse - an
    automated tool for measuring web-page quality. Be sure to tune in this afternoon
    for the
    Mobile Web: State of the Union
    talk to hear more about building rich mobile
    web experiences.



Infrastructure and services to take mobile apps and the Web to the
next level



  • Firebase
    � At last year's I/O, we expanded Firebase to a full mobile development platform
    with products to help you build your app and grow your business. Over a million
    developers now use Firebase, and we're doubling down on our efforts to simplify
    more every-day developer challenges. We're giving more insights to understand
    app performance through Firebase Performance Monitoring, introducing integration
    between Hosting and Cloud Functions, adding support for Phone Number
    Authentication, and continuing to improve Analytics in a number of ways. We've
    also started open
    sourcing our SDKs
    .

  • Mobile web
    developer certifications
    � At I/O'16 we launched the Associate Android
    Developer Certification. This year, we're adding two new certifications for web
    developers: the Mobile
    Sites Certification
    and the Mobile Web
    Specialist Certification
    .



Powerful tools to acquire and engage new users; grow successful
businesses



  • Google
    Play Console
    � We announced several powerful, new features and reports in
    the Play Console to help developers improve their app's performance, manage
    releases with confidence, reach a global audience, and grow their business. The
    Play Console also has a new name, to reflect its broadened business uses, and a
    fresh look to make it easier to get things done.

  • Android
    Instant Apps
    � We opened Android Instant Apps, a new way to run Android apps
    without requiring installation, to all
    developers
    . Now anyone can build and publish an instant app. There are also
    more than 50 new experiences available for users to try out from a variety of
    brands, such as Jet, New York Times, Vimeo and Zillow.

  • Payments, Monetization & Ads � We introduced a Google Payment API that
    enables developers to give their customers the ability to pay in apps and online
    with credit or debit cards saved to their Google Account. New AdMob integration
    with Google Analytics for Firebase helps them monetize efficiently and updates
    to Universal Apps Campaigns will help them grow their user base.



New interfaces to push the limits of what's possible



  • Actions
    on Google for the Google Assistant
    � We brought Actions on Google to phones,
    introduced new features and functionality, improved our SDK and more. We also
    launched the Actions Console, a new developer console that helps developers work
    as a team, and collect data on app usage, performance and user discovery
    patterns. This new console is integrated with the Firebase and Google Cloud
    consoles.

  • VR
    and AR at Google
    � We'll have more to share on the latest Daydream platform
    features and developer tools during our "VR and AR at Google" session tomorrow
    (May 18) at 9:30 AM PT in the Amphitheatre and on the livestream.


It's important to us that developers are successful. In addition to building
products that help solve developer challenges, we're on the ground in over 130
countries, growing and expanding the developer community through programs such
as Women Techmakers & Google
Developer Groups (GDGs).
We're also investing in training programs like Google Developers
Certification
and courses through Udacity and other partners to help
developers deepen their technical capability. We're also excited to announce two
large multi-product developer events, Google Developer Days, which are planned
for Europe (September 2017 in Krakow, Poland) and India (December 2017 in
Bangalore, India). If you are interested to find out more, sign up for updates
on g.co/gdd2017.


During Google I/O, attendees and viewers have an opportunity to dive deep into
a number of these areas with 14 content tracks and 140+ breakout sessions --
covering Android to Assistant to VR -- and all livestreamed. We've also launched
over 70 codelabs to get
developers up and running with our latest APIs today.


Whether it's Android, Chrome, Play, VR/AR, the Cloud, and the Mobile Web � we're
constantly investing in the platforms that connect developers to billions of
users around the world. Thank you to the continued support and feedback from the
developer community.

Android Announces Support for Kotlin



By Mike Cleron, Director,
Android Platform



Today the Android team is excited to announce that we are officially adding
support for the Kotlin programming
language. Kotlin is a brilliantly designed, mature language that we believe will
make Android development faster and more fun. It has already been adopted by
several major developers � Expedia, Flipboard, Pinterest, Square, and others �
for their production apps. Kotlin also plays well with the Java programming
language; the effortless interoperation between the two languages has been a
large part of Kotlin's appeal.




The Kotlin plug-in is now bundled with Android Studio 3.0 and is available for
immediate download.
Kotlin was developed by JetBrains, the
same people who created IntelliJ,
so it is not surprising that the IDE support for Kotlin is outstanding.


In addition to the IDE support, we're announcing a collaboration with JetBrains
to move Kotlin into a non-profit foundation. (Kotlin is already open sourced
under Apache2.)




Say "Hello" to Kotlin


Kotlin will be very familiar to anyone who has used the Java programming
language.



package helloWorld

fun main(args: Array) {
println("Hello World!")
}

At first glance, you will see comforting elements like curly braces, classes,
packages, functions and methods. But as you go deeper, you will discover that
although Kotlin is based on familiar concepts, it is a uniquely modern, elegant
and pragmatic riff on those models. In particular, Kotlin is highly expressive
with minimal syntactic friction between your thoughts and what you have to type
in order to express those thoughts. If when writing code you have asked yourself
questions that began "why do I have to �?" you will be pleased to learn that in
Kotlin the answer to many of those questions is "you don't!"



For example, perhaps you have asked why you need to type in a bunch of
boilerplate getters and setters as well as overriding equals(),
hashCode() and toString() when implementing a simple
class. Here is a typical example from the Java programming language (in a
microscopic font for brevity).



public class Customer {
private String name;
private String email;
private String company;

public Customer(String name) {
this(name, "", "");
}

public Customer(String name, String email) {
this(name, email, "");

}

public Customer(String name, String email, String company) {
this.name = name;
this.email = email;
this.company = company;
}

public String getName() {
return name;
}

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

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Customer customer = (Customer) o;

if (name != null ? !name.equals(customer.name) : customer.name != null) return false;
if (email != null ? !email.equals(customer.email) : customer.email != null) return false;
return company != null ? company.equals(customer.company) : customer.company == null;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (company != null ? company.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", company='" + company + '\'' +
'}';
}
}

In Kotlin, you don't have to type any of that. This single line is equivalent to
the entire class above.



data class Customer(var name: String, var email: String = "",
var company: String = "")



History and Reference


Kotlin has been around for quite a while; it was announced back in 2011 and the
first preview was released in 2012. Kotlin 1.0 was released in 2016, at which
point JetBrains committed to maintaining backwards compatibility for stable
features from 1.0 forward.



You can find excellent training material and references at https://kotlinlang.org/. The Android team has
found the Kotlin
Koans
tutorial to be especially helpful as a quick way to get started
writing some Kotlin snippets. These tutorials range from the simple to the
sublime as the material progresses from the basics to more sophisticated Kotlin
idioms.




Why Kotlin?


Why did the Android team decide to support Kotlin? Most importantly, it was
because we think Kotlin is a great language that will make writing Android apps
easier and more enjoyable.



Kotlin is also a great match for the existing Android ecosystem. It is 100%
compatible with the Java programming language. You can add as little or as much
Kotlin into your existing codebase as you want and mix the two languages freely
within the same project. Calling out to Kotlin code from code written in the
Java programming language Just Works�. Going the other direction usually works
without any developer effort too via some automatically applied translation
conventions (for example, things like property getters and setters are created
for you). With the help of a few Kotlin annotations, you can also customize how
the translation is performed.



Finally, many, many developers have told us they love the Kotlin language. (Many
of our own developers on the Android team have also been saying similar things.)
There is already an enthusiastic
community
of Kotlin developers
for Android, and the Android team has been routinely peppered with questions
about Kotlin at public events. The Android community has spoken, and we
listened.




A Quick Tour


To help you get a sense of where all of the excitement around Kotlin is coming
from, here is a quick, very-much-not-comprehensive tour of some of the
particularly appealing aspects of Kotlin:



Nullable


The Kotlin compiler enforces that variables that can hold null values are
explicitly declared � thus no more NullPointerExceptions at runtime!



var neverNull: String = "something"
var mightBeNull: String? = null // "?" indicates this can be null

if (neverNull.length > 0) { // This is OK

}

if (mightBeNull.length > 0) { // Compiler catches this error for you

}

Named parameters and default arguments


We've all seen methods that have too many parameters to keep track of. For
example:



fun orderPizza(size: Size, pepperoni: Boolean, mushrooms: Boolean,
ham: Boolean, pineapple: Boolean, pickles: Boolean,
sausage: Boolean, peppers: Boolean, onion: Boolean)
{
...
}

// Wait� did I just order pickles on my pizza?
// Why do we even have that option?
orderPizza(Size.LARGE, true, false, false, false, true,
false, true, false)

Compare that to a similar scenario using named parameters and default arguments:



fun orderPizza(size: Size,
pepperoni: Boolean = false,
mushrooms: Boolean = false,
ham: Boolean = false,
pineapple: Boolean = false,
pickles: Boolean = false,
sausage: Boolean = false,
peppers: Boolean = false,
onion: Boolean = false)
{
...
}

orderPizza(Size.LARGE, ham = true, mushrooms = true)

In addition to helping to avoid tragic pizza outcomes, this is much easier to
read. It also reduces the number of variants of overloaded functions you need to
write.



When statement


Kotlin has a variation of a switch statement that allows matching on arbitrary
expressions.



// Please don't put this in your app!
when {
password.equals("password") -> println("Insecure password!")
password.length < 4 -> println("Too short!")
else -> {
println("Secure password!")
}
}

Smart Casts


Why should you have to cast something to a class right after you just tested
that it is an instance of that class? In Kotlin, you don't have to do that
anymore.



if (obj is String) {
// Compiler casts obj to a String for you.
// (Would work with && instead of nested ifs too.)
if (obj.length > 0) {

}
}

This generalizes to the when statement as well:



// Assume reasonable implementations of Cat and Dog
when (obj) {
is Cat -> obj.meow(...)
is Dog -> obj.woof(...)
else -> {

}
}

Extension functions


Kotlin lets you essentially retcon
new methods onto an existing type. If you, like many people, wish that the
String class had a toPigLatin method, you can now add it yourself
without having to create a new helper class to wrap String or going through the
trouble of serving on a language committee:



// The "String." prefix indicates that this method should
// extend the existing String class
fun String.toPigLatin() : String {
...
}

val plainOldString : String = "some text"

// Can now call toPigLatin as if were a method on String
println(plainOldString.toPigLatin())

// Or:
println("some text".toPigLatin())

Destructuring Declarations


We have already seen how easy it is to define a simple data class:



data class Order(val itemCode: String, val quantity: Int,
val price: Float)

A function that uses one of these classes as the return type is very close to
supporting multiple return values:



fun getOrder(...): Order {
...
return Order(itemCode, quantity, price);
}

To get all the way there, you can use the destructuring declaration syntax. The
following statement takes the Order object, extracts its three
properties, and then assigns them to the three variables what,
howMany and howMuch � all courtesy of the Kotlin
compiler, which also infers the correct types for you.



val (what, howMany, howMuch) = getOrder(...)

Lambdas


Kotin has an extremely concise syntax for lambdas that makes is easy to express
powerful functional programming paradigms. Here's a simple example that uses a
lambda to test that everything in a collection is a String:



fun allStrings(collection: Collection)=
collection.all { it is String }

That lambda syntax is building block of one of Kotlin's coolest features: the
ability to create builders that use JSON-like syntax that also happens to be
syntactically valid Kotlin. This example is adapted from an extended discussion
here,
but you can get the flavor of what it possible with this snippet:



fun generatePage(withEmphasis : Boolean) {
val result =
html {
head {
title { +"Kotlin Builders" }
}
body {
h1 { +"Kotlin Builders" }
p {
+"This is "
if (withEmphasis) b { +"really " }
+"interesting"
a(href = "https://goo.gl/rHwJio") { +"More here" }
}
}
}
println(result)
}

There are a couple of interesting things going on here. First, this shows how
expressive Kotlin's functional syntax can be: in this example,
"html", "head", "body, etc. are all just
functions written in Kotlin and the stuff in curly braces that follows are
functional parameters. (This snippet uses functions with names that match HTML
tags to build a representation of a web page, but of course you can use this
pattern to build any complex data structure with whatever names you want.) The
second interesting thing is the "withEmphasis" conditional. This
may look like we are mixing code (if (withEmphasis) �) with data
(all the HTML-esque tags), but the "data" here is actually just more code. Since
it is all really just code, this lets you build complex data structures using a
declarative syntax while also having inline access to the full capabilities of
the Kotlin language.




Getting Started


If you want to get started with Kotlin, you can start playing with code online
immediately here. Just hit the green
triangle to compile and run.



To try Kotlin in your app, follow these steps:



  1. Download
    Android Studio 3.0


  2. Open one of your existing ".java" files

  3. Invoke "Code > Convert Java File to Kotlin File"


The IDE will then walk you through adding Kotlin dependencies into your project,
and then convert the code to functionally equivalent Kotlin code. (The IDE will
also offer to touch up all of the call sites to the converted class when
suitable to be more idiomatic Kotlin such as when static methods are moved to
companion objects.)



You can also find a lot more information on how to start using
Kotlin
on developer.android.com.