Can't invoke android.graphics.Color.getHtmlColor


#1

I can’t invoke getHtmlColor() while this method is actually present in the Android source code. Is this a bug? I’m using kotlin 1.2.0 with Android Studio 3.0.1. The targetCompileSdk is 26.

The code can be as simple as:

import android.graphics.Color

fun foo() {
    val c = Color.getHtmlColor("black")  // <= compiler complains: "Unsolved reference: getHtmlColor"
}
  • UPDATE:
    I found I couldn’t use ParserFactory.makeParser() from org.w3c.css.sac.helpers.ParserFactory either. Am I missed something? I can use Android Studio’s “Navigate - Declaration” to jump to the source code of ParserFactory, and makeParser() is there!
import org.w3c.css.sac.helpers.ParserFactory
import org.xml.sax.InputSource

fun handleCss(css: String) {
        val source = InputSource(StringReader("h1 { background: #ffcc44; }"))
        val p = ParserFactory.makeParser() //  <= compiler complains: "Unsolved reference: makeParser
}

#2

Look at the documentation! The compiler is correct, there is no method with the name getHtmlColor on the Color class.


#3

How about the ParserFactory.makeParser()?
And indeed the Android documentation doesn’t mention getHtmlColor(), seem like it is a private method. But when I look into the source code I found it’s signature is:

@ColorInt
    public static int getHtmlColor(@NonNull String color) {
        Integer i = sColorNameMap.get(color.toLowerCase(Locale.ROOT));
        if (i != null) {
            return i;
        } else {
            try {
                return XmlUtils.convertValueToInt(color, -1);
            } catch (NumberFormatException nfe) {
                return -1;
            }
        }
    }

the source code is here
How can it be for me to not able to invoke it from outside?


#4

If you look at the source you will find the @hide statement in the javadoc. Google do some hacks with that to make it actually hidden, at least at compile time. If you look at the implementation, I’m not sure why the method is present at all, but I don’t think it does belong in platform API (for a library it would be fine).

As for the ParserFactory, that class does not exist at all in the SDK. Note that if you use interal API’s you will get yourself on shaky ground as not only is there no guarantee about them (their presence or semantics) in the future, it is also possible that they are not part of the compatibility requirements and vendors may have made changes themselves.