Bug 13338 - How to use a .jar without making a binding.
Summary: How to use a .jar without making a binding.
Status: RESOLVED FIXED
Alias: None
Product: Documentation
Classification: Xamarin
Component: Mono for Android ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Tom Opgenorth
URL:
Depends on:
Blocks:
 
Reported: 2013-07-19 08:35 UTC by Alek Slater
Modified: 2013-07-29 09:50 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample solution demonstrating the problem (435.81 KB, application/zip)
2013-07-26 02:02 UTC, Alek Slater
Details
Working Solution (436.70 KB, application/zip)
2013-07-26 02:53 UTC, Alek Slater
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Alek Slater 2013-07-19 08:35:01 UTC
I'm unable to locate any documentation that deals with how to use a jar file in a monodroid application without making a binding for it.

Quite often, I only need to use a couple of methods in a particular jar, and trying to get the binding projects to work always result in failure.

In the past, the following approach worked:

IntPtr Flurry_Class = JNIEnv.FindClass("com/flurry/android/FlurryAgent");
IntPtr Flurry_logEvent = JNIEnv.GetStaticMethodID (Flurry_Class, "logEvent", "(Ljava/lang/String;)V");
Java.Lang.String key = new Java.Lang.String(eventName);
JNIEnv.CallStaticVoidMethod(Flurry_Class,Flurry_logEvent,new JValue(key));						

However, in recent monodroid versions, this no longer seems to work so easily.
Now I no longer use Flurry in my app, and Testflight recently released an Android SDK, so the following with testflight:

IntPtr TF_Class = JNIEnv.FindClass("com/testflightapp/lib/TestFlight");
IntPtr TF_Method = JNIEnv.GetStaticMethodID (TF_Class, "passCheckpoint", "(Landroid/content/Context;Ljava/lang/String;)V");
Java.Lang.String key = new Java.Lang.String(eventName);
JNIEnv.CallStaticVoidMethod(TF_Class,TF_Method,new JValue(key));

Does not seem to have any effect.
I get the following exception:
Exception of type 'Java.Lang.NoClassDefFoundError' was thrown.

So even though I've added the jar to the project, and I've experimented with changing the build action for the jar, it still does not seem to find the java class in the env. How to add it to the env? Whats the current approach that works with the current version of xamarin studio / mono for android / android.

Would it be possible to add some documentation on how to achieve adding a jar to a monodroid project and using said jar without resulting to the overly complicated bindings that never ever seem to work (I have tried a lot, and I've not succeeded in binding a single project).

I believe this is also reflected in the huge number of monotouch-bindings, and almost total lack of monodroid-bindings.
Comment 1 Tom Opgenorth 2013-07-25 14:38:41 UTC
Hi Alek,

Could you provide a sample project to demonstrate the problem and help us determine why this isn't working? Thanks.
Comment 2 Alek Slater 2013-07-26 02:02:36 UTC
Created attachment 4445 [details]
Sample solution demonstrating the problem
Comment 3 Alek Slater 2013-07-26 02:03:29 UTC
@Tom, I've attached a sample solution demonstrating the problem ( attachment 4445 [details] ). Let me know if you need anything else.
Comment 4 Alek Slater 2013-07-26 02:53:06 UTC
Created attachment 4446 [details]
Working Solution

It seems to work now.
Comment 5 Alek Slater 2013-07-26 02:55:58 UTC
@Tom, following a recent upgrade to Mono and Mono for Android, I can now see that it works. I.e. I am no longer getting exceptions like Exception of type 'Java.Lang.NoClassDefFoundError' was thrown, but rather
Exceptions referring to Method not found, which led me to a bug in my implementation.

So yeah, I think everything works now regarding the JNIEnv approach.
I've updated my Solution to what seems to work for me at least in case you should find it useful for any reason.
Comment 6 Tom Opgenorth 2013-07-29 09:50:33 UTC
Great  - glad it was as simple as and update.  We'll keep your sample in mind for the future.