Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Java to Kotlin

Gerard
March 20, 2018

From Java to Kotlin

Did you hear about Kotlin? Maybe you find the language really cool but you are afraid about the migration? You don't know risks and how you can migrate your professionnal and/or personnal projects? This talk should give you some answers and help you in your reflections!

Gerard

March 20, 2018
Tweet

More Decks by Gerard

Other Decks in Programming

Transcript

  1. FROM JAVA TO
    Gerard Paligot

    View Slide

  2. WHAT’S KOTLIN?

    View Slide

  3. WHAT’S KOTLIN?
    - Started in mid 2010
    - Avoid huge Java codebase
    - Want a modern, expressive language
    - Easy to introduce in the existing environment
    Presentation

    View Slide

  4. WHAT’S KOTLIN?
    - Full Java interoperability
    - Compile as fast as Java
    - More concise than Java
    - Prevent more kinds of errors than Java
    - Was simpler than Scala
    Design Goals

    View Slide

  5. WHY USE KOTLIN?
    - In a future, will replace Java as first language
    - High adoption of the Android community

    View Slide

  6. WHY USE KOTLIN?
    High adoption of the Android community

    View Slide

  7. WHY USE KOTLIN?
    - In a future, will replace Java as first language
    - High adoption of the Android community
    - Easy learning for iOS and Android developers
    - Null safety
    - Compilation speed

    View Slide

  8. WHY USE KOTLIN?
    Compilation speed

    View Slide

  9. WHY USE KOTLIN?
    - In a future, will replace Java as first language
    - High adoption of the Android community
    - Easy learning for iOS and Android developers
    - Null safety
    - Compilation speed
    - Less code is better!

    View Slide

  10. HOW TO START?
    - Experimenting with a legacy Java/Android project
    - Run automatic conversion to Kotlin and learn what changed
    - Practice more and inform what’s new at each new Kotlin version

    View Slide

  11. HOW TO START
    // Top level build.gradle file
    buildscript {
    ext.kotlin_version = '1.2.30'
    // ...
    dependencies {
    // ...
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
    }
    // build.gradle file of your app module
    // ...
    apply plugin: 'kotlin-android'
    // ...
    dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    // ...
    }

    View Slide

  12. HOW TO START
    // Top level build.gradle file
    buildscript {
    ext.kotlin_version = '1.2.30'
    // ...
    dependencies {
    // ...
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
    }
    // build.gradle file of your app module
    // ...
    apply plugin: 'kotlin-android'
    // ...
    dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    // ...
    }

    View Slide

  13. HOW TO START
    // Top level build.gradle file
    buildscript {
    ext.kotlin_version = '1.2.30'
    // ...
    dependencies {
    // ...
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
    }
    // build.gradle file of your app module
    // ...
    apply plugin: 'kotlin-android'
    // ...
    dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    // ...
    }

    View Slide

  14. JAVA TO KOTLIN… JAVA STYLE

    View Slide

  15. JAVA TO KOTLIN… JAVA STYLE
    public class MyFragment extends Fragment {
    private static final String KEY_TITLE = "KEY_TITLE";
    private static final String KEY_MESSAGE = "KEY_MESSAGE";
    public static MyFragment newInstance(String newTitle, String newMessage) {
    Bundle args = new Bundle();
    args.putString(KEY_TITLE, newTitle);
    args.putString(KEY_MESSAGE, newMessage);
    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
    }
    }

    View Slide

  16. JAVA TO KOTLIN… JAVA STYLE
    public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.my_fragment, container);
    }
    }

    View Slide

  17. JAVA TO KOTLIN… JAVA STYLE
    public class MyFragment extends Fragment {
    private TextView mTitleTextView;
    private TextView mMessageTextView;
    private Button mSubmitButton;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mTitleTextView = view.findViewById(R.id.titleTextView);
    mTitleTextView.setText(getArguments().getString(KEY_TITLE));
    mMessageTextView = view.findViewById(R.id.messageTextView);
    mMessageTextView.setText(getArguments().getString(KEY_MESSAGE));
    mSubmitButton = view.findViewById(R.id. submitButton);
    mSubmitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Log.d("MyFragment", "Somebody click on this button!");
    Log.d("MyFragment", "Title: " + mTitleTextView.toString()
    + ", Message: " + mMessageTextView.toString());
    }
    });
    }
    }

    View Slide

  18. JAVA TO KOTLIN… JAVA STYLE
    public class MyFragment extends Fragment {
    private static final String KEY_TITLE = "KEY_TITLE";
    private static final String KEY_MESSAGE = "KEY_MESSAGE";
    public static MyFragment newInstance(String newTitle, String newMessage) {
    Bundle args = new Bundle();
    args.putString(KEY_TITLE, newTitle);
    args.putString(KEY_MESSAGE, newMessage);
    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
    }
    }

    View Slide

  19. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    companion object {
    private val KEY_TITLE = "KEY_TITLE"
    private val KEY_MESSAGE = "KEY_MESSAGE"
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  20. JAVA TO KOTLIN… JAVA STYLE
    + private const val KEY_TITLE = "KEY_TITLE"
    + private const val KEY_MESSAGE = "KEY_MESSAGE"
    class MyFragment : Fragment() {
    companion object {
    - private val KEY_TITLE = "KEY_TITLE"
    - private val KEY_MESSAGE = "KEY_MESSAGE"
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  21. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  22. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    + private val Bundle.title: String
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  23. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    + private val Bundle.title: String
    + get() = getString(KEY_TITLE)
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  24. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    + private var Bundle.title: String
    + get() = getString(KEY_TITLE)
    + set(value) {
    + putString(KEY_TITLE, value)
    + }
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  25. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    private var Bundle.title: String
    get() = getString(KEY_TITLE)
    set(value) {
    putString(KEY_TITLE, value)
    }
    + private var Bundle.message: String
    + get() = getString(KEY_MESSAGE)
    + set(value) {
    + putString(KEY_MESSAGE, value)
    + }
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.putString(KEY_TITLE, newTitle)
    args.putString(KEY_MESSAGE, newMessage)
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  26. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    private var Bundle.title: String
    get() = getString(KEY_TITLE)
    set(value) {
    putString(KEY_TITLE, value)
    }
    private var Bundle.message: String
    get() = getString(KEY_MESSAGE)
    set(value) {
    putString(KEY_MESSAGE, value)
    }
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    - args.putString(KEY_TITLE, newTitle)
    - args.putString(KEY_MESSAGE, newMessage)
    + args.title = newTitle
    + args.message = newMessage
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }

    View Slide

  27. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    private var Bundle.title: String
    get() = getString(KEY_TITLE)
    set(value) {
    putString(KEY_TITLE, value)
    }
    private var Bundle.message: String
    get() = getString(KEY_MESSAGE)
    set(value) {
    putString(KEY_MESSAGE, value)
    }
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.title = newTitle
    args.message = newMessage
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  28. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle()
    args.title = newTitle
    args.message = newMessage
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  29. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    - val args = Bundle()
    - args.title = newTitle
    - args.message = newMessage
    + val args = Bundle().apply {
    + title = newTitle
    + message = newMessage
    + }
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  30. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    val args = Bundle().apply {
    title = newTitle
    message = newMessage
    }
    val fragment = MyFragment()
    fragment.arguments = args
    return fragment
    }
    }
    }

    View Slide

  31. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    - val args = Bundle().apply {
    - title = newTitle
    - message = newMessage
    - }
    - val fragment = MyFragment()
    - fragment.arguments = args
    - return fragment
    + return MyFragment().apply {
    + arguments = Bundle().apply {
    + title = newTitle
    + message = newMessage
    + }
    + }
    }
    }
    }

    View Slide

  32. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    return MyFragment().apply {
    arguments = Bundle().apply {
    title = newTitle
    message = newMessage
    }
    }
    }
    }
    }

    View Slide

  33. JAVA TO KOTLIN… JAVA STYLE
    private const val KEY_TITLE = "KEY_TITLE"
    private const val KEY_MESSAGE = "KEY_MESSAGE"
    private var Bundle.title: String
    get() = getString(KEY_TITLE)
    set(value) {
    putString(KEY_TITLE, value)
    }
    private var Bundle.message: String
    get() = getString(KEY_MESSAGE)
    set(value) {
    putString(KEY_MESSAGE, value)
    }
    class MyFragment : Fragment() {
    companion object {
    fun newInstance(newTitle: String, newMessage: String): MyFragment {
    return MyFragment().apply {
    arguments = Bundle().apply {
    title = newTitle
    message = newMessage
    }
    }
    }
    }
    }

    View Slide

  34. JAVA TO KOTLIN… JAVA STYLE
    public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.my_fragment, container);
    }
    }

    View Slide

  35. JAVA TO KOTLIN… JAVA STYLE
    override fun onCreateView(inflater: LayoutInflater?,
    container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    return inflater?.inflate(R.layout.my_fragment, container)
    ?: LayoutInflater.from(context).inflate(R.layout.my_fragment, container)
    }

    View Slide

  36. JAVA TO KOTLIN… JAVA STYLE

    View Slide

  37. JAVA TO KOTLIN… JAVA STYLE
    public class MyFragment extends Fragment {
    private TextView mTitleTextView;
    private TextView mMessageTextView;
    private Button mSubmitButton;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mTitleTextView = view.findViewById(R.id.titleTextView);
    mTitleTextView.setText(getArguments().getString(KEY_TITLE));
    mMessageTextView = view.findViewById(R.id.messageTextView);
    mMessageTextView.setText(getArguments().getString(KEY_MESSAGE));
    mSubmitButton = view.findViewById(R.id. submitButton);
    mSubmitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Log.d("MyFragment", "Somebody click on this button!");
    Log.d("MyFragment", "Title: " + mTitleTextView.toString()
    + ", Message: " + mMessageTextView.toString());
    }
    });
    }
    }

    View Slide

  38. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    private var mTitleTextView: TextView? = null
    private var mMessageTextView: TextView? = null
    private var mSubmitButton: Button? = null
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView!!.text = arguments.getString(KEY_TITLE)
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView!!.text = arguments.getString(KEY_MESSAGE)
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton!!.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView!!.toString()
    + ", Message: " + mMessageTextView!!.toString())
    }
    }
    }

    View Slide

  39. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    private var mTitleTextView: TextView? = null
    private var mMessageTextView: TextView? = null
    private var mSubmitButton: Button? = null
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    - mTitleTextView!!.text = arguments.getString(KEY_TITLE)
    + mTitleTextView!!.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    - mMessageTextView!!.text = arguments.getString(KEY_MESSAGE)
    + mMessageTextView!!.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton!!.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView!!.toString()
    + ", Message: " + mMessageTextView!!.toString())
    }
    }
    }

    View Slide

  40. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    private var mTitleTextView: TextView? = null
    private var mMessageTextView: TextView? = null
    private var mSubmitButton: Button? = null
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView!!.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView!!.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton!!.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView!!.toString()
    + ", Message: " + mMessageTextView!!.toString())
    }
    }
    }

    View Slide

  41. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    - private var mTitleTextView: TextView? = null
    - private var mMessageTextView: TextView? = null
    - private var mSubmitButton: Button? = null
    + private lateinit var mTitleTextView: TextView
    + private lateinit var mMessageTextView: TextView
    + private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView!!.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView!!.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton!!.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView!!.toString()
    + ", Message: " + mMessageTextView!!.toString())
    }
    }
    }

    View Slide

  42. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    private lateinit var mTitleTextView: TextView
    private lateinit var mMessageTextView: TextView
    private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView!!.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView!!.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton!!.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView!!.toString()
    + ", Message: " + mMessageTextView!!.toString())
    }
    }
    }

    View Slide

  43. JAVA TO KOTLIN… JAVA STYLE
    class MyFragment : Fragment() {
    private lateinit var mTitleTextView: TextView
    private lateinit var mMessageTextView: TextView
    private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    - mTitleTextView!!.text = arguments.title
    + mTitleTextView.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    - mMessageTextView!!.text = arguments.message
    + mMessageTextView.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    - mSubmitButton!!.setOnClickListener {
    + mSubmitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView!!.toString()
    + ", Message: " + mMessageTextView!!.toString())
    }
    }
    }

    View Slide

  44. class MyFragment : Fragment() {
    private lateinit var mTitleTextView: TextView
    private lateinit var mMessageTextView: TextView
    private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: " + mTitleTextView.toString()
    + ", Message: " + mMessageTextView.toString())
    }
    }
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  45. class MyFragment : Fragment() {
    private lateinit var mTitleTextView: TextView
    private lateinit var mMessageTextView: TextView
    private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    - Log.d("MyFragment", "Title: " + mTitleTextView.toString()
    - + ", Message: " + mMessageTextView.toString())
    + Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView")
    }
    }
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  46. class MyFragment : Fragment() {
    private lateinit var mTitleTextView: TextView
    private lateinit var mMessageTextView: TextView
    private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView")
    }
    }
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  47. // Top level build.gradle file
    buildscript {
    ext.kotlin_version = '1.2.30'
    // ...
    dependencies {
    // ...
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
    }
    // build.gradle file of your app module
    // ...
    apply plugin: ‘kotlin-android'
    + apply plugin: 'kotlin-android-extensions'
    // ...
    dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    // ...
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  48. class MyFragment : Fragment() {
    private lateinit var mTitleTextView: TextView
    private lateinit var mMessageTextView: TextView
    private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView.text = arguments.title
    mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView.text = arguments.message
    mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView")
    }
    }
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  49. + import kotlinx.android.synthetic.main.my_fragment.*
    class MyFragment : Fragment() {
    - private lateinit var mTitleTextView: TextView
    - private lateinit var mMessageTextView: TextView
    - private lateinit var mSubmitButton: Button
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    - mTitleTextView = view!!.findViewById(R.id.titleTextView)
    mTitleTextView.text = arguments.title
    - mMessageTextView = view.findViewById(R.id.messageTextView)
    mMessageTextView.text = arguments.message
    - mSubmitButton = view.findViewById(R.id.submitButton)
    mSubmitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView")
    }
    }
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  50. import kotlinx.android.synthetic.main.my_fragment.*
    class MyFragment : Fragment() {
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    titleTextView.text = arguments.title
    messageTextView.text = arguments.message
    submitButton.setOnClickListener {
    Log.d("MyFragment", "Somebody click on this button!")
    Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView")
    }
    }
    }
    JAVA TO KOTLIN… JAVA STYLE

    View Slide

  51. View Slide

  52. KOTLIN TO BINARY TO JAVA

    View Slide

  53. KOTLIN TO BINARY TO JAVA

    View Slide

  54. class MyClass
    abstract class MyAbsClass
    open class MyOpenClass
    data class MyDataClass(val field: String)
    KOTLIN TO BINARY TO JAVA

    View Slide

  55. KOTLIN TO BINARY TO JAVA

    View Slide

  56. public final class MyClass {
    }
    public abstract class MyAbsClass {
    }
    public class MyOpenClass {
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  57. public final class MyDataClass {
    @NotNull
    private final String field;
    @NotNull
    public final String getField() { return this.field; }
    public MyDataClass(@NotNull String field) { // ... }
    @NotNull
    public final MyDataClass copy(@NotNull String field) { // ... }
    public String toString() { // ... }
    public int hashCode() { // ... }
    public boolean equals(Object var1) { // ... }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  58. open class MethodExamples {
    fun m() {}
    open fun mOpen() {}
    }
    interface Method2Examples {
    fun mOpen() {}
    }
    class Usage : MethodExamples(), Method2Examples{
    override fun mOpen() {
    super.mOpen()
    super.mOpen()
    }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  59. public class MethodExamples {
    public final void m() {
    }
    public void mOpen() {
    }
    }
    public interface Method2Examples {
    void mOpen();
    public static final class DefaultImpls {
    public static void mOpen(Method2Examples $this) {
    }
    }
    }
    public final class Usage extends MethodExamples implements Method2Examples {
    public void mOpen() {
    Method2Examples.DefaultImpls.mOpen(this);
    super.mOpen();
    }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  60. class MyClass(val listener: (test: String) -> Unit) {
    fun m() {
    listener("Result")
    }
    }
    fun main(args: Array) {
    MyClass { it ->
    // Do something really cool!
    }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  61. public final class MyClass {
    @NotNull
    private final Function1 listener;
    public final void m() {
    this.listener.invoke("Result");
    }
    @NotNull
    public final Function1 getListener() {
    return this.listener;
    }
    public MyClass(@NotNull Function1 listener) {
    Intrinsics.checkParameterIsNotNull(listener, "listener");
    super();
    this.listener = listener;
    }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  62. /** A function that takes 0 arguments. */
    public interface Function0 : Function {
    /** Invokes the function. */
    public operator fun invoke(): R
    }
    /** A function that takes 1 argument. */
    public interface Function1 : Function {
    /** Invokes the function with the specified argument. */
    public operator fun invoke(p1: P1): R
    }
    /** A function that takes 2 arguments. */
    public interface Function2 : Function {
    /** Invokes the function with the specified arguments. */
    public operator fun invoke(p1: P1, p2: P2): R
    }
    // ...
    /** A function that takes 22 arguments. */
    public interface Function22P11, in P12, in P13, in P14, in P15, in P16, in P17, in P18, in P19, in P20, in P21, in P22, out R> :
    Function {
    /** Invokes the function with the specified arguments. */
    public operator fun invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9
    p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p
    P20, p21: P21, p22: P22): R
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  63. View Slide

  64. fun MutableList.swap(index1: Int, index2: Int) {
    val tmp = this[index1]
    this[index1] = this[index2]
    this[index2] = tmp
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  65. public final class ExtensionsKt {
    public static final void swap(@NotNull List $receiver, int index1, int index2) {
    Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
    int tmp = ((Number)$receiver.get(index1)).intValue();
    $receiver.set(index1, $receiver.get(index2));
    $receiver.set(index2, Integer.valueOf(tmp));
    }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  66. object MyObject {
    var test: String = ""
    }
    fun main(args: Array) {
    MyObject.test = "new value"
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  67. public final class MyObject {
    @NotNull
    private static String test;
    public static final MyObject INSTANCE;
    @NotNull
    public final String getTest() {
    return test;
    }
    public final void setTest(@NotNull String var1) {
    Intrinsics.checkParameterIsNotNull(var1, "");
    test = var1;
    }
    static {
    MyObject var0 = new MyObject();
    INSTANCE = var0;
    test = "";
    }
    }
    KOTLIN TO BINARY TO JAVA

    View Slide

  68. TRAPS

    View Slide

  69. fun m(typeClass: AbsType) {
    }
    open class AbsType
    class SubType1 : AbsType()
    class SubType2 : AbsType()
    TRAPS

    View Slide

  70. fun m(typeClass: AbsType) {
    + when (typeClass) {
    + is SubType1 -> println("Typed by subtype 1")
    + is SubType2 -> println("Typed by subtype 2")
    + else -> println("No type identified")
    + }
    }
    open class AbsType
    class SubType1 : AbsType()
    class SubType2 : AbsType()
    TRAPS

    View Slide

  71. fun m(typeClass: AbsType) {
    when (typeClass) {
    is SubType1 -> println("Typed by subtype 1")
    - is SubType2 -> println("Typed by subtype 2")
    + is SubType2 -> {
    + println("Typed by subtype 2")
    + typeClass.children.forEach {
    + when (it) {
    + is SubType1 -> println("Child typed by subtype 1")
    + is SubType2 -> println("Child typed by subtype 2")
    + else -> println("No type identified")
    + }
    + }
    + }
    else -> println("No type identified")
    }
    }
    open class AbsType
    class SubType1 : AbsType()
    - class SubType2 : AbsType()
    + class SubType2(val children: List) : AbsType()
    TRAPS

    View Slide

  72. fun m(typeClass: AbsType) {
    when (typeClass) {
    is SubType1 -> println("Typed by subtype 1")
    is SubType2 -> {
    println("Typed by subtype 2")
    typeClass.children.forEach {
    when (it) {
    is SubType1 -> println("Child typed by subtype 1")
    is SubType2 -> println("Child typed by subtype 2")
    else -> println("No type identified")
    }
    }
    }
    else -> println("No type identified")
    }
    }
    open class AbsType
    class SubType1 : AbsType()
    class SubType2(val children: List) : AbsType()
    TRAPS

    View Slide

  73. variable.toList()
    variable.toInt()
    variable.joinToString { ... }
    variable.copy()
    variable.plus(...)
    variable.minus(...)
    ...
    variable.doEverything()
    TRAPS

    View Slide

  74. WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  75. someObject?.takeIf { status }.apply { doThis() }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  76. MyNullSafetyClass var7 = status ? someObject : null;
    doThis();
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  77. - someObject?.takeIf { status }.apply { doThis() }
    + someObject?.takeIf { status }?.apply { doThis() }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  78. MyNullSafetyClass var7 = status ? someObject : null;
    if((status ? someObject : null) != null) {
    doThis();
    }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  79. null.apply { ... }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  80. class MyClass
    class MyClass2(val test: String, val test2: String)
    class MyClass3(val clazz: MyClass2?)
    fun main(args: Array) {
    val m: MyClass? = MyClass()
    // No compilation error.
    if (m != null) {
    m.toString()
    }
    // No compilation error.
    "${m!!.toString()}${m.toString()}"
    // No compilation error.
    val myClass3 = MyClass3(MyClass2("", ""))
    val test = myClass3.clazz!!.test
    val test2 = myClass3.clazz.test2
    }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  81. class MyClass
    class MyClass2(val test: String, val test2: String)
    class MyClass3(val clazz: MyClass2?)
    class MyClass4 {
    private var myClass2: MyClass2? = null
    fun m() {
    val test = myClass2!!.test
    // error, wtf this shit?!
    val test2 = myClass2.test2
    }
    }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  82. class MyClass
    class MyClass2(val test: String, val test2: String)
    class MyClass3(val clazz: MyClass2?)
    class MyClass4 {
    private var myClass2: MyClass2? = null
    fun m() {
    if (myClass2 != null) {
    val test = myClass2!!.test
    val test2 = myClass2!!.test2
    }
    }
    }
    WTF (˽°□°҂˽Ɨ ˍʓˍ

    View Slide

  83. SUMMARY
    - Kotlin is mature and ready for production
    - Designed to be compatible and better than Java
    - Easy to learn and high adoption by the Android community
    - Be curious

    View Slide

  84. FROM JAVA TO
    Gerard Paligot

    View Slide