Skip to content

Commit

Permalink
Add NativeMainSource alternative to Android Sample App (#44076)
Browse files Browse the repository at this point in the history
* Add NativeMainSource alternative to Android Sample

* Move nativeMainSource from parameter to property

Co-authored-by: Mitchell Hwang <[email protected]>
  • Loading branch information
mdh1418 and Mitchell Hwang authored Nov 2, 2020
1 parent 7c9c347 commit 63a877b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
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

0 comments on commit 63a877b

Please sign in to comment.