Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin .jar binding - 'Bitmap could not be found'

I have a .jar file with a Java interface called MyService, which has a method:

boolean printBitmapObject(android.graphics.Bitmap bitmap, byte[] result);

I would like to use this .jar file in C# in my Android Xamarin project in Visual Studio 2015. I did following steps:

  1. I created a new library binding project.
  2. I put the .jar file into Jars directory of the new project with BuildAction property set to InputJar.
  3. The project was built successfully.
  4. I added reference to the library binding project into my main project.

Unfortunately, I cannot build my main project anymore, because there is an error in the auto-generated C# code:

    public interface IMyService : global::Android.OS.IInterface
    {
       bool PrintBitmapObject (Bitmap bmp, byte[] result); //"Bitmap" is red underlined
    }

with message "The type or namespace name 'Bitmap' could not be found (are you missing a using directive or an assembly reference?)".

There is also an auto-generated abstract class IMyServiceStub, which also doesn't recognize Bitmap. Moreover, it doesn't know some auto-generated BitmapStub either:

public abstract class IMyServiceStub : global::Android.OS.Binder, global::Android.OS.IInterface, IMyService
{
    ...
    protected override bool OnTransact (int code, global::Android.OS.Parcel data, global::Android.OS.Parcel reply, int flags)
    {
        ...
        case TransactionPrintBitmapObject: {
                data.EnforceInterface (descriptor);
                Bitmap arg0 = default (Bitmap); //Bitmap red underlined
                arg0 = BitmapStub.AsInterface (data.ReadStrongBinder ()); //BitmapStub red underlined
                byte [] arg1 = default (byte []);
                var result = this.PrintBitmapObject (arg0, arg1);
                reply.WriteNoException ();
                reply.WriteInt (result ? 1 : 0);
                data.WriteByteArray (arg1);
                return true;
                }
        ...
    }

}

I tried adding using Bitmap = Android.Graphics.Bitmap to the auto-generated file, but it doesn't help, because BitmapStub remains unknown and my changes are overwritten during the next build.

Is there anything I can do to fix this issue? Or is it a known bug in Xamarin (I use version 4.0)?

like image 270
vojta Avatar asked Jun 03 '16 14:06

vojta


1 Answers

Jar binding will not work with Android Library Projects, you need to include .aar file. Jar files do not reference android specific libraries in jar, but .aar file correctly references all required android related libraries.

like image 59
Akash Kava Avatar answered Nov 01 '22 23:11

Akash Kava