export.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export.h"
  31. #include "editor/editor_export.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "io/marshalls.h"
  35. #include "io/zip_io.h"
  36. #include "os/file_access.h"
  37. #include "os/os.h"
  38. #include "platform/android/logo.gen.h"
  39. #include "platform/android/run_icon.gen.h"
  40. #include "project_settings.h"
  41. #include "version.h"
  42. #include <string.h>
  43. static const char *android_perms[] = {
  44. "ACCESS_CHECKIN_PROPERTIES",
  45. "ACCESS_COARSE_LOCATION",
  46. "ACCESS_FINE_LOCATION",
  47. "ACCESS_LOCATION_EXTRA_COMMANDS",
  48. "ACCESS_MOCK_LOCATION",
  49. "ACCESS_NETWORK_STATE",
  50. "ACCESS_SURFACE_FLINGER",
  51. "ACCESS_WIFI_STATE",
  52. "ACCOUNT_MANAGER",
  53. "ADD_VOICEMAIL",
  54. "AUTHENTICATE_ACCOUNTS",
  55. "BATTERY_STATS",
  56. "BIND_ACCESSIBILITY_SERVICE",
  57. "BIND_APPWIDGET",
  58. "BIND_DEVICE_ADMIN",
  59. "BIND_INPUT_METHOD",
  60. "BIND_NFC_SERVICE",
  61. "BIND_NOTIFICATION_LISTENER_SERVICE",
  62. "BIND_PRINT_SERVICE",
  63. "BIND_REMOTEVIEWS",
  64. "BIND_TEXT_SERVICE",
  65. "BIND_VPN_SERVICE",
  66. "BIND_WALLPAPER",
  67. "BLUETOOTH",
  68. "BLUETOOTH_ADMIN",
  69. "BLUETOOTH_PRIVILEGED",
  70. "BRICK",
  71. "BROADCAST_PACKAGE_REMOVED",
  72. "BROADCAST_SMS",
  73. "BROADCAST_STICKY",
  74. "BROADCAST_WAP_PUSH",
  75. "CALL_PHONE",
  76. "CALL_PRIVILEGED",
  77. "CAMERA",
  78. "CAPTURE_AUDIO_OUTPUT",
  79. "CAPTURE_SECURE_VIDEO_OUTPUT",
  80. "CAPTURE_VIDEO_OUTPUT",
  81. "CHANGE_COMPONENT_ENABLED_STATE",
  82. "CHANGE_CONFIGURATION",
  83. "CHANGE_NETWORK_STATE",
  84. "CHANGE_WIFI_MULTICAST_STATE",
  85. "CHANGE_WIFI_STATE",
  86. "CLEAR_APP_CACHE",
  87. "CLEAR_APP_USER_DATA",
  88. "CONTROL_LOCATION_UPDATES",
  89. "DELETE_CACHE_FILES",
  90. "DELETE_PACKAGES",
  91. "DEVICE_POWER",
  92. "DIAGNOSTIC",
  93. "DISABLE_KEYGUARD",
  94. "DUMP",
  95. "EXPAND_STATUS_BAR",
  96. "FACTORY_TEST",
  97. "FLASHLIGHT",
  98. "FORCE_BACK",
  99. "GET_ACCOUNTS",
  100. "GET_PACKAGE_SIZE",
  101. "GET_TASKS",
  102. "GET_TOP_ACTIVITY_INFO",
  103. "GLOBAL_SEARCH",
  104. "HARDWARE_TEST",
  105. "INJECT_EVENTS",
  106. "INSTALL_LOCATION_PROVIDER",
  107. "INSTALL_PACKAGES",
  108. "INSTALL_SHORTCUT",
  109. "INTERNAL_SYSTEM_WINDOW",
  110. "INTERNET",
  111. "KILL_BACKGROUND_PROCESSES",
  112. "LOCATION_HARDWARE",
  113. "MANAGE_ACCOUNTS",
  114. "MANAGE_APP_TOKENS",
  115. "MANAGE_DOCUMENTS",
  116. "MASTER_CLEAR",
  117. "MEDIA_CONTENT_CONTROL",
  118. "MODIFY_AUDIO_SETTINGS",
  119. "MODIFY_PHONE_STATE",
  120. "MOUNT_FORMAT_FILESYSTEMS",
  121. "MOUNT_UNMOUNT_FILESYSTEMS",
  122. "NFC",
  123. "PERSISTENT_ACTIVITY",
  124. "PROCESS_OUTGOING_CALLS",
  125. "READ_CALENDAR",
  126. "READ_CALL_LOG",
  127. "READ_CONTACTS",
  128. "READ_EXTERNAL_STORAGE",
  129. "READ_FRAME_BUFFER",
  130. "READ_HISTORY_BOOKMARKS",
  131. "READ_INPUT_STATE",
  132. "READ_LOGS",
  133. "READ_PHONE_STATE",
  134. "READ_PROFILE",
  135. "READ_SMS",
  136. "READ_SOCIAL_STREAM",
  137. "READ_SYNC_SETTINGS",
  138. "READ_SYNC_STATS",
  139. "READ_USER_DICTIONARY",
  140. "REBOOT",
  141. "RECEIVE_BOOT_COMPLETED",
  142. "RECEIVE_MMS",
  143. "RECEIVE_SMS",
  144. "RECEIVE_WAP_PUSH",
  145. "RECORD_AUDIO",
  146. "REORDER_TASKS",
  147. "RESTART_PACKAGES",
  148. "SEND_RESPOND_VIA_MESSAGE",
  149. "SEND_SMS",
  150. "SET_ACTIVITY_WATCHER",
  151. "SET_ALARM",
  152. "SET_ALWAYS_FINISH",
  153. "SET_ANIMATION_SCALE",
  154. "SET_DEBUG_APP",
  155. "SET_ORIENTATION",
  156. "SET_POINTER_SPEED",
  157. "SET_PREFERRED_APPLICATIONS",
  158. "SET_PROCESS_LIMIT",
  159. "SET_TIME",
  160. "SET_TIME_ZONE",
  161. "SET_WALLPAPER",
  162. "SET_WALLPAPER_HINTS",
  163. "SIGNAL_PERSISTENT_PROCESSES",
  164. "STATUS_BAR",
  165. "SUBSCRIBED_FEEDS_READ",
  166. "SUBSCRIBED_FEEDS_WRITE",
  167. "SYSTEM_ALERT_WINDOW",
  168. "TRANSMIT_IR",
  169. "UNINSTALL_SHORTCUT",
  170. "UPDATE_DEVICE_STATS",
  171. "USE_CREDENTIALS",
  172. "USE_SIP",
  173. "VIBRATE",
  174. "WAKE_LOCK",
  175. "WRITE_APN_SETTINGS",
  176. "WRITE_CALENDAR",
  177. "WRITE_CALL_LOG",
  178. "WRITE_CONTACTS",
  179. "WRITE_EXTERNAL_STORAGE",
  180. "WRITE_GSERVICES",
  181. "WRITE_HISTORY_BOOKMARKS",
  182. "WRITE_PROFILE",
  183. "WRITE_SECURE_SETTINGS",
  184. "WRITE_SETTINGS",
  185. "WRITE_SMS",
  186. "WRITE_SOCIAL_STREAM",
  187. "WRITE_SYNC_SETTINGS",
  188. "WRITE_USER_DICTIONARY",
  189. NULL
  190. };
  191. class EditorExportAndroid : public EditorExportPlatform {
  192. GDCLASS(EditorExportAndroid, EditorExportPlatform)
  193. Ref<ImageTexture> logo;
  194. Ref<ImageTexture> run_icon;
  195. struct Device {
  196. String id;
  197. String name;
  198. String description;
  199. int api_level;
  200. };
  201. struct APKExportData {
  202. zipFile apk;
  203. EditorProgress *ep;
  204. };
  205. Vector<Device> devices;
  206. bool devices_changed;
  207. Mutex *device_lock;
  208. Thread *device_thread;
  209. volatile bool quit_request;
  210. static void _device_poll_thread(void *ud) {
  211. EditorExportAndroid *ea = (EditorExportAndroid *)ud;
  212. while (!ea->quit_request) {
  213. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  214. if (FileAccess::exists(adb)) {
  215. String devices;
  216. List<String> args;
  217. args.push_back("devices");
  218. int ec;
  219. OS::get_singleton()->execute(adb, args, true, NULL, &devices, &ec);
  220. Vector<String> ds = devices.split("\n");
  221. Vector<String> ldevices;
  222. for (int i = 1; i < ds.size(); i++) {
  223. String d = ds[i];
  224. int dpos = d.find("device");
  225. if (dpos == -1)
  226. continue;
  227. d = d.substr(0, dpos).strip_edges();
  228. //print_line("found devuce: "+d);
  229. ldevices.push_back(d);
  230. }
  231. ea->device_lock->lock();
  232. bool different = false;
  233. if (ea->devices.size() != ldevices.size()) {
  234. different = true;
  235. } else {
  236. for (int i = 0; i < ea->devices.size(); i++) {
  237. if (ea->devices[i].id != ldevices[i]) {
  238. different = true;
  239. break;
  240. }
  241. }
  242. }
  243. if (different) {
  244. print_line("DIFFERENT!");
  245. Vector<Device> ndevices;
  246. for (int i = 0; i < ldevices.size(); i++) {
  247. Device d;
  248. d.id = ldevices[i];
  249. for (int j = 0; j < ea->devices.size(); j++) {
  250. if (ea->devices[j].id == ldevices[i]) {
  251. d.description = ea->devices[j].description;
  252. d.name = ea->devices[j].name;
  253. d.api_level = ea->devices[j].api_level;
  254. }
  255. }
  256. if (d.description == "") {
  257. //in the oven, request!
  258. args.clear();
  259. args.push_back("-s");
  260. args.push_back(d.id);
  261. args.push_back("shell");
  262. args.push_back("cat");
  263. args.push_back("/system/build.prop");
  264. int ec;
  265. String dp;
  266. OS::get_singleton()->execute(adb, args, true, NULL, &dp, &ec);
  267. Vector<String> props = dp.split("\n");
  268. String vendor;
  269. String device;
  270. d.description + "Device ID: " + d.id + "\n";
  271. d.api_level = 0;
  272. for (int j = 0; j < props.size(); j++) {
  273. String p = props[j];
  274. if (p.begins_with("ro.product.model=")) {
  275. device = p.get_slice("=", 1).strip_edges();
  276. } else if (p.begins_with("ro.product.brand=")) {
  277. vendor = p.get_slice("=", 1).strip_edges().capitalize();
  278. } else if (p.begins_with("ro.build.display.id=")) {
  279. d.description += "Build: " + p.get_slice("=", 1).strip_edges() + "\n";
  280. } else if (p.begins_with("ro.build.version.release=")) {
  281. d.description += "Release: " + p.get_slice("=", 1).strip_edges() + "\n";
  282. } else if (p.begins_with("ro.build.version.sdk=")) {
  283. d.api_level = p.get_slice("=", 1).to_int();
  284. } else if (p.begins_with("ro.product.cpu.abi=")) {
  285. d.description += "CPU: " + p.get_slice("=", 1).strip_edges() + "\n";
  286. } else if (p.begins_with("ro.product.manufacturer=")) {
  287. d.description += "Manufacturer: " + p.get_slice("=", 1).strip_edges() + "\n";
  288. } else if (p.begins_with("ro.board.platform=")) {
  289. d.description += "Chipset: " + p.get_slice("=", 1).strip_edges() + "\n";
  290. } else if (p.begins_with("ro.opengles.version=")) {
  291. uint32_t opengl = p.get_slice("=", 1).to_int();
  292. d.description += "OpenGL: " + itos(opengl >> 16) + "." + itos((opengl >> 8) & 0xFF) + "." + itos((opengl)&0xFF) + "\n";
  293. }
  294. }
  295. d.name = vendor + " " + device;
  296. //print_line("name: "+d.name);
  297. //print_line("description: "+d.description);
  298. }
  299. ndevices.push_back(d);
  300. }
  301. ea->devices = ndevices;
  302. ea->devices_changed = true;
  303. }
  304. ea->device_lock->unlock();
  305. }
  306. uint64_t wait = 3000000;
  307. uint64_t time = OS::get_singleton()->get_ticks_usec();
  308. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  309. OS::get_singleton()->delay_usec(1000);
  310. if (ea->quit_request)
  311. break;
  312. }
  313. }
  314. if (EditorSettings::get_singleton()->get("export/android/shutdown_adb_on_exit")) {
  315. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  316. if (!FileAccess::exists(adb)) {
  317. return; //adb not configured
  318. }
  319. List<String> args;
  320. args.push_back("kill-server");
  321. OS::get_singleton()->execute(adb, args, true);
  322. };
  323. }
  324. String get_project_name(const String &p_name) const {
  325. String aname;
  326. if (p_name != "") {
  327. aname = p_name;
  328. } else {
  329. aname = ProjectSettings::get_singleton()->get("application/config/name");
  330. }
  331. if (aname == "") {
  332. aname = _MKSTR(VERSION_NAME);
  333. }
  334. return aname;
  335. }
  336. String get_package_name(const String &p_package) {
  337. String pname = p_package;
  338. String basename = ProjectSettings::get_singleton()->get("application/config/name");
  339. basename = basename.to_lower();
  340. String name;
  341. bool first = true;
  342. for (int i = 0; i < basename.length(); i++) {
  343. CharType c = basename[i];
  344. if (c >= '0' && c <= '9' && first) {
  345. continue;
  346. }
  347. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
  348. name += String::chr(c);
  349. first = false;
  350. }
  351. }
  352. if (name == "")
  353. name = "noname";
  354. pname = pname.replace("$genname", name);
  355. return pname;
  356. }
  357. static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
  358. /*
  359. * By not compressing files with little or not benefit in doing so,
  360. * a performance gain is expected attime. Moreover, if the APK is
  361. * zip-aligned, assets stored as they are can be efficiently read by
  362. * Android by memory-mapping them.
  363. */
  364. // -- Unconditional uncompress to mimic AAPT plus some other
  365. static const char *unconditional_compress_ext[] = {
  366. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  367. // These formats are already compressed, or don't compress well:
  368. ".jpg", ".jpeg", ".png", ".gif",
  369. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  370. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  371. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  372. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  373. ".amr", ".awb", ".wma", ".wmv",
  374. // Godot-specific:
  375. ".webp", // Same reasoning as .png
  376. ".cfb", // Don't let small config files slow-down startup
  377. ".scn", // Binary scenes are usually already compressed
  378. ".stex", // Streamable textures are usually already compressed
  379. // Trailer for easier processing
  380. NULL
  381. };
  382. for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
  383. if (p_path.to_lower().ends_with(String(*ext))) {
  384. return false;
  385. }
  386. }
  387. // -- Compressed resource?
  388. if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
  389. // Already compressed
  390. return false;
  391. }
  392. // --- TODO: Decide on texture resources according to their image compression setting
  393. return true;
  394. }
  395. static zip_fileinfo get_zip_fileinfo() {
  396. OS::Time time = OS::get_singleton()->get_time();
  397. OS::Date date = OS::get_singleton()->get_date();
  398. zip_fileinfo zipfi;
  399. zipfi.tmz_date.tm_hour = time.hour;
  400. zipfi.tmz_date.tm_mday = date.day;
  401. zipfi.tmz_date.tm_min = time.min;
  402. zipfi.tmz_date.tm_mon = date.month;
  403. zipfi.tmz_date.tm_sec = time.sec;
  404. zipfi.tmz_date.tm_year = date.year;
  405. zipfi.dosDate = 0;
  406. zipfi.external_fa = 0;
  407. zipfi.internal_fa = 0;
  408. return zipfi;
  409. }
  410. static Error save_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  411. APKExportData *ed = (APKExportData *)p_userdata;
  412. String dst_path = p_path;
  413. dst_path = dst_path.replace_first("res://", "assets/");
  414. zip_fileinfo zipfi = get_zip_fileinfo();
  415. zipOpenNewFileInZip(ed->apk,
  416. dst_path.utf8().get_data(),
  417. &zipfi,
  418. NULL,
  419. 0,
  420. NULL,
  421. 0,
  422. NULL,
  423. _should_compress_asset(p_path, p_data) ? Z_DEFLATED : 0,
  424. Z_DEFAULT_COMPRESSION);
  425. zipWriteInFileInZip(ed->apk, p_data.ptr(), p_data.size());
  426. zipCloseFileInZip(ed->apk);
  427. ed->ep->step("File: " + p_path, 3 + p_file * 100 / p_total);
  428. return OK;
  429. }
  430. void _fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet) {
  431. const int CHUNK_AXML_FILE = 0x00080003;
  432. const int CHUNK_RESOURCEIDS = 0x00080180;
  433. const int CHUNK_STRINGS = 0x001C0001;
  434. const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  435. const int CHUNK_XML_END_TAG = 0x00100103;
  436. const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  437. const int CHUNK_XML_START_TAG = 0x00100102;
  438. const int CHUNK_XML_TEXT = 0x00100104;
  439. const int UTF8_FLAG = 0x00000100;
  440. Vector<String> string_table;
  441. uint32_t ofs = 0;
  442. uint32_t header = decode_uint32(&p_manifest[ofs]);
  443. uint32_t filesize = decode_uint32(&p_manifest[ofs + 4]);
  444. ofs += 8;
  445. //print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));
  446. uint32_t string_count;
  447. uint32_t styles_count;
  448. uint32_t string_flags;
  449. uint32_t string_data_offset;
  450. uint32_t styles_offset;
  451. uint32_t string_table_begins;
  452. uint32_t string_table_ends;
  453. Vector<uint8_t> stable_extra;
  454. String version_name = p_preset->get("version/name");
  455. int version_code = p_preset->get("version/code");
  456. String package_name = p_preset->get("package/unique_name");
  457. int orientation = p_preset->get("screen/orientation");
  458. bool screen_support_small = p_preset->get("screen/support_small");
  459. bool screen_support_normal = p_preset->get("screen/support_normal");
  460. bool screen_support_large = p_preset->get("screen/support_large");
  461. bool screen_support_xlarge = p_preset->get("screen/support_xlarge");
  462. String user_perms[MAX_USER_PERMISSIONS];
  463. for (int i = 0; i < MAX_USER_PERMISSIONS; i++) {
  464. user_perms[i] = p_preset->get("user_permissions/" + itos(i));
  465. }
  466. Set<String> perms;
  467. const char **aperms = android_perms;
  468. while (*aperms) {
  469. bool enabled = p_preset->get("permissions/" + String(*aperms).to_lower());
  470. if (enabled)
  471. perms.insert(String(*aperms));
  472. aperms++;
  473. }
  474. while (ofs < (uint32_t)p_manifest.size()) {
  475. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  476. uint32_t size = decode_uint32(&p_manifest[ofs + 4]);
  477. switch (chunk) {
  478. case CHUNK_STRINGS: {
  479. int iofs = ofs + 8;
  480. string_count = decode_uint32(&p_manifest[iofs]);
  481. styles_count = decode_uint32(&p_manifest[iofs + 4]);
  482. string_flags = decode_uint32(&p_manifest[iofs + 8]);
  483. string_data_offset = decode_uint32(&p_manifest[iofs + 12]);
  484. styles_offset = decode_uint32(&p_manifest[iofs + 16]);
  485. /*
  486. printf("string count: %i\n",string_count);
  487. printf("flags: %i\n",string_flags);
  488. printf("sdata ofs: %i\n",string_data_offset);
  489. printf("styles ofs: %i\n",styles_offset);
  490. */
  491. uint32_t st_offset = iofs + 20;
  492. string_table.resize(string_count);
  493. uint32_t string_end = 0;
  494. string_table_begins = st_offset;
  495. for (uint32_t i = 0; i < string_count; i++) {
  496. uint32_t string_at = decode_uint32(&p_manifest[st_offset + i * 4]);
  497. string_at += st_offset + string_count * 4;
  498. ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
  499. ERR_FAIL_COND(string_flags & UTF8_FLAG);
  500. if (string_flags & UTF8_FLAG) {
  501. } else {
  502. uint32_t len = decode_uint16(&p_manifest[string_at]);
  503. Vector<CharType> ucstring;
  504. ucstring.resize(len + 1);
  505. for (uint32_t j = 0; j < len; j++) {
  506. uint16_t c = decode_uint16(&p_manifest[string_at + 2 + 2 * j]);
  507. ucstring[j] = c;
  508. }
  509. string_end = MAX(string_at + 2 + 2 * len, string_end);
  510. ucstring[len] = 0;
  511. string_table[i] = ucstring.ptr();
  512. }
  513. //print_line("String "+itos(i)+": "+string_table[i]);
  514. }
  515. for (uint32_t i = string_end; i < (ofs + size); i++) {
  516. stable_extra.push_back(p_manifest[i]);
  517. }
  518. //printf("stable extra: %i\n",int(stable_extra.size()));
  519. string_table_ends = ofs + size;
  520. //print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));
  521. } break;
  522. case CHUNK_XML_START_TAG: {
  523. int iofs = ofs + 8;
  524. uint32_t line = decode_uint32(&p_manifest[iofs]);
  525. uint32_t nspace = decode_uint32(&p_manifest[iofs + 8]);
  526. uint32_t name = decode_uint32(&p_manifest[iofs + 12]);
  527. uint32_t check = decode_uint32(&p_manifest[iofs + 16]);
  528. String tname = string_table[name];
  529. //printf("NSPACE: %i\n",nspace);
  530. //printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
  531. //printf("CHECK: %x\n",check);
  532. uint32_t attrcount = decode_uint32(&p_manifest[iofs + 20]);
  533. iofs += 28;
  534. //printf("ATTRCOUNT: %x\n",attrcount);
  535. for (uint32_t i = 0; i < attrcount; i++) {
  536. uint32_t attr_nspace = decode_uint32(&p_manifest[iofs]);
  537. uint32_t attr_name = decode_uint32(&p_manifest[iofs + 4]);
  538. uint32_t attr_value = decode_uint32(&p_manifest[iofs + 8]);
  539. uint32_t attr_flags = decode_uint32(&p_manifest[iofs + 12]);
  540. uint32_t attr_resid = decode_uint32(&p_manifest[iofs + 16]);
  541. String value;
  542. if (attr_value != 0xFFFFFFFF)
  543. value = string_table[attr_value];
  544. else
  545. value = "Res #" + itos(attr_resid);
  546. String attrname = string_table[attr_name];
  547. String nspace;
  548. if (attr_nspace != 0xFFFFFFFF)
  549. nspace = string_table[attr_nspace];
  550. else
  551. nspace = "";
  552. //printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
  553. //printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
  554. //printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
  555. //printf("ATTR %i FLAGS: %x\n",i,attr_flags);
  556. //printf("ATTR %i RESID: %x\n",i,attr_resid);
  557. //replace project information
  558. if (tname == "manifest" && attrname == "package") {
  559. print_line("FOUND package");
  560. string_table[attr_value] = get_package_name(package_name);
  561. }
  562. //print_line("tname: "+tname);
  563. //print_line("nspace: "+nspace);
  564. //print_line("attrname: "+attrname);
  565. if (tname == "manifest" && /*nspace=="android" &&*/ attrname == "versionCode") {
  566. print_line("FOUND versionCode");
  567. encode_uint32(version_code, &p_manifest[iofs + 16]);
  568. }
  569. if (tname == "manifest" && /*nspace=="android" &&*/ attrname == "versionName") {
  570. print_line("FOUND versionName");
  571. if (attr_value == 0xFFFFFFFF) {
  572. WARN_PRINT("Version name in a resource, should be plaintext")
  573. } else
  574. string_table[attr_value] = version_name;
  575. }
  576. if (tname == "activity" && /*nspace=="android" &&*/ attrname == "screenOrientation") {
  577. encode_uint32(orientation == 0 ? 0 : 1, &p_manifest[iofs + 16]);
  578. /*
  579. print_line("FOUND screen orientation");
  580. if (attr_value==0xFFFFFFFF) {
  581. WARN_PRINT("Version name in a resource, should be plaintext")
  582. } else {
  583. string_table[attr_value]=(orientation==0?"landscape":"portrait");
  584. }*/
  585. }
  586. if (tname == "uses-feature" && /*nspace=="android" &&*/ attrname == "glEsVersion") {
  587. print_line("version number: " + itos(decode_uint32(&p_manifest[iofs + 16])));
  588. }
  589. if (tname == "uses-permission" && /*nspace=="android" &&*/ attrname == "name") {
  590. if (value.begins_with("godot.custom")) {
  591. int which = value.get_slice(".", 2).to_int();
  592. if (which >= 0 && which < MAX_USER_PERMISSIONS && user_perms[which].strip_edges() != "") {
  593. string_table[attr_value] = user_perms[which].strip_edges();
  594. }
  595. } else if (value.begins_with("godot.")) {
  596. String perm = value.get_slice(".", 1);
  597. if (perms.has(perm) || (p_give_internet && perm == "INTERNET")) {
  598. print_line("PERM: " + perm);
  599. string_table[attr_value] = "android.permission." + perm;
  600. }
  601. }
  602. }
  603. if (tname == "supports-screens") {
  604. if (attrname == "smallScreens") {
  605. encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  606. } else if (attrname == "normalScreens") {
  607. encode_uint32(screen_support_normal ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  608. } else if (attrname == "largeScreens") {
  609. encode_uint32(screen_support_large ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  610. } else if (attrname == "xlargeScreens") {
  611. encode_uint32(screen_support_xlarge ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  612. }
  613. }
  614. iofs += 20;
  615. }
  616. } break;
  617. }
  618. //printf("chunk %x: size: %d\n",chunk,size);
  619. ofs += size;
  620. }
  621. //printf("end\n");
  622. //create new andriodmanifest binary
  623. Vector<uint8_t> ret;
  624. ret.resize(string_table_begins + string_table.size() * 4);
  625. for (uint32_t i = 0; i < string_table_begins; i++) {
  626. ret[i] = p_manifest[i];
  627. }
  628. ofs = 0;
  629. for (int i = 0; i < string_table.size(); i++) {
  630. encode_uint32(ofs, &ret[string_table_begins + i * 4]);
  631. ofs += string_table[i].length() * 2 + 2 + 2;
  632. //print_line("ofs: "+itos(i)+": "+itos(ofs));
  633. }
  634. ret.resize(ret.size() + ofs);
  635. uint8_t *chars = &ret[ret.size() - ofs];
  636. for (int i = 0; i < string_table.size(); i++) {
  637. String s = string_table[i];
  638. //print_line("savint string :"+s);
  639. encode_uint16(s.length(), chars);
  640. chars += 2;
  641. for (int j = 0; j < s.length(); j++) { //include zero?
  642. encode_uint16(s[j], chars);
  643. chars += 2;
  644. }
  645. encode_uint16(0, chars);
  646. chars += 2;
  647. }
  648. for (int i = 0; i < stable_extra.size(); i++) {
  649. ret.push_back(stable_extra[i]);
  650. }
  651. while (ret.size() % 4)
  652. ret.push_back(0);
  653. uint32_t new_stable_end = ret.size();
  654. uint32_t extra = (p_manifest.size() - string_table_ends);
  655. ret.resize(new_stable_end + extra);
  656. for (uint32_t i = 0; i < extra; i++)
  657. ret[new_stable_end + i] = p_manifest[string_table_ends + i];
  658. while (ret.size() % 4)
  659. ret.push_back(0);
  660. encode_uint32(ret.size(), &ret[4]); //update new file size
  661. encode_uint32(new_stable_end - 8, &ret[12]); //update new string table size
  662. //print_line("file size: "+itos(ret.size()));
  663. p_manifest = ret;
  664. }
  665. static String _parse_string(const uint8_t *p_bytes, bool p_utf8) {
  666. uint32_t offset = 0;
  667. uint32_t len = decode_uint16(&p_bytes[offset]);
  668. if (p_utf8) {
  669. //don't know how to read extended utf8, this will have to be for now
  670. len >>= 8;
  671. }
  672. offset += 2;
  673. //printf("len %i, unicode: %i\n",len,int(p_utf8));
  674. if (p_utf8) {
  675. Vector<uint8_t> str8;
  676. str8.resize(len + 1);
  677. for (uint32_t i = 0; i < len; i++) {
  678. str8[i] = p_bytes[offset + i];
  679. }
  680. str8[len] = 0;
  681. String str;
  682. str.parse_utf8((const char *)str8.ptr());
  683. return str;
  684. } else {
  685. String str;
  686. for (uint32_t i = 0; i < len; i++) {
  687. CharType c = decode_uint16(&p_bytes[offset + i * 2]);
  688. if (c == 0)
  689. break;
  690. str += String::chr(c);
  691. }
  692. return str;
  693. }
  694. }
  695. void _fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest) {
  696. const int UTF8_FLAG = 0x00000100;
  697. print_line("*******************GORRRGLE***********************");
  698. uint32_t header = decode_uint32(&p_manifest[0]);
  699. uint32_t filesize = decode_uint32(&p_manifest[4]);
  700. uint32_t string_block_len = decode_uint32(&p_manifest[16]);
  701. uint32_t string_count = decode_uint32(&p_manifest[20]);
  702. uint32_t string_flags = decode_uint32(&p_manifest[28]);
  703. const uint32_t string_table_begins = 40;
  704. Vector<String> string_table;
  705. String package_name = p_preset->get("package/name");
  706. //printf("stirng block len: %i\n",string_block_len);
  707. //printf("stirng count: %i\n",string_count);
  708. //printf("flags: %x\n",string_flags);
  709. for (uint32_t i = 0; i < string_count; i++) {
  710. uint32_t offset = decode_uint32(&p_manifest[string_table_begins + i * 4]);
  711. offset += string_table_begins + string_count * 4;
  712. String str = _parse_string(&p_manifest[offset], string_flags & UTF8_FLAG);
  713. if (str.begins_with("godot-project-name")) {
  714. if (str == "godot-project-name") {
  715. //project name
  716. str = get_project_name(package_name);
  717. } else {
  718. String lang = str.substr(str.find_last("-") + 1, str.length()).replace("-", "_");
  719. String prop = "application/config/name_" + lang;
  720. if (ProjectSettings::get_singleton()->has(prop)) {
  721. str = ProjectSettings::get_singleton()->get(prop);
  722. } else {
  723. str = get_project_name(package_name);
  724. }
  725. }
  726. }
  727. string_table.push_back(str);
  728. }
  729. //write a new string table, but use 16 bits
  730. Vector<uint8_t> ret;
  731. ret.resize(string_table_begins + string_table.size() * 4);
  732. for (uint32_t i = 0; i < string_table_begins; i++) {
  733. ret[i] = p_manifest[i];
  734. }
  735. int ofs = 0;
  736. for (int i = 0; i < string_table.size(); i++) {
  737. encode_uint32(ofs, &ret[string_table_begins + i * 4]);
  738. ofs += string_table[i].length() * 2 + 2 + 2;
  739. }
  740. ret.resize(ret.size() + ofs);
  741. uint8_t *chars = &ret[ret.size() - ofs];
  742. for (int i = 0; i < string_table.size(); i++) {
  743. String s = string_table[i];
  744. encode_uint16(s.length(), chars);
  745. chars += 2;
  746. for (int j = 0; j < s.length(); j++) {
  747. encode_uint16(s[j], chars);
  748. chars += 2;
  749. }
  750. encode_uint16(0, chars);
  751. chars += 2;
  752. }
  753. //pad
  754. while (ret.size() % 4)
  755. ret.push_back(0);
  756. //change flags to not use utf8
  757. encode_uint32(string_flags & ~0x100, &ret[28]);
  758. //change length
  759. encode_uint32(ret.size() - 12, &ret[16]);
  760. //append the rest...
  761. int rest_from = 12 + string_block_len;
  762. int rest_to = ret.size();
  763. int rest_len = (p_manifest.size() - rest_from);
  764. ret.resize(ret.size() + (p_manifest.size() - rest_from));
  765. for (int i = 0; i < rest_len; i++) {
  766. ret[rest_to + i] = p_manifest[rest_from + i];
  767. }
  768. //finally update the size
  769. encode_uint32(ret.size(), &ret[4]);
  770. p_manifest = ret;
  771. //printf("end\n");
  772. }
  773. public:
  774. enum {
  775. MAX_USER_PERMISSIONS = 20
  776. };
  777. typedef Error (*EditorExportSaveFunction)(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total);
  778. public:
  779. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  780. int api = p_preset->get("graphics/api");
  781. if (api == 0)
  782. r_features->push_back("etc");
  783. else
  784. r_features->push_back("etc2");
  785. }
  786. virtual void get_export_options(List<ExportOption> *r_options) {
  787. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "graphics/api", PROPERTY_HINT_ENUM, "OpenGL ES 2.0,OpenGL ES 3.0"), 1));
  788. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "graphics/32_bits_framebuffer"), true));
  789. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "one_click_deploy/clear_previous_install"), true));
  790. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "apk"), ""));
  791. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "apk"), ""));
  792. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));
  793. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/code", PROPERTY_HINT_RANGE, "1,65535,1"), 1));
  794. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "version/name"), "1.0"));
  795. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name"), "org.godotengine.$genname"));
  796. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/name"), ""));
  797. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/icon", PROPERTY_HINT_FILE, "png"), ""));
  798. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/signed"), true));
  799. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architecture/arm"), true));
  800. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architecture/x86"), false));
  801. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/immersive_mode"), true));
  802. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "screen/orientation", PROPERTY_HINT_ENUM, "Landscape,Portrait"), 0));
  803. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_small"), true));
  804. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_normal"), true));
  805. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_large"), true));
  806. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_xlarge"), true));
  807. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release", PROPERTY_HINT_GLOBAL_FILE, "keystore"), ""));
  808. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_user"), ""));
  809. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_password"), ""));
  810. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "apk_expansion/enable"), false));
  811. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/SALT"), ""));
  812. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/public_key", PROPERTY_HINT_MULTILINE_TEXT), ""));
  813. const char **perms = android_perms;
  814. while (*perms) {
  815. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "permissions/" + String(*perms).to_lower()), false));
  816. perms++;
  817. }
  818. for (int i = 0; i < MAX_USER_PERMISSIONS; i++) {
  819. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "user_permissions/" + itos(i)), false));
  820. }
  821. //r_options->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  822. }
  823. virtual String get_name() const {
  824. return "Android";
  825. }
  826. virtual String get_os_name() const {
  827. return "Android";
  828. }
  829. virtual Ref<Texture> get_logo() const {
  830. return logo;
  831. }
  832. virtual bool poll_devices() {
  833. bool dc = devices_changed;
  834. devices_changed = false;
  835. return dc;
  836. }
  837. virtual int get_device_count() const {
  838. device_lock->lock();
  839. int dc = devices.size();
  840. device_lock->unlock();
  841. return dc;
  842. }
  843. virtual String get_device_name(int p_device) const {
  844. ERR_FAIL_INDEX_V(p_device, devices.size(), "");
  845. device_lock->lock();
  846. String s = devices[p_device].name;
  847. device_lock->unlock();
  848. return s;
  849. }
  850. virtual String get_device_info(int p_device) const {
  851. ERR_FAIL_INDEX_V(p_device, devices.size(), "");
  852. device_lock->lock();
  853. String s = devices[p_device].description;
  854. device_lock->unlock();
  855. return s;
  856. }
  857. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  858. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  859. device_lock->lock();
  860. EditorProgress ep("run", "Running on " + devices[p_device].name, 3);
  861. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  862. if (adb == "") {
  863. EditorNode::add_io_error("ADB executable not configured in settings, can't run.");
  864. device_lock->unlock();
  865. return ERR_UNCONFIGURED;
  866. }
  867. //export_temp
  868. ep.step("Exporting APK", 0);
  869. const bool use_remote = (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) || (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT);
  870. const bool use_reverse = devices[p_device].api_level >= 21;
  871. if (use_reverse)
  872. p_debug_flags |= DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST;
  873. String export_to = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpexport.apk";
  874. Error err = export_project(p_preset, true, export_to, p_debug_flags);
  875. if (err) {
  876. device_lock->unlock();
  877. return err;
  878. }
  879. List<String> args;
  880. int rv;
  881. bool remove_prev = p_preset->get("one_click_deploy/clear_previous_install");
  882. String version_name = p_preset->get("version/name");
  883. String package_name = p_preset->get("package/unique_name");
  884. if (remove_prev) {
  885. ep.step("Uninstalling..", 1);
  886. print_line("Uninstalling previous version: " + devices[p_device].name);
  887. args.push_back("-s");
  888. args.push_back(devices[p_device].id);
  889. args.push_back("uninstall");
  890. args.push_back(get_package_name(package_name));
  891. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  892. }
  893. print_line("Installing into device (please wait..): " + devices[p_device].name);
  894. ep.step("Installing to Device (please wait..)..", 2);
  895. args.clear();
  896. args.push_back("-s");
  897. args.push_back(devices[p_device].id);
  898. args.push_back("install");
  899. args.push_back("-r");
  900. args.push_back(export_to);
  901. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  902. if (err || rv != 0) {
  903. EditorNode::add_io_error("Could not install to device.");
  904. device_lock->unlock();
  905. return ERR_CANT_CREATE;
  906. }
  907. if (use_remote) {
  908. if (use_reverse) {
  909. static const char *const msg = "** Device API >= 21; debugging over USB **";
  910. EditorNode::get_singleton()->get_log()->add_message(msg);
  911. print_line(String(msg).to_upper());
  912. args.clear();
  913. args.push_back("-s");
  914. args.push_back(devices[p_device].id);
  915. args.push_back("reverse");
  916. args.push_back("--remove-all");
  917. OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  918. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  919. int dbg_port = EditorSettings::get_singleton()->get("network/debug/remote_port");
  920. args.clear();
  921. args.push_back("-s");
  922. args.push_back(devices[p_device].id);
  923. args.push_back("reverse");
  924. args.push_back("tcp:" + itos(dbg_port));
  925. args.push_back("tcp:" + itos(dbg_port));
  926. OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  927. print_line("Reverse result: " + itos(rv));
  928. }
  929. if (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT) {
  930. int fs_port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  931. args.clear();
  932. args.push_back("-s");
  933. args.push_back(devices[p_device].id);
  934. args.push_back("reverse");
  935. args.push_back("tcp:" + itos(fs_port));
  936. args.push_back("tcp:" + itos(fs_port));
  937. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  938. print_line("Reverse result2: " + itos(rv));
  939. }
  940. } else {
  941. static const char *const msg = "** Device API < 21; debugging over Wi-Fi **";
  942. EditorNode::get_singleton()->get_log()->add_message(msg);
  943. print_line(String(msg).to_upper());
  944. }
  945. }
  946. ep.step("Running on Device..", 3);
  947. args.clear();
  948. args.push_back("-s");
  949. args.push_back(devices[p_device].id);
  950. args.push_back("shell");
  951. args.push_back("am");
  952. args.push_back("start");
  953. if ((bool)EditorSettings::get_singleton()->get("export/android/force_system_user") && devices[p_device].api_level >= 17) { // Multi-user introduced in Android 17
  954. args.push_back("--user");
  955. args.push_back("0");
  956. }
  957. args.push_back("-a");
  958. args.push_back("android.intent.action.MAIN");
  959. args.push_back("-n");
  960. args.push_back(get_package_name(package_name) + "/org.godotengine.godot.Godot");
  961. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  962. if (err || rv != 0) {
  963. EditorNode::add_io_error("Could not execute on device.");
  964. device_lock->unlock();
  965. return ERR_CANT_CREATE;
  966. }
  967. device_lock->unlock();
  968. return OK;
  969. }
  970. virtual Ref<Texture> get_run_icon() const {
  971. return run_icon;
  972. }
  973. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  974. r_missing_templates = find_export_template("android_debug.apk") == String() || find_export_template("android_release.apk") == String();
  975. bool valid = !r_missing_templates;
  976. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  977. String err;
  978. if (!FileAccess::exists(adb)) {
  979. valid = false;
  980. err += "ADB executable not configured in editor settings.\n";
  981. }
  982. String js = EditorSettings::get_singleton()->get("export/android/jarsigner");
  983. if (!FileAccess::exists(js)) {
  984. valid = false;
  985. err += "OpenJDK 6 jarsigner not configured in editor settings.\n";
  986. }
  987. String dk = EditorSettings::get_singleton()->get("export/android/debug_keystore");
  988. if (!FileAccess::exists(dk)) {
  989. valid = false;
  990. err += "Debug Keystore not configured in editor settings.\n";
  991. }
  992. bool apk_expansion = p_preset->get("apk_expansion/enable");
  993. if (apk_expansion) {
  994. /*
  995. if (apk_expansion_salt=="") {
  996. valid=false;
  997. err+="Invalid SALT for apk expansion.\n";
  998. }
  999. */
  1000. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  1001. if (apk_expansion_pkey == "") {
  1002. valid = false;
  1003. err += "Invalid public key for apk expansion.\n";
  1004. }
  1005. }
  1006. r_error = err;
  1007. return valid;
  1008. }
  1009. virtual String get_binary_extension() const {
  1010. return "apk";
  1011. }
  1012. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) {
  1013. String src_apk;
  1014. EditorProgress ep("export", "Exporting for Android", 105);
  1015. if (p_debug)
  1016. src_apk = p_preset->get("custom_package/debug");
  1017. else
  1018. src_apk = p_preset->get("custom_package/release");
  1019. src_apk = src_apk.strip_edges();
  1020. if (src_apk == "") {
  1021. if (p_debug) {
  1022. src_apk = find_export_template("android_debug.apk");
  1023. } else {
  1024. src_apk = find_export_template("android_release.apk");
  1025. }
  1026. if (src_apk == "") {
  1027. EditorNode::add_io_error("Package not found: " + src_apk);
  1028. return ERR_FILE_NOT_FOUND;
  1029. }
  1030. }
  1031. FileAccess *src_f = NULL;
  1032. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  1033. ep.step("Creating APK", 0);
  1034. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  1035. if (!pkg) {
  1036. EditorNode::add_io_error("Could not find template APK to export:\n" + src_apk);
  1037. return ERR_FILE_NOT_FOUND;
  1038. }
  1039. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  1040. int ret = unzGoToFirstFile(pkg);
  1041. zlib_filefunc_def io2 = io;
  1042. FileAccess *dst_f = NULL;
  1043. io2.opaque = &dst_f;
  1044. String unaligned_path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpexport-unaligned.apk";
  1045. zipFile unaligned_apk = zipOpen2(unaligned_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  1046. bool export_x86 = p_preset->get("architecture/x86");
  1047. bool export_arm = p_preset->get("architecture/arm");
  1048. bool export_arm64 = p_preset->get("architecture/arm64");
  1049. bool use_32_fb = p_preset->get("graphics/32_bits_framebuffer");
  1050. bool immersive = p_preset->get("screen/immersive_mode");
  1051. bool _signed = p_preset->get("package/signed");
  1052. bool apk_expansion = p_preset->get("apk_expansion/enable");
  1053. String cmdline = p_preset->get("command_line/extra_args");
  1054. int version_code = p_preset->get("version/code");
  1055. String version_name = p_preset->get("version/name");
  1056. String package_name = p_preset->get("package/unique_name");
  1057. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  1058. String release_keystore = p_preset->get("keystore/release");
  1059. String release_username = p_preset->get("keystore/release_user");
  1060. String release_password = p_preset->get("keystore/release_password");
  1061. while (ret == UNZ_OK) {
  1062. //get filename
  1063. unz_file_info info;
  1064. char fname[16384];
  1065. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  1066. bool skip = false;
  1067. String file = fname;
  1068. Vector<uint8_t> data;
  1069. data.resize(info.uncompressed_size);
  1070. //read
  1071. unzOpenCurrentFile(pkg);
  1072. unzReadCurrentFile(pkg, data.ptr(), data.size());
  1073. unzCloseCurrentFile(pkg);
  1074. //write
  1075. if (file == "AndroidManifest.xml") {
  1076. _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG));
  1077. }
  1078. if (file == "resources.arsc") {
  1079. _fix_resources(p_preset, data);
  1080. }
  1081. if (file == "res/drawable/icon.png") {
  1082. String icon = p_preset->get("package/icon");
  1083. icon = icon.strip_edges();
  1084. bool found = false;
  1085. if (icon != "" && icon.ends_with(".png")) {
  1086. FileAccess *f = FileAccess::open(icon, FileAccess::READ);
  1087. if (f) {
  1088. data.resize(f->get_len());
  1089. f->get_buffer(data.ptr(), data.size());
  1090. memdelete(f);
  1091. found = true;
  1092. }
  1093. }
  1094. if (!found) {
  1095. String appicon = ProjectSettings::get_singleton()->get("application/config/icon");
  1096. if (appicon != "" && appicon.ends_with(".png")) {
  1097. FileAccess *f = FileAccess::open(appicon, FileAccess::READ);
  1098. if (f) {
  1099. data.resize(f->get_len());
  1100. f->get_buffer(data.ptr(), data.size());
  1101. memdelete(f);
  1102. }
  1103. }
  1104. }
  1105. }
  1106. if (file == "lib/x86/libgodot_android.so" && !export_x86) {
  1107. skip = true;
  1108. }
  1109. if (file.match("lib/armeabi*/libgodot_android.so") && !export_arm) {
  1110. skip = true;
  1111. }
  1112. if (file.match("lib/arm64*/libgodot_android.so") && !export_arm64) {
  1113. skip = true;
  1114. }
  1115. if (file.begins_with("META-INF") && _signed) {
  1116. skip = true;
  1117. }
  1118. print_line("ADDING: " + file);
  1119. if (!skip) {
  1120. // Respect decision on compression made by AAPT for the export template
  1121. const bool uncompressed = info.compression_method == 0;
  1122. zip_fileinfo zipfi = get_zip_fileinfo();
  1123. zipOpenNewFileInZip(unaligned_apk,
  1124. file.utf8().get_data(),
  1125. &zipfi,
  1126. NULL,
  1127. 0,
  1128. NULL,
  1129. 0,
  1130. NULL,
  1131. uncompressed ? 0 : Z_DEFLATED,
  1132. Z_DEFAULT_COMPRESSION);
  1133. zipWriteInFileInZip(unaligned_apk, data.ptr(), data.size());
  1134. zipCloseFileInZip(unaligned_apk);
  1135. }
  1136. ret = unzGoToNextFile(pkg);
  1137. }
  1138. ep.step("Adding Files..", 1);
  1139. Error err = OK;
  1140. Vector<String> cl = cmdline.strip_edges().split(" ");
  1141. for (int i = 0; i < cl.size(); i++) {
  1142. if (cl[i].strip_edges().length() == 0) {
  1143. cl.remove(i);
  1144. i--;
  1145. }
  1146. }
  1147. gen_export_flags(cl, p_flags);
  1148. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  1149. /*String host = EditorSettings::get_singleton()->get("filesystem/file_server/host");
  1150. int port = EditorSettings::get_singleton()->get("filesystem/file_server/post");
  1151. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  1152. cl.push_back("--remote-fs");
  1153. cl.push_back(host+":"+itos(port));
  1154. if (passwd!="") {
  1155. cl.push_back("--remote-fs-password");
  1156. cl.push_back(passwd);
  1157. }*/
  1158. } else {
  1159. //all files
  1160. if (apk_expansion) {
  1161. String apkfname = "main." + itos(version_code) + "." + get_package_name(package_name) + ".obb";
  1162. String fullpath = p_path.get_base_dir().plus_file(apkfname);
  1163. err = save_pack(p_preset, fullpath);
  1164. if (err != OK) {
  1165. EditorNode::add_io_error("Could not write expansion package file: " + apkfname);
  1166. return OK;
  1167. }
  1168. cl.push_back("--use_apk_expansion");
  1169. cl.push_back("--apk_expansion_md5");
  1170. cl.push_back(FileAccess::get_md5(fullpath));
  1171. cl.push_back("--apk_expansion_key");
  1172. cl.push_back(apk_expansion_pkey.strip_edges());
  1173. } else {
  1174. APKExportData ed;
  1175. ed.ep = &ep;
  1176. ed.apk = unaligned_apk;
  1177. err = export_project_files(p_preset, save_apk_file, &ed);
  1178. }
  1179. }
  1180. if (use_32_fb)
  1181. cl.push_back("--use_depth_32");
  1182. if (immersive)
  1183. cl.push_back("--use_immersive");
  1184. if (cl.size()) {
  1185. //add comandline
  1186. Vector<uint8_t> clf;
  1187. clf.resize(4);
  1188. encode_uint32(cl.size(), &clf[0]);
  1189. for (int i = 0; i < cl.size(); i++) {
  1190. CharString txt = cl[i].utf8();
  1191. int base = clf.size();
  1192. clf.resize(base + 4 + txt.length());
  1193. encode_uint32(txt.length(), &clf[base]);
  1194. copymem(&clf[base + 4], txt.ptr(), txt.length());
  1195. print_line(itos(i) + " param: " + cl[i]);
  1196. }
  1197. zip_fileinfo zipfi = get_zip_fileinfo();
  1198. zipOpenNewFileInZip(unaligned_apk,
  1199. "assets/_cl_",
  1200. &zipfi,
  1201. NULL,
  1202. 0,
  1203. NULL,
  1204. 0,
  1205. NULL,
  1206. 0, // No compress (little size gain and potentially slower startup)
  1207. Z_DEFAULT_COMPRESSION);
  1208. zipWriteInFileInZip(unaligned_apk, clf.ptr(), clf.size());
  1209. zipCloseFileInZip(unaligned_apk);
  1210. }
  1211. zipClose(unaligned_apk, NULL);
  1212. unzClose(pkg);
  1213. if (err) {
  1214. return err;
  1215. }
  1216. if (_signed) {
  1217. String jarsigner = EditorSettings::get_singleton()->get("export/android/jarsigner");
  1218. if (!FileAccess::exists(jarsigner)) {
  1219. EditorNode::add_io_error("'jarsigner' could not be found.\nPlease supply a path in the editor settings.\nResulting apk is unsigned.");
  1220. return OK;
  1221. }
  1222. String keystore;
  1223. String password;
  1224. String user;
  1225. if (p_debug) {
  1226. keystore = EditorSettings::get_singleton()->get("export/android/debug_keystore");
  1227. password = EditorSettings::get_singleton()->get("export/android/debug_keystore_pass");
  1228. user = EditorSettings::get_singleton()->get("export/android/debug_keystore_user");
  1229. ep.step("Signing Debug APK..", 103);
  1230. } else {
  1231. keystore = release_keystore;
  1232. password = release_password;
  1233. user = release_username;
  1234. ep.step("Signing Release APK..", 103);
  1235. }
  1236. if (!FileAccess::exists(keystore)) {
  1237. EditorNode::add_io_error("Could not find keystore, unable to export.");
  1238. return ERR_FILE_CANT_OPEN;
  1239. }
  1240. List<String> args;
  1241. args.push_back("-digestalg");
  1242. args.push_back("SHA1");
  1243. args.push_back("-sigalg");
  1244. args.push_back("MD5withRSA");
  1245. String tsa_url = EditorSettings::get_singleton()->get("export/android/timestamping_authority_url");
  1246. if (tsa_url != "") {
  1247. args.push_back("-tsa");
  1248. args.push_back(tsa_url);
  1249. }
  1250. args.push_back("-verbose");
  1251. args.push_back("-keystore");
  1252. args.push_back(keystore);
  1253. args.push_back("-storepass");
  1254. args.push_back(password);
  1255. args.push_back(unaligned_path);
  1256. args.push_back(user);
  1257. int retval;
  1258. OS::get_singleton()->execute(jarsigner, args, true, NULL, NULL, &retval);
  1259. if (retval) {
  1260. EditorNode::add_io_error("'jarsigner' returned with error #" + itos(retval));
  1261. return ERR_CANT_CREATE;
  1262. }
  1263. ep.step("Verifying APK..", 104);
  1264. args.clear();
  1265. args.push_back("-verify");
  1266. args.push_back("-keystore");
  1267. args.push_back(keystore);
  1268. args.push_back(unaligned_path);
  1269. args.push_back("-verbose");
  1270. OS::get_singleton()->execute(jarsigner, args, true, NULL, NULL, &retval);
  1271. if (retval) {
  1272. EditorNode::add_io_error("'jarsigner' verification of APK failed. Make sure to use jarsigner from Java 6.");
  1273. return ERR_CANT_CREATE;
  1274. }
  1275. }
  1276. // Let's zip-align (must be done after signing)
  1277. static const int ZIP_ALIGNMENT = 4;
  1278. ep.step("Aligning APK..", 105);
  1279. unzFile tmp_unaligned = unzOpen2(unaligned_path.utf8().get_data(), &io);
  1280. if (!tmp_unaligned) {
  1281. EditorNode::add_io_error("Could not find temp unaligned APK.");
  1282. return ERR_FILE_NOT_FOUND;
  1283. }
  1284. ERR_FAIL_COND_V(!tmp_unaligned, ERR_CANT_OPEN);
  1285. ret = unzGoToFirstFile(tmp_unaligned);
  1286. io2 = io;
  1287. dst_f = NULL;
  1288. io2.opaque = &dst_f;
  1289. zipFile final_apk = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  1290. // Take files from the unaligned APK and write them out to the aligned one
  1291. // in raw mode, i.e. not uncompressing and recompressing, aligning them as needed,
  1292. // following what is done in https://github.com/android/platform_build/blob/master/tools/zipalign/ZipAlign.cpp
  1293. int bias = 0;
  1294. while (ret == UNZ_OK) {
  1295. unz_file_info info;
  1296. memset(&info, 0, sizeof(info));
  1297. char fname[16384];
  1298. char extra[16384];
  1299. ret = unzGetCurrentFileInfo(tmp_unaligned, &info, fname, 16384, extra, 16384 - ZIP_ALIGNMENT, NULL, 0);
  1300. String file = fname;
  1301. Vector<uint8_t> data;
  1302. data.resize(info.compressed_size);
  1303. // read
  1304. int method, level;
  1305. unzOpenCurrentFile2(tmp_unaligned, &method, &level, 1); // raw read
  1306. long file_offset = unzGetCurrentFileZStreamPos64(tmp_unaligned);
  1307. unzReadCurrentFile(tmp_unaligned, data.ptr(), data.size());
  1308. unzCloseCurrentFile(tmp_unaligned);
  1309. // align
  1310. int padding = 0;
  1311. if (!info.compression_method) {
  1312. // Uncompressed file => Align
  1313. long new_offset = file_offset + bias;
  1314. padding = (ZIP_ALIGNMENT - (new_offset % ZIP_ALIGNMENT)) % ZIP_ALIGNMENT;
  1315. }
  1316. memset(extra + info.size_file_extra, 0, padding);
  1317. // write
  1318. zip_fileinfo zipfi = get_zip_fileinfo();
  1319. zipOpenNewFileInZip2(final_apk,
  1320. file.utf8().get_data(),
  1321. &zipfi,
  1322. extra,
  1323. info.size_file_extra + padding,
  1324. NULL,
  1325. 0,
  1326. NULL,
  1327. method,
  1328. level,
  1329. 1); // raw write
  1330. zipWriteInFileInZip(final_apk, data.ptr(), data.size());
  1331. zipCloseFileInZipRaw(final_apk, info.uncompressed_size, info.crc);
  1332. bias += padding;
  1333. ret = unzGoToNextFile(tmp_unaligned);
  1334. }
  1335. zipClose(final_apk, NULL);
  1336. unzClose(tmp_unaligned);
  1337. if (err) {
  1338. return err;
  1339. }
  1340. return OK;
  1341. }
  1342. virtual void get_platform_features(List<String> *r_features) {
  1343. r_features->push_back("mobile");
  1344. r_features->push_back("Android");
  1345. }
  1346. EditorExportAndroid() {
  1347. Ref<Image> img = memnew(Image(_android_logo));
  1348. logo.instance();
  1349. logo->create_from_image(img);
  1350. img = Ref<Image>(memnew(Image(_android_run_icon)));
  1351. run_icon.instance();
  1352. run_icon->create_from_image(img);
  1353. device_lock = Mutex::create();
  1354. device_thread = Thread::create(_device_poll_thread, this);
  1355. devices_changed = true;
  1356. quit_request = false;
  1357. }
  1358. ~EditorExportAndroid() {
  1359. quit_request = true;
  1360. Thread::wait_to_finish(device_thread);
  1361. memdelete(device_lock);
  1362. memdelete(device_thread);
  1363. }
  1364. };
  1365. void register_android_exporter() {
  1366. String exe_ext;
  1367. if (OS::get_singleton()->get_name() == "Windows") {
  1368. exe_ext = "*.exe";
  1369. }
  1370. EDITOR_DEF("export/android/adb", "");
  1371. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/adb", PROPERTY_HINT_GLOBAL_FILE, exe_ext));
  1372. EDITOR_DEF("export/android/jarsigner", "");
  1373. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/jarsigner", PROPERTY_HINT_GLOBAL_FILE, exe_ext));
  1374. EDITOR_DEF("export/android/debug_keystore", "");
  1375. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/debug_keystore", PROPERTY_HINT_GLOBAL_FILE, "keystore"));
  1376. EDITOR_DEF("export/android/debug_keystore_user", "androiddebugkey");
  1377. EDITOR_DEF("export/android/debug_keystore_pass", "android");
  1378. EDITOR_DEF("export/android/force_system_user", false);
  1379. EDITOR_DEF("export/android/timestamping_authority_url", "");
  1380. EDITOR_DEF("export/android/shutdown_adb_on_exit", true);
  1381. Ref<EditorExportAndroid> exporter = Ref<EditorExportAndroid>(memnew(EditorExportAndroid));
  1382. EditorExport::get_singleton()->add_export_platform(exporter);
  1383. }