Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
IAndroidDeployTargets.cs
1using System;
2using System.Linq;
3using Nuke.Common;
4using Nuke.Common.IO;
5using Nuke.Common.Tooling;
6using Serilog;
7
9
10/// <summary>
11/// Build component for common tasks debugging an Android app
12/// </summary>
13[ParameterPrefix("Android")]
15{
16 /// <summary>
17 /// <para>
18 /// **NUKE PARAMETER**
19 /// </para>
20 /// Disable uninstalling before deploying a new APK
21 /// </summary>
22 [Parameter]
23 bool NoUninstall => TryGetValue<bool?>(() => NoUninstall) ?? false;
24
25 /// <summary>
26 /// <para>
27 /// **NUKE TARGET**
28 /// </para>
29 /// Package and install the product on a connected android device.
30 /// Only executed when target-platform is set to Android.
31 /// <code>
32 /// \--NoUninstall
33 /// \--AppName (adv.)
34 /// </code>
35 /// </summary>
36 Target InstallOnAndroid => _ => _
37 .Description(
38 """
39 |
40 | Package and install the product on a connected android device.
41 | Only executed when target-platform is set to Android
42 |
43 | --NoUninstall
44 | --AppName (adv.)
45
46 """
47 )
48 .OnlyWhenStatic(this.IsAndroidPlatform)
49 .DependsOn(SetupPlatformSdk)
50 .After<IPackageTargets>(p => p.Package)
51 .After(Build)
52 .Executes(() =>
53 {
54 var adb = this.GetAndroidSdk().GetAdb(this);
55 var appName = this.GetAppName();
56
57 var apkFile = this.GetApkFile();
58 Assert.True(apkFile.FileExists());
59
60 if (!NoUninstall)
61 {
62 try
63 {
64 Log.Information("Uninstall {0} (failures here are not fatal)", appName);
65 adb($"uninstall {appName}");
66 }
67 catch (Exception e)
68 {
69 Log.Warning(e, "Uninstallation threw errors, but that might not be a problem");
70 }
71 }
72
73 Log.Information("Installing {0}", apkFile);
74 adb($"install {apkFile}");
75
76 var storagePath = adb("shell echo $EXTERNAL_STORAGE")!
77 .FirstOrDefault(o => !string.IsNullOrWhiteSpace(o.Text))
78 .Text;
79
80 Assert.False(string.IsNullOrWhiteSpace(storagePath), "Couldn't get a storage path from the device");
81
82 if (!NoUninstall)
83 {
84 try
85 {
86 Log.Information("Removing existing assets from device (failures here are not fatal)");
87 adb($"shell rm -r {storagePath}/UE4Game/{ProjectName}");
88 adb($"shell rm -r {storagePath}/UE4Game/UE4CommandLine.txt");
89 adb($"shell rm -r {storagePath}/obb/{appName}");
90 adb($"shell rm -r {storagePath}/Android/obb/{appName}");
91 adb($"shell rm -r {storagePath}/Download/obb/{appName}");
92 }
93 catch (Exception e)
94 {
95 Log.Warning(e, "Removing existing asset files threw errors, but that might not be a problem");
96 }
97 }
98
99 var obbName = $"main.1.{appName}";
100 var obbFile = apkFile.Parent / (obbName + ".obb");
101
102 if (obbFile.FileExists())
103 {
104 Log.Information("Installing {0}", obbFile);
105
106 adb($"push {obbFile} {storagePath}/obb/{appName}/{obbName}.obb");
107 }
108
109 Log.Information("Grant READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE to the apk for reading OBB file or game file in external storage.");
110
111 adb($"shell pm grant {appName} android.permission.READ_EXTERNAL_STORAGE");
112 adb($"shell pm grant {appName} android.permission.WRITE_EXTERNAL_STORAGE");
113
114 Log.Information("Done installing {0}", appName);
115 });
116
117 /// <summary>
118 /// <para>
119 /// **NUKE TARGET**
120 /// </para>
121 /// Launch the product on android but wait for debugger. Only executed when target-platform is set to Android
122 /// <code>
123 /// \--AppName (adv.)
124 /// </code>
125 /// </summary>
126 Target DebugOnAndroid => _ => _
127 .Description(
128 """
129 |
130 | Launch the product on android but wait for debugger.
131 | This requires ADB to be in your PATH and NDK to be correctly configured.
132 | Only executed when target-platform is set to Android
133 |
134 | --AppName (adv.)
135 """
136 )
137 .OnlyWhenStatic(this.IsAndroidPlatform)
138 .DependsOn(SetupPlatformSdk)
139 .After(InstallOnAndroid)
140 .Executes(() =>
141 {
142 var adb = this.GetAndroidSdk().GetAdb(this);
143 var appName = this.GetAppName();
144
145 Log.Information("Running {0} but wait for a debugger to be attached", appName);
146 adb($"shell am start -D -n {appName}/com.epicgames.ue4.GameActivity");
147 });
148}
Target for packaging the current project we're working on.
Target Package
Same as running Package Project from Editor.
Base interface for build components which require an UnrealBuild main class.
Build component for common tasks debugging an Android app.