Search code examples
androiddexandroid-securityexceptionandroid-14

Problems encountered when developing apps with Android 14


I found that after my app reached a fair size (e.g. by adding multiple libraries), running the app threw java.lang.SecurityException: writable dex file '.../code_cache/.overlay/base.apk/classes2.dex' is not allowed.

If I then remove most of the libraries leaving only those that were added by default, and run again, it could work. But then if I add a tiny bit of code, like a log, it could fail with the same error.

If I want it to run without this error, I have to uninstall the app and then run again from Android Studio. This is very inconvenient, because every time I make some changes, I have to uninstall the app. I wouldn't imagine anyone would like to develop Android apps like this.

Does anyone know a solution to this problem?


Solution

  • I was also having this problem, so I took a look at this documentation DexClassLoader, and decided to do this.

    package com.example
    
    import android.app.Application
    
    class BaseApp : Application() {
        override fun onCreate() {
            super.onCreate()
             val dexOutputDir: File = codeCacheDir
             dexOutputDir.setReadOnly()
        }
    }
    

    Just putting dexOutputDir.setReadOnly() in my application solved the problem

    Uninstall and reinstall the app again