Peter
Peter

Reputation: 952

How to use Flurry Analytics (.jar) in Xmarin.Android application?

I want to use Flurry in my Xamarin application. To do that I need to use Flurry Analytics for Android, which is a .jar file.

To do that I have created Bindings Library and included FlurryAnalytics-4.2.0.jar as an InputJar. But after buliding I get this error:

Com.Flurry.Sdk.Eg' already defines a member called 'A' with the same parameter types

And indeed Binding Library generates two methods called 'A' with same parameters but with different code inside of them.

Deleting one of the methods manully does not work. Adding special line to Metadata.xml file may rename methods, but at least one of these methods is needed (I do not know which one).

Does anyone know how can I solve this problem?

Upvotes: 4

Views: 519

Answers (2)

Peter
Peter

Reputation: 952

Here is ready solution, cheers!

class Flurry
{
    public const string ApiKeyValue = "YOUR_API_KEY";

    private readonly IntPtr _flurryClass;

    private readonly IntPtr _flurryOnStartSession;
    private readonly IntPtr _flurryOnEndSession;
    private readonly IntPtr _flurrySetContinueSessionMillis;

    public Flurry()
    {
        _flurryClass = JNIEnv.FindClass("com/flurry/android/FlurryAgent");

        _flurryOnStartSession = JNIEnv.GetStaticMethodID(_flurryClass, "onStartSession", "(Landroid/content/Context;Ljava/lang/String;)V");
        _flurryOnEndSession = JNIEnv.GetStaticMethodID(_flurryClass, "onEndSession", "(Landroid/content/Context;)V");
        _flurrySetContinueSessionMillis = JNIEnv.GetStaticMethodID(_flurryClass, "setContinueSessionMillis", "(J)V"); 
    }

    public void OnStartActivity(Activity activity)
    {
        try
        {
            JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnStartSession, new JValue(activity), new JValue(new Java.Lang.String(ApiKeyValue)));
        }
        catch (Exception) { }
    }

    public void OnStopActivity(Activity activity)
    {
        try
        {
            JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnEndSession, new JValue(activity));
        }
        catch (Exception) { }
    }

    public void setContinueSessionMillis(long millis)
    {
        try
        {
            JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetContinueSessionMillis, new JValue(millis));
        }
        catch (Exception) { }
    }

}

Upvotes: 1

Nikhil
Nikhil

Reputation: 1151

Flurry has obfuscated their APIs, so all the function names are A, B, C, AA, etc. Flurry makes liberal use of inner classes. C# does not have inner classes. Xamarin fakes them by moving them out of their encapsulation. This causes naming collisions.

The bright side is we do not need direct access to the vast majority of Flurry's library, so wrappers are not needed. I told it to exclude the internals by adding the following to Metadata.xml in the Transforms folder.

<metadata>
  <remove-node path="/api/package[@name='com.flurry.sdk']" />
  <remove-node path="/api/package[@name='com.flurry.android.impl.ads']" />
</metadata>

This tells it to exclude those namespaces from the wrapper generator.

Upvotes: 0

Related Questions