10

Is it possible to configure Android Studio to display only the @drawable resources that are inside the project folder?

The project I'm working on is an Industry project and rarely requires me to use R resources.

enter image description here

Machado
  • 14,105
  • 13
  • 56
  • 97
  • you can make like this `@package:drawable/filename` – Fouad Wahabi May 29 '15 at 13:23
  • @FouadWahabi , AS doesn't recognize this command for *autocomplete*. – Machado May 29 '15 at 13:27
  • 2
    it's mentioned in the devoleper.android site : http://developer.android.com/guide/topics/resources/drawable-resource.html , I don't kno exactly how it works – Fouad Wahabi May 29 '15 at 13:36
  • 2
    @FouadWahabi I believe your suggestion requires the OP to type `@package:drawable/filename` for **each** autocomplete. I don't think this answers the OP's question about removing the unused resource references from the autocomplete list. – Code-Apprentice Jun 06 '15 at 22:51
  • Found no solution yet. Typing the package name seems not like a good option to me. – Machado Aug 05 '15 at 12:32
  • 2
    It seems AS 1.3 can it. But haven't find out how. See here: https://twitter.com/JakeWharton/status/629490397677400064 and here: https://www.youtube.com/watch?v=_Rox-HXhRfI&feature=youtu.be&t=144 – StefMa Aug 07 '15 at 06:37
  • 1
    Yes it looks like it does! I have edited the question title now because of it. – Machado Aug 07 '15 at 17:50

2 Answers2

4

I don't think you can do that actually, only library developers can choose to hide the resources in their aar distributions.

So you are dependent on library developers to do that for you. Luckily the latest Android support library should already have done this for you.

The way library developers can hide resources in their library is:

  1. Create a new folder on the same level as res called res-public (name not important) with a subfolder called values:
src
    main
         java
         res
         res-public
             values
  1. In that folder create a new file called public.xml, where you define all resources that you want to have public with their name followed by type
<resources>
    <public type="drawable" name="btn_login"/>
</resources>
  1. Then make sure to reference this new folder in your build.gradle file:
android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    sourceSets {
        main.res.srcDirs 'res', 'res-public'
    }

    defaultConfig {
       ...
    }
}
  1. Make sure you are using at least version 1.3 of the Gradle plugin
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
    }
}
  1. Compile your library into an aar file and include that into your project. Only the btn_login resource will now be visible.
    • Check here how to include a local aar file, so you can test without pushing to a Maven repo.
Jeroen Mols
  • 3,436
  • 17
  • 24
  • above solution didn't work for me. still, I am able to access the private resource in main app module – AKASH WANGALWAR Nov 26 '18 at 08:29
  • Indeed, this just excludes the resources from autocomplete. However, Android Studio should give you a warning if you try and use "hidden" resources. Note that namespaces are coming to resources pretty soon, which should enable true private resources: https://youtu.be/GlwvVJNWlWg?t=1448 – Jeroen Mols Nov 27 '18 at 13:33
  • but I am to get private resources in autocomplete. check this out. https://stackoverflow.com/questions/53430517/unable-to-make-android-library-resources-private – AKASH WANGALWAR Nov 28 '18 at 09:09
  • How you manage to get it worked? Its not working for me (Gradle plugin: 3.3.1) – Alphonsa Augustine Feb 12 '19 at 12:45
4

Looking through the AS's 'Code Completion' section, I didn't find any setting which allows to hide the SDK resources and use the app/lib resources instead:

enter image description here

The recently implemented feature, mentioned by StefMa, probably will not help you as it works the other way: it allows library developers to hide some of the resources from the aar package and show only selected portion of resources, aka public recources. Chris Banes made a good intro in this feature here.

aga
  • 27,954
  • 13
  • 86
  • 121