Problems encountered when developing apps with Android 14

9.3k Views Asked by At

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?

2

There are 2 best solutions below

2
AllanRibas On BEST ANSWER

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

6
bilabila On

I have the same error when testing my app on android 14 emulator. DexClassLoader is only allowed to use a readonly dex file. So I add File("path/to/abc.dex").setReadOnly() before DexClassLoader to solve my issue.

From your log ../code_cache/.../classes2.dex, some kind of dex loading probably used in your code or dependencies. So you should make these dex files readonly before they are loaded.