Android api 21 app crash because of vector assets - android

I'm using svg format icons in my app. my application's minimum API level is 21 and target is 24. I knew that I can convert svg format icons into usual android vectors by android studio's "Vector asset" tool. ( which is accessible if you right-click on res/drawable directory -> new -> vector asset. )
Created vectors by this approach result well in android api higher than 21. but it causes app crashing in android 5 devices. though in google's documentation on vector assets it is said that vector assets are supported on api level 21 and higher.
Is there any idea that what can I do with this?
here's a sample of errors while app is trying to inflate an activity/fragment xml and a vector asset causes the crash:
android.view.InflateException: Binary XML file line #26: Error inflating class ImageView

Related

PNG generated from XML not found in drawable folder

I’m working on a project with minSdk 23 and compileSDK 31. I’m seeing an issue when running the app on API 23 where when loading a vector resource via
ImageVector.vectorResource(id = R.drawable.vectorID)
I receive an error
android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi-v4/vectorID.png
But I can load the drawable successfully using AppCompatResources.getDrawable(LocalContext.current, R.drawable.vectorID)
Looking at the build folder, folders drawable-[x][xx][etc]hdpi are generated with the xml converted to pngs. But there is no -v4 folders generated.
Why are api level 4 folders being generated for a nexus5 api 23 device? Didn't androidx drop all these version tags? Furthermore, any suggestions on how to get around this?
Appcompat version is androidx.appcompat:appcompat:1.3.1

Can't process attribute android:fillColor="#android:color/white" [duplicate]

This question already has answers here:
References to other resources are not supported by build-time PNG generation
(9 answers)
Closed 10 months ago.
I downloaded an icon from google Material.io. while trying to build my project after integrating it, I ran into the error that says: Can't process attribute android:fillColor="#android:color/white"
Here is a screenshot:
In the app build.gradle add the following line within the android section:
defaultConfig{
vectorDrawables.useSupportLibrary = true
}
Check This For Further Detail :Vector drawables overview
Open the drawable you downloaded and replace android:fillColor="#android:color/white" with android:fillColor="#ffffff". In vector drawables the fillColor attribute must be set explicitly and not reference other resources
There are two ways to fix this.
One quick option is to go to the problematic XML file and change android:fillColor="#android:color/white" to android:fillColor="#FFFFFF". The error would disappear immediately. However, this problem would still recur if you have any other file with a similar line in the future.
Here's permanent solution:
Go to your build.gradle file and add this:
defaultConfig{
vectorDrawables.useSupportLibrary = true
}
Sync and the error would disappear immediately.
You should use AppCompatTheme to access to ?attr/colorControlNormal
AS 3.3.2 / gradle-4.10.1
I had the same compiler problem:
Error: java.lang.RuntimeException: java.lang.RuntimeException: Error while processing .../main/res/drawable/ic_white_set.xml : Can't process attribute android:fillColor="#color/selector_tab_color": references to other resources are not supported by build-time PNG generation.
I opened the incriminated file and got the following Lint warning:
Resource references will not work correctly in images generated for this vector icon for API < 21; check generated icon to make sure it looks acceptable.
Inspection info:Vector icons require API 21 or API 24 depending on used features, but when minSdkVersion is less than 21 or 24 and Android Gradle plugin 1.4 or higher is used, a vector drawable placed in the drawable folder is automatically moved to drawable-anydpi-v21 or drawable-anydpi-v24 and bitmap images are generated for different screen resolutions for backwards compatibility. However, there are some limitations to this raster image generation, and this lint check flags elements and attributes that are not fully supported. You should manually check whether the generated output is acceptable for those older devices. Issue id: VectorRaster
Then I checked my build.gradle files and, sure enough, they all had minSdkVersion to 16.
So, as an alternative to the #Bhavesh Moradiya solution, I set minSdkVersionto 21 and the problem was solved.
The drawback is that you lose support for devices with SDK < 16 though.

Compound Android Xml Shape: Consisting of a Line, an Arc and Another Line

Could you please help me draw this shape as one xml file:
The thing is, I would like to have it as one drawable xml shape to be used as a background image.
Please help.
Not really an answer. Just stating that the problem was solved using a vector drawable:
Refs:
How to convert a PNG image to a SVG?
Error importing Vector Asset into Android Studio
Code Analysis Error (Unexpected namespace prefix) after upgrading Android Support Library 23.2.0
https://developer.android.com/studio/write/vector-asset-studio.html#svg
I tried importing the svg in Android studio through the vector assets tool but got numerous errors about the <svg tag not being declared and also an error about a sudden end of file.
The thing is you are not supposed to use *.svg files in android studio.
You have to convert a, e.g. png file to an svg file and the tool you use to convert the svg to a readable xml is svg2android. There is a minimum version for the gradle dependency and some configuration to be done in the app:build.gradle file. The app:srcCompat resource is not immediately recognisable which is why I provided the third link.
Shout out if you need further explanation.

Difference between app:srcCompat and android:src in Android's layout XML

Whenever I create an ImageView with icon added using Android Studio's Vector Assets, I'm getting an error at the line app:srcCompat="#drawable/ic_play"
When I change the app:srcCompat with android:src, the error is gone but the icon looks pixelated.
What is the main difference between
app:srcCompat="#drawable/ic_play"
and
android:src="#drawable/ic_play"
app:srcCompat
is the most foolproof method of integrating vector drawables into your app.Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices
Note
As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat .
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
android:src
Sets a drawable as the content of this ImageView.It will display in
its original size. No automatic scaling .
If you are using android:src="#drawable/some_vector" without vectorDrawables.useSupportLibrary = true in build.gradle file and your app have vector images (vector drawable), then while building the apk file Android gradle plugin generates a lot of *.png files for different screens (hdpi, xhdpi...) from each of your vector drawable (only for API =< 19). The result - bigger size of apk.
When using app:srcCompat="#drawable/some_vector" with vectorDrawables.useSupportLibrary = true android uses vector drawable files without generating *.png files.
You can check this with Android Studio apk analyzer tool. Just build apk with and without vectorDrawables.useSupportLibrary = true.
I think this is the main difference.
Use:
app:srcCompat="#drawable/backImage"
The srcCompat attribute is actually defined within AppCompat library.
Important: you will need to add the appropriate namespace for this.
xmlns:app="http://schemas.android.com/apk/res-auto"
Note
What you are getting seems to be just a lint error that can be ignored. I have tried and gotten the same error, but it is working correctly.
You can use tools:ignore="MissingPrefix" to avoid seeing this error, temporarily.
I hope this helps.
app:srcCompat="some_resource"
is refer that it is AppCompatActivity src which comes in support library while
android:src="some_resource"
refers to simple activity.
Vectors and animated vectors were only supported in recent versions of the framework. srcCompat can be used with the compatibility library to make them work, but this only works with the certain views in the support library. Notice that app: is used instead of android:. This means its not part of the framework, but a parameter defined by your app.
Android 5.0 (API level 21) and higher provides vector drawable support so in order to support vector drawables in older versions app:srcCompat was added

Android : how to ignore an xml element below a defined Android SDK version?

I have in my xml an element from a third party library. Let say that it is a specific button named "SuperButton":
<com.library.SuperButton
android:id="#+id/my_super_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
My project should run on Android 2.1 but my SuperButton is only designed for Android 2.2 and higher (Froyo). What should I add in my xml to make sure that the SuperButton will be ignored (and consequently not displayed ; and will not crash the app...) on Android 2.1 ?
Thanks !!!
You'll have to create two layout files.
Put the 2.1 version in the /res/layout/ folder and the 2.2+ version in the /res/layout-v8/ folder.
Now everything with API level 8 or higher (see http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) will take the xml from the layout-v8 folder. Everything else from the default folder.

Categories

Resources