Skip to content Skip to sidebar Skip to footer

Getting Nosuchmethoderror Exception On Com.android.prefs.androidlocation.getavdfolder() When Trying To Run "ionic Cordova Run --emulator"

I just upgraded my cordova and ionic (npm upgrade -g cordova ionic) and now when I run 'ionic cordova run --emulator' I'm getting the following error message: Exception in thread '

Solution 1:

As it turns out, I just used Android Studio to update the SDK tools. Go to Preferences -> Appearance & Behavior -> System Settings -> Android SDK, then click the SDK Tools tab. I updated the following packages:

  • Android SDK Build-Tools
  • Android Emulator
  • Android SDK Platform-Tools
  • Android SDK Tools
  • Intel x86 Emulator Accelerator
  • Support Repository->Android Support Repository
  • Support Repository->Google Repository

I'm honestly not sure which package solved my problem but I imagine updating all of them at the same time was a good idea. Hope this helps someone.

Solution 2:

tl;dr You've run into a jar hell issue, probably caused by the Android SDK patcher. The only thing that worked for me was to delete everything in the ANDROID_HOME directory and used IntelliJ IDEA's Android SDK manager (which Android Studio is built on top) and re-install Android SDK. Note that updating Android SDK tools might work, as it worked in Les Buchanan's answer but it's more of a lucky fix for something that more often than not, it's caused by the SDK patcher itself.

Long explanation: Ionic, Cordova, etc. build on top of what Android SDK offers. cordova run --emulator invokes avdmanager because you need an Android Virtual Device to emulate in the first place. So I've run it directly and still ran into OP's issue, clearly eliminating cordova and ionic from possible causes:

$ $ANDROID_HOME/tools/bin/avdmanager.bat
Exception in thread "main" java.lang.NoSuchMethodError: com.android.prefs.AndroidLocation.getAvdFolder()Ljava/lang/String;
        at com.android.sdklib.tool.AvdManagerCli.init(AvdManagerCli.java:278)
        at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:210)
        at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)

If you look into the andmanager.bat file, you'll see that invokes AvdManagerCli main class around line 68-69:

@rem Execute avdmanager
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %AVDMANAGER_OPTS%  -classpath "%CLASSPATH%" com.android.sdklib.tool.AvdManagerCli %CMD_LINE_ARGS%

One line above it sets the CLASSPATH variable, which, among other things, includes this entry: APP_HOME%\lib\common-26.0.0-dev.jar. If you look inside it, e.g. unzip and open the com.android.prefs.AndroidLocation class, you'll notice that getAvdFolder method exists. Then why is it avdmanager complaining about it? When I've faced similar issues in the past, this usually meant that there are 2 or more versions of this class in the classpath and/or the class gets loaded from the incorrect and often outdated jar file.

Therefore, I wanted to see which jar is loaded for this class. Back to avdmanager.bat at line 68-69, added -verbose:class flag where AvdManagerCli is invoked with that java command above, and ran avdmanager.bat again, this time showing me the classes being loaded:

[Opened C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
[Loaded java.lang.Objectfrom C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
[Loaded java.io.Serializablefrom C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
...
...
many
...
entries
...
ahead
...
[Loaded com.android.sdklib.util.CommandLineParser$Argfrom file:/C:/Users/sburcea/AppData/Local/Android/Sdk/tools/lib/sdklib-26.0.0-dev.jar]
[Loaded com.android.prefs.AndroidLocation from file:/C:/Users/sburcea/AppData/Local/Android/Sdk/tools/lib/common.jar]
Exception in thread "main" [Loaded java.lang.Throwable$PrintStreamOrWriterfrom C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
[Loaded java.lang.Throwable$WrappedPrintStreamfrom C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
[Loaded java.util.IdentityHashMap from C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
[Loaded java.util.IdentityHashMap$KeySetfrom C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar]
java.lang.NoSuchMethodError: com.android.prefs.AndroidLocation.getAvdFolder()Ljava/lang/String;
        at com.android.sdklib.tool.AvdManagerCli.init(AvdManagerCli.java:278)
        at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:210)
        at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)

A-HA! So com.android.prefs.AndroidLocation was loaded from ANDROID_HOME/tools/lib/common.jar which wasn't even mentioned in the classpath! Looking inside ANDROID_HOME\lib\common.jar revealed that it contains com.android.prefs.AndroidLocation class but it does NOT have #getAvdFolder. It was probably a leftover from the older SDKs that I had installed on my machine. I needed a clean-up anyway and deleted the folder and re-installed the Android SDK and the problem was gone. I still wanted to know which jar loads the com.android.prefs.AndroidLocation class and I've added the -verbose:class back in my avdmanager.bat:

$ /c/Users/sburcea/AppData/Local/Android/Sdk/tools/bin/avdmanager.bat | grep AndroidLocation
[Loaded com.android.prefs.AndroidLocation$AndroidLocationException from file:/C:/Users/sburcea/AppData/Local/Android/Sdk/tools/lib/common-26.0.0-dev.jar]
[Loaded com.android.prefs.AndroidLocation from file:/C:/Users/sburcea/AppData/Local/Android/Sdk/tools/lib/common-26.0.0-dev.jar]
[Loaded com.android.prefs.AndroidLocation$Global from file:/C:/Users/sburcea/AppData/Local/Android/Sdk/tools/lib/common-26.0.0-dev.jar]

Post a Comment for "Getting Nosuchmethoderror Exception On Com.android.prefs.androidlocation.getavdfolder() When Trying To Run "ionic Cordova Run --emulator""