AndroidProjectGenerator.cpp 6.4 KB

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