1 min read

How to fix Ambiguous method call on getClass()” in Android Studio”

There is a bug in IntelliJ that will show an ambiguous method call error when using getClass() without an explicit cast. It looks like this:

Ambiguous method call. BothgetClass () in Object andgetClass () in Object match.

This is a false positive, as you can still compile and run your application fine. There are two accepted workarounds to remove Android Studio from reporting this as an error. If you cast all getClass calls with Object, it will remove the error. There is a better workaround that will remove the erroneous error that does not require you to modify your code.

Steps

  1. Open Finder
  2. Navigate to the target SDK’s source. For example, if you are targeting API 19, navigate to $ANDROID_HOME/sources/android-19/java/lang/
  3. Open Object.java in a text editor (such as Atom or TextWranger)
  4. Scroll to public final native Class<?> getClass(); (line 241 for API 19)
  5. Remove the unbounded wildcard so the line now looks like: public final native Class getClass();
  6. Save the file, and your erroneous errors should disappear

 

Ready to transform your brand?