AndroidProjectGenerator.cpp 13 KB

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