Marked as private warning after changing colour of

2019-01-19 11:40发布

问题:

After defining a custom colour for the back arrow in the action bar, a warning is then returned. What can be done to get rid of this warning?

        final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        upArrow.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
        actionBar.setHomeAsUpIndicator(upArrow);

The resource @drawable/abc_ic_ab_back_mtrl_am_alpha is marked as private in com.android.support:appcompat-v7

回答1:

See Private Resources in Android Libraries for reason and fix.

In short, drawable/abc_ic_ab_back_mtrl_am_alpha comes from appcompat-v7 and is intended to be used only by that library. You should not use it.

If you really want to use it, copy it to your project

the best practice is to copy the resources for private use



回答2:

Instead of doing Mohammad's

android {
    lintOptions {
        disable 'PrivateResource'
    } 
}

I'd recommend to do the following, which is a local fix for a statement. The advantage is not having to deactivate the lint-checks globally (which can easily be forgotten to activate again later on).

For XML:

tools:ignore="PrivateResource"

For Code:

@SuppressLint("PrivateResource")

Effectively, your code then should look something like this:

XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
    tools:ignore="PrivateResource" />

Code:

@SuppressLint("PrivateResource")
final Drawable upArrow = ContextCompat.getDrawable(context, R.drawable.abc_ic_ab_back_mtrl_am_alpha);


回答3:

It seems it's a bug in android this post form a project member in Android Open Source Project - Issue Tracker

Replying to a similar Issue

The root cause here is that when a library depends on another library, it imports all the resources from any libraries it depends on into its own declared R.txt (in the AAR file).

However, it doesn't include the public.txt declaration from those dependencies, so now it ends up exposing these symbols as declared but not public -- e.g. private.

I'm considering two fix alternatives:

(1) in the resource visibility lookup for a library, remove any symbols imported from a dependency (whether or not that dependency provides visibility information), or

(2) reverse the visibility lookup logic: if we find a symbol as public in any library, consider it public rather than the current logic which says that if a symbol is declared as private anywhere, it is.

I think I'm going with 2; the current logic doesn't make sense when considering the scenario that symbols end up inlined in downstream libraries.

as they said :

The only workaround for now is to turn off the private resource lint check:

    android {
        lintOptions {
            disable 'PrivateResource'
        } 
    }

they are saying it was fixed but I had run through this issue today and I'm using android studio 1.5.1 and gradle 1.5



回答4:

While I prefer to keep the Lint suppressions as close to the source as possible, as it's done in this answer, if that doesn't work it's also possible to list the suppressions in a lint.xml file located in your app folder. It would look something like:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="<issue-id>">
    <ignore path="<path-to-source>.<file-extension>"/>
</issue>

Where issue-id is the type you want to suppress, for example UseSparseArrays. Android Studio will also autocomplete this field for you.