AndroidProjectGenerator.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Poco/File.h>
  8. #include <Atomic/IO/FileSystem.h>
  9. #include <Atomic/IO/File.h>
  10. #include "../ToolSystem.h"
  11. #include "../ToolEnvironment.h"
  12. #include "../Project/Project.h"
  13. #include "../Project/ProjectBuildSettings.h"
  14. #include "BuildMac.h"
  15. #include "BuildSystem.h"
  16. #include "AndroidProjectGenerator.h"
  17. namespace ToolCore
  18. {
  19. AndroidProjectGenerator::AndroidProjectGenerator(Context* context) :
  20. Object(context)
  21. {
  22. }
  23. AndroidProjectGenerator::~AndroidProjectGenerator()
  24. {
  25. }
  26. bool AndroidProjectGenerator::Generate()
  27. {
  28. if (!GenerateAndroidManifest())
  29. return false;
  30. if (!GenerateStringXML())
  31. return false;
  32. if (!GenerateLocalProperties())
  33. return false;
  34. if (!GenerateProjectProperties())
  35. return false;
  36. if (!GenerateActivitySource())
  37. return false;
  38. return true;
  39. }
  40. bool AndroidProjectGenerator::GenerateActivitySource()
  41. {
  42. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  43. Project* project = toolSystem->GetProject();
  44. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  45. String packageName = settings->GetPackageName();
  46. if (!packageName.Length())
  47. {
  48. errorText_ = "Invalid Package Name";
  49. return false;
  50. }
  51. Vector<String> elements = settings->GetPackageName().Split('.');
  52. String path;
  53. path.Join(elements, "/");
  54. path = buildPath_ + "/src/" + path;
  55. Poco::File dirs(path.CString());
  56. dirs.createDirectories();
  57. if (!dirs.exists())
  58. {
  59. errorText_ = "Unable to create ";
  60. return false;
  61. }
  62. String source;
  63. source.AppendWithFormat("package %s;\n", packageName.CString());
  64. source += "import org.libsdl.app.SDLActivity;\n";
  65. source += "public class AtomicGameEngine extends SDLActivity {\n";
  66. source += "}\n";
  67. File file(context_, path + "/AtomicGameEngine.java", FILE_WRITE);
  68. if (!file.IsOpen())
  69. return false;
  70. file.Write(source.CString(), source.Length());
  71. return true;
  72. }
  73. bool AndroidProjectGenerator::GenerateLocalProperties()
  74. {
  75. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  76. ToolPrefs* prefs = tenv->GetToolPrefs();
  77. String sdkPath = prefs->GetAndroidSDKPath();
  78. if (!sdkPath.Length())
  79. {
  80. errorText_ = "Invalid Android SDK Path";
  81. return false;
  82. }
  83. String props;
  84. props.AppendWithFormat("sdk.dir=%s", sdkPath.CString());
  85. File file(context_, buildPath_ + "/local.properties", FILE_WRITE);
  86. if (!file.IsOpen())
  87. return false;
  88. file.Write(props.CString(), props.Length());
  89. return true;
  90. }
  91. bool AndroidProjectGenerator::GenerateProjectProperties()
  92. {
  93. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  94. Project* project = toolSystem->GetProject();
  95. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  96. String apiString = settings->GetSDKVersion();
  97. if (!apiString.Length())
  98. {
  99. errorText_ = "Invalid Android API";
  100. return false;
  101. }
  102. String props;
  103. props.AppendWithFormat("target=%s", apiString.CString());
  104. File file(context_, buildPath_ + "/project.properties", FILE_WRITE);
  105. if (!file.IsOpen())
  106. return false;
  107. file.Write(props.CString(), props.Length());
  108. return true;
  109. }
  110. bool AndroidProjectGenerator::GenerateStringXML()
  111. {
  112. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  113. Project* project = toolSystem->GetProject();
  114. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  115. String appName = settings->GetAppName();
  116. if (!appName.Length())
  117. {
  118. errorText_ = "Invalid App Name";
  119. return false;
  120. }
  121. String strings = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  122. strings += "<resources>\n";
  123. strings.AppendWithFormat("<string name=\"app_name\">%s</string>\n", appName.CString());
  124. strings += "</resources>\n";
  125. File file(context_, buildPath_ + "/res/values/strings.xml", FILE_WRITE);
  126. if (!file.IsOpen())
  127. return false;
  128. file.Write(strings.CString(), strings.Length());
  129. return true;
  130. }
  131. bool AndroidProjectGenerator::GenerateAndroidManifest()
  132. {
  133. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  134. Project* project = toolSystem->GetProject();
  135. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  136. String package = settings->GetPackageName();
  137. if (!package.Length())
  138. {
  139. errorText_ = "Invalid Package Name";
  140. return false;
  141. }
  142. // TODO: from settings
  143. String activityName = "AtomicGameEngine";
  144. if (!activityName.Length())
  145. {
  146. errorText_ = "Invalid Activity Name";
  147. return false;
  148. }
  149. String manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  150. manifest += "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n";
  151. manifest.AppendWithFormat("package=\"%s\"\n", package.CString());
  152. manifest += "android:versionCode=\"1\"\n";
  153. manifest += "android:versionName=\"1.0\">\n";
  154. manifest += "<uses-permission android:name=\"android.permission.INTERNET\" />\n";
  155. manifest += "<uses-feature android:glEsVersion=\"0x00020000\" />\n";
  156. manifest += "<uses-sdk android:targetSdkVersion=\"12\" android:minSdkVersion=\"10\" />\n";
  157. manifest += "<application android:label=\"@string/app_name\" android:icon=\"@drawable/icon\">\n";
  158. manifest.AppendWithFormat("<activity android:name=\".%s\"\n", activityName.CString());
  159. manifest += "android:label=\"@string/app_name\"\n";
  160. manifest += "android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\"\n";
  161. manifest += "android:configChanges=\"keyboardHidden|orientation\"\n";
  162. manifest += "android:screenOrientation=\"landscape\">\n";
  163. manifest += "<intent-filter>\n";
  164. manifest += "<action android:name=\"android.intent.action.MAIN\" />\n";
  165. manifest += "<category android:name=\"android.intent.category.LAUNCHER\" />\n";
  166. manifest += "</intent-filter>\n";
  167. manifest += "</activity>\n";
  168. manifest += "</application>\n";
  169. manifest += "</manifest>\n";
  170. File file(context_, buildPath_ + "/AndroidManifest.xml", FILE_WRITE);
  171. if (!file.IsOpen())
  172. return false;
  173. file.Write(manifest.CString(), manifest.Length());
  174. return true;
  175. }
  176. }
  177. /*
  178. */