|
| 1 | +using Beyond.NET.Core; |
| 2 | + |
| 3 | +namespace Beyond.NET.Builder.Android; |
| 4 | + |
| 5 | +public class AndroidBuilder |
| 6 | +{ |
| 7 | + public record BuildResult( |
| 8 | + string? AndroidARM64LibraryPath, |
| 9 | + string OutputDirectoryPath |
| 10 | + ); |
| 11 | + |
| 12 | + public string ProductName { get; } |
| 13 | + public string OutputDirectory { get; } |
| 14 | + public bool BuildAndroidARM64 { get; } |
| 15 | + |
| 16 | + private ILogger Logger => Services.Shared.LoggerService; |
| 17 | + |
| 18 | + private string OutputProductName => ProductName; |
| 19 | + private string OutputLibraryFileName => $"lib{OutputProductName}.so"; |
| 20 | + |
| 21 | + public AndroidBuilder( |
| 22 | + string productName, |
| 23 | + string outputDirectory, |
| 24 | + bool buildAndroidARM64 |
| 25 | + ) |
| 26 | + { |
| 27 | + ProductName = productName; |
| 28 | + OutputDirectory = outputDirectory; |
| 29 | + BuildAndroidARM64 = buildAndroidARM64; |
| 30 | + } |
| 31 | + |
| 32 | + public BuildResult Build( |
| 33 | + string? androidARM64BuildPath |
| 34 | + ) |
| 35 | + { |
| 36 | + Logger.LogInformation("Building Android libraries"); |
| 37 | + |
| 38 | + string? androidARM64LibraryPath = null; |
| 39 | + |
| 40 | + if (androidARM64BuildPath is not null && BuildAndroidARM64) |
| 41 | + { |
| 42 | + androidARM64LibraryPath = Path.Combine(androidARM64BuildPath, OutputLibraryFileName); |
| 43 | + |
| 44 | + if (!File.Exists(androidARM64LibraryPath)) |
| 45 | + { |
| 46 | + var libraryWithoutLibName = OutputLibraryFileName.Replace("lib", ""); |
| 47 | + androidARM64LibraryPath = Path.Combine(androidARM64BuildPath, libraryWithoutLibName); |
| 48 | + } |
| 49 | + |
| 50 | + if (!File.Exists(androidARM64LibraryPath)) |
| 51 | + { |
| 52 | + throw new FileNotFoundException($"Android library not found at {androidARM64BuildPath}"); |
| 53 | + } |
| 54 | + |
| 55 | + Logger.LogInformation($"Android library found at: {androidARM64LibraryPath}"); |
| 56 | + } |
| 57 | + |
| 58 | + // Create output directory structure for Android |
| 59 | + string androidOutputPath = Path.Combine(OutputDirectory, "android"); |
| 60 | + Directory.CreateDirectory(androidOutputPath); |
| 61 | + |
| 62 | + if (androidARM64LibraryPath is not null) |
| 63 | + { |
| 64 | + string arm64OutputDir = Path.Combine(androidOutputPath, "arm64-v8a"); |
| 65 | + Directory.CreateDirectory(arm64OutputDir); |
| 66 | + string destPath = Path.Combine(arm64OutputDir, OutputLibraryFileName); |
| 67 | + File.Copy(androidARM64LibraryPath, destPath, true); |
| 68 | + Logger.LogInformation($"Copied library to: {destPath}"); |
| 69 | + } |
| 70 | + |
| 71 | + if (androidARM64BuildPath is not null && BuildAndroidARM64) |
| 72 | + { |
| 73 | + string debugSymbolsOutputPath = Path.Combine(androidOutputPath, "android-debug-symbols"); |
| 74 | + Directory.CreateDirectory(debugSymbolsOutputPath); |
| 75 | + |
| 76 | + Logger.LogInformation($"Copying all files from {androidARM64BuildPath} to {debugSymbolsOutputPath}"); |
| 77 | + CopyAllFiles(androidARM64BuildPath, debugSymbolsOutputPath); |
| 78 | + |
| 79 | + Logger.LogInformation($"Copied debug symbols to: {debugSymbolsOutputPath}"); |
| 80 | + } |
| 81 | + |
| 82 | + Logger.LogInformation($"Android build completed. Output directory: {androidOutputPath}"); |
| 83 | + |
| 84 | + return new BuildResult( |
| 85 | + androidARM64LibraryPath, |
| 86 | + androidOutputPath |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + private void CopyAllFiles(string sourceDirectory, string destinationDirectory) |
| 91 | + { |
| 92 | + if (!Directory.Exists(sourceDirectory)) |
| 93 | + { |
| 94 | + throw new DirectoryNotFoundException($"Source directory not found: {sourceDirectory}"); |
| 95 | + } |
| 96 | + |
| 97 | + Directory.CreateDirectory(destinationDirectory); |
| 98 | + |
| 99 | + foreach (string filePath in Directory.GetFiles(sourceDirectory)) |
| 100 | + { |
| 101 | + string fileName = Path.GetFileName(filePath); |
| 102 | + string destFilePath = Path.Combine(destinationDirectory, fileName); |
| 103 | + |
| 104 | + File.Copy(filePath, destFilePath, overwrite: true); |
| 105 | + Logger.LogDebug($"Copied: {fileName}"); |
| 106 | + } |
| 107 | + |
| 108 | + foreach (string dirPath in Directory.GetDirectories(sourceDirectory)) |
| 109 | + { |
| 110 | + string dirName = Path.GetFileName(dirPath); |
| 111 | + string destDirPath = Path.Combine(destinationDirectory, dirName); |
| 112 | + |
| 113 | + CopyAllFiles(dirPath, destDirPath); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments