The evolution of Java? Kotlin exploration and integration of Android project

Source: Internet
Author: User

Welcome to follow my GitHub and follow my csdn.

Kotlin is a JVM-based programming language developed by JetBrains Corporation and is now open source.
IntelliJ idea, Pycharm, Android Studio and other Ides, is from JetBrains company.

Introduced:
Statically typed programming language for the JVM, Android and the browser. 100% interoperable with Java?

A static type programming language for the JVM, Android, web, etc., fully compatible with Java.

Kotlin as the current hot discussion in the Android community, has released the 1.0.0-RC version , and has been the focus of Google, after all, Android Studio is based on the JetBrains framework. Java files can be converted directly to Kotlin files, both of which have very strong interoperability.

Talk was cheap, show you the code. Let's take a look at how to integrate Kotlin in Android projects.

The GitHub example in this article

1. Configuration Items

Create a new, empty Android project.
Project Gradle:
Add the Kotlin version number, use the latest 1.0.0-RC, and keep the version consistent.

buildscript {    ext.kotlin"1.0.0-rc-1036"}

Module Gradle:
Adding the path to the class must be added in the module because the kotlin-android-extensions plugin uses dependencies.

buildscript {    repositories {        jcenter()    }    dependencies {        "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}

Add Kotlin support for Android plugins

‘kotlin-android‘‘kotlin-android-extensions‘

Kotlin-android is used to compile the Android code, Kotlin-android-extensions is used to extend the binding relationship.

Add the Java compilation path and create the Kotlin folder in main to hold the Kotlin code (recommended).

    sourceSets {        ‘src/main/kotlin‘    }

Add a dependency on Maven.

dependencies {    "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}

Complete support for the Kotlin module Build.gradle.

Buildscript {repositories {jcenter ()} dependencies {Classpath"Org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin _version"}}apply plugin:' Com.android.application 'Apply plugin:' kotlin-android 'Apply plugin:' Kotlin-android-extensions 'Android {Compilesdkversion atBuildtoolsversion"23.0.2"Defaultconfig {ApplicationID"Me.chunyu.spike.wcl_kotlin_demo"Minsdkversion -Targetsdkversion atVersioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '}    }SourceSets {main.java.srcDirs + =' Src/main/kotlin '}}dependencies {Compile filetree (dir:' Libs ', include: [' *.jar ']) Testcompile' junit:junit:4.12 'Compile' com.android.support:appcompat-v7:23.1.1 'Compile"Org.jetbrains.kotlin:kotlin-stdlib:$kotlin _version"}
2. Installing plugins

Android Studio's Kotlin plugin, which supports the language features of Kotlin.
Select: Preferences, Plugins, Install jetbrains plugin, Kotlin.

You only need to install Kotlin, because Kotlin extensions is already included in the Kotlin.

3. Conversion code

Select the Java files that you want to convert, such as Mainactivity.java,
Using Command+shift+a, start the action, enter convert, find the command, and you can convert it, as

Or
Choose Code--Convert Java file to Kotlin file, or you can use shortcut keys.

Cut the. kt file into the Kotlin folder and you can use it.

It is recommended that the Kotlin file be stored separately from the Java file, but can be used together.

4. Adding resources

Kotlin's code is very concise, and setting properties is convenient.

Import Android. Support. V7. App. AppcompatactivityImport Android. OS. BundleImport Kotlinx. Android. Synthetic. Main. Activity_main.*/** * Kotlin The main class, add the settings property. */Class Mainactivity:appcompatactivity () {Override fun OnCreate (Savedinstancestate:bundle?) {Super. OnCreate(savedinstancestate) Setcontentview (R. Layout. Activity_main) Main_tv_message. Text="Hello Kotlin"Main_tv_message. TextSize=20.0F}}

The ID is the TextView of Main_tv_message, and the text is "Hello Kotlin".
The code style is very similar to scripting languages like Python and is easy to write.

Show

The official provides a more abundant explanation about Kotlin, we can also read the source code. Just for fun.
OK, that ' s all! Enjoy it!

The evolution of Java? Kotlin exploration and integration of Android project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.