dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

AndroidNativeLibrary not getting picked in Release mode. #8363

Open s3743761 opened 11 months ago

s3743761 commented 11 months ago

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

VS 2022 17.7.4, Maui 7.0.92

Description

AndroidNativeLibrary/.so file is not getting picked in release mode but works in debug mode.

I am using a library which requires android native libraries to work. I have build the .so files and added to libs/{ABI} and these are getting picked in Debug mode and the functioality works. on Release mode I get dll not found for the same library.

Steps to Reproduce

This works on Debug mode but will fail with libheif dll not found on Release mode.

Link to public reproduction project repository

https://github.com/s3743761/Maui-repro/tree/main

Did you find any workaround?

No response

Relevant log output

No response

s3743761 commented 11 months ago

I guess adding the following in your csproj solves the issue. But I dont know why? I know useInterpreter is set to true by default for Debug for Hot Reload but I can't understand how this is linked with my case.

<UseInterpreter>True</UseInterpreter>
<MtouchProfiling>True</MtouchProfiling>
dellis1972 commented 11 months ago

Can you check to see if the libde265.so and libheif.so are making it into the aab file? Also which device are you running this on , specifically which abi is it (x86, x86_64, armeabi-v7a or arm64-v8a).

microsoft-github-policy-service[bot] commented 11 months ago

Hi @s3743761. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

s3743761 commented 11 months ago

@dellis1972 How do you check if the files are being included in .aab? also I am pretty sure it's not being included in Release mode without these enteries https://github.com/xamarin/xamarin-android/issues/8363#issuecomment-1733012862. As if it would have, it wouldn't say dll not found.

I am testing it on arm64-v8a and both WSA

dellis1972 commented 11 months ago

@s3743761

The aab file is just a zip file, you can rename the file to .zip and then extract all the contents. The libraries folders should be in the base folder.

s3743761 commented 11 months ago

@dellis1972 Yes they are being included.

dellis1972 commented 11 months ago

@s3743761 it might then be worth checking the dependencies of the .so files. One of the reasons a .so fails to load is if its compiled against library or other .so which cannot be found, you might be either missing a .so file or it was compiled against a library which is not present on the device... that said why it would work in debug but not release does kinda mean it is finding the dependencies. But it would be good to rule that out.

@grendello do you know the best way to check dependencies on android based .so files?

grendello commented 11 months ago

It would require the presence of NDK (or command line compiler tools on macOS) so that the llvm-readelf binary is available:

$ llvm-readelf --needed-libs ./bin/Debug/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-x64/8.0.0-rc.2.23466.4/runtimes/android-x64/native/libmonosgen-2.0.so
NeededLibraries [
  libc.so
  libdl.so
  liblog.so
  libm.so
  libz.so
]

llvm-readelf can be found in the Android NDK, in the $NDK_ROOT/toolchains/llvm/prebuilt/$OS-$ARCH/bin/ directory

prabhavmehra commented 11 months ago

@s3743761 it might then be worth checking the dependencies of the .so files. One of the reasons a .so fails to load is if its compiled against library or other .so which cannot be found, you might be either missing a .so file or it was compiled against a library which is not present on the device... that said why it would work in debug but not release does kinda mean it is finding the dependencies. But it would be good to rule that out.

@grendello do you know the best way to check dependencies on android based .so files?

how does that change between a release and debug version? because the same app works in debug but not release

dellis1972 commented 11 months ago

@prabhavmehra I tested your sample on my dotnet 7 install against an arm emulator and it works ok .

I see this

10-03 13:02:25.362  2007  2007 I DOTNET  : heif-dec 1.0.0 LibHeifSharp v3.1.0 libheif v1.16.2
10-03 13:02:25.362  2007  2007 I DOTNET  : HERE2____________________
10-03 13:02:25.362  2007  2007 I DOTNET  : HEVC decoders:
10-03 13:02:25.362  2007  2007 I DOTNET  : LiST______________________________________
10-03 13:02:25.362  2007  2007 I DOTNET  : libde265 = libde265 HEVC decoder, version 1.0.12
10-03 13:02:25.362  2007  2007 I DOTNET  : AV1 decoders:
10-03 13:02:25.362  2007  2007 I DOTNET  : LiST______________________________________
10-03 13:02:25.362  2007  2007 I DOTNET  : 
10-03 13:02:25.363  2007  2007 I DOTNET  : herenew
10-03 13:02:25.363  2007  2007 I DOTNET  : nerw
10-03 13:02:25.363  2007  2007 I DOTNET  : HERE00___________________________________________________________________________________
10-03 13:02:25.363  2007  2007 I DOTNET  : HERE02___________________________________________________________________________________
10-03 13:02:25.363  2007  2007 I DOTNET  : HERE01___________________________________________________________________________________8
10-03 13:02:25.363  2007  2007 I DOTNET  : HERE12___________________________________________________________________________________
10-03 13:02:25.391  2007  2007 I DOTNET  : new
10-03 13:02:25.392  2007  2007 I DOTNET  : HERE13___________________________________________________________________________________
10-03 13:02:25.392  2007  2007 I DOTNET  : HERE14___________________________________________________________________________________
10-03 13:02:25.392  2007  2007 I DOTNET  : HERE15___________________________________________________________________________________
10-03 13:02:25.392  2007  2007 I DOTNET  : newinside
10-03 13:02:25.449  2007  2007 I DOTNET  : newinside2
s3743761 commented 11 months ago

@dellis1972 can you try it on an physical device if possible or WSA. I didn't try it to emulator

dellis1972 commented 11 months ago

The sample worked on my Pixel 4

gleblebedev commented 6 months ago

Fixed similar issue by adding

<AndroidBundleConfigurationFile>BundleConfig.json</AndroidBundleConfigurationFile>

into project PropertyGroup

and adding BundleConfig.json file with

{
"optimizations" : {
"uncompress_native_libraries" : {}
}
}
xaviersand commented 3 months ago

Fixed similar issue by adding

<AndroidBundleConfigurationFile>BundleConfig.json</AndroidBundleConfigurationFile>

into project PropertyGroup

and adding BundleConfig.json file with

{
"optimizations" : {
"uncompress_native_libraries" : {}
}
}

Thanks bro, that fixed a similar issue in my project in which i did a "Jar binding" of a 3rd party library (an android manufacturer gave me .jar files with a SDK for their hardware). i was only running in debug.