Class Not found Android 4.4

Korir Amos
3 min readSep 18, 2019

--

When building android application, we always try to support the early versions of android .Firebase test lab helps to quickly run emulated test in various phone models, providing screenshots for each phone model and test results. 🚀 We still use manual testing approach with real actual phones. There are some difference between an actual device and an emulated environment considering how the applications runs in these enviroments, among the test phones used is Android 4.4, this is to avoid “it works in my phone” 🙈 which is android version 8 or 9. 😆

In many occasion the application would run well in the new android version but fails with crushes or might not even open in early version. To manage this we either not support them at all, have legacy code (implementation) or disabling certain features in the earlier versions. But there is need to maintain same user experience for all the app users without losing even a single user by supporting their old phones 👍

#class not found

This is a common error when the app code size increases, this is as a result of using heavy libraries such as firebase libraries. When the methods count goes over 64k ,build will fail with different errors depending on the android version. This has been documented in here https://developer.android.com/studio/run/emulator-commandline.html

Physical testing with Android 4.4 with a small memory space.

To solve this you have to add multidex dependency in the gradle file and set multiDexEnabled to true as in below snippet.

android {    
defaultConfig {
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}

Within the application class, this can be solved in two ways either have the class extend MultiDexApplication or extend Application and override the “attachBaseContex ” method and install multidex.

option 1

public class MyApplication extends MultiDexApplication{
//other methods here
}

Option 2

public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}

Manifest

Add the application class name in the manifest file. Missing this step will result in the same error so check to ensure the application name and the package is correct.

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

There are many question and solution in stack overflow that are related to this error but it took me a while to get it working. The above steps had already been implemented but the application was still crushing on launch with the error “the application class could not instantiated and the application class could not be found”. 😶 Tried updating all library dependencies but nothing.

All these were happening in debug mode apk, After studying the logs for some time; “the application class could be instantiated” and “the class loader could not find Application class” it gave me an hint that something was wrong with the build process. I made a release apk and it ran success on the phone but the debug version still failed.

At last it worked 😡 😡
By just cleaning the project and making the project. 😆

Summary
To solve error of the same kind:

  1. Ensure multidex is setup properly
  2. Try to invalidate cache and restart.
  3. Clean and rebuild the project

#debug&&build

--

--

Korir Amos

Senior Android Engineer- Mobile Application Developer