When running my app I get the following error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.name.app/com.name.app.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class "com.name.app.MainActivity" on path:
DexPathList[[zip file "/data/app/com.name.app-2/base.apk"],
nativeLibraryDirectories=[/vendor/lib, /system/lib]]
I have used the Android Studio apk analyzer (Build > Analyze APK) to analyse my apk (app/build/outputs/apk/debug/app-debug.apk) and the structure of the classes.dex
is
java/
lang/*
util/*
io/*
com/google/devtools/build/...
I noticed that my own classes, and thus the MainActivity, are missing. For as far as I understand dex files (from this SO question) my own classes should be there, too. As they are not there, I see why I get the error that my class is not found.
I get this error on my own device: Samsung, Lollipop 5.0.2, API 21, and on a virtual device: Google Pixel, Oreo 8.0.0, API 26.
What I have tried
I have googled and found multiple suggestions, but none of them worked for me. These suggestions were:
Disable instant run. File > Settings > Build, Execution, Deployment > Instant Run > uncheck instant run.
Clean the project. Build > Clean. Then rebuild the project, Build > Rebuild.
Invalidate Caches/Restart.
Uninstall the application from the device and try again.
Use different path naming in the
Manifest
. Changeandroid:name=".MainActivity"
intoandroid:name="com.name.app.MainActivity"
in theactivity
tag.Disable pre-dexing, as in this answer.
Added the following to build.gradle
dexOptions {
preDexLibraries false
}
- Enable multidex, followed this link. First enabled multidex in
build.gradle
, and then added theMultiDexApplication
to my manifest. MyMainActivity
extendsAppCompatActivity
(and thus it does not extendApplication
).
Enabling multidex gives a similar error:
java.lang.RuntimeException: Unable to instantiate application android.support.multidex.MultiDexApplication:
java.lang.ClassNotFoundException: Didn't find class "android.support.multidex.MultiDexApplication" on path:
DexPathList[...]
Below are the additions to enable multidex.
build.gradle
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 26
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
manifest (just the relevant part)
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
Remark There was another suggestion related to libraries, see this answer. However, this was targeted at Eclipse and I'm not sure if it could be a solution to my problem. Gradle should deal with all the dependencies, right? Just in case, here is how I listed all my dependencies in Gradle (Kotlin version 1.2.51):
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation 'org.junit.platform:junit-platform-engine:1.2.0'
testImplementation 'org.jetbrains.spek:spek-api:1.1.5'
testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
}
Update
I'm using Android Studio 3.1.3. I have tried to run my app using another laptop (via version control, using Android Studio 3.1.3) and there the error does not appear. As the files are exactly the same, I suspect there is something wrong with my Android Studio setup.
Removing Android Studio and all its settings, and then downloading and installing Android Studio from here did not solve the problem.