Build an APK Extractor Android App using Kotlin

Rajdeep Singh
MindOrks
Published in
5 min readAug 20, 2018

An APK Extractor app lets you manage the apps in your phone, providing you with features to extract the apk out of any app, share the apk using other apps and uninstall the apps from your phone. In this tutorial we will be going hands on to build the app using Kotlin.

Prerequisites: Basic knowledge of android app development, Kotlin and willpower to handle me till the end of this article.

So let’s dive right in and start building the app. We’ll learn the necessary topics on the go.

As our app will extract APKs so lets call it APK Extractor (Such creativity, much wow ¯\_(ツ)_/¯).

You can find the source code for this app here -https://github.com/rajdeep1008/ApkExtractor

Creating the project

Create a new android studio project with the name APK Extractor. Select the Kotlin support checkbox -> Select desired API level -> Select Empty Activity and finish. This will create a basic android studio project with MainActivity.kt and activity_main.xml files.

Adding the dependencies

Open up the app level build.gradle file and add the following dependencies :

We will be using the anko library to make our asynctask work easier so as to load the apk details on a background thread. Apache io commons will be used to ease some file related functions, and design and cardview support library are for UI purposes.

Configuring the Manifest file

To save our extracted apk, we are going to need WRITE_EXTERNAL_STORAGE permission. So let’s add it quickly :

We are going to request this permission at runtime too as this falls into the dangerous permissions list starting from Android 6.0 and higher.

Creating the User Interface

Let’s start by creating the user interface for our app. Our app will consist of a simple list showing all the apps currently installed in our phone with 5 options:

  1. Extract the apk from the app to a specified directory on our phone.
  2. Share the apk using other apps or save it to dropbox, google drive, etc.
  3. Uninstall the app.
  4. Open the app in playstore.
  5. Launch the app.

This is what our app will look like.

To show our installed apps on screen, we are going to use a Recyclerview in a Relative layout along with a Progressbar to show loading icon while the APKs are being fetched.

Our activity_main.xml looks like this now :

Next, we are going to define how a single item in our Recyclerview will look like. Create a xml file and name it layout_apk_item.xml.

Writing the Kotlin part

Now with our UI parts set up, let’s start with the coding part.

First of all, make a new package named models and add a Kotlin file called Apk.kt. This is a data class and works like a POJO(Plain Old Java Object) to hold the required information about our apk object. In case you are unfamiliar with data classes in Kotlin, have a look here.

It has a constructor with 4 parameters, appInfo of type ApplicationInfo which consist of all the details about each of the application installed, while appName, packageName and version are Strings to ease up our work so we don’t have to extract them again and again.

Next up is our adapter class for recyclerview. Create a package with the name adapter and create ApkListAdapter.kt class which extends RecyclerView.Adapter and accept 2 parameters-i.e., an arraylist of our Apk objects and a context. It consists of an interface which our MainActivity will implement so as to get the click callbacks with the correct package name.

onCreateViewHolder and onBindViewHolder methods create and set up viewholders for each of our item in the arraylist and inflates our earlier created xml for item layout.

Our adapter also holds an inner class ApkListViewHolder which extends from RecyclerView.ViewHolder. It binds up the click listeners for our extract, share, uninstall and menu buttons.

The functions to extract the apk and request permissions at runtime are defined in a class called Utilities.kt in a package called extras.

The checkPermission method will handle the permissions for writing to external storage and alert the user in case the user denies it. makeAppDir method will make a directory with the name “ApkExtractor” in the external storage. extractApk method creates a File object from the sourceDir variable of ApplicationInfo in our Apk object and copy the apk file to our newly created app directory from where the users can easily find all the extracted apks. getShareableIntent method creates an Intent with the extracted apk URI and handles the Action and Extras part.

Just hang on a little more, we are almost done with the app. Just one last file called MainActivity which uses all the previously created Kotlin files. MainActivity implements the ApkListAdapter.OnContextItemClickListener which we created earlier to handle the clicked item.

In the onCreate method, we set up the recyclerview, progressbar and our ApkListAdapter. Utilities.checkPermission() method is also called in the onCreate to ask the user for necessary permissions.

The method loadApk() consists of the main functionality of our app. We call getInstalledPackages on the package manager to get the list of installed apps and put them up in out Apk arraylist. All this is done on the background thread using the doAsync from Anko library. Once the process is done, we notify the adapter and hide the progressbar.

With this, your Apk extractor app is ready. Of course this app can be improved a lot by adding features such as automatically refreshing the apk list after uninstalling any app or dividing the list into system and user installed apps and so on. Feel free to work upon the code and create amazing apps out of it.

You can find a better version on which i am working currently here.

Thanks for reading this article. Be sure to clap and share this article if you found it helpful. It would let others get this article and spread the knowledge.

Let’s become friends on Twitter, Linkedin, Github, and Facebook.

By the way, i am starting with a weekly newsletter thedevweekly where i will handpick articles across web, mobile and systems and will balance out articles about learning new technologies as well as learning insides of the tech stack from some of the biggest tech companies.

So, if you are a beginner dev or an experienced one and looking for a weekly digest of nicely curated tech articles, sign up here.

--

--