BuildAndroid.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/StringUtils.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/IO/MemoryBuffer.h>
  26. #include "../ToolSystem.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Subprocess/SubprocessSystem.h"
  29. #include "../Project/Project.h"
  30. #include "../Project/ProjectBuildSettings.h"
  31. #include "../Platform/PlatformAndroid.h"
  32. #include "../Assets/AssetDatabase.h"
  33. #include "AndroidProjectGenerator.h"
  34. #include "BuildSystem.h"
  35. #include "BuildEvents.h"
  36. #include "BuildAndroid.h"
  37. namespace ToolCore
  38. {
  39. BuildAndroid::BuildAndroid(Context* context, Project* project) : BuildBase(context, project, PLATFORMID_ANDROID)
  40. {
  41. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  42. // this cast isn't great
  43. platformAndroid_ = (PlatformAndroid*) toolSystem->GetPlatformByID(PLATFORMID_ANDROID);
  44. }
  45. BuildAndroid::~BuildAndroid()
  46. {
  47. }
  48. void BuildAndroid::HandleADBStartActivityComplete(StringHash eventType, VariantMap& eventData)
  49. {
  50. }
  51. // adb shell am start -n com.package.name/com.package.name.ActivityName
  52. void BuildAndroid::RunADBStartActivity()
  53. {
  54. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  55. String adbCommand = platformAndroid_->GetADBCommand();
  56. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  57. Project* project = toolSystem->GetProject();
  58. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  59. String stringArgs;
  60. const char* cpackage = settings->GetPackageName().CString();
  61. stringArgs.AppendWithFormat("shell am start -n %s/%s.AtomicGameEngine",cpackage, cpackage);
  62. Vector<String> args = stringArgs.Split(' ');
  63. currentBuildPhase_ = ADBStartActivity;
  64. Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
  65. if (!subprocess)
  66. {
  67. FailBuild("BuildFailed::RunStartActivity");
  68. return;
  69. }
  70. VariantMap buildOutput;
  71. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting Android Activity</color>\n\n";
  72. SendEvent(E_BUILDOUTPUT, buildOutput);
  73. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBStartActivityComplete));
  74. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  75. }
  76. void BuildAndroid::HandleRunADBInstallComplete(StringHash eventType, VariantMap& eventData)
  77. {
  78. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  79. if (!code)
  80. {
  81. RunADBStartActivity();
  82. }
  83. else
  84. {
  85. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  86. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  87. }
  88. }
  89. void BuildAndroid::RunADBInstall()
  90. {
  91. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  92. String adbCommand = platformAndroid_->GetADBCommand();
  93. Vector<String> args = String("install -r ./bin/Atomic-debug-unaligned.apk").Split(' ');
  94. currentBuildPhase_ = ADBInstall;
  95. Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
  96. if (!subprocess)
  97. {
  98. FailBuild("BuildFailed::RunADBInstall");
  99. return;
  100. }
  101. VariantMap buildOutput;
  102. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Installing on Android Device</color>\n\n";
  103. SendEvent(E_BUILDOUTPUT, buildOutput);
  104. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleRunADBInstallComplete));
  105. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  106. }
  107. void BuildAndroid::HandleADBListDevicesComplete(StringHash eventType, VariantMap& eventData)
  108. {
  109. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  110. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  111. if (!code)
  112. {
  113. // check if we have any devices attached, otherwise adb install
  114. // will hang looking for devices
  115. bool noDevices = true;
  116. if (deviceListText_.Length())
  117. {
  118. MemoryBuffer reader(deviceListText_.CString(), deviceListText_.Length() + 1);
  119. while (!reader.IsEof())
  120. {
  121. String line = reader.ReadLine();
  122. if (line.Length() && line[0] >= '0' && line[0] <= '9')
  123. {
  124. noDevices = false;
  125. break;
  126. }
  127. }
  128. }
  129. if (!noDevices)
  130. RunADBInstall();
  131. else
  132. {
  133. // can't proceed, though success
  134. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_);
  135. }
  136. }
  137. else
  138. {
  139. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  140. }
  141. }
  142. void BuildAndroid::HandleADBListDevicesOutputEvent(StringHash eventType, VariantMap& eventData)
  143. {
  144. // E_SUBPROCESSOUTPUT
  145. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  146. deviceListText_ += text;
  147. // convert to a build output event and forward to subscribers
  148. VariantMap buildOutputData;
  149. buildOutputData[BuildOutput::P_TEXT] = text;
  150. SendEvent(E_BUILDOUTPUT, buildOutputData);
  151. }
  152. void BuildAndroid::RunADBListDevices()
  153. {
  154. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  155. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  156. String adbCommand = platformAndroid_->GetADBCommand();
  157. deviceListText_.Clear();
  158. Vector<String> args = String("devices").Split(' ');
  159. currentBuildPhase_ = ADBListDevices;
  160. Subprocess* subprocess = subs->Launch(adbCommand, args, "");
  161. if (!subprocess)
  162. {
  163. FailBuild("BuildFailed::RunADBListDevices");
  164. return;
  165. }
  166. VariantMap buildOutput;
  167. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Listing Android Devices</color>\n\n";
  168. SendEvent(E_BUILDOUTPUT, buildOutput);
  169. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBListDevicesComplete));
  170. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildAndroid, HandleADBListDevicesOutputEvent));
  171. }
  172. void BuildAndroid::HandleAntDebugComplete(StringHash eventType, VariantMap& eventData)
  173. {
  174. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  175. if (!code)
  176. {
  177. RunADBListDevices();
  178. }
  179. else
  180. {
  181. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  182. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  183. }
  184. }
  185. void BuildAndroid::RunAntDebug()
  186. {
  187. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  188. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  189. ToolPrefs* tprefs = tenv->GetToolPrefs();
  190. Poco::Process::Env env;
  191. #ifdef ATOMIC_PLATFORM_OSX
  192. String antCommand = tprefs->GetAntPath();
  193. Vector<String> args;
  194. args.Push("debug");
  195. #else
  196. // C:\ProgramData\Oracle\Java\javapath;
  197. Vector<String> args;
  198. String antCommand = "cmd";
  199. String antPath = tprefs->GetAntPath() + "/ant.bat";
  200. env["JAVA_HOME"] = tprefs->GetJDKRootPath().CString();
  201. // ant is a batch file on windows, so have to run with cmd /c
  202. args.Push("/c");
  203. args.Push("\"" + antPath + "\"");
  204. args.Push("debug");
  205. #endif
  206. currentBuildPhase_ = AntBuildDebug;
  207. Subprocess* subprocess = subs->Launch(antCommand, args, buildPath_, env);
  208. if (!subprocess)
  209. {
  210. FailBuild("BuildFailed::RunAntDebug");
  211. return;
  212. }
  213. VariantMap buildOutput;
  214. buildOutput[BuildOutput::P_TEXT] = "<color #D4FB79>Starting Android Deployment</color>\n\n";
  215. SendEvent(E_BUILDOUTPUT, buildOutput);
  216. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleAntDebugComplete));
  217. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  218. }
  219. void BuildAndroid::Initialize()
  220. {
  221. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  222. Project* project = tsystem->GetProject();
  223. Vector<String> defaultResourcePaths;
  224. GetDefaultResourcePaths(defaultResourcePaths);
  225. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  226. {
  227. AddResourceDir(defaultResourcePaths[i]);
  228. }
  229. BuildDefaultResourceEntries();
  230. // TODO: smart filtering of cache
  231. String projectResources = project->GetResourcePath();
  232. AddProjectResourceDir(projectResources);
  233. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  234. String cachePath = db->GetCachePath();
  235. AddProjectResourceDir(cachePath);
  236. BuildProjectResourceEntries();
  237. }
  238. void BuildAndroid::Build(const String& buildPath)
  239. {
  240. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  241. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  242. Project* project = tsystem->GetProject();
  243. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  244. Initialize();
  245. //generate manifest file
  246. String manifest;
  247. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  248. {
  249. BuildResourceEntry* entry = resourceEntries_[i];
  250. manifest += entry->packagePath_;
  251. if ( i != resourceEntries_.Size() - 1)
  252. manifest += ";";
  253. }
  254. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  255. if (fileSystem->DirExists(buildPath_))
  256. fileSystem->RemoveDir(buildPath_, true);
  257. String buildSourceDir = tenv->GetToolDataDir();
  258. String androidProject = buildSourceDir + "Deployment/Android";
  259. // Copy the base android project
  260. fileSystem->CopyDir(androidProject, buildPath_);
  261. Vector<String> defaultResourcePaths;
  262. GetDefaultResourcePaths(defaultResourcePaths);
  263. String projectResources = project->GetResourcePath();
  264. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  265. {
  266. fileSystem->CopyDir(defaultResourcePaths[i], buildPath_ + "/assets/" + GetFileName(RemoveTrailingSlash(defaultResourcePaths[i])));
  267. }
  268. fileSystem->CopyDir(project->GetProjectPath() + "Cache/", buildPath_ + "/assets/Cache");
  269. fileSystem->CopyDir(projectResources, buildPath_ + "/assets/AtomicResources");
  270. // write the manifest
  271. SharedPtr<File> mfile(new File(context_, buildPath_ + "/assets/AtomicManifest", FILE_WRITE));
  272. mfile->WriteString(manifest);
  273. mfile->Close();
  274. AndroidProjectGenerator gen(context_);
  275. gen.SetBuildPath(buildPath_);
  276. if (!gen.Generate())
  277. {
  278. FailBuild(gen.GetErrorText());
  279. return;
  280. }
  281. RunAntDebug();
  282. }
  283. }