Skip to content

AndroidNativeLibrary not getting included in Release [Android] #10223

@J0nathan550

Description

@J0nathan550

Android framework version

net9.0-android

Affected platform version

VS 2022

Description

Hello, when I debug my application it works just fine, but in Release my AndroidNativeLibrary gets trimmed away.

Is there any solution to this?

.csproj of Android:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
	<OutputType>Exe</OutputType>
	<TargetFramework>net9.0-android</TargetFramework>
	<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
	<Nullable>enable</Nullable>
	<ApplicationId>com.tyrrrz.youtubedownloader</ApplicationId>
	<ApplicationVersion>1</ApplicationVersion>
	<ApplicationDisplayVersion>1.0.0.0</ApplicationDisplayVersion>
	<AndroidPackageFormat>apk</AndroidPackageFormat>
	<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
  </PropertyGroup>

	<ItemGroup>
    <AndroidResource Include="Icon.png">
      <Link>Resources\drawable\Icon.png</Link>
    </AndroidResource>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Avalonia.Android" Version="11.3.1" />
    <PackageReference Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.0.1.16" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\YoutubeDownloader.Core\YoutubeDownloader.Core.csproj" />
    <ProjectReference Include="..\YoutubeDownloader\YoutubeDownloader.csproj" />
  </ItemGroup>

	<PropertyGroup>
		<DownloadFFmpegAndroid>true</DownloadFFmpegAndroid>
	</PropertyGroup>

	<!-- Include FFmpeg binaries for different Android architectures -->
	<ItemGroup>
		<!-- ARM (32-bit)
		<AndroidAsset Include="arm\ffmpeg" Condition="Exists('arm\ffmpeg')">
			<Link>assets\arm\ffmpeg</Link>
		</AndroidAsset>-->

		<!-- ARM64 (64-bit) -->

		<!-- Remove for now, as these architectures are not commonly used in Android development anymore
		<AndroidAsset Include="armv7-a\ffmpeg" Condition="Exists('armv7-a\ffmpeg')">
			<Link>assets\armv7-a\ffmpeg</Link>
		</AndroidAsset>

		<AndroidAsset Include="arm-v7n\ffmpeg" Condition="Exists('arm-v7n\ffmpeg')">
			<Link>assets\arm-v7n\ffmpeg</Link>
		</AndroidAsset>
		-->
		
		<!-- x86 (32-bit Intel)
		<AndroidAsset Include="i686\ffmpeg" Condition="Exists('i686\ffmpeg')">
			<Link>assets\i686\ffmpeg</Link>
		</AndroidAsset>-->

		<!-- x86_64 (64-bit Intel) -->
		<AndroidNativeLibrary Include="x86_64\ffmpeg" Condition="Exists('x86_64\ffmpeg')">
			<Link>libs\x86_64\ffmpeg</Link>
			<CopyToOutputDirectory>Always</CopyToOutputDirectory>
		</AndroidNativeLibrary>

		<AndroidNativeLibrary Include="arm64-v8a\ffmpeg" Condition="Exists('arm64-v8a\ffmpeg')">
			<Link>libs\arm64-v8a\ffmpeg</Link>
			<CopyToOutputDirectory>Always</CopyToOutputDirectory>
		</AndroidNativeLibrary>

	</ItemGroup>

	<!-- Target to download FFmpeg binaries for Android -->
	<Target Name="DownloadFFmpegAndroid" BeforeTargets="BeforeBuild" Condition="$(DownloadFFmpegAndroid) AND !Exists('arm64-v8a\ffmpeg') AND !Exists('arm\ffmpeg')">

		<!-- Download all architectures by default for maximum compatibility -->
		<Exec Command="pwsh -ExecutionPolicy Bypass -File $(ProjectDir)/Download-FFmpeg-Android.ps1 -DownloadAll -OutputPath $(ProjectDir)" LogStandardErrorAsError="true" ContinueOnError="false" />
	</Target>
	
</Project>

FFMPEGInitializer:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using YoutubeDownloader.Framework;
using YoutubeDownloader.ViewModels.Dialogs;

namespace YoutubeDownloader.Android;

public static class AndroidFFmpegInitializer
{
    public static async void Initialize()
    {
        var extractedPath = await ExtractFFmpegFromAssetsAsync();
        if (!string.IsNullOrEmpty(extractedPath))
        {
            Core.Downloading.FFmpeg.SetCustomPath(extractedPath);
        }
    }

    private static async Task<string?> ExtractFFmpegFromAssetsAsync()
    {
        try
        {
            var appInfo = global::Android.App.Application.Context.ApplicationInfo;
            var path = Path.Combine(appInfo!.NativeLibraryDir!, "ffmpeg");
            return path;
        }
        catch (Exception ex)
        {
            DialogManager dialogManager = new();
            MessageBoxViewModel messageBoxViewModel = new()
            {
                Title = "FFmpeg Extraction Error",
                Message = $"Failed to extract FFmpeg from assets. Please ensure the file exists in the assets directory.\n{ex.Message}",
                DefaultButtonText = "OK"
            };
            await dialogManager.ShowDialogAsync(messageBoxViewModel);
            Debug.WriteLine($"FFmpeg extraction failed: {ex.Message}");
            return null;
        }
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.tyrrrz.youtubedownloader" android:versionCode="1" android:versionName="1.0.0.0">
	<uses-permission android:name="android.permission.INTERNET" />
	<application android:label="YoutubeDownloader" android:icon="@drawable/Icon" />
</manifest>

Steps to Reproduce

NONE

Did you find any workaround?

NONE

Relevant log output

NONE

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: App+Library BuildIssues when building Library projects or Application projects.need-attentionA xamarin-android contributor needs to review

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions