How To Build An Android APK Explorer App - Hive Programmers

avatar

Greetings to my favorite science community online, StemSocial.

It's @skyehi and I'm truly grateful to be back to continue my series on Android Programming tutorials for beginners.

I had a few engagements earlier today which is why I'm making this post a bit late but I knew I didn't want to miss a day continuing this fun series.

We have really advanced in our App development skills and for today's blog I wanted to introduce you to a few new Android programming concepts by building a very interesting App.

Original Image Source by lmonk72 from Pixabay

For today's tutorial we're going to be building An Android APK Explorer. For those of you that have been wondering exactly what APK stands for, well it stands for Android Package or Android Package Kit.

It's more like the file format that is used by Android in distributing Android applications. So as a programmer, if you want to share your finished app on Google play store or directly to friends, you would need to convert all your source code into an apk file.

However nowadays things are changing a little bit. Google play store receives Android app publications in aab format instead of APK. AAB stands for Android App Bundle and it's a more modern file format for Android applications.

For today's blog, we'll be focused on building an APK Explorer App.

Creating Our Android Studio Project

Of course guys before you can starting developing Android Apps on your computer, you would need to make sure that Android Studio and Java Development kit are installed. If you need any assistance installing the softwares, please let me know in the comments section below.

Now at this point I'll assume all my readers have successfully installed Android Studio on their computers.

We would want to create a new project. Open the Android Studio software and select "Start a new Android Studio project," .

Now guys you will be directed to a page where you can write the name of your app. I'll call my project "APK Explorer" . You can name it anyhow you want it. After you're satisfied with the name, also ensure that you select Java as the programming language.

There are two main programming languages we could use to build our app. We could either use Java or Kotlin. We'll be using Java because it's the older and more basic language and it's still widely used. Although Kotlin is the more modern language which would reduce bugs in your Android app.

As we keep making progress in our tutorial, we'll switch things up and start using Kotlin as well.

Now guys for the app template, I'll recommend that you choose an "Empty Activity" to make things simple.

You'll also be given the option to set the project package name. When you're through with all of that, you can click "Finish" to create your project.

Our User Interface Design

We have successfully set-up our project and the first thing to work on is the user interface of our application.

The design layout will be created in the (activity_main.xml) page. This is the part of our App that the user will see and interact with.

We'll start with a basic interface and as we make more progress with this series, we'll rebuild the project in Kotlin language with a more advanced design layout.

So to design the app, open the res/layout/activity_main.xml file and write your design code there.

For a very simple APK Explorer, we would only use a TextView for displaying information and a Button to trigger the file selection.

Here's how your code should look like


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="APK Explorer"

        android:textSize="24sp"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="16dp"/>


    <Button

        android:id="@+id/selectButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Select APK"

        android:layout_below="@+id/textView"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="16dp"/>

</RelativeLayout>

Handling the User Interaction

Since we're though with the design of the user interface, it's now time for our backend code. In this step, we'll write the code to give functionality to the button we created in our activity_main.xmlpage.

This code to handle Button Click will be written in (MainActivity.java) page.

So guys, open the MainActivity.java file and add the following code below to handle the button click and open a file picker.

Here's the code to add and remember that misspelling any of the codes could bring an error. We may not be handling errors in this particular tutorial so please write it accurately and have fun


import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;


public class MainActivity extends Activity {


    private static final int PICK_APK_REQUEST = 1;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        Button selectButton = findViewById(R.id.selectButton);

        selectButton.setOnClickListener(v -> openFilePicker());

    }


    private void openFilePicker() {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

        intent.setType("*/*");

        startActivityForResult(intent, PICK_APK_REQUEST);

    }

}

Handling the Result

We have successfully written the code for the button we created to work so it's time to handle the results from the file picker and also display the selected file's path. This step will also be done in MainActivity.java

Here's how the code should look like.


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == PICK_APK_REQUEST && resultCode == RESULT_OK && data != null) {

        Uri apkUri = data.getData();

        if (apkUri != null) {

            String apkPath = apkUri.getPath();

            TextView textView = findViewById(R.id.textView);

            textView.setText("Selected APK: " + apkPath);

        }

    }

}

Getting Storage Permissions

Our app needs to access the External storage of the Android device to be able to find all the APKs stored. This means that we would need to write the code that gives our app permission to read the External storage of the Android device.

If you've been following my tutorials, you would already be able to guess where we'll be writing the permission code. It will be written in AndroidManifest.xml

Here's how our storage permission code should look like.


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

It's Time Run Our App

Congratulations guys, we're done writing the code for our app, it's time to run the application. As always you have two choices. You can either run the project on an emulator or a physical device.

In future tutorials, I'll share the links to some of the Android emulators if you want to use them but I personally prefer using my Android Device.

When the App opens on your phone or emulator, click the "Select APK" button, choose an APK file from the file picker, and observe the displayed path.

That's it guys, we have built and run our Android APK Explorer. As we keep developing more Apps, you'll get better and soon become an expert Android app developer.


Thank you so much for taking the time to read today's blog. I hope you enjoyed this tutorial. As always, if you're having any trouble writing the code or running the app, feel free to share that in the comments section and I'll be more than happy to respond.

Have a lovely day and catch you next time on StemSocial. Goodbye 👨‍💻


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
1 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000