Migrating the AOSP QuickSearchBox App to Kotlin | by Android Builders | Android Builders | Sep, 2022 | Frost Tech

almost Migrating the AOSP QuickSearchBox App to Kotlin | by Android Builders | Android Builders | Sep, 2022 will cowl the most recent and most present instruction with regards to the world. entry slowly fittingly you comprehend competently and accurately. will progress your data dexterously and reliably


introduced by Ryan O’Leary, Android Intern

For the previous three years, the Android Open Supply Venture (AOSP) Purposes staff has been engaged on changing AOSP purposes from Java to Kotlin. This search started as a part of Android’s dedication to develop increasingly more with kotlin first. Kotlin is a protected, pragmatic, and concise language, with a lot of language-specific advantages. Helpful options of the Kotlin language embody:

  • null safety: Kotlin sorts are non-nullable except explicitly specified. That is extremely helpful for builders to keep away from hard-to-trace null pointer exceptions.
  • Conciseness: Kotlin permits builders to scale back the quantity of boilerplate code, expressing extra with fewer strains of code in comparison with Java.
  • Java interoperability: The interoperability between Kotlin and the Java programming language was very helpful for this undertaking because the migration may very well be finished incrementally and is beneficial for all builders engaged on blended Java and Kotlin tasks.
  • Jetpack libraries and coroutines: Kotlin gives language help for light-weight, structured concurrency via routines. Moreover, Android improvement content material is Kotlin-first and gives entry to helpful Jetpack libraries.

AOSP interns transformed the AOSP Deskclock app in 2019 and the Calendar app in 2020, detailing the migration course of in comparable articles. This 12 months, the AOSP staff got down to absolutely convert the QuickSearchBox app as a part of a summer season 2022 internship undertaking. Launched in 2009 by way of the Android 1.6 launch, the QuickSearchBox app permits customers to look each on their gadget and on the internet proper from your property display, offering strategies primarily based on downloaded content material, contacts, apps, and browser historical past. Over the course of 6 weeks, over 11,000 strains of Java code throughout the QuickSearchBox app had been transformed to Kotlin to showcase greatest practices in Android improvement and supply QuickSearchBox app performance with Kotlin first in thoughts.

To port the QuickSearchBox app to Kotlin, we use the Kotlin conversion instrument that’s included in Android Studio. Our course of adopted 5 steps:

  1. Copy the .java file to a .kt file of the identical identify utilizing the command: cp ExampleFile.java ExampleFile.kt
  2. Use the supplied conversion instrument to transform Java code to Kotlin
  3. Add the .java file to an include_srcs property on the Android.bp file in order that the conversion can occur incrementally, one file at a time.
  4. Resolve compilation errors and some other errors launched by the conversion
  5. Run and go unit assessments to confirm the correctness of the Kotlin-based implementation, and run guide assessments to confirm function parity between Kotlin and the legacy Java-based software.

The git historical past of every of those steps was stored to indicate the migration course of to third-party builders and could be considered for every file within the AOSP QuickSearchBox principal department. The bug fixing step on this course of was obligatory attributable to a number of frequent points present in Android Studio’s Java to Kotlin conversion instrument, which occurred in most AOSP QuickSearchBox recordsdata that had been migrated.

The secondfrequent issues and their options they’re as follows:

Frequent downside: Required import statements would ceaselessly be eliminated within the transformed Kotlin file

Answer: Manually convert the required import declarations

Frequent downside: the override The key phrase was typically not added to strategies and variables tagged @Override

Answer: add manually override modifier

Frequent downside: A much bigger situation encountered was changing nullable variables to non-nullable variables in Kotlin code. This brought on a wide range of errors, from kind mismatches to giant sections of QuickSearchBox code not executing, because the logic that relied on checking for null values ​​was not used. Changing one file at a time additionally meant that utilizing inherently nullable sorts within the unconverted Java recordsdata would create issues when attempting to assign, override, or return these inherited variables in Kotlin code.

Answer: Typically, these properties really wanted to be nullable to attain the specified performance. The answer right here was to easily use nullable sorts by including `?` to the kind declaration. This required including protected calls (`?.`) the place acceptable and altering anticipated sorts the place essential to keep away from mismatch errors. Whereas this will likely appear contradictory to the null-safe nature of Kotlin, some properties wanted to stay nullable as they had been set asynchronously, and all variables that might turn into non-nullable had been.

Frequent downside: Features beginning with get and set had been modified by the converter to variables with explicitly outlined getters/setters, however many usages didn’t change and had been left undefined

Answer: Manually change the operate calls transformed to the variable identify; e.g. getSuggestions() -> strategies

Frequent downside: Makes use of of getClass() they weren’t modified by the converter within the Kotlin code. Not like Java, Kotlin doesn’t help calling getClass() on objects to retrieve a token of kind Class.

Answer: Use Kotlin’s class reference syntax, altering the makes use of of getClass() a ::class to return the KClass token.

Frequent downside: The QuickSearchBox undertaking was constructed with the -Werror flag on, and a standard supply of errors was using java.util.Assortment in a Kotlin class. When porting an software to Kotlin, it’s endorsed that builders use libraries constructed for Kotlin to maximise language-specific advantages.

Answer: Swap to utilizing the Kotlin equal of the required class. This has the good thing about elevated safety, as the category could be specified as mutable or immutable when wanted.

i.e. java.util.Assortment -> kotlin.collections.Assortment or kotlin.collections.MutableCollection

Because the migration befell, the API degree of the QuickSearchBox software was additionally up to date to the most recent model. This concerned changing a number of deprecated operate calls with the brand new variations advisable by Android. An instance of this was the deprecated use of AsyncTask inside SearchBaseUrlHelper.

AsyncTask was deprecated at API degree 30, and the Android documentation recommends utilizing the java.util.concurrent or Kotlin concurrency utilities. Since this undertaking is geared in direction of exhibiting the advantages of growing in Kotlin, we change using AsyncTask with a Kotlin routine scope and an async block to situation community requests asynchronously. Though, like Java, Kotlin helps threading, coroutines don’t block and don’t stack, which permits for decrease reminiscence utilization.

Lastly, a refined supply of bugs that builders ought to concentrate on comes from Kotlin’s conversion operate that adjustments strategies whose names start with ‘get’ and ‘set’ to variables with explicitly outlined getters and setters. When this transformation takes place, the converter will rename the migrated operate to a variable of the identical identify, besides with out the previous ‘get’ or ‘set’. Nonetheless, the converter doesn’t examine for present makes use of of the identical identify, which may introduce devastating bugs within the transformed code. In giant recordsdata with many makes use of of those variables, these errors could be extremely tough to detect. This was seen within the file. DelayingSuggestionsAdapterthe place the road:

Modified in Kotlin code to:

This launched a refined runtime bug the place an object was freed too quickly, throwing an exception in a completely totally different file. Whereas the repair was a easy one line change, maintaining monitor of all of the abuse brought on by the conversion could be time consuming for builders, and ideally the converter would use a extra thorough code examine earlier than creating new variables.

After the conversion was full, we ran some benchmark assessments to investigate the adjustments that occurred throughout the AOSP QuickSearchBox software when migrating to Kotlin. Some fascinating efficiency metrics that we will use to match the Java and Kotlin variations of the absolutely working QuickSearchBox software embody:

After the migration was full, we noticed a major discount of about 11.5% within the complete # LOC (strains of code). It is also price noting that the migration of the QuickSearchBox app targeted on sustaining the prevailing performance of the app by changing it to Kotlin, with out closely refactoring the code construction. If we had been to rewrite your complete QuickSearchBox software from Kotlin, we consider it might be potential to attain even larger measurement discount by higher using Kotlin’s terse nature.

We noticed a reasonably vital enhance in APK measurement, nevertheless this enhance could be attributed to the brand new inclusion of the kotlinx-coroutines and androidx.core libraries on this undertaking. The androidx.core library was added to exchange a number of deprecated operate calls with the advisable overrides for QuickSearchBox to focus on the most recent API degree, and would have occurred whatever the Kotlin migration. For the reason that complete measurement of the QuickSearchBox APK was nonetheless fairly small (lower than a complete of MB), we consider this enhance is justifiable.

Lastly, the clear compile time for the code was averaged at 10 makes an attempt for every language. These assessments and former benchmarks had been run on a machine with 96 cores and 180GB of RAM. Actually, we noticed a slight lower in complete construct time on the Kotlin model of the app, probably because of the lower in #LOC, the transfer to a number of up to date API calls, and using Kotlin coroutines.

In complete, the AOSP QuickSearchBox software migration took roughly 6 weeks, with 1 intern engaged on the undertaking. Conversion charge elevated vastly with familiarity with Git/Repo, Kotlin, and customary bugs present in Android Studio’s Kotlin instrument. Midway via the undertaking, the method of changing the file, fixing bugs, formatting, and sustaining Git historical past was extremely environment friendly, with as much as 10 recordsdata migrated per day. In the end, this undertaking helped display the advantages of changing an software to Kotlin, from extra concise syntax to offering entry to helpful libraries and light-weight concurrency, and may act for example for different builders seeking to port their very own purposes.

I want the article kind of Migrating the AOSP QuickSearchBox App to Kotlin | by Android Builders | Android Builders | Sep, 2022 provides perception to you and is beneficial for surcharge to your data

Migrating the AOSP QuickSearchBox App to Kotlin | by Android Developers | Android Developers | Sep, 2022