3

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. Change android:name=".MainActivity" into android:name="com.name.app.MainActivity" in the activity 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 the MultiDexApplication to my manifest. My MainActivity extends AppCompatActivity (and thus it does not extend Application).

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.

Abby
  • 1,610
  • 3
  • 19
  • 35
  • is your mainactivity class is kotlin class ? – Nirav Bhavsar Jul 09 '18 at 12:28
  • hope your **com.name.app.MainActivity** is already defined in Manifest.xml – Sreehari Jul 09 '18 at 12:30
  • @NiravBhavsar No, my MainActivity is a Java class. I have other Kotlin classes. – Abby Jul 09 '18 at 13:27
  • @Sreehari Yes, of course it is. – Abby Jul 09 '18 at 13:30
  • Where does that `7866bT-i2U8fT-8nEVVFCA==` come from? – OneCricketeer Jul 09 '18 at 13:33
  • @cricket_007 I honestly haven't got the slightest idea... I haven't been able to find the `/data` directory, either. – Abby Jul 09 '18 at 13:41
  • That directory is on the device itself. Are you installing to an emulator or a physical device? Is this a brand new project or previous one? Has the app ever installed correctly? – OneCricketeer Jul 09 '18 at 13:45
  • 1
    @cricket_007 That was on an emulator. Just ran it on a physical device, and that path makes much more sense. I'll edit the question to avoid confusion. This is not a brand new project, it's a previous one that I'm improving (about a year later then when it was first 'finished') and slowly converting the Java to Kotlin. We have upgraded Gradle to 4.7 recently, so maybe that has something to do with it (although I remember it working fine with that). – Abby Jul 09 '18 at 14:04
  • Did you rename the package name at any point, or is this just a typo? `com.nane.app/com.name.app` – OneCricketeer Jul 09 '18 at 14:07
  • @cricket_007 Just a typo. These are dummy names. – Abby Jul 09 '18 at 14:08

1 Answers1

1

The structure of my folders was

.gradle/
.idea/
app/
    build/
    src/main
        gen/
        java/
        res/
build/
gradle/

The gen folder should not be here. This folder contained generated files, like R.java. These files were also (however correctly) in app/build/generated/source/*. Simply deleting the gen folder and cleaning the project solved the problem.

I have no idea how that gen folder ended up there, as I removed the project from my disk and cloned the git repository (that does not have this folder) several times. As soon as I find out how it got there, I will update this answer.

Abby
  • 1,610
  • 3
  • 19
  • 35