Writing in the front

I’ve been working on Android with Kotlin for over two weeks. The benefits are so great, it’s fun to code. Kotlin is simple, easy to use, and has very little code. So cool things, dare not private, so write out to share with everyone.

You may not believe the headline, but it’s not an exaggeration. As you follow this series of articles, you will discover the code. That’s a lot less. This series of articles will use Kotlin to create an Android app from scratch (not a wechat app). Instead of the traditional way of learning a language (e.g. Kotlin Mastery in 21 Days, Kotlin Beginner to Master). Learning grammar in the process of creation, I think it is meaningless to just learn grammar, you will forget it. Because you don’t learn for the sake of learning, you learn for the sake of using. If you use it a lot, you’ll remember it.

As for what Kotlin is, it is simply a new language running on the JVM. It is short and fast, and it has many advantages.

  1. Seamlessly interworking with Java allows Java code and Kotlin code to call each other
  2. One-click Java to Kotlin, if you have legacy Java code, one-click conversion
  3. Function extension function, no longer know what is called inheritance

Create your first Kotlin app!

  1. First open your AS and, AS usual, create an Android app.
  2. Then find the Plugin option in the Setting and search for the Kotlin Plugin. Download Android from the official warehouse.

  3. Add the following code to app Gradle

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

Copy the code
  1. Add the following code to project Gradle (note that when you configure it yourself, change it to the latest version)
Classpath "org. Jetbrains. Kotlin: kotlin - gradle - plugin: 1.0.4." "Copy the code
  1. Then switch to MainActivity and press CMD + Alt + Shift + K (on MAC) to convert the Java code to Kotlin code.

Analyze the transformation code

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}Copy the code

After the transformation

class MainActivity : AppCompatActivity() {

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

When comparing the two pieces of code, you can see the syntax changes:

  1. Extends becomes:
  2. The function definition keyword has changed to fun
  3. The semicolon is gone
  4. The variable names are written in front of the type and those are syntactic changes, and there will be some later, when you use them more, you’ll remember them. We will continue to summarize the grammar changes we encounter later.

Codify Hello World as Hello Kotlin

Start by adding an ID to the textView in the XML file

Copy the code

In traditional Java code, to change the text of a TextView, findViewById () followed by setText() would be bloated. It’s time to try Kotlin!

Let’s use Kotlin to do the same thing:

textView.text = "hello kotlin"

Copy the code

Nani ???? In one sentence?? Oh my God?? What the hell ????

The forehead.. You need to calm down. I know you’re freaking out inside. But it does, as you can see, findViewByID is gone, and instead it’s just writing the ID. Kotlin can find the control by ID instead of findViewByID, so you need to pay attention to the import header to import it

import kotlinx.android.synthetic.main.activity_main.*

Copy the code

Also, the setText is missing. This is a feature of Kotlin where all getters and setters can be omitted and changed directly to.text

How do you feel? Is the amount of code reduced a lot? In fact, it’s only a fraction of the total. That’s the end of this article (too short? Don’t worry, you start to create a project. Series of articles, since we dug a hole, we will not procrastinate. )

Next: The Secret book! Extension function The code address used in this project