Getting started with Google’s ML Kit in 2023 using Android Studio Kotlin

Waqar Mumtaz
5 min readDec 16, 2022

--

The purpose of this step-by-step tutorial is to help you get started with the ML Kit provided by Google through a Kotlin-based Android Studio project. If you have an existing project, this article can help you integrate the ML Kit into that project as well. All ML Kit APIs require Android API level 19 or higher. So, make sure that your app’s build file uses a minSdkVersion value of 19 or higher [1].

What is ML Kit:

Machine Learning Kit (ML Kit) is a powerful yet intelligent SDK which provides various machine learning expertise to the developer without having a deeper knowledge of how machine learning works. Various components of the ML Kit like Face Detection, Text Recognition, Object Detection, Translation, and Smart Reply will help you add Artificial Intelligence to your Android and iOS apps. All the solutions provided by ML Kit are optimized and ready to use on the end device without the assistance of any remote machine.

The Gems of ML Kit:

ML Kit provides solutions under two major API categories. One group of APIs are categorized as the Vision APIs and the other ones are Natural Language APIs [2]. Google has added some new APIs recently to the ML Kit solutions. The complete list of the APIs provided by Vision and Natural Language Processing is as follows by December 2022.

Vision APIs:

  • Barcode Scanning
  • Face Detection
  • Face Mesh Detection
  • Text Recognition
  • Image Labeling
  • Object Detection and Tracking
  • Digital Ink Recognition
  • Pose Detection
  • Selfie Segmentation

Natural Language APIs:

  • Language Identification
  • Translation
  • Smart Reply
  • Entity Extraction

Let’s Create a New Project:

Enough introduction, Right? Let’s create a new Android Studio Project having ML Kit integrated within it.

  • Open the Android Studio and select ‘New Project’ placed at the top right of the Window.
  • Select ‘No Activity’ if you don’t know much about the other options and click ‘Next’.
Activity Selection
  • Write the name of your project and optionally change the Package name. Here I have chosen Kotlin as a Language and the Minimum SDK is API 28 by default. As I already told you that your Minimum SDK level must be 19 if you plan to use ML Kit in your project.
Choose Project Name and Minimum SDK Level
  • Don’t worry if Android Studio starts to download some required components like Android SDK Platform and Build-Tools before starting the newly created project.
  • Once the project starts, it may optionally download the required version of the gradle-plugin and update the indexes.
  • Partially Congratulations as you have passed Milestone 1. 😃

It’s Time to Add the ML Kit to your App:

ML Kit solutions are based on the Mchine Learning Models trained to perform some specific job like detecting a face within an image. ML Kit provides two options for integration called bundled or unbundled. In a bundled way, the model file is downloaded at build time and will be part of your application. And the other option is to just put in some configurations and the model file will be downloaded when the user will try to use the app for the first time. The following comparison chart will help you summarize the discussion [1].

Bundled Vs. Unbundled ML Kit

If the app size is not a major issue in your scenario then you can consider the bundled way of ML Kit integration. We will learn both types of integrations one by one.

Prerequisites for any Type of Integration:

This API requires Android API level 19 or above. Make sure that your app’s build file under Gradle Scripts section uses a minSdkVersion value of 19 or higher.

Bundled ML Kit Integration:

Just observe the image above, there are two build.gradle files. One is Project level and the other is a Module/App-level build file under Gradle Scripts. Add the dependencies for the ML Kit Android libraries to your module’s app-level gradle file, which is usually app/build.gradle.

For bundling the model with your app:

dependencies {
// ...
// Use this dependency to bundle the model with your app
implementation 'com.google.mlkit:face-detection:16.1.5'
}

Unbundled ML Kit Integration:

To add ML Kit in an unbundled way, you have to follow the following two steps.

  1. For using the model in Google Play Services:
dependencies {
// ...
// Use this dependency to use the dynamically downloaded model in Google Play Services
implementation 'com.google.android.gms:play-services-mlkit-facedetection:17.1.0'
}

2. If you choose to use the model in Google Play Services, you can configure your app to automatically download the model to the device after your app is installed from the Play Store. To do so, add the following declaration to your app’s AndroidManifest.xml file:

<application ...>
...
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="face" >
<!-- To use multiple models: android:value="face,model2,model3" -->
</application>
Changes in AndroidManifest.xml

You can also explicitly check the model availability and request download through Google Play services ModuleInstallClient API [3]. This part is the out of the scope of this tutorial.

If you don’t enable install-time model downloads or request explicit download, the model is downloaded the first time you run the detector. Requests you make before the download has been completed will produce no results.

Conclusion:

Are we forgetting something? Oh, that’s the Full Congratulations. Finally, you have a template project which can be used to start using ML Kit Face Detection API. Here I want to inform you that you can similarly use any ML Kit API. You can also use more than one API in the same app. For this, you only have to add related API dependency in app/build.gradle file.

References:

[1] “Detect faces with ML Kit on Android,” Google Developers. https://developers.google.com/ml-kit/vision/face-detection/android (accessed Dec. 09, 2022).

[2] “ML Kit | Google Developers.” https://developers.google.com/ml-kit (accessed Dec. 09, 2022).

[3] “Ensuring API availability with ModuleInstallClient | Google Play services,” Google Developers. https://developers.google.com/android/guides/module-install-apis (accessed Dec. 10, 2022).

--

--