6

I have Kotlin MultiDex application using Dagger 2 for DI. Everything almost fine, but I have device with API 17 (Android 4.2.2), and sometimes application crashes immediately after launch with exception

java.lang.RuntimeException: Unable to instantiate application com.example.android.App: java.lang.ClassNotFoundException: Didn't find class "com.example.android.App" on path: DexPathList[[zip file "/data/app/com.example.android-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.android-1, /vendor/lib, /system/lib]]

Typically, this occurs after the Build -> Project also after File -> Invalidate caches / Restart but not always. Usually Rebuild project helps but I would like to completely fix these situations.

Please tell me what could be the problem?

My build.gradle (not full):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'io.sentry.android.gradle'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.android"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 58
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = "example.apk"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.11"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0"
...

    // LiveData + ViewModel
    implementation "android.arch.lifecycle:extensions:1.1.1"

    implementation "com.android.support:multidex:1.0.3"
    implementation "com.android.support.constraint:constraint-layout:1.1.3"
...
     // Dagger
    implementation "com.google.dagger:dagger:2.19"
    implementation "com.google.dagger:dagger-android-support:2.19"
    kapt "com.google.dagger:dagger-compiler:2.19"
    kapt "com.google.dagger:dagger-android-processor:2.19"
...
}

My AndroidManifest.xml (not full):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.android">

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name=".App"
        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"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity
            android:name=".activities.SplashActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

My application class (App.kt, not full)

class App: DaggerApplication() {

    override fun onCreate() {
        super.onCreate()
    ...
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> =
        DaggerAppComponent.builder().application(this).build()

    }
}
ibogolyubskiy
  • 2,271
  • 3
  • 26
  • 52

0 Answers0