Liu Peng (HJ Android development engineer)


Written in the book of the former



The previous”What do you think about Kotlin being trending on Google IO?Kotlin has been used in our project for more than two years. So any new language that has not been officially approved (and experience has taught us that it is) will always run into problems. I think that’s why Kotlin came out in 2011, and then came out at IO this year, and then of course blew up all over the web.


So what are the pitfalls of the project?


Idleness is a disaster. Although good, do not drink too much




Java one-click convert to Kotlin


When I first learned about Kotlin, the Android Studio plugin provided the ability to convert Java files to Kotlin files with one click, so sometimes when you see old Java code, you might just convert Kotlin with one click and then write new requirements. Or when a Java method needs to be written to a Kotlin class, the IDE does the conversion for you.

Convenience laziness is a disaster.

Because of the Kotlin optional, the IDE can directly cast the (a!! .value), then the data may be null, and after the online, the data will crash if it is abnormal in some scenarios.

On the other hand, if the original Java class has Runnable, then use one key convert, IDE directly crash restart.

Ii: DexGuard confusion crash (NoSuchMethodError )


Dexguard was used in our project to reinforce confusion, and the first headache was the server data return problem.


val drawable = imageView? .drawable ? : ColorDrawable(Color.LTGRAY)

transitionDrawable = TransitionDrawable(arrayOf(drawable, ColorDrawable()))

This code is OK until it is obfuscated, but then crashes. The assumption at the time was multiple “?” Dexguard causes coding error crash during the confusion process. The solution is:


if(imageView == null) return

val drawable = imageView.drawable ? : ColorDrawable(Color.LTGRAY)

Three: debugging pit let me live without love again


This pit is divided into two parts. One is library debugging, the other is normal debugging main project.


Let’s talk about debugging the main project code, the main development process of the building numerous times encountered the problem is when you cheerfully set a breakpoint debug. IDE tells you in the lower right corner.


Kotlin plugin crash

However, in most cases, I just want to see if the data sent by the server is ok, because the data sent by the server is called back in an asynchronous callback, and when you select a variable to reference the data, the IDE will tell you an exception and you can’t view the data. Shed tears…


When we talk about library debugging, we’re referring to the library where Kotlin wrote the debug code, and all the files look like this:




Cry Kotlin library


Sorry, you can’t set breakpoints to debug. At that time, the owner of the building is also very helpless backward, in the debug process using source code dependency:




Debug library



Four: No trinary operation


In Java, we can write anything like this:


String content = contentStringBuilder.toString().length() >=140? ContentStringBuilder. ToString (). The substring (0139) : contentStringBuilder. The toString ();

But, to say sorry, Kotlin doesn’t support this syntax anymore. Can only if else:


val a = if(some) valueA else valueB

But we thought it would be too painful, so we wrote function:




select


In the process of use, it can be similar to Java’s three-mesh operation, which is as smooth to write as it is to use.


val a = select(some,valueA,valueB)

But, until one day when a Java lang. IndexOutOfBoundsException, surprised. It’s not scientific. It’s impossible to walk to another value. However, when you calm down and review the above three operations, it is not hard to see why, because both values are passed as parameters, so the operation has been carried out in the process of passing the parameter. We do not give up, the transformation of a version is to solve the headache of the problem.




The incoming block

Five: other pits


  1. Java can’t call Kotlin’s Reified function, so use Kotlin as your first language.


  2. Optionality is really just a “?” Unlike other languages, there is not a specific type optional, so sometimes I have no choice but to introduce third party support.


  3. One of the most fun keywords in Extension is the ability to extend all sorts of desired methods without looking at specific documentation, plus operator overloading, such as not using the stupid FindViewByid, and sharp DSL support. But say no, it’s not the same concept as Swift, so if you’re interested I’ll take the time to talk about Swift VS Kotlin and show you two languages at once.


  4. Vararg vars: T Use *vars.


Write in the last


Previous post “A Google IO makes Kotlin a hot search. What do you think?” As mentioned in the second part, Now Kotlin is very stable in our project. Of course, this is inevitable. Otherwise, how can we live up to Kotlin’s own characteristics?


The above memories of some pits, just to let you take detours, I hope to bring you a little help. Go ahead and hug Kotlin. We’re already here. I believe you can too. Now that Google is supporting us, we don’t have to ask questions in the Kotlin forum. Now we are happy, not fighting alone!!