-1

I am trying to run an android project on an older device with Android 18. The project works fine on Android >21, but on Android 18 I am getting the following error:

03-11 01:22:31.147 2343-2608/? E/Watchdog: !@Sync 466 03-11 01:23:01.152 2343-2608/? E/Watchdog: !@Sync 467 03-11 01:23:16.602 24016-24016/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.MainActivity" on path: /data/app/com.example-1.apk at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) at android.app.ActivityThread.access$700(ActivityThread.java:159) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MainActivity" on path: /data/app/com.example-1.apk at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64) at java.lang.ClassLoader.loadClass(ClassLoader.java:501) at java.lang.ClassLoader.loadClass(ClassLoader.java:461) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)  at android.app.ActivityThread.access$700(ActivityThread.java:159)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:176)  at android.app.ActivityThread.main(ActivityThread.java:5419)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:525)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)  at dalvik.system.NativeStart.main(Native Method)  03-11 01:23:17.052 2343-2441/? E/android.os.Debug: !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error 03-11 01:23:23.087 2698-2698/? E/Launcher: Error finding setting, default accessibility to not found: accessibility_enabled 03-11 01:23:23.147 2343-2441/? E/ViewRootImpl: sendUserActionEvent() mView == null 03-11 01:23:23.162 2343-4810/? E/EnterpriseContainerManager: ContainerPolicy Service is not yet ready!!! 03-11 01:23:23.377 24110-24132/? E/Babel: canonicalizeMccMnc: invalid mccmnc 03-11 01:23:23.377 24110-24132/? E/Babel: canonicalizeMccMnc: invalid mccmnc nullnull 03-11 01:23:31.152 2343-2608/? E/Watchdog: !@Sync 468

My Android manifest looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Why can the main class not be found? How can I make the app compatible with Android 18?

EDIT 1: I tried addiong multidex capabilities in MainActivity:

public class MainActivity extends AppCompatActivity implements IResults {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
...

And in gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true
    }
...

dependencies {
    // Generic dependencies
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'

    // Support libraries
    compile "com.android.support:appcompat-v7:$supportLibraryVersion"
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
    compile 'com.android.support:multidex:1.0.1'
...

But still no luck. Any other ideas how to solve this?

bear
  • 663
  • 1
  • 14
  • 33
  • 1
    add `android:name="com.example.MainActivity` clean-rebuild and run – IntelliJ Amiya Mar 11 '17 at 06:00
  • So you didn't test on 19 or 20? – OneCricketeer Mar 11 '17 at 06:01
  • 1
    thanks, I tried to rebuild after adding the full package name to android:name, but still the same error. Any other ideas how to solve this @IntelliJAmiya? – bear Mar 11 '17 at 06:11
  • @cricket_007 no but thats irrelevant for my question – bear Mar 11 '17 at 06:14
  • did you update your android studio? – Mehran Zamani Mar 11 '17 at 06:14
  • @MehranZamani well i have the version from a couple months ago, is there a recent update I should be aware of? – bear Mar 11 '17 at 06:15
  • last version is 2.3 but if you update your android studio and then faced this error. you can go to [this page](http://stackoverflow.com/questions/36881299/java-lang-classnotfoundexception-after-android-studio-update). – Mehran Zamani Mar 11 '17 at 06:18
  • you may have [this issue](http://stackoverflow.com/questions/37114768/how-to-resolve-classnotfoundexception-in-android-studio). if you send us complete log, we can help better. – Mehran Zamani Mar 11 '17 at 06:22
  • @MehranZamani I updated my post to the full log output. I updated now to 2.3 and did the suggestions with rebuild etc. in the post you shared, but still getting the same error. Any other ideas? – bear Mar 11 '17 at 06:25
  • Why the downvotes? – bear Mar 11 '17 at 06:27
  • you have `65k methods limit` problem. you can go to last link i sent. – Mehran Zamani Mar 11 '17 at 06:27
  • @MehranZamani I read through the article and adapted my code accordingly (see the edit in my post). Still the same error, any other ideas? – bear Mar 11 '17 at 06:39
  • I'm not sure why you think it is irrelevant. You mentioned 21+ and 18. There were two other Android releases between those and you could identify exactly which one breaks your code, but I would guess it would be anything < 21 – OneCricketeer Mar 11 '17 at 14:25
  • By the way, you duplicated both dependencies `appcompat-v7` and `design` – OneCricketeer Mar 11 '17 at 16:43

1 Answers1

2

You have to use MultidexApplication

<application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

or put MultiDex.install(this); in your Application class

babedev
  • 135
  • 3
  • 5
  • I override the application class (see manifest) so I can't make your first suggestion. I have MultiDex.install(this); in the MainActivity (see bottom of my post), but still getting the same. Any other ideas? – bear Mar 11 '17 at 06:49
  • 1
    Do you mean you have your `Application` class? Have you try put `MultiDex.install(this);` in that `Application`. – babedev Mar 11 '17 at 10:23
  • The error says nothing about DexException. Why is this needed? – OneCricketeer Mar 11 '17 at 14:22
  • ok adding it to the application class solved it. @cricket_007 see comments above about 65k methods limit – bear Mar 11 '17 at 16:40
  • @bear No part of your question says `Unable to execute dex: method ID not in [0, 0xffff]: 65536` – OneCricketeer Mar 11 '17 at 16:43