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.

 

 

 

 

 

 

 

 

 

 

 

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *