Bug 35412 - EmbeddedNativeLibrary does not deploy .so files to .APK inside a Java Binding Project.
Summary: EmbeddedNativeLibrary does not deploy .so files to .APK inside a Java Binding...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 6.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: dean.ellis
URL:
Depends on:
Blocks:
 
Reported: 2015-10-30 13:53 UTC by Jon Douglas [MSFT]
Modified: 2017-07-13 22:13 UTC (History)
2 users (show)

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


Attachments
Reproduction (3.24 MB, application/x-zip-compressed)
2015-10-30 13:53 UTC, Jon Douglas [MSFT]
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 Jon Douglas [MSFT] 2015-10-30 13:53:58 UTC
Created attachment 13591 [details]
Reproduction

*Description:

EmbeddedNativeLibrary does not deploy .so files to .APK inside a Bindings Project. It does work using the AndroidNativeLibrary Build Action inside an Android Application.

1) Error during runtime:

10-30 11:52:43.122 E/mono-rt (23837): [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/KoolPOSTest.KoolPOSTest-1/base.apk"],nativeLibraryDirectories=[/data/app/KoolPOSTest.KoolPOSTest-1/lib/arm, /data/app/KoolPOSTest.KoolPOSTest-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]] couldn't find "libfreetypefont.so"
10-30 11:52:43.122 E/mono-rt (23837): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () in /Users/builder/data/lanes/2098/f94dc5b2/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:line 143
10-30 11:52:43.122 E/mono-rt (23837): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) in /Users/builder/data/lanes/2098/f94dc5b2/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:line 1029
10-30 11:52:43.122 E/mono-rt (23837): at Android.Runtime.JNIEnv.FinishCreateInstance (IntPtr instance, IntPtr jclass, IntPtr constructorId, Android.Runtime.JValue* constructorParameters) in /Users/builder/data/lanes/2098/f94dc5b2/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:line 306
10-30 11:52:43.122 E/mono-rt (23837): at Android.Runtime.JNIEnv.FinishCreateInstance (IntPtr instance, IntPtr jclass, IntPtr constructorId, Android.Runtime.JValue[] constructorParameters) in /Users/builder/data/lanes/2098/f94dc5b2/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:line 312
10-30 11:52:43.122 E/mono-rt (23837): at Com.BW.Spdev.Printer..ctor () in C:\Users\Jon\Downloads\KoolPOSLib\KoolPOSLib\KoolPOSLib\obj\Debug\generated\src\Com.BW.Spdev.Printer.cs:line 51
10-30 11:52:43.122 E/mono-rt (23837): at KoolPOSTest.MainActivity+<>c__DisplayClass1_0.<OnCreate>b__0 (System.Object , System.EventArgs ) in C:\Users\Jon\Downloads\KoolPOSLib\KoolPOSLib\KoolPOSTest\MainActivity.cs:line 30
10-30 11:52:43.122 E/mono-rt (23837): at Android.Views.View+IOnClickListenerImplementor.OnClick (Android.Views.View v) in /Users/builder/data/lanes/2098/f94dc5b2/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:line 1938
10-30 11:52:43.122 E/mono-rt (23837): at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v) in /Users/builder/data/lanes/2098/f94dc5b2/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:line 1903
10-30 11:52:43.122 E/mono-rt (23837): at (wrapper dynamic-method) System.Object:12589c62-3ba5-4e07-8101-eae86af6c640 (intptr,intptr,intptr)
10-30 11:52:43.122 E/mono-rt (23837): --- End of managed exception stack trace ---

It seems that it cannot find this .so library. Taking a look inside the jar shows the following code executed:

System.loadLibrary("freetypefont");
System.loadLibrary("S1007");

1) See our Documentation on how to include specific .so files for respective Architectures: https://developer.xamarin.com/guides/android/advanced_topics/using_native_libraries/

In this case, I just did armeabi and armeabi-v7a as I do not know where the .so files came from or if they are compatible with other architectures.

You can do this two different ways:

A) Include the .so files in the Java Binding Library with the following structure as per the documentation:

libs/<ABI>/mynativelibrary.so

In this case I just did the two abis, which looked like this:

libs/armeabi/libfreetypefont.so
libs/armeabi/libS1007.so

libs/armeabi-v7a/libfreetypefont.so
libs/armeabi-v7a/libS1007.so

Build Action - EmbeddedNativeLibrary

Copy to Output Directory - Copy Always

A small note is that the prefix "lib" should be in front of your .so file.

However it seems that the .so files do not get pushed to the device - http://screencast.com/t/IhwHbVww

Good news is the other method does work. Please see B) to get this to work.

B) Instead of having the .so files in the Binding Library, you can include them in the actual application via the same method but a different Build Action

libs/<ABI>/mynativelibrary.so

In this case I just did the two abis, which looked like this:

libs/armeabi/libfreetypefont.so
libs/armeabi/libS1007.so

libs/armeabi-v7a/libfreetypefont.so
libs/armeabi-v7a/libS1007.so

Build Action - AndroidNativeLibrary

Copy to Output Directory - Copy Always

A small note is that the prefix "lib" should be in front of your .so file.

This will then show the respective libraries: http://screencast.com/t/u8YRewuno

And the code should execute: http://screencast.com/t/1uv0gEYq24

2) I would also remove any additional ABI in your Android Project's Properties -> Android Options -> Advanced -> Uncheck the ones you don't add support for, in our case everything but (armeabi and armeabi-v7a)

*Reproduction:

Attached configured with settings above.

It's setup to fail with the respective .SO files included in the Java Bindings Library as EmbeddedNativeLibrary. Try to deploy to device and click the button and you should get an UnsatisfiedLinkError like the observations in A)

If you "Exclude From Project" the libs folder inside the Java Bindings Library and "Include into Project" the libs folder inside the Application Project, you should be successful as per the observations above in B).

Device Tests done on Nexus 6 (API 21)

*Version Information:

Microsoft Visual Studio Professional 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00079

Installed Version: Professional

Xamarin   4.0.0.1566 (5abb0de)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   6.0.0.11 (f94dc5b)
Visual Studio plugin to enable development for Xamarin.Android.
Comment 1 Atsushi Eno 2015-11-01 23:27:58 UTC
dean: I think we had some issues with related to native library retrieval from bindings or libraries at app build time. It seems about that too.
Comment 2 Jon Douglas [MSFT] 2017-07-13 22:13:29 UTC
I am unable to reproduce this behavior and I believe this has been fixed since. After attempting to reproduce, the libraries are indeed making it to the APK with EmbeddedNativeLibrary