diff options
author | Simon Rozsival <simon@rozsival.com> | 2022-07-20 12:49:36 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-20 12:49:36 +0300 |
commit | e9b40f5ebe6edea9ff6095aa5052fc3d0d5aaf0f (patch) | |
tree | 589560993cc9e35cb84f267507c2daf5a06bd075 /src/tasks | |
parent | 47974ffa3f03d05369c62858a6d143ea47fef649 (diff) |
[Android] Fix some System.Net tests (#71385)
* Implement IncludeNetworkSecurityConfig
* Use IncludeNetworkSecurityConfig
* Fix tests
* Remove active issue attributes
* Rename certificate file
* Revert unnecessary changes
* TMP: re-throw caught exception to reveal more information in logs
* Revert "TMP: re-throw caught exception to reveal more information in logs"
This reverts commit 039d051d6ac60c1c3f1baf37b0c06872f87cac84.
* TMP: add and update res folders until they are merged into runtime-assets
* Fix System.Net.Requests tests
* Re-enable System.Net.Http functional tests
* Try fixing failing test
* Skip failing test
* Add missing using
* Remove temporary test data
* Relax exception type assertion for Android
* Code cleanup
* Fix test failures in System.Net.WebSockets.Client.Tests
* Remove active issue attribute
* Remove most explicit usages of the server certificate custom validation callback in tests
* Fix several tests
* Remove weird condition
* Code clean-up
* Remove unrelated active issue attributes
* Turn ActiveIssue attributes into permanent SkipOnPlatform
* Update explanation for SkipOnPlatform
* Revert partial fix and keep active issue
* Keep active issue for android x64 and x86
* Remove invalid attribute
* Fix test on Browser
* Allow all certificates by default
* Fix build
* Fix http client handler factory on Browser
* Skip failing test
* Fix net48 build
* Revert unintentional change in WinHttpHandler tests
* Avoid using unsupported property on Browser
* Remove unnecessary default value
* Avoid using unsupported property on Browser
Diffstat (limited to 'src/tasks')
-rw-r--r-- | src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs | 3 | ||||
-rw-r--r-- | src/tasks/AndroidAppBuilder/ApkBuilder.cs | 29 | ||||
-rw-r--r-- | src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml | 3 |
3 files changed, 33 insertions, 2 deletions
diff --git a/src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs b/src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs index b2ae7276fe9..66eef2cd542 100644 --- a/src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs +++ b/src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs @@ -80,6 +80,8 @@ public class AndroidAppBuilderTask : Task /// </summary> public string? NativeMainSource { get; set; } + public bool IncludeNetworkSecurityConfig { get; set; } + public string? KeyStorePath { get; set; } public bool ForceInterpreter { get; set; } @@ -105,6 +107,7 @@ public class AndroidAppBuilderTask : Task apkBuilder.BuildToolsVersion = BuildToolsVersion; apkBuilder.StripDebugSymbols = StripDebugSymbols; apkBuilder.NativeMainSource = NativeMainSource; + apkBuilder.IncludeNetworkSecurityConfig = IncludeNetworkSecurityConfig; apkBuilder.KeyStorePath = KeyStorePath; apkBuilder.ForceInterpreter = ForceInterpreter; apkBuilder.ForceAOT = ForceAOT; diff --git a/src/tasks/AndroidAppBuilder/ApkBuilder.cs b/src/tasks/AndroidAppBuilder/ApkBuilder.cs index 9ed098ac55a..11333b29e39 100644 --- a/src/tasks/AndroidAppBuilder/ApkBuilder.cs +++ b/src/tasks/AndroidAppBuilder/ApkBuilder.cs @@ -24,6 +24,7 @@ public class ApkBuilder public string OutputDir { get; set; } = ""!; public bool StripDebugSymbols { get; set; } public string? NativeMainSource { get; set; } + public bool IncludeNetworkSecurityConfig { get; set; } public string? KeyStorePath { get; set; } public bool ForceInterpreter { get; set; } public bool ForceAOT { get; set; } @@ -57,6 +58,12 @@ public class ApkBuilder throw new ArgumentException($"MainLibraryFileName='{mainLibraryFileName}' was not found in AppDir='{AppDir}'"); } + var networkSecurityConfigFilePath = Path.Combine(AppDir, "res", "xml", "network_security_config.xml"); + if (IncludeNetworkSecurityConfig && !File.Exists(networkSecurityConfigFilePath)) + { + throw new ArgumentException($"IncludeNetworkSecurityConfig is set but the file '{networkSecurityConfigFilePath}' was not found"); + } + if (string.IsNullOrEmpty(abi)) { throw new ArgumentException("abi should not be empty (e.g. x86, x86_64, armeabi-v7a or arm64-v8a"); @@ -172,6 +179,7 @@ public class ApkBuilder Directory.CreateDirectory(Path.Combine(OutputDir, "obj")); Directory.CreateDirectory(Path.Combine(OutputDir, "assets-tozip")); Directory.CreateDirectory(Path.Combine(OutputDir, "assets")); + Directory.CreateDirectory(Path.Combine(OutputDir, "res")); var extensionsToIgnore = new List<string> { ".so", ".a", ".gz" }; if (StripDebugSymbols) @@ -183,6 +191,7 @@ public class ApkBuilder // Copy sourceDir to OutputDir/assets-tozip (ignore native files) // these files then will be zipped and copied to apk/assets/assets.zip var assetsToZipDirectory = Path.Combine(OutputDir, "assets-tozip"); + Utils.DirectoryCopy(AppDir, assetsToZipDirectory, file => { string fileName = Path.GetFileName(file); @@ -199,9 +208,20 @@ public class ApkBuilder // aapt complains on such files return false; } + if (file.Contains("/res/")) + { + // exclude everything in the `res` folder + return false; + } return true; }); + // copy the res directory as is + if (Directory.Exists(Path.Combine(AppDir, "res"))) + { + Utils.DirectoryCopy(Path.Combine(AppDir, "res"), Path.Combine(OutputDir, "res")); + } + // add AOT .so libraries foreach (var aotlib in aotLibraryFiles) { @@ -373,6 +393,11 @@ public class ApkBuilder if (!string.IsNullOrEmpty(NativeMainSource)) File.Copy(NativeMainSource, javaActivityPath, true); + string networkSecurityConfigAttribute = + IncludeNetworkSecurityConfig + ? "a:networkSecurityConfig=\"@xml/network_security_config\"" + : string.Empty; + string envVariables = ""; foreach (ITaskItem item in EnvironmentVariables) { @@ -390,6 +415,7 @@ public class ApkBuilder File.WriteAllText(Path.Combine(OutputDir, "AndroidManifest.xml"), Utils.GetEmbeddedResource("AndroidManifest.xml") .Replace("%PackageName%", packageId) + .Replace("%NetworkSecurityConfig%", networkSecurityConfigAttribute) .Replace("%MinSdkLevel%", MinApiLevel)); string javaCompilerArgs = $"-d obj -classpath src -bootclasspath {androidJar} -source 1.8 -target 1.8 "; @@ -414,7 +440,8 @@ public class ApkBuilder string debugModeArg = StripDebugSymbols ? string.Empty : "--debug-mode"; string apkFile = Path.Combine(OutputDir, "bin", $"{ProjectName}.unaligned.apk"); - Utils.RunProcess(logger, aapt, $"package -f -m -F {apkFile} -A assets -M AndroidManifest.xml -I {androidJar} {debugModeArg}", workingDir: OutputDir); + string resources = IncludeNetworkSecurityConfig ? "-S res" : string.Empty; + Utils.RunProcess(logger, aapt, $"package -f -m -F {apkFile} -A assets {resources} -M AndroidManifest.xml -I {androidJar} {debugModeArg}", workingDir: OutputDir); var dynamicLibs = new List<string>(); dynamicLibs.Add(Path.Combine(OutputDir, "monodroid", "libmonodroid.so")); diff --git a/src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml b/src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml index 9d28b5efbdf..befd2e446a6 100644 --- a/src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml +++ b/src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml @@ -7,7 +7,8 @@ <uses-permission a:name="android.permission.INTERNET"/> <uses-permission a:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission a:name="android.permission.WRITE_EXTERNAL_STORAGE"/> - <application a:label="%PackageName%" + <application a:label="%PackageName%" + %NetworkSecurityConfig% a:largeHeap="true"> <activity a:name="net.dot.MainActivity" a:exported="true"> <intent-filter> |