Kotlin and Android

 

Foreword

Android now officially supports the object-oriented programming language Kotlin.

A plugin for Kotlin is embedded in Android Studio (starting from version 3.0).

See the article from the official Android documentation: https://developer.android.com/kotlin/index.html

 

Tagging principles with Kotlin

Kotlin is entirely supported by Android Studio 3.0, so it 's relatively simple to create new projects containing Kotlin files, add Kotlin files to an existing project, or convert Java code into Kotlin (see the “Get Started with Kotlin” section of the official documentation for more details).

Kotlin enables full interoperability with Java language. This is why the operating principles and the integration methods documented in your “Getting started” section remain unchanged.

Android Studio 3.0 offers different conversion tools. It 's therefore possible to convert a Java file (with a .java extension) into a Kotlin file (with a .kt extension) by opening the file, then by selecting the menu Code > Convert Java File to Kotlin File. You may also create a new Kotlin file (menu File > New > Kotlin File/Class) and then insert Java code. In this case, a dialog box will open, asking if you wish to automatically convert the code. The examples of Android tagging defined in Java can therefore be inserted as is, leading to automatic conversion.

 

Tagging example

Tagging a Gesture with Debugger in Java

package com.atinternet;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.atinternet.tracker.ATInternet;
import com.atinternet.tracker.Debugger;
import com.atinternet.tracker.Tracker;

public class MainActivity extends AppCompatActivity {

    Tracker tracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tracker = ATInternet.getInstance().getDefaultTracker();

        findViewById(R.id.sendHit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tracker.Gestures().add("TouchTest").sendTouch();
            }
        });

        Debugger.create(this, tracker);
    }
}

Equivalent tagging in Kotlin

package com.atinternet

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.atinternet.tracker.ATInternet
import com.atinternet.tracker.Debugger
import com.atinternet.tracker.Tracker

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tracker: Tracker = ATInternet.getInstance().getDefaultTracker()

        val button = findViewById<Button>(R.id.sendHit)

        button.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                tracker.Gestures().add("TouchTest").sendTouch()
            }
        })

        Debugger.create(this, tracker)
    }
}
Last update: 03/01/2018