Release mode build not working in MAUI app but works in Debug mode - Android

570 Views Asked by At

I created an MAUI app, which uses a reference DLL, the assembly used to perform GET and POST operations to our service.

The API call works perfectly in Debug mode in Android but not working in Release mode. This issue is happening only on Android and not on iOS. In iOS with release mode, the app is working without any issues, using assembly the APIs are working properly.

I have tried adding AssemblyResolve event code for loading the DLL if there was any issue on loading DLL in MauiProgram.cs under CreateMauiApp()

 `AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

 private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
 {
     try
     {
         string codeBase = Assembly.GetExecutingAssembly().Location;
         if (!string.IsNullOrWhiteSpace(codeBase))
         {
             string filename = $"{Path.GetDirectoryName(codeBase)}/TRIMServiceModel.dll";
             if (File.Exists(filename))
             {
                 return Assembly.Load(File.ReadAllBytes(filename));
             }
         }
     }
     catch (Exception ex)
     {
         Debug.Write(ex.Message);
     }
     return null;
 }`

I am not sure why the API not performing the particular POST/GET operation only when running the App in release mode on android device but the same works with iOS(both debug and release mode).

Below is my csproj file propertygroup details for the reference as well:

`<PropertyGroup>
    <TargetFrameworks>;net7.0-android;net7.0-ios</TargetFrameworks> 
    <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
    <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
    <OutputType>Exe</OutputType>
    <RootNamespace>XXXX</RootNamespace>
    <UseMaui>true</UseMaui>
    <SingleProject>true</SingleProject>
    <ImplicitUsings>enable</ImplicitUsings>
    <ProduceReferenceAssembly>true</ProduceReferenceAssembly>
    <!-- Display name -->
    <ApplicationTitle>XXXX</ApplicationTitle>

    <!-- App Identifier -->
    <ApplicationId>XXXXXXX</ApplicationId>
    <ApplicationIdGuid>XXXXX-XXXX-XXXX-XXXX-XXXXXXXX</ApplicationIdGuid>

    <!-- Versions -->
    <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
    <ApplicationVersion>1</ApplicationVersion>

    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">16.1</SupportedOSPlatformVersion>      
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">31.0</SupportedOSPlatformVersion>      
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-android|AnyCPU'">
  <JavaMaximumHeapSize>5G</JavaMaximumHeapSize>
  <EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-android|AnyCPU'">
<UseInterpreter>true</UseInterpreter>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
  <CreatePackage>false</CreatePackage>
  <CodesignKey>iPhone Developer</CodesignKey>
  <CodesignEntitlements>Platforms\iOS\Entitlements.plist</CodesignEntitlements>
  <MtouchLink>SdkOnly</MtouchLink>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'">
  <CreatePackage>false</CreatePackage>
<UseInterpreter>true</UseInterpreter>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>`

Any inputs will be helpful.

2

There are 2 best solutions below

1
Stephen Quan On

The documentation highlights two methods for getting the Assembly

    1. Type.Assembly
    1. Assembly.GetExecutingAssembly()

For the former, it clarifies it as "Get the assembly for a known type in that assembly.". Some examples are:

  • typeof(Example).Assembly
  • typeof(MainPage).Assembly

Whereas the latter is "Get the currently executing assembly".

The latter can change whilst your app is running (i.e. it behaves like a mutable value), particularly, on Android, since it may be in the assembly for Android. So, the former Type.Assembly method is more consistent/reliable for your use case.

Reference:

0
Jianwei Sun - MSFT On

Try to disable trimming and AOT:

enter image description here

For more info you can refer to Linker behavior | Linker.