BuildAndroid.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "AndroidProjectGenerator.h"
  33. #include "BuildSystem.h"
  34. #include "BuildEvents.h"
  35. #include "BuildAndroid.h"
  36. namespace ToolCore
  37. {
  38. BuildAndroid::BuildAndroid(Context* context, Project* project) : BuildBase(context, project, PLATFORMID_ANDROID)
  39. {
  40. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  41. // this cast isn't great
  42. platformAndroid_ = (PlatformAndroid*) toolSystem->GetPlatformByID(PLATFORMID_ANDROID);
  43. }
  44. BuildAndroid::~BuildAndroid()
  45. {
  46. }
  47. void BuildAndroid::HandleADBStartActivityComplete(StringHash eventType, VariantMap& eventData)
  48. {
  49. }
  50. // adb shell am start -n com.package.name/com.package.name.ActivityName
  51. void BuildAndroid::RunADBStartActivity()
  52. {
  53. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  54. String adbCommand = platformAndroid_->GetADBCommand();
  55. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  56. Project* project = toolSystem->GetProject();
  57. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  58. String stringArgs;
  59. const char* cpackage = settings->GetPackageName().CString();
  60. stringArgs.AppendWithFormat("shell am start -n %s/%s.AtomicGameEngine",cpackage, cpackage);
  61. Vector<String> args = stringArgs.Split(' ');
  62. currentBuildPhase_ = ADBStartActivity;
  63. Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
  64. if (!subprocess)
  65. {
  66. FailBuild("StartActivity operation did not launch successfully.");
  67. return;
  68. }
  69. VariantMap buildOutput;
  70. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting Android Activity</color>\n\n";
  71. SendEvent(E_BUILDOUTPUT, buildOutput);
  72. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBStartActivityComplete));
  73. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  74. }
  75. void BuildAndroid::HandleRunADBInstallComplete(StringHash eventType, VariantMap& eventData)
  76. {
  77. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  78. if (!code)
  79. {
  80. RunADBStartActivity();
  81. }
  82. else
  83. {
  84. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  85. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  86. }
  87. }
  88. void BuildAndroid::RunADBInstall()
  89. {
  90. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  91. ToolPrefs* prefs = tenv->GetToolPrefs();
  92. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  93. String adbCommand = platformAndroid_->GetADBCommand();
  94. Vector<String> args;
  95. if ( prefs->GetReleaseCheck() > 0 ) // install release apk
  96. args = String("install -r ./bin/Atomic-release.apk").Split(' ');
  97. else
  98. args = String("install -r ./bin/Atomic-debug-unaligned.apk").Split(' ');
  99. currentBuildPhase_ = ADBInstall;
  100. Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
  101. if (!subprocess)
  102. {
  103. FailBuild("APK Device Installation operation did not launch successfully.");
  104. return;
  105. }
  106. VariantMap buildOutput;
  107. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Installing on Android Device</color>\n\n";
  108. SendEvent(E_BUILDOUTPUT, buildOutput);
  109. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleRunADBInstallComplete));
  110. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  111. }
  112. void BuildAndroid::HandleADBListDevicesComplete(StringHash eventType, VariantMap& eventData)
  113. {
  114. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  115. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  116. if (!code)
  117. {
  118. // check if we have any devices attached, otherwise adb install
  119. // will hang looking for devices
  120. bool noDevices = true;
  121. if (deviceListText_.Length())
  122. {
  123. MemoryBuffer reader(deviceListText_.CString(), deviceListText_.Length() + 1);
  124. while (!reader.IsEof())
  125. {
  126. String line = reader.ReadLine();
  127. if (line.Length() && line[0] >= '0' && line[0] <= '9')
  128. {
  129. noDevices = false;
  130. break;
  131. }
  132. }
  133. }
  134. if (!noDevices)
  135. RunADBInstall();
  136. else
  137. {
  138. // can't proceed, though success
  139. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_);
  140. }
  141. }
  142. else
  143. {
  144. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  145. }
  146. }
  147. void BuildAndroid::HandleADBListDevicesOutputEvent(StringHash eventType, VariantMap& eventData)
  148. {
  149. // E_SUBPROCESSOUTPUT
  150. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  151. deviceListText_ += text;
  152. // convert to a build output event and forward to subscribers
  153. VariantMap buildOutputData;
  154. buildOutputData[BuildOutput::P_TEXT] = text;
  155. SendEvent(E_BUILDOUTPUT, buildOutputData);
  156. }
  157. void BuildAndroid::RunADBListDevices()
  158. {
  159. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  160. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  161. String adbCommand = platformAndroid_->GetADBCommand();
  162. deviceListText_.Clear();
  163. Vector<String> args = String("devices").Split(' ');
  164. currentBuildPhase_ = ADBListDevices;
  165. Subprocess* subprocess = subs->Launch(adbCommand, args, "");
  166. if (!subprocess)
  167. {
  168. FailBuild("Android List Device operation did not launch successfully.");
  169. return;
  170. }
  171. VariantMap buildOutput;
  172. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Listing Android Devices</color>\n\n";
  173. SendEvent(E_BUILDOUTPUT, buildOutput);
  174. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBListDevicesComplete));
  175. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildAndroid, HandleADBListDevicesOutputEvent));
  176. }
  177. void BuildAndroid::HandleAntDebugComplete(StringHash eventType, VariantMap& eventData)
  178. {
  179. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  180. if (!code)
  181. {
  182. RunADBListDevices();
  183. }
  184. else
  185. {
  186. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  187. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  188. }
  189. }
  190. void BuildAndroid::RunAntDebug()
  191. {
  192. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  193. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  194. ToolPrefs* tprefs = tenv->GetToolPrefs();
  195. Poco::Process::Env env;
  196. String buildApk = "debug"; // the default
  197. if ( tprefs->GetReleaseCheck() > 0 ) // create release apk
  198. buildApk = "release";
  199. #ifdef ATOMIC_PLATFORM_OSX
  200. String antCommand = tprefs->GetAntPath();
  201. Vector<String> args;
  202. args.Push(buildApk);
  203. #endif
  204. #ifdef ATOMIC_PLATFORM_WINDOWS
  205. // C:\ProgramData\Oracle\Java\javapath;
  206. Vector<String> args;
  207. String antCommand = "cmd";
  208. String antPath = tprefs->GetAntPath() + "/ant.bat";
  209. env["JAVA_HOME"] = tprefs->GetJDKRootPath().CString();
  210. // ant is a batch file on windows, so have to run with cmd /c
  211. args.Push("/c");
  212. args.Push("\"" + antPath + "\"");
  213. args.Push(buildApk);
  214. #endif
  215. #ifdef ATOMIC_PLATFORM_LINUX
  216. String antCommand = tprefs->GetAntPath();
  217. if ( antCommand.Empty() ) // user didnt fill it out, use installed one
  218. {
  219. antCommand = "/usr/bin/ant"; // system default if installed
  220. }
  221. else
  222. {
  223. antCommand.Append("/ant");
  224. }
  225. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  226. if ( !fileSystem->FileExists ( antCommand) )
  227. {
  228. FailBuild("The ant program can not be found, check the Ant Path in Build Settings.");
  229. }
  230. Vector<String> args;
  231. args.Push(buildApk);
  232. #endif
  233. currentBuildPhase_ = AntBuildDebug;
  234. Subprocess* subprocess = subs->Launch(antCommand, args, buildPath_, env);
  235. if (!subprocess)
  236. {
  237. FailBuild("The ant build operation did not launch successfully.");
  238. return;
  239. }
  240. VariantMap buildOutput;
  241. buildOutput[BuildOutput::P_TEXT] = "<color #D4FB79>Starting Android " + buildApk + " Deployment</color>\n\n";
  242. SendEvent(E_BUILDOUTPUT, buildOutput);
  243. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleAntDebugComplete));
  244. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  245. }
  246. void BuildAndroid::Initialize()
  247. {
  248. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  249. Project* project = tsystem->GetProject();
  250. Vector<String> defaultResourcePaths;
  251. GetDefaultResourcePaths(defaultResourcePaths);
  252. String projectResources = project->GetResourcePath();
  253. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  254. {
  255. AddResourceDir(defaultResourcePaths[i]);
  256. }
  257. // TODO: smart filtering of cache
  258. AddResourceDir(project->GetProjectPath() + "Cache/");
  259. AddResourceDir(projectResources);
  260. BuildResourceEntries();
  261. }
  262. void BuildAndroid::Build(const String& buildPath)
  263. {
  264. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  265. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  266. Project* project = tsystem->GetProject();
  267. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  268. Initialize();
  269. if (!BuildClean(buildPath_))
  270. return;
  271. //generate manifest file
  272. String manifest;
  273. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  274. {
  275. BuildResourceEntry* entry = resourceEntries_[i];
  276. manifest += entry->packagePath_;
  277. if ( i != resourceEntries_.Size() - 1)
  278. manifest += ";";
  279. }
  280. String buildSourceDir = tenv->GetToolDataDir();
  281. String androidProject = buildSourceDir + "Deployment/Android";
  282. // Copy the base android project
  283. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  284. if( !BuildCopyDir(androidProject, buildPath_))
  285. return;
  286. Vector<String> defaultResourcePaths;
  287. GetDefaultResourcePaths(defaultResourcePaths);
  288. String projectResources = project->GetResourcePath();
  289. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  290. {
  291. if ( !BuildCopyDir(defaultResourcePaths[i], buildPath_ + "/assets/" + GetFileName(RemoveTrailingSlash(defaultResourcePaths[i]))))
  292. return;
  293. }
  294. if( !BuildCopyDir(project->GetProjectPath() + "Cache/", buildPath_ + "/assets/Cache"))
  295. return;
  296. if( !BuildCopyDir(projectResources, buildPath_ + "/assets/AtomicResources"))
  297. return;
  298. // write the manifest
  299. SharedPtr<File> mfile(new File(context_, buildPath_ + "/assets/AtomicManifest", FILE_WRITE));
  300. mfile->WriteString(manifest);
  301. mfile->Close();
  302. //check for Deployment/Android/libs/armeabi-v7a/libAtomicPlayer.so
  303. if ( !fileSystem->FileExists(androidProject + "/libs/armeabi-v7a/libAtomicPlayer.so") )
  304. {
  305. FailBuild( "The file libAtomicPlayer.so is not found. This is required for APK generation." );
  306. return;
  307. }
  308. AndroidProjectGenerator gen(context_, this);
  309. gen.SetBuildPath(buildPath_);
  310. if (!gen.Generate())
  311. {
  312. FailBuild(gen.GetErrorText());
  313. return;
  314. }
  315. RunAntDebug();
  316. }
  317. }