0

My app min SDK is 21 so its no need to add

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

to Application class since its only for lower than 21 (anyway I tried to add it with no result).
My build.gradle config file has

defaultConfig {vectorDrawables.useSupportLibrary = true}

setting and no proguard/minify enabled

    debuggable true
    crunchPngs false
    minifyEnabled false
    shrinkResources false

    splits.abi.enable false
    splits.density.enable false

Anyway I checked the compiled .apk file using AndroidStudio and insured that all my vector drawables are really present in res/drawable folder, so its not proguard/shrinker or like.
Also I have to mention that I'm using androidx instead of support in this project.
So I've tried all possible variations of image setting:

setImageResource(imgResId);
setImageDrawable(ContextCompat.getDrawable(context, imgResId));
setImageDrawable(AppCompatResources.getDrawable(context, imgResId));
setImageDrawable(VectorDrawableCompat.createFromResource(resources, imgResId));
setImageDrawable(VectorDrawableCompat.create(resources, imgResId, null));
setImageDrawable(ResourcesCompat.getDrawable(resources, imgResId, null));

without any result (actually the result was always an app crash). Sending real Theme instead of null did not help either.
It is a common PagerAdapter and vector drawable images are being set to corresponding ImageView on every page dynamically, so it's not that static xml android:src="@drawable\ic_img" case when you should (but not must) replace it with app:srcCompat="@drawable\ic_img".
And I also tried to wrap image into selector like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/vector_image" />
</selector>

but no luck - crash report changes a bit but finally it leads to the same exception.
Nothing helped me from this SO question and any other SO questions I found as well.
My vector drawables are using gradient which works only starting from API 24. I'm testing my app on API 26 device but tried to replace all gradients by adding android:fillColor into the path section and commenting gradient section, like:

<path
    android:pathData="M268.177,..."
    android:strokeWidth="1"
    android:fillType="nonZero"
    android:strokeColor="#00000000"
    android:fillColor="#FFEF5E32">
    <!--<aapt:attr name="android:fillColor">-->
        <!--<gradient-->
            <!--android:startY="84.70677"-->
            <!--android:startX="284.02185"-->
            <!--android:endY="70.083954"-->
            <!--android:endX="252.21419"-->
            <!--android:type="linear">-->
            <!--<item android:offset="0" android:color="#FFEF5E32"/>-->
            <!--<item android:offset="1" android:color="#FFF68F32"/>-->
        <!--</gradient>-->
    <!--</aapt:attr>-->
</path>

And no luck...
I realize that importing my vectors to png/webp will most probably fix the issue but I want to use vector drawables.

Community
  • 1
  • 1
Stan
  • 6,511
  • 8
  • 55
  • 87

1 Answers1

0

When I was applying patch for gradient sections I noticed that AndroidStudio highlighted these gradient sections saying that it is supported from API 24 and my project min API is 21. So I have decided to check other warnings like:

Very long vector path (1456 characters), which is bad for performance. Considering reducing precision, removing minor details or rasterizing vector.

So I've started suspecting it as an issue source because there was an error message (red colored text) given in build window of AndroidStudio stating:

string too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'.

But there is no glue or pointing to source of this problem, its just states that I have a very long String somewhere in my project, so it could be preshipped realm database file for instance or something like.
Finally I found an anomaly in one of my vector drawables - it was a path string containing about 51k chars! Of course no warning was given to me at importing time (svg to xml, done using AndroidStudio).
Removing this path solved the crash issue but also a part of an image was lost.
So I've degrouped a problematic path group using Sketch and after importing it resulted in 2 paths of 19k and 24k chars, which is not good either, but it works now without any crash.
btw I am unable to find any information on string length limitations for vector/xml but gradle does apply this limitation at build time, so its known.

Image displays AndroidStudio warning and compile error

The limit of one xml string length revealed in this SO question and there is no official document on that could be found.

Stan
  • 6,511
  • 8
  • 55
  • 87