Problems encountered when developing apps with Android 14
Asked Answered
S

4

16

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?

Sorry answered 17/6, 2023 at 23:32 Comment(14)
Maybe try using MultiDex?Aspirate
why did you tag android-14? is this something specifically related to that OS?Huddersfield
@Huddersfield Because the problem I am having is only on Android 14. Earlier Android versions work fine. Android 14 hasn't been released until August this year.Sorry
@WeiWen : Are you using Dynamic Code Loading (DCL) in your project or can you get info if the libraries are using this feature ?Niles
@Niles I didn't use DCL. And I don't think any of the libraries that I included uses DCL. All of them are Android or google libraries, like navigation, lifecycle, material, etc.Sorry
Try turning off “build automatically “ in settings. slack-chats.kotlinlang.org/t/518884/…Niles
Did you ever get to the bottom of this? I've been seeing it non-deterministically. The call chain seems to originate from native android OS code.Phoneme
@Phoneme No. I haven't found a solution. I am not actively developing any app on Android 14, just want to see what features there are. I am hoping google will find out the issue and fix it.Sorry
The issue seems to not be reproducible for me anymore without updating any of the libs I was using so the issue may have been resolved on the Android side.Phoneme
Not sure if you have resolved the issue but in my case the issue was that I needed to update to java 11 and update the dependencies versions to newer ones.Champagne
@Champagne I updated Source Compatibility and Target Compatibility to version 11, and updated all the libraries to the latest. But still, even if I just add a print in code, I still get "Writable dex file is not allowed error".Sorry
same issue here, It's getting to the point that I feel I must update one dependency at a time and perform 3 hours of troubleshooting.Heterosexuality
I have same problem, but it's quite strange. If I made some changes in xml/layouts or when I clear cache of app then suddenly I can install app without that problem. WTF is going onHackery
I still have this problem with google game servicesAccouter
H
9

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

Hom answered 29/9, 2023 at 3:21 Comment(4)
This does not appear to fix the issue when the latest official play ads library writes a .jar file into /cacheLapel
Upgrade Android Studio version Android Studio Hedgehog | 2023.1.1 Patch 2 and this should be resolved.Norvall
In Java, adding this in your onCreate() method fixes the issue: getCodeCacheDir().setReadOnly();Metallic
Didn't work for me, while using AS Electric Eel | 2022.1.1 Patch 2Simpkins
S
2

If you just upgraded your app to use targetSdk 34, did a clean-project and a full invalidate cache, you might end-up with this error trying to launch the app in emulator or real device.

Tried other suggestions to no avail. Did uninstall/relaunch the app and problem solved.

I previously added the getCodeCacheDir().setReadOnly() to app's constructor to no avail and did not remove it after uninstall/launch, but I doubt it's of any use.

Simpkins answered 30/7 at 9:37 Comment(0)
C
1

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.

Colewort answered 6/9, 2023 at 9:44 Comment(7)
where do I add the code: File("...").setReadOnly()?Sorry
before where you use DexClassLoader or just at Application / Activity onCreate.Colewort
I updated Android Studio. When I had the problem, I used Android Studio Chipmunk 2021.2.1. Now I updated to Android Studio Giraffe 2022.3.1 and I no longer have the problem. Maybe after I installed the old Android Studio version back, I will test your suggested solution.Sorry
Seeing a similar error when building with Iguana 2023.2.1 - the error appears in the pre-launch report and the app is apparently writing a jar to the app's /cache folder. No idea what that is about as no jar is being explicitly written by the app but may be written by com.google.android.gms.policy_ads_fdr_dynamite (?!)Lapel
@Colewort I tested adding your suggested code, but I still got "Writable dex file is not allowed" error. I tried to add the code in onCreate method of both the activity and the application classes. But neither one worked. The path I tried is something like this: ../code_cache/.overlay/base.apk/classes2.dex, assuming the full path that Android Studio pointed to is: /data/data/com.example.testandroid14/code_cache/.overlay/base.apk/classes2.dexSorry
did anyone solved it?Dachi
After adding the setReadOnly which didn't help, I simply uninstalled/reinstalled the app and issue was gone!Simpkins
H
1

For me updating Android Studio to the newest version fixed a problem.

Hackery answered 8/7 at 11:21 Comment(1)
To ensure this answer is useful to readers in the future, please edit this to note what version of Android Studio you were using (i.e., which exhibited this behavior) and what version you upgraded to (i.e., which solved this behavior). Thank youPebrook

© 2022 - 2024 — McMap. All rights reserved.