AndroidProjectGenerator.cpp 14 KB

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