I'm working on android SDK library which external application will use as .aar file, in my SDK i have Drawable resources, the thing is some of the customers that use the SDK want to be able to override some of the Drawables with their own resources.
My question is how this can be done, i tried to put in app module drawable with same name as in sdk module but different image, it seems not stable and random, sometimes it use the actual icon from app module sometimes from sdk.
What i do wrong?
Is this can be fixed with gradle flavors?
Once I had to do it, and to use my own Theme and Styles I had to recreate the intire layout with same name and same components and making the changes that were necessary
After some research i find the solution, in you SDK build.gradle file you have to add config
defaultConfig{
....
....
vectorDrawables.useSupportLibrary = true
}
This will prevent android to create png's from your vector assets
Related
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.
I have a simple flashlight app in which while debugging, I noticed that there are several RTL (Right to left) layout folders in the res folder of my apk. So I googled it and set supportsRtl attribute to false in my manifest file which was previously true. But even after this change the folders are still present. I have tried rebuilding the project or clean the project but of no use. The folders are named like drawable-ldrtl-hdpi-v17 and others like drawable-ldrtl-mdpi-v17 , xhdpi, xxhdpi etc. I am surprised at v-17 suffix because my min sdk version is 21. Also they contain files like abc_ic_menu_copy_mtrl_am_alpha.png which seems weird to me. Can someone please help me to prevent these files from being generated as they unnecessary increase my apk size. Any help would be great.
Delete them if you don't want them. Android Studio doesn't go in and delete already generated files, because it doesn't know if you plan on using them later.
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
I have this needs.
I have developed an app and I want to duplicate it. I can copy and paste the project but if I do this 10 times can be a problem. The problem is, if I found a bug in the 10th duplicate, I have to review all other projects and I don't want to do this.
There is a clean way to solve this problem?
Are you using Android Studio and/or gradle? This sounds like the perfect use case for productFlavors. Among other things, different product flavors can point to different source directories. In this case, your app's build.gradle (NOT your project's top level build.gradle) might look like this:
android {
... other android stuff ...
productFlavors {
firstapp {
... other configuration. e.g. applicationId, minifyEnabled, etc ...
}
secondapp {
... other configuration. e.g. applicationId, minifyEnabled, etc ...
}
}
}
In order to enable different icons, you'd want to include 2 extra src folders (siblings to you main directory): /firstapp and /secondapp. The only thing that you need add to each of these directories is your icon, one color in each.
In Android, since you icons usually live in the drawable-x directories, your directory structure will look something like this:
/src
/main
... all your normal source code ...
/firstapp
/res
/drawable-xhdpi
my_icon.png
/secondapp
/res
/drawable-xhdpi
my_icon.png
Notice that the icons take the same name. It's up to you to change the color.
The last step is building your app. When you want the first icon, you'll build first app (something like ./gradlew assembleFirstappRelease). When you want to second icon, you'll build the second app (something like ./gradlew assembleSecondappRelease).
Long-time Eclipse-ADT user, and I've started looking into Android Studio since most samples on the web are now built with gradle. Now I just recently installed Android Studio on my machine and am trying out the samples from developer.android.com, specifically this one: FloatingActionButton
I imported the project in Android Studio, run it on my Nexus 5, works fine. The FABs are rounded and has drop shadows and all.
Now out of curiosity I tried to build the same project in Eclipse. So I fired up Eclipse, created a new project, copy-pasted everything from the Application folder into their proper destinations, then run the project on the same device.
The buttons are square.
Looking into the code, the following lines are responsible for giving the buttons a round shape are as follows:
Under FloatingActionButton.java
setOutlineProvider(new ViewOutlineProvider() {
#Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, getWidth(), getHeight());
}
});
setClipToOutline(true);
Now question is, why does these snippets of code work on Android Studio but not in Eclipse?
It is most likely related to which dependencies are in your project.
Using Gradle, you can configure wildcards for dependency version numbers, meaning that your project will always have the latest version of that library (for better or for worse)
With Eclipse-ANT you are most likely using manually downloaded and imported libraries, of a static version number. And the particular version you are using is compiling square buttons
Finally got it to work. The issue is that the AndroidManifest.xml that i copied over from the Application/src/main folder does not have a min SDK and a target SDK version -- both of which are defined in the build.gradle file, in the /Application directory. I added both and the FABs are now rounded with shadows and all.
Since I can't give bounty to myself, I'd give it to the answer who can explain further, or at least point to something that documents this change.
Now in AndroidStudio you can specify the most of the manifest configuration in build.gradle it will first refers to the module's build.gradle for this configs it is helpful and required for many things one i know is as follows.
Android studio and gradle gives you many new features one of them is buildtypes and flavours using which you can create a new flavours of your one project with very much less efforts, see in eclipse you need to create a whole new project for another flavour of your project while in android studio you can do it in single project. See when you define another flavour you can also define some of the manifest properties that will apply only to that flavour like you can change project name, package name, min sdk, target sdk, version, permission and so on.
There will only will be one manifest so if you declare multiple flavours and define this kind of changes than build.gradle file will fetch that changes and applies to the manifest specifically for that flavour so your manifest will get updated for that flavour.