Hello Kotlin(使用Kotlin开发Android)


在2017年Google I/O上,Google宣布Android支持Kotlin作为官方开发语言
同步在官网上线了Kotlin的入门级文档


Kotlin and Android

  • Kotlin与Java兼容,可以混合式的编码

If you're interested in using Kotlin, it's easy to get started because it works side by side with Java and C++ on Android. So you can keep your existing code, continue to use the various Android libraries, and incrementally add Kotlin code to your project. Unlike almost any other language, Kotlin is a drop-in replacement you can use bi-directionally—you can call into the Java language from Kotlin, and you can call into Kotlin from the Java language.

  • 有着JetBrains公司(Kotlin发明者)的大力支持,Android Studio3.0起会内置Kotlin开发工具

Of course, IDE support is also crucial, and we have it. Android Studio is built upon IntelliJ IDEA, an IDE built by JetBrains—the same company that created the Kotlin language. The JetBrains team has been working for years to make sure Kotlin works great with IntelliJ IDEA. So we're inheriting all their hard work. Starting with Android Studio 3.0, tooling support for Kotlin is bundled directly into Android Studio.

  • Kotlin的开源精神与Android一致,Google与JetBrains会推动Kotlin成为一个非盈利的组织

We believe Kotlin is an excellent fit for Android not only because it gives developers what they want, but also because it matches the spirit of Android. Just like Android, Kotlin has always been an open source project, primarily under Apache 2. It involves an entire community and does not belong to just one company. So we're working with JetBrains to move Kotlin into a non-profit foundation. Our choice of Kotlin reaffirms our commitment to an open developer ecosystem as we evolve and grow the Android platform, and we are excited to see the language evolve.

  • 除了Kotlin,Google还增加了对Java和C++的支持

And while we're adding Kotlin as an official Android language, we're also expanding our investment in our existing languages. For example, in Android O we added support for more Java 8 libraries. In Android Studio 3.0, Java 8 language features are now directly supported with the javac compiler. And on C++, we're making ongoing investments such as expanding performance profiling tools and APK debugging tools to fully cover the native experience; and significantly improving libc support and updating the NDK to ensure you can access modern headers even when you target older APIs.


Get Started with Kotlin on Android

  • 如果想要方便的体验使用Kotlin开发Android,建议下载Android Studio3.0预览版,在2.3.2版本上需要下载Kotlin相关插件
  • 强烈建议用梯子翻墙,否则新建工程的时候会因为无法引入依赖资源导致失败
  • 新建工程,勾选“Include Kotlin support”


  • 创建Activity,Source Language选择“Kotlin”(也可以使用Code->Convert Java File to Kotlin File直接转换MainActivity.java)


  • 自动生成的代码


  • 如果希望区分Java和Kotlin代码,可以在src->main下新建kotlin目录


  • 同时在build.gradle中配置资源路径


  • 我们给MainActivity增加一个跳转功能


  • 编译->运行

Kotlin on Android FAQ

  • 像调试Java一样调试Kotlin

Debugging Kotlin works just like debugging Java code. You don't need to do anything differently.

  • Kotlin中可以透明调用Java方法

Yes. Kotlin provides Java language interoperability. This is a design that allows Kotlin code to transparently call Java language methods, coupled with annotations that make it easy to expose Kotlin-only functionality to Java code. Kotlin files that don't use any Kotlin-specific semantics can be directly referenced from Java code without any annotations at all. Combined, this allows you to granularly mix Java code with Kotlin code.

  • Kotlin完全支持JNI

Yes, JNI is fully supported with Kotlin. Simply mark the JNI method with the external modifier.

  • 使用Kotlin开发,会增大约1M的apk包大小

The Kotlin runtime adds about 7,000 methods and ~1MB to your debug APK. The net impact might be less if you use Kotlin to replace another library in your project, such as Guava or RxJava. This size also reduces when you optimize the APK for release with Proguard, just like other app code and libraries.

  • Kotlin相较于Java,没有性能上的提示

Kotlin doesn't have a direct performance impact, but just as with the Java language, you should be thoughtful about how you use it. For example, repeated copying between new collection instances can impact GC performance, and calling a method that accepts non-null types adds a method call for the null check (though you can disable runtime null checks in the compiler with -Xno-param-assertions).


Resources to Learn Kotlin

推荐阅读更多精彩内容