Android application ClassNotFoundException after d

2019-08-04 11:59发布

问题:

I have an android app with a custom Application class.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Utils.isThereUpdatedVersion(this);
    }
}

The isThereUpdatedVersion function asks a php page is. There is a new version of my app available and there is a button one goes to another activity with a button to download an install the new version.

I added the android:name attribute to my Manifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".application.MyApplication">

When I run my app through Android Studio, the app is running perfectly. Is there is a new version i can download it and install it. But after the installation, I open the app and I get this error and the app crashes.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.andre_jacq_ing.preleveurgmail, PID: 2915
    java.lang.RuntimeException: Unable to instantiate application com.andre_jacq_ing.preleveurgmail.application.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.andre_jacq_ing.preleveurgmail.application.MyApplication" on path: DexPathList[[zip file "/data/app/com.andre_jacq_ing.preleveurgmail-1/base.apk"],nativeLibraryDirectories=[/data/app/com.andre_jacq_ing.preleveurgmail-1/lib/arm64, /system/lib64, /vendor/lib64]]
        at android.app.LoadedApk.makeApplication(LoadedApk.java:839)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5855)
        at android.app.ActivityThread.-wrap3(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6688)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.andre_jacq_ing.preleveurgmail.application.MyApplication" on path: DexPathList[[zip file "/data/app/com.andre_jacq_ing.preleveurgmail-1/base.apk"],nativeLibraryDirectories=[/data/app/com.andre_jacq_ing.preleveurgmail-1/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1000)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:828)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5855) 
        at android.app.ActivityThread.-wrap3(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6688) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

Can someone help my with this one ?

Edit

I am calling an URL on my server that redirect me to my .apk and i use this code :

URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            int lenghtOfFile = c.getContentLength();

            String PATH = "/mnt/sdcard/Download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "app.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int count;
            long total = 0;
            while ((count = is.read(buffer)) != -1) {
                total += count;
                fos.write(buffer, 0, count);
                publishProgress((int) (total * 100 / lenghtOfFile));
            }
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(intent);

回答1:

Well, actually it was because of Android Studio's Instant Run. I just disable it and it worked like a charm.

Despite all thank's for your help.



回答2:

If your package name is correct then try to clean your project and also uninstall the app. Then reinstall the app.