AndroidProjectGenerator.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <Poco/File.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/IO/File.h>
  25. #include "../ToolSystem.h"
  26. #include "../ToolEnvironment.h"
  27. #include "../Project/Project.h"
  28. #include "../Project/ProjectBuildSettings.h"
  29. #include "BuildMac.h"
  30. #include "BuildSystem.h"
  31. #include "AndroidProjectGenerator.h"
  32. namespace ToolCore
  33. {
  34. AndroidProjectGenerator::AndroidProjectGenerator(Context* context) :
  35. Object(context)
  36. {
  37. }
  38. AndroidProjectGenerator::~AndroidProjectGenerator()
  39. {
  40. }
  41. bool AndroidProjectGenerator::Generate()
  42. {
  43. if (!GenerateAndroidManifest())
  44. return false;
  45. if (!GenerateStringXML())
  46. return false;
  47. if (!GenerateLocalProperties())
  48. return false;
  49. if (!GenerateProjectProperties())
  50. return false;
  51. if (!GenerateActivitySource())
  52. return false;
  53. return true;
  54. }
  55. bool AndroidProjectGenerator::GenerateActivitySource()
  56. {
  57. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  58. Project* project = toolSystem->GetProject();
  59. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  60. String packageName = settings->GetPackageName();
  61. if (!packageName.Length())
  62. {
  63. errorText_ = "Invalid Package Name";
  64. return false;
  65. }
  66. Vector<String> elements = settings->GetPackageName().Split('.');
  67. String path;
  68. path.Join(elements, "/");
  69. path = buildPath_ + "/src/" + path;
  70. Poco::File dirs(path.CString());
  71. dirs.createDirectories();
  72. if (!dirs.exists())
  73. {
  74. errorText_ = "Unable to create ";
  75. return false;
  76. }
  77. String source;
  78. source.AppendWithFormat("package %s;\n", packageName.CString());
  79. source += "import org.libsdl.app.SDLActivity;\n";
  80. source += "public class AtomicGameEngine extends SDLActivity {\n";
  81. source += "}\n";
  82. File file(context_, path + "/AtomicGameEngine.java", FILE_WRITE);
  83. if (!file.IsOpen())
  84. return false;
  85. file.Write(source.CString(), source.Length());
  86. return true;
  87. }
  88. bool AndroidProjectGenerator::GenerateLocalProperties()
  89. {
  90. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  91. ToolPrefs* prefs = tenv->GetToolPrefs();
  92. String sdkPath = prefs->GetAndroidSDKPath();
  93. if (!sdkPath.Length())
  94. {
  95. errorText_ = "Invalid Android SDK Path";
  96. return false;
  97. }
  98. String props;
  99. props.AppendWithFormat("sdk.dir=%s", sdkPath.CString());
  100. File file(context_, buildPath_ + "/local.properties", FILE_WRITE);
  101. if (!file.IsOpen())
  102. return false;
  103. file.Write(props.CString(), props.Length());
  104. return true;
  105. }
  106. bool AndroidProjectGenerator::GenerateProjectProperties()
  107. {
  108. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  109. Project* project = toolSystem->GetProject();
  110. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  111. String apiString = settings->GetSDKVersion();
  112. if (!apiString.Length())
  113. {
  114. errorText_ = "Invalid Android API";
  115. return false;
  116. }
  117. String props;
  118. props.AppendWithFormat("target=%s", apiString.CString());
  119. File file(context_, buildPath_ + "/project.properties", FILE_WRITE);
  120. if (!file.IsOpen())
  121. return false;
  122. file.Write(props.CString(), props.Length());
  123. return true;
  124. }
  125. bool AndroidProjectGenerator::GenerateStringXML()
  126. {
  127. FileSystem* fs = GetSubsystem<FileSystem>();
  128. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  129. Project* project = toolSystem->GetProject();
  130. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  131. String appName = settings->GetAppName();
  132. if (!appName.Length())
  133. {
  134. errorText_ = "Invalid App Name";
  135. return false;
  136. }
  137. String strings = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  138. strings += "<resources>\n";
  139. strings.AppendWithFormat("<string name=\"app_name\">%s</string>\n", appName.CString());
  140. strings += "</resources>\n";
  141. // Create res/values if it doesn't exist
  142. if (!fs->DirExists(buildPath_ + "/res/values"))
  143. {
  144. fs->CreateDirsRecursive(buildPath_ + "/res/values");
  145. }
  146. // Check that we successfully created it
  147. if (!fs->DirExists(buildPath_ + "/res/values"))
  148. {
  149. errorText_ = "Unable to create directory: " + buildPath_ + "/res/values";
  150. return false;
  151. }
  152. File file(context_, buildPath_ + "/res/values/strings.xml", FILE_WRITE);
  153. if (!file.IsOpen())
  154. {
  155. errorText_ = "Unable to write: " + buildPath_ + "/res/values/strings.xml";
  156. return false;
  157. }
  158. file.Write(strings.CString(), strings.Length());
  159. return true;
  160. }
  161. bool AndroidProjectGenerator::GenerateAndroidManifest()
  162. {
  163. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  164. Project* project = toolSystem->GetProject();
  165. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  166. String package = settings->GetPackageName();
  167. if (!package.Length())
  168. {
  169. errorText_ = "Invalid Package Name";
  170. return false;
  171. }
  172. // TODO: from settings
  173. String activityName = "AtomicGameEngine";
  174. if (!activityName.Length())
  175. {
  176. errorText_ = "Invalid Activity Name";
  177. return false;
  178. }
  179. String manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  180. manifest += "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n";
  181. manifest.AppendWithFormat("package=\"%s\"\n", package.CString());
  182. manifest += "android:versionCode=\"1\"\n";
  183. manifest += "android:versionName=\"1.0\">\n";
  184. manifest += "<uses-permission android:name=\"android.permission.INTERNET\" />\n";
  185. manifest += "<uses-feature android:glEsVersion=\"0x00020000\" />\n";
  186. manifest += "<uses-sdk android:targetSdkVersion=\"12\" android:minSdkVersion=\"10\" />\n";
  187. manifest += "<application android:label=\"@string/app_name\" android:icon=\"@drawable/icon\">\n";
  188. manifest.AppendWithFormat("<activity android:name=\".%s\"\n", activityName.CString());
  189. manifest += "android:label=\"@string/app_name\"\n";
  190. manifest += "android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\"\n";
  191. manifest += "android:configChanges=\"keyboardHidden|orientation\"\n";
  192. manifest += "android:screenOrientation=\"landscape\">\n";
  193. manifest += "<intent-filter>\n";
  194. manifest += "<action android:name=\"android.intent.action.MAIN\" />\n";
  195. manifest += "<category android:name=\"android.intent.category.LAUNCHER\" />\n";
  196. manifest += "</intent-filter>\n";
  197. manifest += "</activity>\n";
  198. manifest += "</application>\n";
  199. manifest += "</manifest>\n";
  200. File file(context_, buildPath_ + "/AndroidManifest.xml", FILE_WRITE);
  201. if (!file.IsOpen())
  202. return false;
  203. file.Write(manifest.CString(), manifest.Length());
  204. return true;
  205. }
  206. }
  207. /*
  208. */