Hello everyone, I am the Cang King.

The following is my series of related articles, you can refer to, you can give a like or follow my article.

[Android] How to make a chapter list of apps with a crash rate of less than three per thousand


I’m sure you’ve all seen this year’s Google I/O, and probably the biggest news for us Android programmers is that Google is making Kotlin the first language for Android development, and that Android studio3.0 will be automatically compatible with Kotlin development.

Then, the next day, tweets and tutorials about Kotlin go viral on your wechat. Then you think the current development process is good, why change it? With a little resistance to new technology, add the same learning to your Android tech graph.

Tools are moving forward, languages are moving forward, and of course people have to move forward.

Then, I want to arm my Gank project with Kotlin.

Let my componentized Gank explode with the recent craze Kotlin.

The address isComponentization Gank (https://github.com/cangwang/Gank/tree/kotlin)

This is the Kotlin branch of componentized Gank. If you think it is useful, please star it. Thank you!


I was in touch with Kotlin in the last two years and wrote it, only to find that I couldn’t even write kotlin’s handler (the learning cost is sometimes quite high). I need to understand the characteristics of Kotlin, and THEN I spent three days to convert my Gank from Java to Kotlin, and the pit I stepped on inside really makes people feel speechless sometimes, here I tell you, I met some problems in this conversion.


Componentized Kotlin configuration

Talk about Kotlin’s configuration.

Our Kotlin installation requires the installation of a Plugin that is compatible with more than versions of Android studio2.0.

Then the current version is 1.1.2-7.



Sure everyone is concerned that you can use the preview version of Android Studio3.0, I step on the pit in advance, found that Android Studio directly converted to compile my Gank component project, found that the compiled package, the root installation failed. (Are you kidding me?) It’s a really big hole…

So remind everyone Android Studio3.0 have the courage to step on the pit first can consider, I made two clock, or failed to compile, or wait for the version of the giant pit fix it.

Kotlin has been around for more than two years now, but it’s still not getting much attention in China, like Swift, and it’s better to say how determined Google is to replace Kotlin as the main language.

You can find the configuration path by going to Tools->Kotlin->Configure Kotlin inProject.



Then you need to select the module you want to configure, or the entire module.



Then he will help you configure the latest 1.1.2-7 version into Depencies



Then kotlin-Android will be introduced to you in every project, and the first pit will come. Cheat a look, this should have no problem ah, but immediately configuration does not pass…



Because here inside com.android. XXX must be introduced first. Kotlin-android must be introduced after it, otherwise error, slowly find their own debugging console information bar.



Add kotlin-stdlib-jre to the dependencies of each module. It will be introduced here later on what the pit will be.



Then we begin our journey of transformation



Here you can choose to convert all project files at once. But generally we just need to go to the Java file in SRC. And some file conversion is to let the project appear stuck phenomenon. I have a project coming up, so I’m going to skip it for now and switch when I know why.

2017.5.24 * * * * * * * *

One handy thing here is that if you paste Java code directly into a Kotlin file, Kotlin will kindly prompt you if you want to convert the code to Kotlin, which will greatly increase our efficiency.

Then I found the cause of the code conversion failure. It was due to Handler conversion problems. If there is a Handler in the code, the Java conversion to Kotlin will have a lot of problems with false death.

It is important that you do not convert Java code into Kotlin code with a Handler or custom Runnable, otherwise the compiler will enter suspended animation. I’ll just have to code it myself.


Two. Code conversion problems

(1) View is not introduced



The Android project should have introduced the View package by default

It’s often the case that you’re using some properties of the View that aren’t introduced.




(2) Value nulling problem



It’s very simple, it needs the object to be non-empty, and if you tell her that your object might now be empty, Kotlin’s hint mechanism is safe.



Here or first empty bar, or use two!! If you burst, you’re asking for trouble.




(3) Value conversion problems



We can’t have any custom View properties here, so it’s best to check if there are any undeclared or converted errors.



I’ll just convert it to float.




(4) When the associated Module is converted, some attributes will be omitted when converting references



For example, when BaseActivity is placed in the Base Module and referenced by other modules, and then declared to convert a module first, you will find that the reference is only declared to be protected



Override is ok here




(5) List conversion



It’s going to go to ArrayList, and Kotlin is going to convert that to MutableList



However, since we normally convert the input to a List object, we need to strongly MutableList



Then there’s the nasty databinding problem

We kotlin configure databinding. In addition to configuring databinding in general,



You also need to configure Kapt



Introduce kapt library




(6) Compilation problems of KAPT



Obviously compileReleaseKotlin and kaptReleaseKotlin collide

The temporary solution here is to lower kotlin’s version, which is now 1.1.2 to 7 and we lower it to 1.1.2 or 1.1.2 to 3.


(7) Databinding file cannot be generated



Well, here’s a method, if you look at the debug version of Gradle Console carefully, it will tell you that some properties have problems.


(8) There is a giant pit here, to remind you of the problem of custom Application.



In fact, the problem is not because of the Application at all,

See the debug version to find the real reason



The DataBindingAdapter cannot find app:imageurl.

Because my ImageLoader is stored in the Base Module without a reference to kapt, its compiled index will not be indexed.

It should also be noted that the main module that generates app also needs to add a reference to Kapt, otherwise even app cannot be generated successfully.


Test not added to kapt compilation

We can see that when we build the project, we add the test folder and references



Because the previous reference was Java’s test library, and Kotlin needs its own test library, we need to change its compiled library.



Address the related address Stackoverflow


(10) Singleton problems

Singleton conversions can’t be completely transformed today, because singleton writing is complicated, so conversions can easily go wrong.

I’m using a singleton for double check



I’m obviously going to use the associated object, and then I’m going to declare the CommonRetrofit object that I’m going to get through get.



As for the teaching of Kotlin, I would like to recommend a basic course of Kotlin in Tencent, which is very recommended and very clear.

My singleton problem solving is based on the tutorial above.

2017.5.24 * * * * * * * *

Handler problem, because converting Handler and Runnable directly will fake dead.

To give you a hint of how to write runnable, if the loop calls runnable with a handler, the handler needs to instantiate the runnable through object first.



It’s not going to get through. We need to convert it to the object declaration call itself in order for it to work.



This is a little bit of a struggle.



3. Problems related to operation

(11) Route jump



I’m using ARouter, I can’t find route matched.

It is likely that the KAPT annotations do not support generating routing RouteMatch files for the time being.

For now, we can only switch to an implicit jump.

(12) Encountered Adapter data problems

I’m sure you’ve all encountered a Fragment display before loading data methods.

Then we’ll update it in setUserVisiblehint



Whereas the data objects in our RecylerView



It says this is empty



When you don’t know the truth, it’s okay to initialize it like this.



However, we need to initialize the datas object. This is the correct initialization, so that it is not null and size can be returned normally.



(13) BindingAdapter problem



Tells us that the loadImage method for ImageLoader cannot be found. Because it’s not static.



Then we add the JvmStatic annotation, which refers to the static property in Java.




(14) DataBindingAdapter has no empty prompt



Looks all right at first



Because of databinding, which is associated with XML, there is no way to know if the data passed by XML is available.

Then, because the URL of the picture returned by Gank may be empty and I did not give the declaration, then the error will be reported as empty value.



Something a question mark can solve.


(15) WebView onPageStart problem



Even the native onPageStated interface has compatibility issues, so do you still want to believe in love? And native is called Java written, more can’t prompt errors.

It looks normal, but there is a problem with the favicon parameter



Still a question mark can solve the problem



There are no non-empty hints for native methods, so be careful when calling native methods that modify the interface themselves.


4. To summarize

I finally jumped out of the hole after three days and felt like I knew More about Kotlin.

1. If there is a problem, you must carefully look at the debugging version. The kAPT conflict may not be the real cause of the debugging version information, and may just be an ADAPTATION problem such as XML.

2. Since all native code is written in Java, some of it does not have non-null processing. In this case, Kotlin needs to take over and be careful when declaring methods and objects.

3. Learn the basics first.


That’s where the specific stomp pits are.

There are still a few issues left, and I’ll refine this example.

Address is componentized Gank (https://github.com/cangwang/Gank/tree/kotlin), we welcome the star support.


So that’s the end of this introduction,

The next section will be more exciting, stay tuned!!

Group number is 316556016, can also scan into the group. I am looking forward to your joining us!!