android: 'NoSuchMethodError' calling static interface method on pre-API-24

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VSMac 8.10.19(build 2)

Description

The problem occurs when I try to call an initialisation native method from my binding library (.aar). I know that the method exist in my Android Binding Library. Deleting folders bin and obj and rebuilding the project does not help. I also watched the file api.xml this method is there, and and I can refer to it. Removing or adding a method to Metadata.xml also doesn’t help. I also decompiled the .dll file and saw this method there.

method: Start(global::Android.Content.Context context, global::package.name.Configuration configuration) The error I’m getting

Java.Lang.NoSuchMethodError
no static method "Lpackage/name/SDK;.start(Landroid/content/Context;Lpackage/name/Configuration;)V"

Steps to Reproduce

Create Android Binding library via .arr, which needs to be initialized in MyApplication.cs Add reference from the Xamarin.Android project to Binding library. Start the app

Did you find any workaround?

No response

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

Hi @jpobst,

I also experienced this issue some time last year when I started developing a binding for the Twilio Conversations SDK for Android. Calling the static interface methods (eg. ConversationsClient.create) always threw a Java.Lang.NoSuchMethodError. I peeked into the SDK package source code & saw that those static interface methods were simply calling through to the implementing class methods, so I worked around the issue by copying the generated C# code for those methods, and changing the class reference to point to the implementing class instead (example).

I recently decided to dig deeper into this issue to find out exactly what the problem was. Here are my findings:

Support for static interface methods in Android was introduced in API 24. To facilitate backward-compatibility for Android projects with Minimum Version or TargetVersion set lower than 24, the Android build tools (d8 maybe?) extract all the static methods of the interface into a separate class (similar to how the Xamarin binding tools generate an additional class for interfaces that contain static methods), whose name is name of the interface + "$-CC", and modifies the calling code to call the static methods in that class instead.

I verified this by creating a JAR containing one interface that has one static method that simply adds 2 integers and returns the result. I then created a binding project for that and added the binding to a blank Xamarin Android project. The app calls the method and displays the result on the screen. You can find those here. The calling code is in MainActivity.cs.

You can observe the issue yourself by cloning the repo and performing the following steps:

  • Set Minimum Android version < API 24
  • Clean & Run the app
  • An error should show on the the screen
  • Set Minimum Android version >= API 24
  • Clean & Run the app
  • The adder result should show on the screen

A possible solution might be to add code in the binding interface to check for the presence of a “InterfaceName$-CC” class and, if it exists, use its handle to invoke the static interface method. If it doesn’t exist, then the interface handle can be used instead.

Hope this helps!

I found workaround for this issue. I changed the name of the Register from [Register ("package/name/Name", "", "Package.Name.INameInvoker")] to [Register ("package/name/Name$-CC", "", "Package.Name.INameInvoker")].

but maybe there is some other way?

@jpobst I’ve been looking for the root of the problem for a very long time, and apparently I found it. I looked through the classes.dex file and saw there that I don’t have a Name type, there is only a Name$-CC type.

Class #0            -
  Class descriptor  : 'Lpackage/name/Name$-CC;'
  Access flags      : 0x1011 (PUBLIC FINAL SYNTHETIC)
  Superclass        : 'Ljava/lang/Object;'
  Interfaces        -
  Static fields     -
  Instance fields   -
  Direct methods    -
    #0              : (in Lpackage/name/Name$-CC;)
      name          : 'start'
      type          : '(Landroid/content/Context;Lpackage/name/Name$Configuration;)V'
      access        : 0x0009 (PUBLIC STATIC)
      code          -
      registers     : 3
      ins           : 2
      outs          : 3
      insns size    : 6 16-bit code units
      catches       : (none)
      positions     : 
      locals        : 
  Virtual methods   -
  source_file_idx   : 8937 (Name.kt)

please tell me what I can do in this case