37

My app is working fine when I test in real device using USB in android studio.But When I send the apk to another device and install there,it is not working.App get crashed.I did not find any right way to solve the problem.Errors are given below:

FATAL EXCEPTION: main Process: com.w3xplorers.cmch,PID: 17715                                                                           java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.w3xplorers.cmch/com.w3xplorers.cmch.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.w3xplorers.cmch.MainActivity" on path: DexPathList[[zip file "/data/app/com.w3xplorers.cmch-1/base.apk"],nativeLibraryDirectories=[/data/app/com.w3xplorers.cmch-1/lib/arm, /system/lib, /vendor/lib, system/vendor/lib, system/vendor/lib/egl, system/lib/hw]]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
    at android.app.ActivityThread.access$900(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:168)
    at android.app.ActivityThread.main(ActivityThread.java:5845)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:797)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.w3xplorers.cmch.MainActivity" on path: DexPathList[[zip file "/data/app/com.w3xplorers.cmch-1/base.apk"],nativeLibraryDirectories=[/data/app/com.w3xplorers.cmch-1/lib/arm, /system/lib, /vendor/lib, system/vendor/lib, system/vendor/lib/egl, system/lib/hw]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1085)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2385)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
    at android.app.ActivityThread.access$900(ActivityThread.java:150) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:168) 
    at android.app.ActivityThread.main(ActivityThread.java:5845) 
    at java.lang.reflect.Method.invoke(Native Method) 
    atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)                                                                         Suppressed: java.lang.ClassNotFoundException: com.w3xplorers.cmch.MainActivity
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)                                                                                ... 12 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

My Manifests is below:

`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.w3xplorers.cmch">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />


<application
    android:name="com.w3xplorers.cmch.EnableMultiDex"
    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" >
    <activity
        android:name="com.w3xplorers.cmch.MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.w3xplorers.cmch.HomeActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="com.w3xplorers.cmch.QuickSms"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="com.w3xplorers.cmch.GroupSms"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="com.w3xplorers.cmch.SelfSms"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="com.w3xplorers.cmch.Contact"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="com.w3xplorers.cmch.Favourite"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="com.w3xplorers.cmch.Info"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name="com.w3xplorers.cmch.Dialog" />
</application>

</manifest>`

Gradle below:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.w3xplorers.cmch"
    minSdkVersion 13
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
{
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha5'
compile 'com.android.support:design:25.3.0'
compile 'com.andreabaccega:android-form-edittext:1.0.3'
compile 'com.android.volley:volley:1.0.0'
compile 'com.squareup.okhttp3:okhttp:3.3.1'
compile 'com.android.support:multidex:1.0.1'
testCompile 'junit:junit:4.12'
}
Aminul
  • 1,738
  • 2
  • 24
  • 42
Avijit
  • 687
  • 2
  • 8
  • 18

27 Answers27

18

I had to enable Java 8 Support after updating to Android Studio 3.5

Add the following lines in your build.gradle file:

android {
    defaultConfig {
            ...
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

You can check the documentation here: https://developer.android.com/studio/write/java8-support

Akshay Choudhry
  • 122
  • 1
  • 1
  • 9
Johan Ferreira
  • 515
  • 4
  • 15
  • 1
    I think this is a more relevant answer today. – Akshay Choudhry Feb 11 '20 at 10:32
  • Fixed it for me! – ores May 04 '20 at 19:38
  • As per the [documentation](https://developer.android.com/studio/write/java8-support) I need to include the following dependencies in app gradle ```coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.0'``` . Then upgraded the gradle plugin to below ```classpath 'com.android.tools.build:gradle:7.2.0'``` – Baskar PC Jan 13 '23 at 14:13
9

Go to your project level build.gradle file and decrease the version of gradle dependency to anything lower than 2.3.0, say use 2.2.3

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    ....
}

Then clean -->build -->install debug apk to other device-->it may be OK then!

ridoy
  • 6,274
  • 2
  • 29
  • 60
8
  1. Quit Android studio
  2. Try to delete .idea and .graddle
  3. Restart android studio and rebuild your project

It worked like a charm for me :)

  • 1
    clean and rebuild worked for me. Sometimes you spend hours on the problem when in reality you need just to clean and rebuild it. I need to remember that next time! – DemoDemo Feb 14 '20 at 20:59
7

in your application tag, change

android:name="com.w3xplorers.cmch.EnableMultiDex"

with

android:name="android.support.multidex.MultiDexApplication" 

Hope this helps :)

more information at:

https://developer.android.com/studio/build/multidex.html

6

I think the problem is AndroidStudio is tying to use "Instant Run" instead of compiling and installing a fresh APK.

Disable "Instant Run" from File -> Settings -> Build,Execution, Deployment -> Instant Run.

Hasan Hashem
  • 463
  • 5
  • 10
4

I did not find any perfect solution for my problem.But when I upload my apk on google play and download from there,app is working fine.This is the most weird solution I have ever found.

Avijit
  • 687
  • 2
  • 8
  • 18
3
defaultConfig {

    minSdkVersion 14
    targetSdkVersion 21

    multiDexEnabled true
}

 dependencies {
     compile 'com.android.support:multidex:1.0.1'
 }
  • 1
    While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/43470823/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 18 '17 at 16:19
  • 1
    Works for me after adding this dependency in build.gradle – Badrinath Aug 28 '17 at 21:40
3
android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}



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

Maybe you don't make your application Multidex.install(this);

Analizer
  • 1,594
  • 16
  • 30
tosslife
  • 31
  • 3
3

You have to follow official documentation: https://developer.android.com/studio/build/multidex.html#mdex-gradle

build.gradle:

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

YourApp class

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}
Kirill Vashilo
  • 1,559
  • 1
  • 18
  • 27
2

I also faced similar problem. In my case I solved the problem by adding this below buildTypes

dexOptions {
    javaMaxHeapSize "4g"
}

also used

defaultConfig { 
          multiDexEnabled true
 } 

and

dependencies {
         compile 'com.android.support:multidex:1.0.0'
}
Tamal
  • 200
  • 1
  • 11
2

As you said, you build the app and pushed to your phone through USB, I believe then you copied the apk from your build directory and trying to push and run on other device. Please try to build your apk without targeting or connecting any device or emulator what i mean that build your apk from terminal without connecting your device. Either it will work or it will show something.

Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42
1

In your build.gradle add

defaultConfig { 
          multiDexEnabled true
 } 

Also add a dependency as

dependencies {
         compile 'com.android.support:multidex:1.0.0'}
1

Its may be because you have Instant Run feature enable so just try to Disable Instant Run.

File -> Settings -> Build,Execution, Deployment -> Instant Run -> Uncheck the checkbox for instant run

Nikunj
  • 3,937
  • 19
  • 33
1

Carefully follow this steps:

STEP 1: Add multidex in app level gradle

implementation 'com.android.support:multidex:1.0.3' 

STEP 2: Enable multidex

defaultConfig { 
    multiDexEnabled true 
} 

STEP 3: Last add this code inside application. Pass parameter from attachBaseContext context in MultiDex.install(<<[parameter]) like:

@Override 
protected void attachBaseContext(Context base) { 
    super.attachBaseContext(base); 
    MultiDex.install(base); //not this use base. MultiDex.install(this); this will lead to some error in some prelolipop device.
}
Ngima Sherpa
  • 1,397
  • 1
  • 13
  • 34
1

After you have tried all the answers mentioned here and it still does not work, try File -> Invalidate Caches/ Restart -> Invalidate and Restart.

It worked for me.

Alankrita Shah
  • 101
  • 2
  • 5
1

THIS WILL WORK!

  • Make sure your manifest activity name has the complete path to activity rather than a shortcut of using "." to access the main package.
1

Flutter dev here with same problem.

File android/app/src/main/kotlin/client/MainActivity.kt was missing.

I think this is the only piece of native android code that link the manifest to the flutter engine.

I had to copy paste from a another project, and change package name

package the.idenfier.of.my.app

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {

}
Xmanoux
  • 3,435
  • 4
  • 15
  • 16
0

It seems random to me to resolve this issue. I simply change the android:name to the MultiDex from android, run it, then change it back to my own app.

Pencilcheck
  • 2,664
  • 3
  • 25
  • 14
0

Clear your project and Invalidate caches/ restart in your in your Android studio file section, it works for me.

Shyam
  • 185
  • 2
  • 7
0

To people running into this and using Kotlin for their instrumentation tests:

Keep in mind that spaces in method names work only for JVM tests and not Android Instrumentation Tests (as noted by Jake Wharton, he also said this might be fixed in the future)

At the moment, when I try to run a test like:

@Test
fun `spaces should work in names`() {
}

Test output says:

No tests were found

And the log says:

Started running tests

Test running failed: Instrumentation run failed due to 'Process crashed.'

Empty test suite.

While Logcat outputs this exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example, PID: 5424
    java.lang.RuntimeException: Unable to instantiate instrumentation
    ComponentInfo{com.example.test/android.support.test.runner.AndroidJUnitRunner}:
    java.lang.ClassNotFoundException: Didn't find class
    "android.support.test.runner.AndroidJUnitRunner"
    on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5730)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
    Caused by: java.lang.ClassNotFoundException: Didn't find class
    "android.support.test.runner.AndroidJUnitRunner" on path:
    DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5728)
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

As you can see, the errors are not very helpful here, so I hope people will find this answer now. The solution is simple, use valid names like:

@Test
fun spaces_dontWorkInNames() {
}
arekolek
  • 9,128
  • 3
  • 58
  • 79
0

You can refer to Mohaimanul Chowdhury's answer. If you're using custom Application class change it from extending Application to MultiDexApplication

for example

class YourApplication : Application()

to

class YourApplication : MultiDexApplication()

it fixed my problem

DTechnlogy
  • 320
  • 4
  • 12
0

Change the extended class of you application class

from public class MyApplication extends Application { ... }

to public class MyApplication extends MultiDexApplication { ... }

For more explanation just follow the official documentation: https://developer.android.com/studio/build/multidex.html#mdex-gradle

it works for me

Nappy
  • 3,016
  • 27
  • 39
0

gradlew clean in yourflutterproject/android
works for me

Lewis Weng
  • 942
  • 7
  • 5
0

Check if the Activity is correctly spelled out in the Android manifest

0

In your AndroidManifest.xml file

Rename this:

<activity
        android:name="com.w3xplorers.cmch.MainActivity"

To this:

<activity
            android:name=".MainActivity"
BradG
  • 673
  • 1
  • 14
  • 26
-1

Can you check your MainActivity is written with Kotlin instead of Java?

I disabled Kotlin and write Activity with Kotlin and It caused this error.

MJ Studio
  • 3,947
  • 1
  • 26
  • 37
-1

Firstly,this can happens if you haven't define your activiy in Manifest.xml file. Secondly,if you convert your java class file into kotlin class file and when you run Project,it will leads to didn't find class on path-dexpathlist. So better to keep it in java.