The Kotlin plug-in is now bundled with Android Studio 3.0. Also available for older version see my last post how to setup Kotlin Plugin in Android Studio.
Here simple Example app of the Say Hello World in Kotlin language. In App show one TextView and One Button click to show Toast to Say Hello World. Follow below steps to run first Hello World App.
Create New Application
Open Android Studio File>New>New Project
Select Include Kotlin support check box and click Next button.(This option available in Android Studio 3.0 Version.)
Select Phone and Tablet, Minimum SDK version and Click Next button.
Select Empty Activity and click Next button.
If you want to change Activity name then change here otherwise go with default MainActivity name and click Finish Button
After you need to check Kotlin plug-in in both root project and app-model build.gradle file.
root project build.gradle
buildscript { ext.kotlin_version = '1.1.2-4' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { maven { url 'https://maven.google.com' } jcenter() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir }
App-model build.gradle file add two apply plugin 'kotlin-android', 'kotlin-android-extensions' and add one dependency compile "org.jetbrains.anko:anko-commons:0.10.1".
Kotlin Android Extensions is a compiler extension that allows you to get rid of findViewById() calls in your code and to replace them with synthetic compiler-generated properties.
org.jetbrains.anko:anko-commons:0.10.1 - Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java. Anko Commons is a lightweight library full of helpers for intents, dialogs, logging and so on;
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.limbani.helloworld" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:25.3.1' testImplementation 'junit:junit:4.12' implementation 'com.android.support.constraint:constraint-layout:1.0.2' compile "org.jetbrains.anko:anko-commons:0.10.1" }
MainActivity.kt file
package com.limbani.helloworld import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.util.Log import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn_hello.setOnClickListener { toast("Say Hello World") // For long toast // longToast("Say Hello World") printButtonClickLog() } } private fun printButtonClickLog() { Log.i("MainActivity", "Hello Button Clicked") } }
activity_main.xml layout file
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.limbani.helloworld.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" android:text="Hello World!" android:textSize="20sp" /> <Button android:id="@+id/btn_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Say Hello" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.limbani.helloworld"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Screenshot of the app.
Resource
https://kotlinlang.org
Android Developer Blog
Anko Library
Thanks for reading this post.
Kotlin is the official language of the Android. Spotify application not responding
ReplyDelete