Newer versions of Android Studio and only two drawable directory - drawable and drawable-v21

41,390

Thank you to everyone who tried to help. You helped me reach the final answer, but no one solution was quite right. @user3137702 was probably the closest, as it IS related to the whole move to vectors/SVGs. I couldn't find a definitive answer, like something directly from Google (although I imagine it is out there), but from what I've gathered from a bunch of articles, there is probably a reason they are doing this.

For starters, it looks like this started in Android Studio 1.4. I am in 1.5 right now. It seems that Android is moving in the direction of no longer needing you to create your own density folders (i.e. mdpi, hdpi, etc.) for drawables (mipmaps is different, so please don't confuse that with what I am talking about). As of Android Studio 1.4, it will take the SVGs you put in the regular drawable folder (as in not the v21 folder), convert them to PNGs, and place them in auto-generated density folders for you during the build sequence (so Gradle does this for you, essentially) for all versions older than API 21. For 21 and up, SVG is supported different, which is a whole other topic. But this essentially makes SVG support backwards compatible all the way to API 1!!!

HOWEVER, there is a BIG catch. This SVG conversion is not always as successful as you might hope. It only supports a subset of SVG files, so depending on how you save it (i.e. what settings are applied when saving), it may not render properly. Even commonly used settings, such as gradient and pattern fills, local IRI references, and transformations are NOT supported (yet). If you are working with SVG files that you didn't generate, you will likely have problems importing them. If you or someone you work with directly generates them, you may have to experiment with how you save the files, and you should test builds often on older versions of Android to make sure it turned out as expected.

To import SVGs into Android Studio 1.4+, follow these simple steps:

  1. Right-click on the res/drawable folder
  2. Select "New"
  3. Select "Vector Asset"
  4. At this point, you can select a "Material Icon", which works really well, and there are a bunch of beautiful "free" icons you can select from. For indie developers, without icon design support, this is nice!
  5. OR - you can select "Local SVG File"
  6. Then choose an SVG from either option with the "choose" option. WARNING: This is where it could possibly go wrong, if the SVG you import isn't saved properly.
  7. Hit "Next"
  8. Verify it is saving in the right place, and then Click "Finish"
  9. At this point, it is reference-able with: android:icon="@drawable/ic_imagename" (using your image name instead of ic_imagename, of course)

@CommonsWare's response was very helpful in leading to the right solution, but from what I saw, generating several variations of new projects from different template and version support settings, there wasn't any way to actually have the old density folders get auto-generated. There is definitely more going on here than just a different template-version selection. But as he said, depending on what template/version you select, you may end up with a different set of those two drawable folder types. But specific to my question, Android Studio does seem to be putting an emphasis on this new approach of not creating your own individual drawable density folders at all.

It's pretty cool, imo, but it still needs some work. In practical terms, I will likely still need to add the drawable density folders to support all the images I work with, until this mechanism gets a little more supportive of all types of SVG renderings.

And one more tidbit - Because this is all handled through Gradle (the actual generation of the density folders) you can add build settings through the flavor mechanism to limit which density folders you want to generate. So if, for example, you feel mdpi images have reached the end of their usefulness for your particular user base and would like to leave that size/density out of your app to shave a couple MB off the app size, you can set that in the Gradle build flavor.

Share:
41,390

Related videos on Youtube

Bourne
Author by

Bourne

Mobile strategist, developer, UX specialist. Platform agnostic, encouraging competition to keep them all improving.

Updated on August 01, 2020

Comments

  • Bourne
    Bourne almost 4 years

    With older versions of Android Studio, all of the drawable bucket folders were created by default (i.e. drawable-mdpi, drawable-hdpi, etc.). In newer versions of Android Studio, it only provides drawable and drawable-v21. Why is that?

    Is there a reason that Android (in its infinite wisdom) no longer gives you the separate bucket folders?

    Just to note, I know you can just add them manually, but I want to make sure there isn't some new best practice reason to not use those folders anymore.

    • alzee
      alzee over 8 years
      I remember reading something about there being a new best practice they are trying to encourage -- using SVG for your drawables instead of raster images, so you only need one image that can be cleanly resized.
    • user2511882
      user2511882 over 8 years
      i think that is inpart because of the introduction of the mipmaps folder
    • Bourne
      Bourne over 8 years
      Thanks for the note. From what I remember, only the newer versions of Android support SVG only, so that wouldn't work on most projects I work on, but I'll look into that being the possible reason. And I may be wrong about the SVG thing too, not being backwards compatible, will check on that!
    • Bourne
      Bourne over 8 years
      @user2511882 According to the Google blog, mipmaps is only supposed to be used for the app icons. So still trying to figure out where to stick the other assets.
    • user2511882
      user2511882 over 8 years
      @Bourne what version is this? 1.5?
    • QuantumTiger
      QuantumTiger over 8 years
      @user3137702 I kind of doubt that, given that Google's own icon packs (design.google.com/icons) are distributed in the mdpi, hdpi etc folders
    • alzee
      alzee over 8 years
      The other images can go into the normal per-density drawable directories. They aren't automatically created on a new project, but if/when you import an image, it can create them for you.
    • Bourne
      Bourne over 8 years
      @user2511882 I am in 1.5. (not sure how far back the change goes, but it seems relatively recent)
    • Androidcoder
      Androidcoder about 6 years
      I always created a drawable-nodpi folder as I found app memory use increased exponentially for anything in the 'drawable' for high resolution screens as android 'scaled up' everything (4x,16x,etc). Now I don't know what I should use.
    • Vadim Kotov
      Vadim Kotov about 5 years
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Yes, the key is that you must click "New" -> "Vector Asset" in order to create the XML drawable from SVG file.
  • K Pradeep Kumar Reddy
    K Pradeep Kumar Reddy over 3 years
    Is this only for SVGs ?? I want to copy a .PNG file to drawable. I'm having two options drawable and drawable-v24. Into which folder we have to copy ??