BuildAndroid.cpp 13 KB

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