Xamarin binding an Android jar library


1. How to write an jar library for Xamarin.Android

Xamarin.Android can only call Android method in jarexplicitly. For example, there is a public class “com.example.test.Test” in jar,provides public static method “SimpleTest()”. Then in your Xamarin.Androidenvironment, you could “using Com.Example.Test”, and then “Test. SimpleTest()”.

When Java package name transformed to xamarin assembly,  first letter of each part should becapitalized. Just like “com.example.test -> Com.Example.Test”.

If you want to deploy your jar as an Android Service, aninterface class for Xamarin should be provided. If you directly “BindService”or “StartService” in Xamarin, your Service in jar would not startup, you shouldwrite an interface class for your service just like the code below:

In Xamarin.Android, code would be like below:

using Com.Trimble.Ssi;

private Interface _itfc = new Interface(this, _serviceConn);
_itfc.Bind();
_itfc.Unbind();

 

2. Create android jar

Generate your code to jar, Right-click yourproject(eclipse), select Export -> Java JAR file.

Unselect all items except src.

You should use the resources of Host app, your jar is only alibrary, no resource should be packaged. Then click finish to export your jarfile.

3. Use jar library in Xamarin.Android

Add new project to your Xamarin solution, select Android-> Bindings Library (Android).

Put your jar file to the binding library project’s Jars pathand add it to the project.

Right-click the jar file in Visual Studio Solution Explorer,modify its Build Action to “EmbededJar”.

Add the binding library project reference to the host.

If your jar has Service,  BroadcastReceiver or permission things thatshould add declarations in AndroidManifest.xml, add them into the Host’s AndroidManifest.xml.

Then write your code to call the jar library like section 1.

Build and run your application.

p.s.

When referencing the Java Binding project directly from aXamarin.Android project, none of the namespaces, classes, or other members ofthe Java Binding project will be visible in the IDE when working on theXamarin.Android project. This bug will be fixed in a future version ofXamarin.Android.

4. Jar library has reference jars and *.so libraries

Put reference jars to the path same as your embededJar,modify their Build Action to “EmbededReferenceJar”

Put reference *.so libraries to the same path, too. *.sofiles should be under the path armeabi and armeabi-v7a.

 

Then modify the .so files Build Action to EmbededNativeLibrary.

4. Bugs

Threre is a bug when jar load *.so in Xamarin environment.

If your jar load *.so failed. Please create a libs directorynext to the directory Jars, then copy the armeabi & armeabi-v7a to the libsdir. Then you have 2 copies of *.so files in different dirs.

Then compile and run, it works well, right? Then delete libsdir. Compile and run again, it’ll still work. Weird? Just follow it.

 

 

 

 

 

 

 

 

 

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值