about Migrating the AOSP QuickSearchBox App to Kotlin | by Android Builders | Android Builders | Sep, 2022 will cowl the most recent and most present suggestion not far off from the world. get into slowly for that purpose you comprehend skillfully and accurately. will accrual your data precisely and reliably
offered by Ryan O’Leary, Android Intern

For the previous three years, the Android Open Supply Challenge (AOSP) Purposes staff has been engaged on changing AOSP functions from Java to Kotlin. This search started as a part of Android’s dedication to develop increasingly more with kotlin first. Kotlin is a secure, pragmatic, and concise language, with plenty 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 cut 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 venture because the migration may very well be finished incrementally and is helpful for all builders engaged on combined Java and Kotlin initiatives.
- Jetpack libraries and coroutines: Kotlin supplies language assist for light-weight, structured concurrency by means of routines. Moreover, Android growth content material is Kotlin-first and supplies 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 totally convert the QuickSearchBox app as a part of a summer season 2022 internship venture. Launched in 2009 through the Android 1.6 launch, the QuickSearchBox app permits customers to go looking each on their system and on the net proper from your private home display screen, offering strategies based mostly on downloaded content material, contacts, apps, and browser historical past. Over the course of 6 weeks, over 11,000 strains of Java code inside the QuickSearchBox app had been transformed to Kotlin to showcase greatest practices in Android growth and supply QuickSearchBox app performance with Kotlin first in thoughts.
To port the QuickSearchBox app to Kotlin, we use the Kotlin conversion software that’s included in Android Studio. Our course of adopted 5 steps:
- Copy the .java file to a .kt file of the identical title utilizing the command:
cp ExampleFile.java ExampleFile.kt
- Use the supplied conversion software to transform Java code to Kotlin
- 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.
- Resolve compilation errors and another errors launched by the conversion
- Run and go unit assessments to confirm the correctness of the Kotlin-based implementation, and run handbook assessments to confirm characteristic parity between Kotlin and the legacy Java-based software.
The git historical past of every of those steps was saved to indicate the migration course of to third-party builders and might be considered for every file within the AOSP QuickSearchBox fundamental department. The bug fixing step on this course of was essential as a consequence of a number of widespread points present in Android Studio’s Java to Kotlin conversion software, which occurred in most AOSP QuickSearchBox information that had been migrated.
The secondwidespread issues and their options they’re as follows:
Frequent downside: Required import statements would regularly be eliminated within the transformed Kotlin file
Resolution: Manually convert the required import declarations
Frequent downside: the override
The key phrase was usually not added to strategies and variables tagged @Override
Resolution: add manually override
modifier
Frequent downside: A much bigger difficulty encountered was changing nullable variables to non-nullable variables in Kotlin code. This brought on a wide range of errors, from sort 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 information would create issues when attempting to assign, override, or return these inherited variables in Kotlin code.
Resolution: Normally, these properties truly wanted to be nullable to attain the specified performance. The answer right here was to easily use nullable sorts by including `?` to the sort declaration. This required including secure calls (`?.`) the place applicable and altering anticipated sorts the place essential to keep away from mismatch errors. Whereas this may occasionally 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 out to be 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
Resolution: Manually change the perform calls transformed to the variable title; 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 assist calling getClass() on objects to retrieve a token of sort Class.
Resolution: Use Kotlin’s class reference syntax, altering the makes use of of getClass()
a ::class
to return the KClass token.
Frequent downside: The QuickSearchBox venture 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 is strongly recommended that builders use libraries constructed for Kotlin to maximise language-specific advantages.
Resolution: Swap to utilizing the Kotlin equal of the required class. This has the good thing about elevated safety, as the category might be specified as mutable or immutable when wanted.
i.e. java.util.Assortment -> kotlin.collections.Assortment or kotlin.collections.MutableCollection
Because the migration occurred, the API stage of the QuickSearchBox software was additionally up to date to the most recent model. This concerned changing a number of deprecated perform calls with the brand new variations beneficial by Android. An instance of this was the deprecated use of AsyncTask inside SearchBaseUrlHelper
.
AsyncTask was deprecated at API stage 30, and the Android documentation recommends utilizing the java.util.concurrent or Kotlin concurrency utilities. Since this venture is geared in direction of displaying the advantages of growing in Kotlin, we substitute using AsyncTask with a Kotlin routine scope and an async block to difficulty 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 pay attention to comes from Kotlin’s conversion perform that modifications 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 perform to a variable of the identical title, besides with out the previous ‘get’ or ‘set’. Nonetheless, the converter doesn’t verify for present makes use of of the identical title, which may introduce devastating bugs within the transformed code. In giant information with many makes use of of those variables, these errors might be extremely tough to detect. This was seen within the file. DelayingSuggestionsAdapter
the 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 wholly completely different file. Whereas the repair was a easy one line change, protecting observe of all of the abuse attributable to the conversion might be time consuming for builders, and ideally the converter would use a extra thorough code verify earlier than creating new variables.
After the conversion was full, we ran some benchmark assessments to research the modifications that occurred inside 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 totally 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 value noting that the migration of the QuickSearchBox app targeted on sustaining the present performance of the app by changing it to Kotlin, with out closely refactoring the code construction. If we had been to rewrite the whole QuickSearchBox software from Kotlin, we imagine it could be doable to attain even better measurement discount by higher using Kotlin’s terse nature.

We noticed a reasonably vital improve in APK measurement, nevertheless this improve might be attributed to the brand new inclusion of the kotlinx-coroutines and androidx.core libraries on this venture. The androidx.core library was added to switch a number of deprecated perform calls with the beneficial overrides for QuickSearchBox to focus on the most recent API stage, 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 imagine this improve 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. The truth is, 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 venture. Conversion fee elevated vastly with familiarity with Git/Repo, Kotlin, and customary bugs present in Android Studio’s Kotlin software. Midway by means of the venture, the method of changing the file, fixing bugs, formatting, and sustaining Git historical past was extremely environment friendly, with as much as 10 information migrated per day. In the end, this venture 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 instance for different builders trying to port their very own functions.
I hope the article roughly Migrating the AOSP QuickSearchBox App to Kotlin | by Android Builders | Android Builders | Sep, 2022 provides perspicacity to you and is helpful for toting as much as your data
Migrating the AOSP QuickSearchBox App to Kotlin | by Android Developers | Android Developers | Sep, 2022