Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use assembly stores when assemblies are to be embedded #9410

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 2 additions & 52 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ public class BuildApk : AndroidTask

public string RuntimeConfigBinFilePath { get; set; }

public bool UseAssemblyStore { get; set; }

public string ZipFlushFilesLimit { get; set; }

public string ZipFlushSizeLimit { get; set; }
Expand Down Expand Up @@ -423,24 +421,15 @@ void AddRuntimeConfigBlob (DSOWrapperGenerator.Config dsoWrapperConfig, ZipArchi
void AddAssemblies (DSOWrapperGenerator.Config dsoWrapperConfig, ZipArchiveEx apk, bool debug, bool compress, IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>> compressedAssembliesInfo, string assemblyStoreApkName)
{
string sourcePath;
AssemblyCompression.AssemblyData compressedAssembly = null;
string compressedOutputDir = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (ApkOutputPath), "..", "lz4"));
AssemblyStoreBuilder? storeBuilder = null;

if (UseAssemblyStore) {
storeBuilder = new AssemblyStoreBuilder (Log);
}
var storeBuilder = new AssemblyStoreBuilder (Log);

// Add user assemblies
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedUserAssemblies, DoAddAssembliesFromArchCollection);

// Add framework assemblies
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedFrameworkAssemblies, DoAddAssembliesFromArchCollection);

if (!UseAssemblyStore) {
return;
}

Dictionary<AndroidTargetArch, string> assemblyStorePaths = storeBuilder.Generate (AppSharedLibrariesDir);

if (assemblyStorePaths.Count == 0) {
Expand All @@ -467,46 +456,7 @@ void DoAddAssembliesFromArchCollection (TaskLoggingHelper log, AndroidTargetArch
// Thus, we no longer just store them in the apk but we call the `GetCompressionMethod` method to find out whether
// or not we're supposed to compress .so files.
sourcePath = CompressAssembly (assembly);
if (UseAssemblyStore) {
storeBuilder.AddAssembly (sourcePath, assembly, includeDebugSymbols: debug);
return;
}

// Add assembly
(string assemblyPath, string assemblyDirectory) = GetInArchiveAssemblyPath (assembly);
string wrappedSourcePath = DSOWrapperGenerator.WrapIt (Log, dsoWrapperConfig, arch, sourcePath, Path.GetFileName (assemblyPath));
AddFileToArchiveIfNewer (apk, wrappedSourcePath, assemblyPath, compressionMethod: GetCompressionMethod (assemblyPath));

// Try to add config if exists
var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config");
AddAssemblyConfigEntry (dsoWrapperConfig, apk, arch, assemblyDirectory, config);

// Try to add symbols if Debug
if (!debug) {
return;
}

string symbols = Path.ChangeExtension (assembly.ItemSpec, "pdb");
if (!File.Exists (symbols)) {
return;
}

string archiveSymbolsPath = assemblyDirectory + MonoAndroidHelper.MakeDiscreteAssembliesEntryName (Path.GetFileName (symbols));
string wrappedSymbolsPath = DSOWrapperGenerator.WrapIt (Log, dsoWrapperConfig, arch, symbols, Path.GetFileName (archiveSymbolsPath));
AddFileToArchiveIfNewer (
apk,
wrappedSymbolsPath,
archiveSymbolsPath,
compressionMethod: GetCompressionMethod (archiveSymbolsPath)
);
}

void EnsureCompressedAssemblyData (string sourcePath, uint descriptorIndex)
{
if (compressedAssembly == null)
compressedAssembly = new AssemblyCompression.AssemblyData (sourcePath, descriptorIndex);
else
compressedAssembly.SetData (sourcePath, descriptorIndex);
storeBuilder.AddAssembly (sourcePath, assembly, includeDebugSymbols: debug);
}

string CompressAssembly (ITaskItem assembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class GeneratePackageManagerJava : AndroidTask

public ITaskItem[] SatelliteAssemblies { get; set; }

public bool UseAssemblyStore { get; set; }

[Required]
public string OutputDirectory { get; set; }

Expand Down Expand Up @@ -76,6 +74,7 @@ public class GeneratePackageManagerJava : AndroidTask
public string AndroidSequencePointsMode { get; set; }
public bool EnableSGenConcurrent { get; set; }
public string? CustomBundleConfigFile { get; set; }
public bool FastDevEnabled { get; set; }

bool _Debug {
get {
Expand Down Expand Up @@ -204,21 +203,7 @@ void AddEnvironment ()
throw new InvalidOperationException ($"Unsupported BoundExceptionType value '{BoundExceptionType}'");
}

int assemblyNameWidth = 0;
Encoding assemblyNameEncoding = Encoding.UTF8;

Action<ITaskItem> updateNameWidth = (ITaskItem assembly) => {
if (UseAssemblyStore) {
return;
}

string assemblyName = Path.GetFileName (assembly.ItemSpec);
int nameBytes = assemblyNameEncoding.GetBytes (assemblyName).Length;
if (nameBytes > assemblyNameWidth) {
assemblyNameWidth = nameBytes;
}
};

int assemblyCount = 0;
bool enableMarshalMethods = EnableMarshalMethods;
HashSet<string> archAssemblyNames = null;
Expand Down Expand Up @@ -249,7 +234,6 @@ void AddEnvironment ()

if (SatelliteAssemblies != null) {
foreach (ITaskItem assembly in SatelliteAssemblies) {
updateNameWidth (assembly);
updateAssemblyCount (assembly);
}
}
Expand All @@ -258,7 +242,6 @@ void AddEnvironment ()
int jnienv_initialize_method_token = -1;
int jnienv_registerjninatives_method_token = -1;
foreach (var assembly in ResolvedAssemblies) {
updateNameWidth (assembly);
updateAssemblyCount (assembly);

if (android_runtime_jnienv_class_token != -1) {
Expand All @@ -272,17 +255,6 @@ void AddEnvironment ()
GetRequiredTokens (assembly.ItemSpec, out android_runtime_jnienv_class_token, out jnienv_initialize_method_token, out jnienv_registerjninatives_method_token);
}

if (!UseAssemblyStore) {
int abiNameLength = 0;
foreach (string abi in SupportedAbis) {
if (abi.Length <= abiNameLength) {
continue;
}
abiNameLength = abi.Length;
}
assemblyNameWidth += abiNameLength + 2; // room for '/' and the terminating NUL
}

MonoComponent monoComponents = MonoComponent.None;
if (MonoComponents != null && MonoComponents.Length > 0) {
foreach (ITaskItem item in MonoComponents) {
Expand Down Expand Up @@ -334,17 +306,16 @@ void AddEnvironment ()
JniAddNativeMethodRegistrationAttributePresent = NativeCodeGenState.TemplateJniAddNativeMethodRegistrationAttributePresent,
HaveRuntimeConfigBlob = haveRuntimeConfigBlob,
NumberOfAssembliesInApk = assemblyCount,
BundledAssemblyNameWidth = assemblyNameWidth,
MonoComponents = (MonoComponent)monoComponents,
NativeLibraries = uniqueNativeLibraries,
HaveAssemblyStore = UseAssemblyStore,
AndroidRuntimeJNIEnvToken = android_runtime_jnienv_class_token,
JNIEnvInitializeToken = jnienv_initialize_method_token,
JNIEnvRegisterJniNativesToken = jnienv_registerjninatives_method_token,
JniRemappingReplacementTypeCount = jniRemappingNativeCodeInfo == null ? 0 : jniRemappingNativeCodeInfo.ReplacementTypeCount,
JniRemappingReplacementMethodIndexEntryCount = jniRemappingNativeCodeInfo == null ? 0 : jniRemappingNativeCodeInfo.ReplacementMethodIndexEntryCount,
MarshalMethodsEnabled = EnableMarshalMethods,
IgnoreSplitConfigs = ShouldIgnoreSplitConfigs (),
FastDevEnabled = FastDevEnabled,
};
LLVMIR.LlvmIrModule appConfigModule = appConfigAsmGen.Construct ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,30 @@ public void BuildAotApplicationWithSpecialCharactersInProject (string testName,
new object[] {
/* supportedAbis */ "arm64-v8a",
/* enableLLVM */ false,
/* usesAssemblyBlobs */ false,
},
new object[] {
/* supportedAbis */ "armeabi-v7a;x86",
/* enableLLVM */ true,
/* usesAssemblyBlobs */ true,
},
new object[] {
/* supportedAbis */ "armeabi-v7a;arm64-v8a;x86;x86_64",
/* enableLLVM */ false,
/* usesAssemblyBlobs */ true,
},
new object[] {
/* supportedAbis */ "armeabi-v7a;arm64-v8a;x86;x86_64",
/* enableLLVM */ true,
/* usesAssemblyBlobs */ false,
},
};

[Test]
[TestCaseSource (nameof (AotChecks))]
public void BuildAotApplicationWithNdkAndBundleAndÜmläüts (string supportedAbis, bool enableLLVM, bool usesAssemblyBlobs)
public void BuildAotApplicationWithNdkAndBundleAndÜmläüts (string supportedAbis, bool enableLLVM)
{
if (IsWindows)
Assert.Ignore ("https://github.com/dotnet/runtime/issues/88625");

var abisSanitized = supportedAbis.Replace (";", "").Replace ("-", "").Replace ("_", "");
var path = Path.Combine ("temp", string.Format ("BuildAotNdk AndÜmläüts_{0}_{1}_{2}", abisSanitized, enableLLVM, usesAssemblyBlobs));
var path = Path.Combine ("temp", string.Format ("BuildAotNdk AndÜmläüts_{0}_{1}", abisSanitized, enableLLVM));
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
AotAssemblies = true,
Expand All @@ -174,7 +170,6 @@ public void BuildAotApplicationWithNdkAndBundleAndÜmläüts (string supportedAb
proj.SetProperty ("AndroidNdkDirectory", AndroidNdkPath);
proj.SetRuntimeIdentifiers (supportedAbis.Split (';'));
proj.SetProperty ("EnableLLVM", enableLLVM.ToString ());
proj.SetProperty ("AndroidUseAssemblyStore", usesAssemblyBlobs.ToString ());
bool checkMinLlvmPath = enableLLVM && (supportedAbis == "armeabi-v7a" || supportedAbis == "x86");
if (checkMinLlvmPath) {
// Set //uses-sdk/@android:minSdkVersion so that LLVM uses the right libc.so
Expand All @@ -195,7 +190,7 @@ public void BuildAotApplicationWithNdkAndBundleAndÜmläüts (string supportedAb
var apk = Path.Combine (Root, b.ProjectDirectory,
proj.OutputPath, $"{proj.PackageName}-Signed.apk");

var helper = new ArchiveAssemblyHelper (apk, usesAssemblyBlobs);
var helper = new ArchiveAssemblyHelper (apk);
Assert.IsTrue (helper.Exists ($"assemblies/{abi}/UnnamedProject.dll"), $"{abi}/UnnamedProject.dll should be in {proj.PackageName}-Signed.apk");
using (var zipFile = ZipHelper.OpenZip (apk)) {
Assert.IsNotNull (ZipHelper.ReadFileFromZip (zipFile,
Expand All @@ -215,21 +210,20 @@ public void BuildAotApplicationWithNdkAndBundleAndÜmläüts (string supportedAb

[Test]
[TestCaseSource (nameof (AotChecks))]
public void BuildAotApplicationAndÜmläüts (string supportedAbis, bool enableLLVM, bool usesAssemblyBlobs)
public void BuildAotApplicationAndÜmläüts (string supportedAbis, bool enableLLVM)
{
if (IsWindows)
Assert.Ignore ("https://github.com/dotnet/runtime/issues/88625");

var abisSanitized = supportedAbis.Replace (";", "").Replace ("-", "").Replace ("_", "");
var path = Path.Combine ("temp", string.Format ("BuildAot AndÜmläüts_{0}_{1}_{2}", abisSanitized, enableLLVM, usesAssemblyBlobs));
var path = Path.Combine ("temp", string.Format ("BuildAot AndÜmläüts_{0}_{1}", abisSanitized, enableLLVM));
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
AotAssemblies = true,
PackageName = "com.xamarin.buildaotappandbundlewithspecialchars",
};
proj.SetRuntimeIdentifiers (supportedAbis.Split (';'));
proj.SetProperty ("EnableLLVM", enableLLVM.ToString ());
proj.SetProperty ("AndroidUseAssemblyStore", usesAssemblyBlobs.ToString ());
using (var b = CreateApkBuilder (path)) {
b.ThrowOnBuildFailure = false;
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
Expand All @@ -240,7 +234,7 @@ public void BuildAotApplicationAndÜmläüts (string supportedAbis, bool enableL
var apk = Path.Combine (Root, b.ProjectDirectory,
proj.OutputPath, $"{proj.PackageName}-Signed.apk");

var helper = new ArchiveAssemblyHelper (apk, usesAssemblyBlobs);
var helper = new ArchiveAssemblyHelper (apk);
Assert.IsTrue (helper.Exists ($"assemblies/{abi}/UnnamedProject.dll"), $"{abi}/UnnamedProject.dll should be in {proj.PackageName}-Signed.apk");
using (var zipFile = ZipHelper.OpenZip (apk)) {
Assert.IsNotNull (ZipHelper.ReadFileFromZip (zipFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,97 +16,81 @@ public partial class BuildTest : BaseTest
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-x86",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-x64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ true,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ true,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm64",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ true,
/* aot */ true,
/* usesAssemblyStore */ false,
},
};

Expand Down
Loading
Loading