AndroidProjectGenerator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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( BuildBase *baseOps )
  42. {
  43. if (!GenerateAndroidManifest())
  44. return false;
  45. if (!GenerateStringXML())
  46. return false;
  47. if (!GenerateLocalProperties(baseOps))
  48. return false;
  49. if (!GenerateProjectProperties())
  50. return false;
  51. if (!GenerateActivitySource())
  52. return false;
  53. if (!CopyUserIcons(baseOps))
  54. return false;
  55. return true;
  56. }
  57. bool AndroidProjectGenerator::GenerateActivitySource()
  58. {
  59. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  60. Project* project = toolSystem->GetProject();
  61. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  62. String packageName = settings->GetPackageName();
  63. if (!packageName.Length())
  64. {
  65. errorText_ = "Invalid App Package name. The general naming convention is com.company.appname";
  66. return false;
  67. }
  68. Vector<String> elements = settings->GetPackageName().Split('.');
  69. String path;
  70. path.Join(elements, "/");
  71. path = buildPath_ + "/src/" + path;
  72. Poco::File dirs(path.CString());
  73. dirs.createDirectories();
  74. if (!dirs.exists())
  75. {
  76. errorText_ = "Project generator unable to create dirs " + path;
  77. return false;
  78. }
  79. String source;
  80. source.AppendWithFormat("package %s;\n", packageName.CString());
  81. source += "import org.libsdl.app.SDLActivity;\n";
  82. source += "public class AtomicGameEngine extends SDLActivity {\n";
  83. source += "}\n";
  84. File file(context_, path + "/AtomicGameEngine.java", FILE_WRITE);
  85. if (!file.IsOpen())
  86. {
  87. errorText_ = "Project generator unable to open file " + path + "/AtomicGameEngine.java";
  88. return false;
  89. }
  90. file.Write(source.CString(), source.Length());
  91. return true;
  92. }
  93. bool AndroidProjectGenerator::GenerateLocalProperties( BuildBase *fileOps )
  94. {
  95. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  96. ToolPrefs* prefs = tenv->GetToolPrefs();
  97. String sdkPath = prefs->GetAndroidSDKPath();
  98. if (!sdkPath.Length())
  99. {
  100. errorText_ = "Invalid Android SDK Path, select the path in Build Settings.";
  101. return false;
  102. }
  103. String props;
  104. props.AppendWithFormat("sdk.dir=%s", sdkPath.CString());
  105. File file(context_, buildPath_ + "/local.properties", FILE_WRITE);
  106. if (!file.IsOpen())
  107. {
  108. errorText_ = "Project generator unable to open file " + buildPath_ + "/local.properties ";
  109. return false;
  110. }
  111. file.Write(props.CString(), props.Length());
  112. if ( prefs->GetReleaseCheck() > 0 ) // if release flag is set ...
  113. {
  114. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  115. String Reldir = prefs->GetReleasePath();
  116. if (!fileSystem->DirExists(Reldir))
  117. {
  118. errorText_ = "Invalid Release Path, select the path in Build Settings.";
  119. return false;
  120. }
  121. String antname = Reldir + "/ant.properties";
  122. if ( !fileSystem->FileExists ( antname) )
  123. {
  124. errorText_ = "The file ant.properties not found in " + Reldir + ", unable to generate Release APK.";
  125. return false;
  126. }
  127. if ( !fileOps->BuildCopyFile ( antname, buildPath_ + "/ant.properties" ))
  128. return false;
  129. }
  130. return true;
  131. }
  132. bool AndroidProjectGenerator::GenerateProjectProperties()
  133. {
  134. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  135. Project* project = toolSystem->GetProject();
  136. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  137. String apiString = settings->GetSDKVersion();
  138. if (!apiString.Length())
  139. {
  140. errorText_ = "Invalid Android API level, Press Refresh and select a valid level.";
  141. return false;
  142. }
  143. String props;
  144. props.AppendWithFormat("target=%s", apiString.CString());
  145. File file(context_, buildPath_ + "/project.properties", FILE_WRITE);
  146. if (!file.IsOpen())
  147. {
  148. errorText_ = "Project generator unable to open file project.properties in " + buildPath_;
  149. return false;
  150. }
  151. file.Write(props.CString(), props.Length());
  152. return true;
  153. }
  154. bool AndroidProjectGenerator::GenerateStringXML()
  155. {
  156. FileSystem* fs = GetSubsystem<FileSystem>();
  157. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  158. Project* project = toolSystem->GetProject();
  159. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  160. String appName = settings->GetAppName();
  161. if (!appName.Length())
  162. {
  163. errorText_ = "Invalid App Name, select the App Name in Build Settings.";
  164. return false;
  165. }
  166. String strings = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  167. strings += "<resources>\n";
  168. strings.AppendWithFormat("<string name=\"app_name\">%s</string>\n", appName.CString());
  169. strings += "</resources>\n";
  170. // Create res/values if it doesn't exist
  171. if (!fs->DirExists(buildPath_ + "/res/values"))
  172. {
  173. fs->CreateDirsRecursive(buildPath_ + "/res/values");
  174. }
  175. // Check that we successfully created it
  176. if (!fs->DirExists(buildPath_ + "/res/values"))
  177. {
  178. errorText_ = "Unable to create directory: " + buildPath_ + "/res/values";
  179. return false;
  180. }
  181. File file(context_, buildPath_ + "/res/values/strings.xml", FILE_WRITE);
  182. if (!file.IsOpen())
  183. {
  184. errorText_ = "Unable to write: " + buildPath_ + "/res/values/strings.xml";
  185. return false;
  186. }
  187. file.Write(strings.CString(), strings.Length());
  188. return true;
  189. }
  190. bool AndroidProjectGenerator::GenerateAndroidManifest()
  191. {
  192. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  193. Project* project = toolSystem->GetProject();
  194. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  195. String package = settings->GetPackageName();
  196. if (!package.Length())
  197. {
  198. errorText_ = "Invalid App Package name. The general naming convention is com.company.appname";
  199. return false;
  200. }
  201. // TODO: from settings -- should this be ProductName ?
  202. String activityName = "AtomicGameEngine";
  203. if (!activityName.Length())
  204. {
  205. errorText_ = "Invalid Activity Name";
  206. return false;
  207. }
  208. String manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  209. manifest += "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n";
  210. manifest.AppendWithFormat("package=\"%s\"\n", package.CString());
  211. manifest += "android:versionCode=\"1\"\n";
  212. manifest += "android:versionName=\"1.0\">\n";
  213. manifest += "<uses-permission android:name=\"android.permission.INTERNET\" />\n";
  214. manifest += "<uses-feature android:glEsVersion=\"0x00020000\" />\n";
  215. manifest += "<uses-sdk android:targetSdkVersion=\"12\" android:minSdkVersion=\"10\" />\n";
  216. manifest += "<application android:label=\"@string/app_name\" android:icon=\"@drawable/icon\">\n";
  217. manifest.AppendWithFormat("<activity android:name=\".%s\"\n", activityName.CString());
  218. manifest += "android:label=\"@string/app_name\"\n";
  219. manifest += "android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\"\n";
  220. manifest += "android:configChanges=\"keyboardHidden|orientation\"\n";
  221. manifest += "android:screenOrientation=\"landscape\">\n";
  222. manifest += "<intent-filter>\n";
  223. manifest += "<action android:name=\"android.intent.action.MAIN\" />\n";
  224. manifest += "<category android:name=\"android.intent.category.LAUNCHER\" />\n";
  225. manifest += "</intent-filter>\n";
  226. manifest += "</activity>\n";
  227. manifest += "</application>\n";
  228. manifest += "</manifest>\n";
  229. File file(context_, buildPath_ + "/AndroidManifest.xml", FILE_WRITE);
  230. if (!file.IsOpen())
  231. return false;
  232. file.Write(manifest.CString(), manifest.Length());
  233. return true;
  234. }
  235. bool AndroidProjectGenerator::CopyUserIcons( BuildBase *fileOps )
  236. {
  237. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  238. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  239. Project* project = toolSystem->GetProject();
  240. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  241. String userIconPath = settings->GetIconPath();
  242. if (!fileSystem->DirExists(userIconPath)) // dont do anything if there is no path defined.
  243. return true;
  244. String userIconDir = userIconPath + "/drawable"; // 1st target dir
  245. String userIconFile = userIconDir + "/logo_large.png"; // 1st target file
  246. String destDir = buildPath_ + "/res/drawable"; // where it should be in the build
  247. if ( fileSystem->FileExists (userIconFile) ) // is there a file there?
  248. {
  249. if ( !fileOps->BuildCopyFile ( userIconFile, destDir + "/logo_large.png" ))
  250. return false;
  251. }
  252. userIconDir = userIconPath + "/drawable-ldpi";
  253. userIconFile = userIconDir + "/icon.png";
  254. destDir = buildPath_ + "/res/drawable-ldpi";
  255. if ( fileSystem->FileExists (userIconFile) )
  256. {
  257. if ( !fileOps->BuildCopyFile ( userIconFile, destDir + "/icon.png"))
  258. return false;
  259. }
  260. userIconDir = userIconPath + "/drawable-mdpi";
  261. userIconFile = userIconDir + "/icon.png";
  262. destDir = buildPath_ + "/res/drawable-mdpi";
  263. {
  264. if ( !fileOps->BuildCopyFile ( userIconFile, destDir + "/icon.png" ))
  265. return false;
  266. }
  267. userIconDir = userIconPath + "/drawable-hdpi";
  268. userIconFile = userIconDir + "/icon.png";
  269. destDir = buildPath_ + "/res/drawable-hdpi";
  270. if ( fileSystem->FileExists (userIconFile) )
  271. {
  272. if ( !fileOps->BuildCopyFile ( userIconFile, destDir + "/icon.png" ))
  273. return false;
  274. }
  275. return true;
  276. }
  277. }
  278. /*
  279. */