Sunday, October 7, 2018

Android Pie Sdk Is Straightaway To A Greater Extent Than Kotlin-Friendly

Posted past times James Lau, Product Manager (@jmslau)

When using the Java programming language, i of the most mutual pitfalls is trying to access a fellow member of a null reference, causing a NullPointerException to live on thrown. Kotlin offers protection against this past times baking nullable in addition to non-nullable types into the type system. This helps eliminate NullPointerExceptions from your code in addition to amend your app's overall quality. When Kotlin code is calling into APIs written inward the Java programming language, it relies on nullability annotations inward those APIs to create upward one's hear the nullability of each parameter in addition to the render type. Unannotated parameters in addition to render types are treated equally platform types, which weakens the null-safety guarantee of Kotlin.

As purpose of yesterday's Android ix announcement, nosotros bring too released a novel Android SDK that contains nullability annotations for to a greater extent than or less of the most often used APIs. This volition save the null-safety guarantee when your Kotlin code is calling into whatsoever annotated APIs inward the SDK. Even if you lot are using the Java programming language, you lot tin yet attain goodness from these annotations past times using Android Studio to select grip of nullability contract violations.

Not a breaking change

Normally, nullability contract violations inward Kotlin trial inward compilation errors. But to ensure the newly annotated APIs are compatible amongst your existing code, nosotros are using an internal machinery provided past times the Kotlin compiler squad to grade the APIs equally lately annotated. Recently annotated APIs volition trial solely inward warnings instead of errors from the Kotlin compiler. You volition involve to usage Kotlin 1.2.60 or later.

Our invention is to bring newly added nullability annotations attain warnings only, in addition to growth the severity grade to errors starting inward the next year's Android SDK. The destination is to render you lot amongst sufficient fourth dimension to update your code.

How to usage the "Kotlin-friendly" SDK

To teach started, become to Tools > SDK Manager inward Android Studio. Select Android SDK on the left menu, in addition to brand certain the SDK Platforms tab is open.

Use SDK Manager inward Android Studio to install SDK for API Level 28 Revision 6

Check Android 8.+ (P) in addition to click OK. This volition install the Android SDK Platform 28 revision vi if it is non already installed. After that, laid your project's compile SDK version to API 28 to outset using the novel Android Pie SDK amongst nullability annotations.

Use the Project Structure Dialog to alter your project's Compile Sdk Version to API 28

You may too involve to update your Kotlin plugin inward Android Studio if it's non already up-to-date. Make certain your Kotlin plugin version is 1.2.60 or afterwards past times going to Tools > Kotlin > Configure Kotlin Plugin Updates.

Once it's laid up, your builds volition outset showing warnings if you lot bring whatsoever code that violates nullability contracts inward the Android SDK. An trial of such a alarm is shown below.

Sample alarm from the Kotlin compiler when code violates a lately added nullability contract inward the Android SDK.

You volition too outset seeing warnings inward Android Studio's code editor if you lot telephone telephone an Android API amongst the wrong nullability. An trial is shown below.

Android Studio alarm near passing a null reference to a parameter annotated equally a lately non-null type inward the android.graphics.Path API.

Leveraging nullability annotations from the Java programming language

You tin attain goodness from the novel nullability annotations fifty-fifty if your code is inward the Java programming language. By default, Android Studio volition highlight whatsoever nullability contract violations amongst a warning, similar the i below:

Android Studio showing a alarm near nullability contract violation inward code written inward the Java programming linguistic communication

To ensure that you lot bring this inspection enabled, you lot tin become to the IDE's settings page in addition to search for "Constant weather & exceptions" inspection in addition to brand certain that especial is checked.

Use the Inspections page nether Settings to ensure the Constant weather & exceptions code inspection is enabled.

If you lot are using the Java programming language, nullability contract violations volition non attain whatsoever compiler alarm or error. Only the in-IDE code inspections are available to flag these issues.

You tin too run code inspections across your entire projection in addition to run into the aggregated results. Click on Analyze > Inspect Code… to start.

What's Next

The Android SDK API surface is really large, in addition to nosotros bring solely annotated a minor percent of the APIs in addition to thus far - in that place is yet lots of move remaining. Over the side past times side several Android SDK releases, nosotros volition maintain to add together nullability annotations to the existing Android APIs, equally good equally making certain novel APIs are annotated.

With the "Kotlin-friendly" Android SDK, the nullability annotations inward AndroidX (part of the Jetpack family), in addition to Android KTX, nosotros are continuing to amend the Android APIs for developers using Kotlin. If you lot bring non yet tried Kotlin, nosotros encourage you lot to endeavour it. Not solely tin Kotlin brand your code to a greater extent than concise, it tin too amend the stability of your apps.

Happy Kotlin-ing!

Related Post

Android Pie Sdk Is Straightaway To A Greater Extent Than Kotlin-Friendly
4/ 5
Oleh