【问题标题】:How to call AAR library methods?如何调用 AAR 库方法?
【发布时间】:2015-07-20 07:23:50
【问题描述】:
我正在尝试为Application Insights Android SDK 创建一个简单的包装器
我在 Android Studio 中构建了库,这导致了此调试 AAR file,然后我将其添加到 Xamarin Studio 中的库绑定项目中。
但是,当我引用它时,我有命名空间:
using Com.Microsoft.Applicationinsights.Library;
但它不包含类 ApplicationInsights,如果它与它所包装的 java 库匹配,它应该包含它。
我不想要一个花哨的完整 CLR 包装器,我只想调用那个以应用的 Context 作为参数的类的“setup”方法,然后能够调用简单的跟踪方法。
【问题讨论】:
标签:
xamarin
java-native-interface
xamarin.android
【解决方案1】:
创建一个新的 Java Binding 库,添加 AAR 文件,然后您可以执行以下操作:
_appInsights = JNIEnv.FindClass ("com/microsoft/applicationinsights/library/ApplicationInsights");
_methodSetup = JNIEnv.GetStaticMethodID(_appInsights, "setup",
"(Landroid/content/Context;Landroid/app/Application;Ljava/lang/String;)V");
调用设置就可以了:
public static void Setup(Android.Content.Context context, Android.App.Application app, String key) {
var c = new JValue(context);
var a = new JValue(app);
var s = new JValue(new Java.Lang.String(key));
JNIEnv.CallStaticVoidMethod (_appInsights, _methodSetup, c, a, s);
}