Forty-eight practical Android skills: Java8 and kotlin on Android
Java and Android are also in the dark.
Things in the technical circle are often led by commercial interests.
This is the only thing in the world.
Java 8
Currently, Android Application Development has been applied to Java 7, but its support for Java 8 is still far away.
Java 8 provides many good features for new technology practitioners.
Lambda expressions, Stream API, Date Time API, and concurrency enhancement are most notable.
Android Application Development fully supports Java 8. Currently, there is no good solution, but there is a corresponding solution for Java 8's private support:
Backward compatibility with lambda expressions: https://github.com/orfjackal/retrolambda
Stream API: http://sourceforge.net/projects/streamsupport/
New time API: http://www.threeten.org/
But I personally think that they will be used in production only when they are unavailable.
These are good materials for technological learning.
The conclusion of Java 8 for Android is that it is not supported currently, but there are private support projects. It is not easy to use the new features of Java 8 in Android application development.
Java, as a high-level language, is always more efficient than Native languages. However, it is in its twenties and is not applicable in some cases.
Around JVM, many new languages have emerged in the world.
For example, in Groovy, Android Studio uses the Gradle build tool based on Groovy.
For example, Clojure is a pure functional programming language.
For example, Scala is used for parallel operations.
Also, another protagonist, kotlin.
Kotlin
On its official website, there is a saying like this:
Statically typed programming languagefor the JVM, Android and the browser100% interoperable with Java
There are three things to express:
1. It is a static JVM-based language.
2. It can be used for Android and web development.
3. Hundreds of millions of interactions with Java
Why choose kotlin?
The official website provides four answers:
1. Concise
It greatly simplifies the tedious use of language. For example, lambdas's support can reduce the amount of code, so it will become easier to read.
2. Security
At the language level, it avoids the annoying NULL pointer exception and so on.
3. Multi-Purpose
Build server applications, Android apps, and web applications
4. Interaction with Java
In practice, you will find how convenient this is.
Kotlin is a relatively new language. The current version is 1.0 beta. It captures some of the pain points that cannot be changed after so many years of development of the Java language, and leaves the language away from its dilemma when it was born.
The following is the first Kotlin Demo built on android studio by Linc according to kotlin for Android Developers.
1. Install the kotlin plug-in
Go to Android Studio's Settings Plugins, and then install three plug-ins: Kotlin, Anko DSL Preview, and Kotlin Android Extensions.
For example:
2. Create a project and configure gradle
Build. gradle under the project:
buildscript { ext.support_version = '23.0.0' ext.kotlin_version = '1.0.0-beta-4589' ext.anko_version = '0.8.1' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }}
Build. gradle in app:
apply plugin: 'com.android.application'apply plugin: 'kotlin-android'android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.linc.kotlinhelloworld" minSdkVersion 19 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.0' compile "com.android.support:recyclerview-v7:$support_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.anko:anko-sdk15:$anko_version" compile "org.jetbrains.anko:anko-support-v4:$anko_version"}buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" }}
3. Use kotlin for programming
The addition of anko allows you to use the control in layout without the findViewById. For example:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) forecast_list.layoutManager = LinearLayoutManager(this); forecast_list.adapter = ForecastListAdapter(items) toast("hello toast.") }
A lambda expression can be used for button events, as shown in the following code:
btnTest.setOnClickListener({btnTest->toast("button click!")})
You can also simplify it:
btnTest.setOnClickListener{toast("button click!")}
For more syntactic sugar, see the official documentation: https://kotlinlang.org/docs/reference/
Refer:
1. Use lambda: http://juude.net/lambdajava/ on android