Skip to content

Add NativeMainSource alternative to Android Sample App #44076

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class AndroidAppBuilderTask : Task

public bool StripDebugSymbols { get; set; }

/// <summary>
/// Path to a custom MainActivity.java with custom UI
/// A default one is used if it's not set
/// </summary>
public string? NativeMainSource { get; set; }

[Output]
public string ApkBundlePath { get; set; } = ""!;

Expand All @@ -60,6 +66,7 @@ public override bool Execute()
apkBuilder.BuildApiLevel = BuildApiLevel;
apkBuilder.BuildToolsVersion = BuildToolsVersion;
apkBuilder.StripDebugSymbols = StripDebugSymbols;
apkBuilder.NativeMainSource = NativeMainSource;
(ApkBundlePath, ApkPackageId) = apkBuilder.BuildApk(SourceDir, abi, MainLibraryFileName, MonoRuntimeHeaders);

return true;
Expand Down
19 changes: 14 additions & 5 deletions tools-local/tasks/mobile.tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public class ApkBuilder
public string? BuildToolsVersion { get; set; }
public string? OutputDir { get; set; }
public bool StripDebugSymbols { get; set; }
public string? NativeMainSource { get; set; }

public (string apk, string packageId) BuildApk(
string sourceDir, string abi, string entryPointLib, string monoRuntimeHeaders)
string sourceDir,
string abi,
string entryPointLib,
string monoRuntimeHeaders)
{
if (!Directory.Exists(sourceDir))
throw new ArgumentException($"sourceDir='{sourceDir}' is empty or doesn't exist");
Expand Down Expand Up @@ -177,24 +181,29 @@ public class ApkBuilder
string javaSrcFolder = Path.Combine(OutputDir, "src", "net", "dot");
Directory.CreateDirectory(javaSrcFolder);

string javaActivityPath = Path.Combine(javaSrcFolder, "MainActivity.java");
string monoRunnerPath = Path.Combine(javaSrcFolder, "MonoRunner.java");

string packageId = $"net.dot.{ProjectName}";

File.WriteAllText(Path.Combine(javaSrcFolder, "MainActivity.java"),
File.WriteAllText(javaActivityPath,
Utils.GetEmbeddedResource("MainActivity.java")
.Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib)));
if (!string.IsNullOrEmpty(NativeMainSource))
File.Copy(NativeMainSource, javaActivityPath, true);

string monoRunner = Utils.GetEmbeddedResource("MonoRunner.java")
.Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib));
File.WriteAllText(Path.Combine(javaSrcFolder, "MonoRunner.java"), monoRunner);
File.WriteAllText(monoRunnerPath, monoRunner);

File.WriteAllText(Path.Combine(OutputDir, "AndroidManifest.xml"),
Utils.GetEmbeddedResource("AndroidManifest.xml")
.Replace("%PackageName%", packageId)
.Replace("%MinSdkLevel%", MinApiLevel));

string javaCompilerArgs = $"-d obj -classpath src -bootclasspath {androidJar} -source 1.8 -target 1.8 ";
Utils.RunProcess(javac, javaCompilerArgs + Path.Combine(javaSrcFolder, "MainActivity.java"), workingDir: OutputDir);
Utils.RunProcess(javac, javaCompilerArgs + Path.Combine(javaSrcFolder, "MonoRunner.java"), workingDir: OutputDir);
Utils.RunProcess(javac, javaCompilerArgs + javaActivityPath, workingDir: OutputDir);
Utils.RunProcess(javac, javaCompilerArgs + monoRunnerPath, workingDir: OutputDir);
Utils.RunProcess(dx, "--dex --output=classes.dex obj", workingDir: OutputDir);

// 3. Generate APK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ public override bool Execute()
}
else
{
AppBundlePath = Xcode.BuildAppBundle(
Path.Combine(binDir, ProjectName, ProjectName + ".xcodeproj"),
Arch, Optimized, DevTeamProvisioning);
AppBundlePath = Xcode.BuildAppBundle(XcodeProjectPath, Arch, Optimized, DevTeamProvisioning);
}
}
}
Expand Down