6

I face the error in Crashlytics by a user of my app: Caused by android.content.res.Resources$NotFoundException Unable to find resource ID #0x7f080043 for a Drawable. I checked it in my APK (under debug) and find out that it is related to drawable "abc_spinner_mtrl_am_alpha". I am using this drawable in background for spinner. I do not create this drawable by myself it is from library appcompat-1.4.1. But in this library it is present in multiple densities etc. Does anyone faced a similar problem and know a solution / root cause of it?

Drawable ID

Caused by android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f080043
       at android.content.res.ResourcesImpl.getResourceName(ResourcesImpl.java:253)
       at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:760)
       at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:630)
       at android.content.res.Resources.loadDrawable(Resources.java:886)
       at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:953)
       at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
       at android.content.res.XResources$XTypedArray.getDrawable(XResources.java:1363)
       at android.view.View.<init>(View.java:4768)
       at android.widget.TextView.<init>(TextView.java:826)
       at android.widget.TextView.<init>(TextView.java:820)
       at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:108)
       at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:103)
       at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:201)
       at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:121)
       at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1566)
       at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1617)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:416)
       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
       at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:204)
       at android.widget.Spinner.onMeasure(Spinner.java:602)
       at androidx.appcompat.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:438)
       at android.view.View.measure(View.java:22071)
       at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:811)
       at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:466)
       at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:372)
       at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:120)
       at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1594)
       at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1708)
       at android.view.View.measure(View.java:22071)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
       at android.view.View.measure(View.java:22071)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
       at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:496)
       at android.view.View.measure(View.java:22071)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:22071)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
       at android.view.View.measure(View.java:22071)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
       at android.view.View.measure(View.java:22071)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
       at android.view.Choreographer.doCallbacks(Choreographer.java:723)
       at android.view.Choreographer.doFrame(Choreographer.java:658)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6494)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Sebastian B
  • 113
  • 1
  • 6
  • Does this answer your question? [Android App Bundle introduces Resource Not found crash in Android app](https://stackoverflow.com/questions/50471888/android-app-bundle-introduces-resource-not-found-crash-in-android-app) – George Shalvashvili Nov 25 '22 at 12:38

1 Answers1

8

It might be due to the resource not having a file in the default folder (only in the xxhdpi and ldrtl-xxhdpi folders). The phone on which the app is crashing probably has a different density and the app is deployed/installed as an android app bundle (.aab) which enables google play to remove the resources it deems unnecessary (like this resource which would not be needed for that device given its different density) to reduce bundle size.

A workaround would be to copy that resource into your project (preferably with a different name to prevent other problems) and place it directly under the resources/drawables folder or in all drawables folders depending on its dimensions (if any). This should ensure it is present in all app bundles regardless of their target screen density.


Reply to comment:

Yes, indeed it is a bug though the android gradle plugin might have logged an error/warning in the build logs about the drawable in question not being provided for all densities. The rule/recommendation is to either have a drawable in the drawables folder or have copies (of proportional size) in each of the mdpi, hdpi, xhdpi, xxhdpi folders . In the case of this support library it only provided an xxhdpi version and probably at runtime the library uses the drawable only on xxhdpi devices. This is a 9 patch image so it can safely be copied to drawables without loss of quality.

Ideally most graphics would consist of vector drawables which can/should be placed in drawables without worrying about device density. Be mindful though about the vector drawable's width and height attributes as these will determine how big it will appear on screen and if these attributes are missing the size will be inferred from that of the view (wrap_content might result in the drawable not being visible). Always be careful to provide resources for all device configurations (densities, screen orientations, ...) when using the aab format to deploy apps because the final archive downloaded by the device will probably have only resources for its density and/or the default ones (drawables, values). In the past (with the apk format) the android runtime would try to scale down a higher density drawable when a drawable for its density and a default one were not available. Now (with aab) that the higher density drawables are left out of the bundle this is no longer possible.

I also think it's a good practice not to use resources from 3rd party libraries directly (like referencing them from your layouts) because they can easily be renamed, moved or deleted (especially with dynamic gradle dependencies) and with the complex/non-deterministic android build system it might not become apparent before the published app crashes on a user's device. If you really need some resource in a library it might be better to copy it to your project. In general it's safer to rely as little as possible on things out of your control.

Crispert
  • 1,102
  • 7
  • 13
  • Thank you for your help! I will do this as suggested to solve the issue. One additional question: But is this then not a bug, in case Google Play deletes some resources from my aab they indicate as useless? Then using images / vectors from libraries like appcompat as in my case will be not possible. – Sebastian B Jan 26 '22 at 20:28
  • In my case I had the image directly in the project, but there are few crashes on Firebase. So the question is, why it does not crash for other similar images? – George Shalvashvili Nov 25 '22 at 10:27
  • @George Shalvashvili Probably the image's resolution is not suitable and the host device's screen density. You could check what dpi the crashing devices had and the image size. The runtime may have considered the image causing the crash too big for the host device and tried to find a smaller/lower res one while the other images might have been suitable for the devices on which they appeared. – Crispert Nov 25 '22 at 12:34