How to add “Application Entry Point” in Kotlin

Coming from iOS developer perspective, I’m accustomed with the fact that when I create new project, there’s this file/class called AppDelegate automatically created for me. Code inside AppDelegate function didFinishLaunchingWithOptions: is guaranteed to be run ahead first before any other code except global variable initializations. Putting code in the AppDelegate is crucial because you can just put code there without care about which ViewController class gets displayed as root.

When developing Kotlin, I missed this feature. Turns out, it exists, but it doesn’t created for you automatically. You have to make it yourself. Luckily, it’s not hard. Unluckily, if you don’t use it frequently, you may forget about this. When you do forget, sometimes searching on the internet for “Kotlin Application Entry” does not lead you to what you’re searching for. So, here I am, making this short note, to remind me and you visitors about this. Something like main(). But what I’m about to show you is not main(), but something similar.

I assume you already have created a new project for this, in Kotlin. But this is actually can be done in Java too.

1. Create the file

Switch the navigation into Android. Go to your project files, and create a new file. Name it whatever. I named it ApplicationEntry.

2. Create the class

The application entry class must be a descendant of class Application. Don’t forget to make necessary import by pressing CMD+Enter or CTRL+Enter.

import android.app.Application

class ApplicationEntry() : Application() {
    
}

3. Create the initializer block

Using this, you can treat the class like any other class. That means, you can add initializer block on the class, which guaranteed will get called before any other code in your app (well, except global variable initializations, of course).

import android.app.Application

class ApplicationEntry() : Application() {

init {
// put any initialization here
}

}

4. Reference this class in Android Manifest

Without referencing this class in Android Manifest, Android wouldn’t even know that this class need to be run first.

You’ll see something like this on your code editor.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>

Add android:name=".ApplicationEntry" attribute to the application block, so it will looks like this: (make note of the dot sign before the name)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapplication">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:name=".ApplicationEntry"
            android:theme="@style/AppTheme">
        <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

The autocomplete will help you to accomplish this.

So that’s it. I know this is trivia, but if you’re new to Kotlin and Android programming, sometimes your search doesn’t lead you to what you’re searching for. Thus I hope you stumbled upon this article and end your endless quest.

MPAndroidChart Explained in Kotlin

I am new to Kotlin and Android programming, and somehow I have a hard time to find a really basic MPAndroidChart plugin explanation. Finally, I found the tutorial from the plugin writer himself. You can read it here.

But I want to rewrite the tutorial from a different point of view, and hope that it can help you (especially all the Android dev beginners) to understand about the concept in a better flow.

Continue reading