forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Make AndroidAppBuilder "dotnet build" friendly (dotnet#42597)
- Loading branch information
Showing
7 changed files
with
161 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 32 additions & 29 deletions
61
...ono/netcore/sample/Android/Program.csproj → ...re/sample/Android/AndroidSampleApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,75 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="BuildApp"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<OutputPath>bin</OutputPath> | ||
<DebugType>Portable</DebugType> | ||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems> | ||
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework> | ||
<MicrosoftNetCoreAppRuntimePackDir>$(ArtifactsBinDir)microsoft.netcore.app.runtime.android-$(TargetArchitecture)\$(Configuration)\runtimes\android-$(TargetArchitecture)</MicrosoftNetCoreAppRuntimePackDir> | ||
<AndroidAppBuilderDir>$(ArtifactsBinDir)AndroidAppBuilder\$(Configuration)\$(NetCoreAppCurrent)</AndroidAppBuilderDir> | ||
<EnableTargetingPackDownload>false</EnableTargetingPackDownload> | ||
<PublishTrimmed>true</PublishTrimmed> | ||
<_TrimmerDefaultAction>link</_TrimmerDefaultAction> | ||
<InvariantGlobalization>true</InvariantGlobalization> <!-- workaround for https://github.com/dotnet/runtime/issues/41866 --> | ||
<TargetArchitecture Condition="'$(TargetArchitecture)'==''">x64</TargetArchitecture> | ||
<TargetOS>Android</TargetOS> | ||
<MicrosoftNetCoreAppRuntimePackDir>$(ArtifactsBinDir)microsoft.netcore.app.runtime.android-$(TargetArchitecture)\$(Configuration)\runtimes\android-$(TargetArchitecture)\</MicrosoftNetCoreAppRuntimePackDir> | ||
<NoWarn>$(NoWarn),CA1050</NoWarn> | ||
</PropertyGroup> | ||
|
||
<!-- Redirect 'dotnet publish' to in-tree runtime pack --> | ||
<Target Name="TrickRuntimePackLocation" AfterTargets="ProcessFrameworkReferences"> | ||
<Target Name="RebuildAndroidAppBuilder"> | ||
<ItemGroup> | ||
<RuntimePack> | ||
<PackageDirectory>$(ArtifactsBinDir)microsoft.netcore.app.runtime.android-$(TargetArchitecture)\$(Configuration)</PackageDirectory> | ||
</RuntimePack> | ||
<AndroidAppBuilderProject Include="$(RepoTasksDir)mobile.tasks\AndroidAppBuilder\AndroidAppBuilder.csproj" /> | ||
</ItemGroup> | ||
<Message Text="Packaged ID: %(RuntimePack.PackageDirectory)" Importance="high" /> | ||
<MSBuild Projects="@(AndroidAppBuilderProject)" | ||
Properties="Configuration=$(Configuration);MSBuildRestoreSessionId=$([System.Guid]::NewGuid())" | ||
Targets="Restore"/> | ||
<MSBuild Projects="@(AndroidAppBuilderProject)" | ||
Properties="Configuration=$(Configuration)" | ||
Targets="Build;Publish"/> | ||
</Target> | ||
|
||
<UsingTask TaskName="AndroidAppBuilderTask" | ||
AssemblyFile="$(AndroidAppBuilderDir)\AndroidAppBuilder.dll" /> | ||
<UsingTask TaskName="AndroidAppBuilderTask" AssemblyFile="$(AndroidAppBuilderTasksAssemblyPath)"/> | ||
|
||
<!-- Build APK during 'dotnet publish' --> | ||
<Target Name="BuildAppBundle" AfterTargets="CopyFilesToPublishDirectory"> | ||
<Target Name="BuildApp" DependsOnTargets="RebuildAndroidAppBuilder;Build"> | ||
<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> | ||
<ApkDir>$(MSBuildThisFileDirectory)$(PublishDir)\apk</ApkDir> | ||
<StripDebugSymbols>False</StripDebugSymbols> | ||
<StripDebugSymbols Condition="'$(Configuration)' == 'Release'">True</StripDebugSymbols> | ||
<AdbTool>$(ANDROID_SDK_ROOT)\platform-tools\adb</AdbTool> | ||
<ApkDir>$(OutputPath)apk</ApkDir> | ||
</PropertyGroup> | ||
|
||
<RemoveDir Directories="$(ApkDir)" /> | ||
<ItemGroup> | ||
<AssemblySearchPaths Include="bin" /> | ||
<AssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackDir)native"/> | ||
<AssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackDir)lib\$(NetCoreAppCurrent)"/> | ||
</ItemGroup> | ||
|
||
<!-- These native libs are not needed for HelloWorld | ||
and can be deleted to save 0.4 Mb for APK size --> | ||
<Delete Files="$(PublishDir)\libSystem.IO.Compression.Native.so" /> | ||
<Delete Files="$(PublishDir)\libSystem.Security.Cryptography.Native.OpenSsl.so" /> | ||
<RemoveDir Directories="$(ApkDir)" /> | ||
|
||
<AndroidAppBuilderTask | ||
SourceDir="$(OutputPath)" | ||
Abi="$(AndroidAbi)" | ||
ProjectName="HelloAndroid" | ||
MonoRuntimeHeaders="%(ResolvedRuntimePack.PackageDirectory)\runtimes\android-$(Platform)\native\include\mono-2.0" | ||
MainLibraryFileName="Program.dll" | ||
MonoRuntimeHeaders="$(MicrosoftNetCoreAppRuntimePackDir)\native\include\mono-2.0" | ||
MainLibraryFileName="$(AssemblyName).dll" | ||
StripDebugSymbols="$(StripDebugSymbols)" | ||
SourceDir="$(MSBuildThisFileDirectory)$(PublishDir)" | ||
AssemblySearchPaths="@(AssemblySearchPaths)" | ||
OutputDir="$(ApkDir)"> | ||
<Output TaskParameter="ApkBundlePath" PropertyName="ApkBundlePath" /> | ||
<Output TaskParameter="ApkPackageId" PropertyName="ApkPackageId" /> | ||
</AndroidAppBuilderTask> | ||
|
||
<Message Importance="High" Text="Apk: $(ApkBundlePath)"/> | ||
<Message Importance="High" Text="PackageId: $(ApkPackageId)"/> | ||
|
||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) kill-server"/> | ||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) start-server"/> | ||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) logcat -c" ContinueOnError="WarnAndContinue" /> | ||
<Message Condition="'$(DeployAndRun)' == 'true'" Importance="High" Text="Uninstalling app if it exists (throws an error if it doesn't but it can be ignored):"/> | ||
<Message Condition="'$(DeployAndRun)' == 'true'" Importance="High" Text="Uninstalling apk (ignore errors if any):"/> | ||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) uninstall net.dot.HelloAndroid" ContinueOnError="WarnAndContinue" /> | ||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) install $(ApkDir)/bin/HelloAndroid.apk" /> | ||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) shell am instrument -w net.dot.HelloAndroid/net.dot.MonoRunner" /> | ||
<Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) logcat -d -s DOTNET" /> | ||
</Target> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
MONO_CONFIG=Release | ||
MONO_ARCH=arm64 | ||
MONO_ARCH=x64 | ||
DOTNET := ../../../../.././dotnet.sh | ||
|
||
all: runtimepack run | ||
|
||
appbuilder: | ||
$(DOTNET) build -c Release ../../../../../tools-local/tasks/mobile.tasks/AndroidAppBuilder/AndroidAppBuilder.csproj | ||
|
||
runtimepack: | ||
../../../../.././build.sh Mono+Libs -os Android -arch $(MONO_ARCH) -c $(MONO_CONFIG) | ||
|
||
run: clean appbuilder | ||
$(DOTNET) publish -c $(MONO_CONFIG) -r android-$(MONO_ARCH) \ | ||
/p:TargetArchitecture=$(MONO_ARCH) /p:DeployAndRun=true | ||
run: | ||
$(DOTNET) build \ | ||
/p:TargetArchitecture=$(MONO_ARCH) \ | ||
/p:TargetOS=Android \ | ||
/p:Configuration=$(MONO_CONFIG) \ | ||
/p:DeployAndRun=true | ||
|
||
clean: | ||
rm -rf bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tools-local/tasks/mobile.tasks/AndroidAppBuilder/AssemblyResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
internal class AssemblyResolver : MetadataAssemblyResolver | ||
{ | ||
private string[] _searchPaths; | ||
|
||
public AssemblyResolver(string[] searchPaths) => _searchPaths = searchPaths; | ||
|
||
public static string[] ResolveDependencies( | ||
string[] assembliesToResolve, string[] searchPaths, bool ignoreErrors = true) | ||
{ | ||
var assemblies = new Dictionary<string, Assembly>(); | ||
var mlc = new MetadataLoadContext(new AssemblyResolver(searchPaths), "System.Private.CoreLib"); | ||
foreach (string assemblyPath in assembliesToResolve) | ||
{ | ||
try | ||
{ | ||
AddAssembly(mlc, mlc.LoadFromAssemblyPath(assemblyPath), assemblies, ignoreErrors); | ||
} | ||
catch (Exception) | ||
{ | ||
if (!ignoreErrors) | ||
{ | ||
throw; | ||
} | ||
} | ||
} | ||
return assemblies.Values.Select(i => i.Location).Distinct().ToArray(); | ||
} | ||
|
||
private static void AddAssembly(MetadataLoadContext mlc, Assembly assembly, Dictionary<string, Assembly> assemblies, bool ignoreErrors) | ||
{ | ||
if (assemblies.ContainsKey(assembly.GetName().Name!)) | ||
return; | ||
assemblies[assembly.GetName().Name!] = assembly; | ||
foreach (AssemblyName name in assembly.GetReferencedAssemblies()) | ||
{ | ||
try | ||
{ | ||
Assembly refAssembly = mlc.LoadFromAssemblyName(name); | ||
AddAssembly(mlc, refAssembly, assemblies, ignoreErrors); | ||
} | ||
catch (Exception) | ||
{ | ||
if (!ignoreErrors) | ||
{ | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public override Assembly? Resolve(MetadataLoadContext context, AssemblyName assemblyName) | ||
{ | ||
string name = assemblyName.Name!; | ||
foreach (string dir in _searchPaths) | ||
{ | ||
string path = Path.Combine(dir, name + ".dll"); | ||
if (File.Exists(path)) | ||
return context.LoadFromAssemblyPath(path); | ||
} | ||
return null; | ||
} | ||
} |