0

I'm pretty baffled by the ANR I'm getting from my application as I don't understand how it could happen.

I've got mutliple ANR for these codes:

  1. File(applicationContext.filesDir).mkdirs()
  2. File(applicationContext.filesDir).exists()

and I get the following ANR report:

1.

main (native): tid=1 systid=30195 
#00 pc 0xc57c8 libc.so 
#01 pc 0x21580 libopenjdk.so 
       at java.io.UnixFileSystem.createDirectory0(UnixFileSystem.java)
       at java.io.UnixFileSystem.createDirectory(UnixFileSystem.java:354)
       at java.io.File.mkdir(File.java:1325)
       at java.io.File.mkdirs(File.java:1352)
#01 pc 0x21fc0 libjavacore.so 
       at libcore.io.Linux.access(Linux.java)
       at libcore.io.ForwardingOs.access(ForwardingOs.java:131)
       at libcore.io.BlockGuardOs.access(BlockGuardOs.java:76)
       at libcore.io.ForwardingOs.access(ForwardingOs.java:131)
       at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8068)
       at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281)
       at java.io.File.exists(File.java:813)

My application targets from Android 5 to Android 12 and only Android 11 and Android 12 are getting these ANRs.

Do you guys have any idea how to solve this ? Should I File(applicationContext.filesDir).mkdirs() on a different an IO Thread to avoid blocking ?

Biscuit
  • 4,840
  • 4
  • 26
  • 54
  • Java or Kotlin? – blackapps May 30 '22 at 16:50
  • Kotlin, but does this make a difference ? – Biscuit May 30 '22 at 17:36
  • Ofcourse. For Java your statements would not work. – blackapps May 30 '22 at 18:33
  • You better could tell actual path instead of what you wrote now. Come to the point. And why not tell what an ANR would be. – blackapps May 30 '22 at 18:34
  • @blackapps an ANR is Application Not Responding, i.e. that popup you get when you block the main thread for too long - the developer console on Google Play tracks them. And Biscuit, you might want to post your actual code - those calls are normal (and not something you should *need* to do on another thread) so if they're taking multiple seconds, it might be something to do with the path you're using, or whether you have permission to write there. Android 11 forced the requirement to use Scoped Storage, and had some earlier issues too: https://blog.mikepenz.dev/a/android-11-ext-storage/ – cactustictacs May 30 '22 at 19:27
  • I would get a compile error and not an ANR if my statement was not working for the language. The actual path is irrelevant as it is simply `applicationContext.filesDir` I'm updating post – Biscuit May 30 '22 at 20:01
  • It is not a permission issue as stated in https://developer.android.com/training/data-storage you do not need permission to access your own app folder. – Biscuit May 30 '22 at 20:02

1 Answers1

0

You should definitely perform all file operations off the main thread, regardless of what else is happening. It's definitely not a permissions issue -- if it were, you would just get a permission denial, not an ANR.

Having said that, you generally wouldn't get an ANR from just checking your app's fileDir, even on the main thread. But my guess is that wherever this AND came from, either the device's internal storage is really slow, or it's likely that your app was moved to external storage. Checking external storage availability takes longer.

Either way, as I said earlier, it doesn't actually matter why it's happening. You should just be performing all the file IO on a separate thread

user496854
  • 6,461
  • 10
  • 47
  • 84