Skip to content

Commit

Permalink
Remove MetadataLoadContext dependency from AndroidAppBuilder (#43339)
Browse files Browse the repository at this point in the history
This will allow AndroidAppBuilder to be packaged without having unnecessary dependencies.
  • Loading branch information
steveisok authored Oct 13, 2020
1 parent ce51448 commit cb51193
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 152 deletions.
8 changes: 4 additions & 4 deletions eng/testing/tests.mobile.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
AssemblyFile="$(AndroidAppBuilderTasksAssemblyPath)" />

<Target Condition="'$(TargetOS)' == 'Android'" Name="BundleTestAndroidApp">
<Error Condition="!Exists('$(MicrosoftNetCoreAppRuntimePackRidDir)')" Text="MicrosoftNetCoreAppRuntimePackRidDir=$(MicrosoftNetCoreAppRuntimePackRidDir) doesn't exist" />

<!-- TEMP: consume OpenSSL binaries from external sources via env. variables.-->
<PropertyGroup>
<AndroidAbi Condition="'$(TargetArchitecture)' == 'arm64'">arm64-v8a</AndroidAbi>
<AndroidAbi Condition="'$(TargetArchitecture)' == 'arm'">armeabi-v7a</AndroidAbi>
<AndroidAbi Condition="'$(TargetArchitecture)' == 'x64'">x86_64</AndroidAbi>
<AndroidAbi Condition="'$(TargetArchitecture)' == 'x86'">x86</AndroidAbi>
</PropertyGroup>

<Error Condition="!Exists('$(MicrosoftNetCoreAppRuntimePackRidDir)')" Text="MicrosoftNetCoreAppRuntimePackRidDir=$(MicrosoftNetCoreAppRuntimePackRidDir) doesn't exist" />

<!-- TEMP: consume OpenSSL binaries from external sources via env. variables -->
<Copy Condition="'$(ANDROID_OPENSSL_AAR)' != ''"
SourceFiles="$(ANDROID_OPENSSL_AAR)\prefab\modules\crypto\libs\android.$(AndroidAbi)\libcrypto.so"
DestinationFolder="$(PublishDir)" SkipUnchangedFiles="true"/>
Expand All @@ -49,7 +49,7 @@

<RemoveDir Directories="$(BundleDir)" />
<AndroidAppBuilderTask
Abi="$(AndroidAbi)"
RuntimeIdentifier="$(RuntimeIdentifier)"
ProjectName="$(AssemblyName)"
MonoRuntimeHeaders="$(MicrosoftNetCoreAppRuntimePackNativeDir)include\mono-2.0"
MainLibraryFileName="AndroidTestRunner.dll"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-->

<PropertyGroup>
<MonoEnableCMake Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsOSX)' == 'true' or '$(TargetsAndroid)' == 'true'">true</MonoEnableCMake>
<MonoEnableCMake Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsOSX)' == 'true'">true</MonoEnableCMake>
<MonoCrossDir Condition="'$(MonoCrossDir)' == '' and '$(ROOTFS_DIR)' != ''">$(ROOTFS_DIR)</MonoCrossDir>
<MonoEnableInterpreter Condition="'$(MonoEnableInterpreter)' == ''">false</MonoEnableInterpreter>
<ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</ScriptExt>
Expand Down
37 changes: 14 additions & 23 deletions src/mono/netcore/sample/Android/AndroidSampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,44 @@
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TargetArchitecture Condition="'$(TargetArchitecture)'==''">x64</TargetArchitecture>
<TargetOS>Android</TargetOS>
<MicrosoftNetCoreAppRuntimePackDir>$(ArtifactsBinDir)microsoft.netcore.app.runtime.android-$(TargetArchitecture)\$(Configuration)\runtimes\android-$(TargetArchitecture)\</MicrosoftNetCoreAppRuntimePackDir>
<EnableTargetingPackDownload>false</EnableTargetingPackDownload>
<RuntimeIdentifier>android-$(TargetArchitecture)</RuntimeIdentifier>
<PublishTrimmed Condition="'$(Configuration)' == 'Release'">true</PublishTrimmed>
<_TrimmerDefaultAction Condition="'$(Configuration)' == 'Release'">link</_TrimmerDefaultAction>
<Optimized Condition="'$(Configuration)' == 'Release'">True</Optimized>
<MicrosoftNetCoreAppRuntimePackDir>$(ArtifactsBinDir)microsoft.netcore.app.runtime.$(RuntimeIdentifier)\$(Configuration)\runtimes\android-$(TargetArchitecture)\</MicrosoftNetCoreAppRuntimePackDir>
<NoWarn>$(NoWarn),CA1050</NoWarn>
</PropertyGroup>

<Target Name="RebuildAndroidAppBuilder">
<!-- Redirect 'dotnet publish' to in-tree runtime pack -->
<Target Name="TrickRuntimePackLocation" AfterTargets="ProcessFrameworkReferences">
<ItemGroup>
<AndroidAppBuilderProject Include="$(RepoTasksDir)mobile.tasks\AndroidAppBuilder\AndroidAppBuilder.csproj" />
<RuntimePack>
<PackageDirectory>$(ArtifactsBinDir)microsoft.netcore.app.runtime.$(RuntimeIdentifier)\$(Configuration)</PackageDirectory>
</RuntimePack>
</ItemGroup>
<MSBuild Projects="@(AndroidAppBuilderProject)"
Properties="Configuration=$(Configuration);MSBuildRestoreSessionId=$([System.Guid]::NewGuid())"
Targets="Restore"/>
<MSBuild Projects="@(AndroidAppBuilderProject)"
Properties="Configuration=$(Configuration)"
Targets="Build;Publish"/>
<Message Text="Packaged ID: %(RuntimePack.PackageDirectory)" Importance="high" />
</Target>

<UsingTask TaskName="AndroidAppBuilderTask" AssemblyFile="$(AndroidAppBuilderTasksAssemblyPath)"/>

<Target Name="BuildApp" DependsOnTargets="RebuildAndroidAppBuilder;Build">
<Target Name="BuildApp" AfterTargets="CopyFilesToPublishDirectory">
<PropertyGroup>
<AndroidAbi Condition="'$(Platform)'=='arm64'">arm64-v8a</AndroidAbi>
<AndroidAbi Condition="'$(Platform)'=='arm'">armeabi-v7a</AndroidAbi>
<AndroidAbi Condition="'$(Platform)'=='x64'">x86_64</AndroidAbi>
<AndroidAbi Condition="'$(AndroidAbi)'==''">$(Platform)</AndroidAbi>
<StripDebugSymbols>False</StripDebugSymbols>
<StripDebugSymbols Condition="'$(Configuration)' == 'Release'">True</StripDebugSymbols>
<AdbTool>$(ANDROID_SDK_ROOT)\platform-tools\adb</AdbTool>
<ApkDir>$(OutputPath)apk\</ApkDir>
</PropertyGroup>

<ItemGroup>
<AssemblySearchPaths Include="$(OutputPath)" />
<AssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackDir)native"/>
<AssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackDir)lib\$(NetCoreAppCurrent)"/>
</ItemGroup>

<RemoveDir Directories="$(ApkDir)" />

<AndroidAppBuilderTask
RuntimeIdentifier="$(RuntimeIdentifier)"
SourceDir="$(OutputPath)"
Abi="$(AndroidAbi)"
ProjectName="HelloAndroid"
MonoRuntimeHeaders="$(MicrosoftNetCoreAppRuntimePackDir)\native\include\mono-2.0"
MainLibraryFileName="$(AssemblyName).dll"
StripDebugSymbols="$(StripDebugSymbols)"
AssemblySearchPaths="@(AssemblySearchPaths)"
OutputDir="$(ApkDir)">
<Output TaskParameter="ApkBundlePath" PropertyName="ApkBundlePath" />
<Output TaskParameter="ApkPackageId" PropertyName="ApkPackageId" />
Expand Down
5 changes: 2 additions & 3 deletions src/mono/netcore/sample/Android/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ runtimepack:
../../../../.././build.sh Mono+Libs -os Android -arch $(MONO_ARCH) -c $(MONO_CONFIG)

run:
$(DOTNET) build \
$(DOTNET) publish \
/p:TargetArchitecture=$(MONO_ARCH) \
/p:TargetOS=Android \
/p:Configuration=$(MONO_CONFIG) \
/p:DeployAndRun=true

clean:
rm -rf bin
rm -rf ../../../../../artifacts/bin/AndroidSampleApp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public class AndroidAppBuilderTask : Task
[Required]
public string SourceDir { get; set; } = ""!;

public ITaskItem[]? AssemblySearchPaths { get; set; }

public ITaskItem[]? ExtraAssemblies { get; set; }

[Required]
public string MonoRuntimeHeaders { get; set; } = ""!;

Expand All @@ -24,11 +20,8 @@ public class AndroidAppBuilderTask : Task
/// </summary>
public string MainLibraryFileName { get; set; } = ""!;

/// <summary>
/// Target arch, can be 'x86', 'x86_64', 'armeabi-v7a' or 'arm64-v8a'
/// </summary>
[Required]
public string Abi { get; set; } = ""!;
public string RuntimeIdentifier { get; set; } = ""!;

public string? ProjectName { get; set; }

Expand Down Expand Up @@ -56,6 +49,8 @@ public override bool Execute()
{
Utils.Logger = Log;

string abi = DetermineAbi();

var apkBuilder = new ApkBuilder();
apkBuilder.ProjectName = ProjectName;
apkBuilder.OutputDir = OutputDir;
Expand All @@ -65,10 +60,25 @@ public override bool Execute()
apkBuilder.BuildApiLevel = BuildApiLevel;
apkBuilder.BuildToolsVersion = BuildToolsVersion;
apkBuilder.StripDebugSymbols = StripDebugSymbols;
apkBuilder.AssemblySearchPaths = AssemblySearchPaths?.Select(a => a.ItemSpec)?.ToArray();
apkBuilder.ExtraAssemblies = ExtraAssemblies?.Select(a => a.ItemSpec)?.ToArray();
(ApkBundlePath, ApkPackageId) = apkBuilder.BuildApk(SourceDir, Abi, MainLibraryFileName, MonoRuntimeHeaders);
(ApkBundlePath, ApkPackageId) = apkBuilder.BuildApk(SourceDir, abi, MainLibraryFileName, MonoRuntimeHeaders);

return true;
}

private string DetermineAbi()
{
switch (RuntimeIdentifier)
{
case "android-x86":
return "x86";
case "android-x64":
return "x86_64";
case "android-arm":
return "armeabi-v7a";
case "android-arm64":
return "arm64-v8a";
default:
throw new ArgumentException(RuntimeIdentifier + " is not supported for Android");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
<PackageReference Include="Microsoft.Build.Framework" Version="$(RefOnlyMicrosoftBuildFrameworkVersion)" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="$(RefOnlyMicrosoftBuildTasksCoreVersion)" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(RefOnlyMicrosoftBuildUtilitiesCoreVersion)" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="4.7.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApkBuilder.cs" />
<Compile Include="AndroidAppBuilder.cs" />
<Compile Include="AssemblyResolver.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>

Expand Down
37 changes: 0 additions & 37 deletions tools-local/tasks/mobile.tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class ApkBuilder
public string? BuildToolsVersion { get; set; }
public string? OutputDir { get; set; }
public bool StripDebugSymbols { get; set; }
public string[]? AssemblySearchPaths { get; set; }
public string[]? ExtraAssemblies { get; set; }

public (string apk, string packageId) BuildApk(
string sourceDir, string abi, string entryPointLib, string monoRuntimeHeaders)
Expand Down Expand Up @@ -102,41 +100,6 @@ public class ApkBuilder
extensionsToIgnore.Add(".dbg");
}

var assembliesToResolve = new List<string>();

if (!string.IsNullOrEmpty(entryPointLibPath))
assembliesToResolve.Add(entryPointLibPath);

if (ExtraAssemblies != null)
assembliesToResolve.AddRange(ExtraAssemblies);

// try to resolve dependencies of entryPointLib + ExtraAssemblies from AssemblySearchPaths
// and copy them to sourceDir
if (AssemblySearchPaths?.Length > 0)
{
string[] resolvedDependencies = AssemblyResolver.ResolveDependencies(assembliesToResolve.ToArray(), AssemblySearchPaths, true);
foreach (string resolvedDependency in resolvedDependencies)
{
string destination = Path.Combine(sourceDir, Path.GetFileName(resolvedDependency));
if (!File.Exists(destination))
File.Copy(resolvedDependency, destination);
}
}
else
{
AssemblySearchPaths = new[] {OutputDir};
}

// copy all native libs from AssemblySearchPaths to sourceDir
// TODO: skip some if not used by the app
string[] allFiles = AssemblySearchPaths.SelectMany(p => Directory.GetFiles(p, "*", SearchOption.AllDirectories)).ToArray();
foreach (string nativeLib in allFiles.Where(f => f.EndsWith(".a") || f.EndsWith(".so")))
{
string destination = Path.Combine(sourceDir, Path.GetFileName(nativeLib));
if (!File.Exists(destination))
File.Copy(nativeLib, destination);
}

// Copy sourceDir to OutputDir/assets-tozip (ignore native files)
// these files then will be zipped and copied to apk/assets/assets.zip
Utils.DirectoryCopy(sourceDir, Path.Combine(OutputDir, "assets-tozip"), file =>
Expand Down

This file was deleted.

0 comments on commit cb51193

Please sign in to comment.