AndroidProjectGenerator.cpp 11 KB

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