2
0

export.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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. #if 0
  44. static const char* android_perms[]={
  45. "ACCESS_CHECKIN_PROPERTIES",
  46. "ACCESS_COARSE_LOCATION",
  47. "ACCESS_FINE_LOCATION",
  48. "ACCESS_LOCATION_EXTRA_COMMANDS",
  49. "ACCESS_MOCK_LOCATION",
  50. "ACCESS_NETWORK_STATE",
  51. "ACCESS_SURFACE_FLINGER",
  52. "ACCESS_WIFI_STATE",
  53. "ACCOUNT_MANAGER",
  54. "ADD_VOICEMAIL",
  55. "AUTHENTICATE_ACCOUNTS",
  56. "BATTERY_STATS",
  57. "BIND_ACCESSIBILITY_SERVICE",
  58. "BIND_APPWIDGET",
  59. "BIND_DEVICE_ADMIN",
  60. "BIND_INPUT_METHOD",
  61. "BIND_NFC_SERVICE",
  62. "BIND_NOTIFICATION_LISTENER_SERVICE",
  63. "BIND_PRINT_SERVICE",
  64. "BIND_REMOTEVIEWS",
  65. "BIND_TEXT_SERVICE",
  66. "BIND_VPN_SERVICE",
  67. "BIND_WALLPAPER",
  68. "BLUETOOTH",
  69. "BLUETOOTH_ADMIN",
  70. "BLUETOOTH_PRIVILEGED",
  71. "BRICK",
  72. "BROADCAST_PACKAGE_REMOVED",
  73. "BROADCAST_SMS",
  74. "BROADCAST_STICKY",
  75. "BROADCAST_WAP_PUSH",
  76. "CALL_PHONE",
  77. "CALL_PRIVILEGED",
  78. "CAMERA",
  79. "CAPTURE_AUDIO_OUTPUT",
  80. "CAPTURE_SECURE_VIDEO_OUTPUT",
  81. "CAPTURE_VIDEO_OUTPUT",
  82. "CHANGE_COMPONENT_ENABLED_STATE",
  83. "CHANGE_CONFIGURATION",
  84. "CHANGE_NETWORK_STATE",
  85. "CHANGE_WIFI_MULTICAST_STATE",
  86. "CHANGE_WIFI_STATE",
  87. "CLEAR_APP_CACHE",
  88. "CLEAR_APP_USER_DATA",
  89. "CONTROL_LOCATION_UPDATES",
  90. "DELETE_CACHE_FILES",
  91. "DELETE_PACKAGES",
  92. "DEVICE_POWER",
  93. "DIAGNOSTIC",
  94. "DISABLE_KEYGUARD",
  95. "DUMP",
  96. "EXPAND_STATUS_BAR",
  97. "FACTORY_TEST",
  98. "FLASHLIGHT",
  99. "FORCE_BACK",
  100. "GET_ACCOUNTS",
  101. "GET_PACKAGE_SIZE",
  102. "GET_TASKS",
  103. "GET_TOP_ACTIVITY_INFO",
  104. "GLOBAL_SEARCH",
  105. "HARDWARE_TEST",
  106. "INJECT_EVENTS",
  107. "INSTALL_LOCATION_PROVIDER",
  108. "INSTALL_PACKAGES",
  109. "INSTALL_SHORTCUT",
  110. "INTERNAL_SYSTEM_WINDOW",
  111. "INTERNET",
  112. "KILL_BACKGROUND_PROCESSES",
  113. "LOCATION_HARDWARE",
  114. "MANAGE_ACCOUNTS",
  115. "MANAGE_APP_TOKENS",
  116. "MANAGE_DOCUMENTS",
  117. "MASTER_CLEAR",
  118. "MEDIA_CONTENT_CONTROL",
  119. "MODIFY_AUDIO_SETTINGS",
  120. "MODIFY_PHONE_STATE",
  121. "MOUNT_FORMAT_FILESYSTEMS",
  122. "MOUNT_UNMOUNT_FILESYSTEMS",
  123. "NFC",
  124. "PERSISTENT_ACTIVITY",
  125. "PROCESS_OUTGOING_CALLS",
  126. "READ_CALENDAR",
  127. "READ_CALL_LOG",
  128. "READ_CONTACTS",
  129. "READ_EXTERNAL_STORAGE",
  130. "READ_FRAME_BUFFER",
  131. "READ_HISTORY_BOOKMARKS",
  132. "READ_INPUT_STATE",
  133. "READ_LOGS",
  134. "READ_PHONE_STATE",
  135. "READ_PROFILE",
  136. "READ_SMS",
  137. "READ_SOCIAL_STREAM",
  138. "READ_SYNC_SETTINGS",
  139. "READ_SYNC_STATS",
  140. "READ_USER_DICTIONARY",
  141. "REBOOT",
  142. "RECEIVE_BOOT_COMPLETED",
  143. "RECEIVE_MMS",
  144. "RECEIVE_SMS",
  145. "RECEIVE_WAP_PUSH",
  146. "RECORD_AUDIO",
  147. "REORDER_TASKS",
  148. "RESTART_PACKAGES",
  149. "SEND_RESPOND_VIA_MESSAGE",
  150. "SEND_SMS",
  151. "SET_ACTIVITY_WATCHER",
  152. "SET_ALARM",
  153. "SET_ALWAYS_FINISH",
  154. "SET_ANIMATION_SCALE",
  155. "SET_DEBUG_APP",
  156. "SET_ORIENTATION",
  157. "SET_POINTER_SPEED",
  158. "SET_PREFERRED_APPLICATIONS",
  159. "SET_PROCESS_LIMIT",
  160. "SET_TIME",
  161. "SET_TIME_ZONE",
  162. "SET_WALLPAPER",
  163. "SET_WALLPAPER_HINTS",
  164. "SIGNAL_PERSISTENT_PROCESSES",
  165. "STATUS_BAR",
  166. "SUBSCRIBED_FEEDS_READ",
  167. "SUBSCRIBED_FEEDS_WRITE",
  168. "SYSTEM_ALERT_WINDOW",
  169. "TRANSMIT_IR",
  170. "UNINSTALL_SHORTCUT",
  171. "UPDATE_DEVICE_STATS",
  172. "USE_CREDENTIALS",
  173. "USE_SIP",
  174. "VIBRATE",
  175. "WAKE_LOCK",
  176. "WRITE_APN_SETTINGS",
  177. "WRITE_CALENDAR",
  178. "WRITE_CALL_LOG",
  179. "WRITE_CONTACTS",
  180. "WRITE_EXTERNAL_STORAGE",
  181. "WRITE_GSERVICES",
  182. "WRITE_HISTORY_BOOKMARKS",
  183. "WRITE_PROFILE",
  184. "WRITE_SECURE_SETTINGS",
  185. "WRITE_SETTINGS",
  186. "WRITE_SMS",
  187. "WRITE_SOCIAL_STREAM",
  188. "WRITE_SYNC_SETTINGS",
  189. "WRITE_USER_DICTIONARY",
  190. NULL};
  191. class EditorExportPlatformAndroid : public EditorExportPlatform {
  192. GDCLASS( EditorExportPlatformAndroid,EditorExportPlatform );
  193. enum {
  194. MAX_USER_PERMISSIONS=20,
  195. SCREEN_SMALL=0,
  196. SCREEN_NORMAL=1,
  197. SCREEN_LARGE=2,
  198. SCREEN_XLARGE=3,
  199. SCREEN_MAX=4
  200. };
  201. String custom_release_package;
  202. String custom_debug_package;
  203. int version_code;
  204. String version_name;
  205. String package;
  206. String name;
  207. String icon;
  208. String cmdline;
  209. bool _signed;
  210. bool apk_expansion;
  211. bool remove_prev;
  212. bool use_32_fb;
  213. bool immersive;
  214. bool export_arm;
  215. bool export_x86;
  216. String apk_expansion_salt;
  217. String apk_expansion_pkey;
  218. int orientation;
  219. String release_keystore;
  220. String release_password;
  221. String release_username;
  222. struct APKExportData {
  223. zipFile apk;
  224. EditorProgress *ep;
  225. };
  226. struct Device {
  227. String id;
  228. String name;
  229. String description;
  230. };
  231. Vector<Device> devices;
  232. bool devices_changed;
  233. Mutex *device_lock;
  234. Thread *device_thread;
  235. Ref<ImageTexture> logo;
  236. Set<String> perms;
  237. String user_perms[MAX_USER_PERMISSIONS];
  238. bool screen_support[SCREEN_MAX];
  239. volatile bool quit_request;
  240. static void _device_poll_thread(void *ud);
  241. String get_package_name();
  242. String get_project_name() const;
  243. void _fix_manifest(Vector<uint8_t>& p_manifest, bool p_give_internet);
  244. void _fix_resources(Vector<uint8_t>& p_manifest);
  245. static Error save_apk_file(void *p_userdata,const String& p_path, const Vector<uint8_t>& p_data,int p_file,int p_total);
  246. static bool _should_compress_asset(const String& p_path, const Vector<uint8_t>& p_data);
  247. protected:
  248. bool _set(const StringName& p_name, const Variant& p_value);
  249. bool _get(const StringName& p_name,Variant &r_ret) const;
  250. void _get_property_list( List<PropertyInfo> *p_list) const;
  251. public:
  252. virtual String get_name() const { return "Android"; }
  253. virtual ImageCompression get_image_compression() const { return IMAGE_COMPRESSION_ETC1; }
  254. virtual Ref<Texture> get_logo() const { return logo; }
  255. virtual bool poll_devices();
  256. virtual int get_device_count() const;
  257. virtual String get_device_name(int p_device) const;
  258. virtual String get_device_info(int p_device) const;
  259. virtual Error run(int p_device,int p_flags=0);
  260. virtual bool requires_password(bool p_debug) const { return !p_debug; }
  261. virtual String get_binary_extension() const { return "apk"; }
  262. virtual Error export_project(const String& p_path, bool p_debug, int p_flags=0);
  263. virtual bool can_export(String *r_error=NULL) const;
  264. EditorExportPlatformAndroid();
  265. ~EditorExportPlatformAndroid();
  266. };
  267. bool EditorExportPlatformAndroid::_set(const StringName& p_name, const Variant& p_value) {
  268. String n=p_name;
  269. if (n=="one_click_deploy/clear_previous_install")
  270. remove_prev=p_value;
  271. else if (n=="custom_package/debug")
  272. custom_debug_package=p_value;
  273. else if (n=="custom_package/release")
  274. custom_release_package=p_value;
  275. else if (n=="version/code")
  276. version_code=p_value;
  277. else if (n=="version/name")
  278. version_name=p_value;
  279. else if (n=="command_line/extra_args")
  280. cmdline=p_value;
  281. else if (n=="package/unique_name")
  282. package=p_value;
  283. else if (n=="package/name")
  284. name=p_value;
  285. else if (n=="package/icon")
  286. icon=p_value;
  287. else if (n=="package/signed")
  288. _signed=p_value;
  289. else if (n=="architecture/arm")
  290. export_arm=p_value;
  291. else if (n=="architecture/x86")
  292. export_x86=p_value;
  293. else if (n=="screen/use_32_bits_view")
  294. use_32_fb=p_value;
  295. else if (n=="screen/immersive_mode")
  296. immersive=p_value;
  297. else if (n=="screen/orientation")
  298. orientation=p_value;
  299. else if (n=="screen/support_small")
  300. screen_support[SCREEN_SMALL]=p_value;
  301. else if (n=="screen/support_normal")
  302. screen_support[SCREEN_NORMAL]=p_value;
  303. else if (n=="screen/support_large")
  304. screen_support[SCREEN_LARGE]=p_value;
  305. else if (n=="screen/support_xlarge")
  306. screen_support[SCREEN_XLARGE]=p_value;
  307. else if (n=="keystore/release")
  308. release_keystore=p_value;
  309. else if (n=="keystore/release_user")
  310. release_username=p_value;
  311. else if (n=="keystore/release_password")
  312. release_password=p_value;
  313. else if (n=="apk_expansion/enable")
  314. apk_expansion=p_value;
  315. else if (n=="apk_expansion/SALT")
  316. apk_expansion_salt=p_value;
  317. else if (n=="apk_expansion/public_key")
  318. apk_expansion_pkey=p_value;
  319. else if (n.begins_with("permissions/")) {
  320. String what = n.get_slicec('/',1).to_upper();
  321. bool state = p_value;
  322. if (state)
  323. perms.insert(what);
  324. else
  325. perms.erase(what);
  326. } else if (n.begins_with("user_permissions/")) {
  327. int which = n.get_slicec('/',1).to_int();
  328. ERR_FAIL_INDEX_V(which,MAX_USER_PERMISSIONS,false);
  329. user_perms[which]=p_value;
  330. } else
  331. return false;
  332. return true;
  333. }
  334. bool EditorExportPlatformAndroid::_get(const StringName& p_name,Variant &r_ret) const{
  335. String n=p_name;
  336. if (n=="one_click_deploy/clear_previous_install")
  337. r_ret=remove_prev;
  338. else if (n=="custom_package/debug")
  339. r_ret=custom_debug_package;
  340. else if (n=="custom_package/release")
  341. r_ret=custom_release_package;
  342. else if (n=="version/code")
  343. r_ret=version_code;
  344. else if (n=="version/name")
  345. r_ret=version_name;
  346. else if (n=="command_line/extra_args")
  347. r_ret=cmdline;
  348. else if (n=="package/unique_name")
  349. r_ret=package;
  350. else if (n=="package/name")
  351. r_ret=name;
  352. else if (n=="package/icon")
  353. r_ret=icon;
  354. else if (n=="package/signed")
  355. r_ret=_signed;
  356. else if (n=="architecture/arm")
  357. r_ret=export_arm;
  358. else if (n=="architecture/x86")
  359. r_ret=export_x86;
  360. else if (n=="screen/use_32_bits_view")
  361. r_ret=use_32_fb;
  362. else if (n=="screen/immersive_mode")
  363. r_ret=immersive;
  364. else if (n=="screen/orientation")
  365. r_ret=orientation;
  366. else if (n=="screen/support_small")
  367. r_ret=screen_support[SCREEN_SMALL];
  368. else if (n=="screen/support_normal")
  369. r_ret=screen_support[SCREEN_NORMAL];
  370. else if (n=="screen/support_large")
  371. r_ret=screen_support[SCREEN_LARGE];
  372. else if (n=="screen/support_xlarge")
  373. r_ret=screen_support[SCREEN_XLARGE];
  374. else if (n=="keystore/release")
  375. r_ret=release_keystore;
  376. else if (n=="keystore/release_user")
  377. r_ret=release_username;
  378. else if (n=="keystore/release_password")
  379. r_ret=release_password;
  380. else if (n=="apk_expansion/enable")
  381. r_ret=apk_expansion;
  382. else if (n=="apk_expansion/SALT")
  383. r_ret=apk_expansion_salt;
  384. else if (n=="apk_expansion/public_key")
  385. r_ret=apk_expansion_pkey;
  386. else if (n.begins_with("permissions/")) {
  387. String what = n.get_slicec('/',1).to_upper();
  388. r_ret = perms.has(what);
  389. } else if (n.begins_with("user_permissions/")) {
  390. int which = n.get_slicec('/',1).to_int();
  391. ERR_FAIL_INDEX_V(which,MAX_USER_PERMISSIONS,false);
  392. r_ret=user_perms[which];
  393. } else
  394. return false;
  395. return true;
  396. }
  397. void EditorExportPlatformAndroid::_get_property_list( List<PropertyInfo> *p_list) const{
  398. p_list->push_back( PropertyInfo( Variant::BOOL, "one_click_deploy/clear_previous_install"));
  399. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE,"apk"));
  400. p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE,"apk"));
  401. p_list->push_back( PropertyInfo( Variant::STRING, "command_line/extra_args"));
  402. p_list->push_back( PropertyInfo( Variant::INT, "version/code", PROPERTY_HINT_RANGE,"1,65535,1"));
  403. p_list->push_back( PropertyInfo( Variant::STRING, "version/name") );
  404. p_list->push_back( PropertyInfo( Variant::STRING, "package/unique_name") );
  405. p_list->push_back( PropertyInfo( Variant::STRING, "package/name") );
  406. p_list->push_back( PropertyInfo( Variant::STRING, "package/icon",PROPERTY_HINT_FILE,"png") );
  407. p_list->push_back( PropertyInfo( Variant::BOOL, "package/signed") );
  408. p_list->push_back( PropertyInfo( Variant::BOOL, "architecture/arm") );
  409. p_list->push_back( PropertyInfo( Variant::BOOL, "architecture/x86") );
  410. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/use_32_bits_view") );
  411. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/immersive_mode") );
  412. p_list->push_back( PropertyInfo( Variant::INT, "screen/orientation",PROPERTY_HINT_ENUM,"Landscape,Portrait") );
  413. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_small") );
  414. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_normal") );
  415. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_large") );
  416. p_list->push_back( PropertyInfo( Variant::BOOL, "screen/support_xlarge") );
  417. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release",PROPERTY_HINT_GLOBAL_FILE,"keystore") );
  418. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release_user" ) );
  419. p_list->push_back( PropertyInfo( Variant::STRING, "keystore/release_password" ) );
  420. p_list->push_back( PropertyInfo( Variant::BOOL, "apk_expansion/enable" ) );
  421. p_list->push_back( PropertyInfo( Variant::STRING, "apk_expansion/SALT" ) );
  422. p_list->push_back( PropertyInfo( Variant::STRING, "apk_expansion/public_key",PROPERTY_HINT_MULTILINE_TEXT ) );
  423. const char **perms = android_perms;
  424. while(*perms) {
  425. p_list->push_back( PropertyInfo( Variant::BOOL, "permissions/"+String(*perms).to_lower()));
  426. perms++;
  427. }
  428. for(int i=0;i<MAX_USER_PERMISSIONS;i++) {
  429. p_list->push_back( PropertyInfo( Variant::STRING, "user_permissions/"+itos(i)));
  430. }
  431. //p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  432. }
  433. static String _parse_string(const uint8_t *p_bytes,bool p_utf8) {
  434. uint32_t offset=0;
  435. uint32_t len = decode_uint16(&p_bytes[offset]);
  436. if (p_utf8) {
  437. //don't know how to read extended utf8, this will have to be for now
  438. len>>=8;
  439. }
  440. offset+=2;
  441. //printf("len %i, unicode: %i\n",len,int(p_utf8));
  442. if (p_utf8) {
  443. Vector<uint8_t> str8;
  444. str8.resize(len+1);
  445. for(uint32_t i=0;i<len;i++) {
  446. str8[i]=p_bytes[offset+i];
  447. }
  448. str8[len]=0;
  449. String str;
  450. str.parse_utf8((const char*)str8.ptr());
  451. return str;
  452. } else {
  453. String str;
  454. for(uint32_t i=0;i<len;i++) {
  455. CharType c = decode_uint16(&p_bytes[offset+i*2]);
  456. if (c==0)
  457. break;
  458. str += String::chr(c);
  459. }
  460. return str;
  461. }
  462. }
  463. void EditorExportPlatformAndroid::_fix_resources(Vector<uint8_t>& p_manifest) {
  464. const int UTF8_FLAG = 0x00000100;
  465. print_line("*******************GORRRGLE***********************");
  466. uint32_t header = decode_uint32(&p_manifest[0]);
  467. uint32_t filesize = decode_uint32(&p_manifest[4]);
  468. uint32_t string_block_len = decode_uint32(&p_manifest[16]);
  469. uint32_t string_count = decode_uint32(&p_manifest[20]);
  470. uint32_t string_flags = decode_uint32(&p_manifest[28]);
  471. const uint32_t string_table_begins = 40;
  472. Vector<String> string_table;
  473. //printf("stirng block len: %i\n",string_block_len);
  474. //printf("stirng count: %i\n",string_count);
  475. //printf("flags: %x\n",string_flags);
  476. for(uint32_t i=0;i<string_count;i++) {
  477. uint32_t offset = decode_uint32(&p_manifest[string_table_begins+i*4]);
  478. offset+=string_table_begins+string_count*4;
  479. String str = _parse_string(&p_manifest[offset],string_flags&UTF8_FLAG);
  480. if (str.begins_with("godot-project-name")) {
  481. if (str=="godot-project-name") {
  482. //project name
  483. str = get_project_name();
  484. } else {
  485. String lang = str.substr(str.find_last("-")+1,str.length()).replace("-","_");
  486. String prop = "application/config/name_"+lang;
  487. if (ProjectSettings::get_singleton()->has(prop)) {
  488. str = ProjectSettings::get_singleton()->get(prop);
  489. } else {
  490. str = get_project_name();
  491. }
  492. }
  493. }
  494. string_table.push_back(str);
  495. }
  496. //write a new string table, but use 16 bits
  497. Vector<uint8_t> ret;
  498. ret.resize(string_table_begins+string_table.size()*4);
  499. for(uint32_t i=0;i<string_table_begins;i++) {
  500. ret[i]=p_manifest[i];
  501. }
  502. int ofs=0;
  503. for(int i=0;i<string_table.size();i++) {
  504. encode_uint32(ofs,&ret[string_table_begins+i*4]);
  505. ofs+=string_table[i].length()*2+2+2;
  506. }
  507. ret.resize(ret.size()+ofs);
  508. uint8_t *chars=&ret[ret.size()-ofs];
  509. for(int i=0;i<string_table.size();i++) {
  510. String s = string_table[i];
  511. encode_uint16(s.length(),chars);
  512. chars+=2;
  513. for(int j=0;j<s.length();j++) {
  514. encode_uint16(s[j],chars);
  515. chars+=2;
  516. }
  517. encode_uint16(0,chars);
  518. chars+=2;
  519. }
  520. //pad
  521. while(ret.size()%4)
  522. ret.push_back(0);
  523. //change flags to not use utf8
  524. encode_uint32(string_flags&~0x100,&ret[28]);
  525. //change length
  526. encode_uint32(ret.size()-12,&ret[16]);
  527. //append the rest...
  528. int rest_from = 12+string_block_len;
  529. int rest_to = ret.size();
  530. int rest_len = (p_manifest.size() - rest_from);
  531. ret.resize(ret.size() + (p_manifest.size() - rest_from) );
  532. for(int i=0;i<rest_len;i++) {
  533. ret[rest_to+i]=p_manifest[rest_from+i];
  534. }
  535. //finally update the size
  536. encode_uint32(ret.size(),&ret[4]);
  537. p_manifest=ret;
  538. //printf("end\n");
  539. }
  540. String EditorExportPlatformAndroid::get_project_name() const {
  541. String aname;
  542. if (this->name!="") {
  543. aname=this->name;
  544. } else {
  545. aname = ProjectSettings::get_singleton()->get("application/config/name");
  546. }
  547. if (aname=="") {
  548. aname=_MKSTR(VERSION_NAME);
  549. }
  550. return aname;
  551. }
  552. void EditorExportPlatformAndroid::_fix_manifest(Vector<uint8_t>& p_manifest,bool p_give_internet) {
  553. const int CHUNK_AXML_FILE = 0x00080003;
  554. const int CHUNK_RESOURCEIDS = 0x00080180;
  555. const int CHUNK_STRINGS = 0x001C0001;
  556. const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  557. const int CHUNK_XML_END_TAG = 0x00100103;
  558. const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  559. const int CHUNK_XML_START_TAG = 0x00100102;
  560. const int CHUNK_XML_TEXT = 0x00100104;
  561. const int UTF8_FLAG = 0x00000100;
  562. Vector<String> string_table;
  563. uint32_t ofs=0;
  564. uint32_t header = decode_uint32(&p_manifest[ofs]);
  565. uint32_t filesize = decode_uint32(&p_manifest[ofs+4]);
  566. ofs+=8;
  567. //print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));
  568. uint32_t string_count;
  569. uint32_t styles_count;
  570. uint32_t string_flags;
  571. uint32_t string_data_offset;
  572. uint32_t styles_offset;
  573. uint32_t string_table_begins;
  574. uint32_t string_table_ends;
  575. Vector<uint8_t> stable_extra;
  576. while(ofs < (uint32_t)p_manifest.size()) {
  577. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  578. uint32_t size = decode_uint32(&p_manifest[ofs+4]);
  579. switch(chunk) {
  580. case CHUNK_STRINGS: {
  581. int iofs=ofs+8;
  582. string_count=decode_uint32(&p_manifest[iofs]);
  583. styles_count=decode_uint32(&p_manifest[iofs+4]);
  584. string_flags=decode_uint32(&p_manifest[iofs+8]);
  585. string_data_offset=decode_uint32(&p_manifest[iofs+12]);
  586. styles_offset=decode_uint32(&p_manifest[iofs+16]);
  587. /*
  588. printf("string count: %i\n",string_count);
  589. printf("flags: %i\n",string_flags);
  590. printf("sdata ofs: %i\n",string_data_offset);
  591. printf("styles ofs: %i\n",styles_offset);
  592. */
  593. uint32_t st_offset=iofs+20;
  594. string_table.resize(string_count);
  595. uint32_t string_end=0;
  596. string_table_begins=st_offset;
  597. for(uint32_t i=0;i<string_count;i++) {
  598. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  599. string_at+=st_offset+string_count*4;
  600. ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
  601. ERR_FAIL_COND(string_flags&UTF8_FLAG);
  602. if (string_flags&UTF8_FLAG) {
  603. } else {
  604. uint32_t len = decode_uint16(&p_manifest[string_at]);
  605. Vector<CharType> ucstring;
  606. ucstring.resize(len+1);
  607. for(uint32_t j=0;j<len;j++) {
  608. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  609. ucstring[j]=c;
  610. }
  611. string_end=MAX(string_at+2+2*len,string_end);
  612. ucstring[len]=0;
  613. string_table[i]=ucstring.ptr();
  614. }
  615. //print_line("String "+itos(i)+": "+string_table[i]);
  616. }
  617. for(uint32_t i=string_end;i<(ofs+size);i++) {
  618. stable_extra.push_back(p_manifest[i]);
  619. }
  620. //printf("stable extra: %i\n",int(stable_extra.size()));
  621. string_table_ends=ofs+size;
  622. //print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));
  623. } break;
  624. case CHUNK_XML_START_TAG: {
  625. int iofs=ofs+8;
  626. uint32_t line=decode_uint32(&p_manifest[iofs]);
  627. uint32_t nspace=decode_uint32(&p_manifest[iofs+8]);
  628. uint32_t name=decode_uint32(&p_manifest[iofs+12]);
  629. uint32_t check=decode_uint32(&p_manifest[iofs+16]);
  630. String tname=string_table[name];
  631. //printf("NSPACE: %i\n",nspace);
  632. //printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
  633. //printf("CHECK: %x\n",check);
  634. uint32_t attrcount=decode_uint32(&p_manifest[iofs+20]);
  635. iofs+=28;
  636. //printf("ATTRCOUNT: %x\n",attrcount);
  637. for(uint32_t i=0;i<attrcount;i++) {
  638. uint32_t attr_nspace=decode_uint32(&p_manifest[iofs]);
  639. uint32_t attr_name=decode_uint32(&p_manifest[iofs+4]);
  640. uint32_t attr_value=decode_uint32(&p_manifest[iofs+8]);
  641. uint32_t attr_flags=decode_uint32(&p_manifest[iofs+12]);
  642. uint32_t attr_resid=decode_uint32(&p_manifest[iofs+16]);
  643. String value;
  644. if (attr_value!=0xFFFFFFFF)
  645. value=string_table[attr_value];
  646. else
  647. value="Res #"+itos(attr_resid);
  648. String attrname = string_table[attr_name];
  649. String nspace;
  650. if (attr_nspace!=0xFFFFFFFF)
  651. nspace=string_table[attr_nspace];
  652. else
  653. nspace="";
  654. //printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
  655. //printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
  656. //printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
  657. //printf("ATTR %i FLAGS: %x\n",i,attr_flags);
  658. //printf("ATTR %i RESID: %x\n",i,attr_resid);
  659. //replace project information
  660. if (tname=="manifest" && attrname=="package") {
  661. print_line("FOUND package");
  662. string_table[attr_value]=get_package_name();
  663. }
  664. //print_line("tname: "+tname);
  665. //print_line("nspace: "+nspace);
  666. //print_line("attrname: "+attrname);
  667. if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionCode") {
  668. print_line("FOUND versionCode");
  669. encode_uint32(version_code,&p_manifest[iofs+16]);
  670. }
  671. if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionName") {
  672. print_line("FOUND versionName");
  673. if (attr_value==0xFFFFFFFF) {
  674. WARN_PRINT("Version name in a resource, should be plaintext")
  675. } else
  676. string_table[attr_value]=version_name;
  677. }
  678. if (tname=="activity" && /*nspace=="android" &&*/ attrname=="screenOrientation") {
  679. encode_uint32(orientation==0?0:1,&p_manifest[iofs+16]);
  680. /*
  681. print_line("FOUND screen orientation");
  682. if (attr_value==0xFFFFFFFF) {
  683. WARN_PRINT("Version name in a resource, should be plaintext")
  684. } else {
  685. string_table[attr_value]=(orientation==0?"landscape":"portrait");
  686. }*/
  687. }
  688. if (tname=="uses-permission" && /*nspace=="android" &&*/ attrname=="name") {
  689. if (value.begins_with("godot.custom")) {
  690. int which = value.get_slice(".",2).to_int();
  691. if (which>=0 && which<MAX_USER_PERMISSIONS && user_perms[which].strip_edges()!="") {
  692. string_table[attr_value]=user_perms[which].strip_edges();
  693. }
  694. } else if (value.begins_with("godot.")) {
  695. String perm = value.get_slice(".",1);
  696. if (perms.has(perm) || (p_give_internet && perm=="INTERNET")) {
  697. print_line("PERM: "+perm);
  698. string_table[attr_value]="android.permission."+perm;
  699. }
  700. }
  701. }
  702. if (tname=="supports-screens" ) {
  703. if (attrname=="smallScreens") {
  704. encode_uint32(screen_support[SCREEN_SMALL]?0xFFFFFFFF:0,&p_manifest[iofs+16]);
  705. } else if (attrname=="normalScreens") {
  706. encode_uint32(screen_support[SCREEN_NORMAL]?0xFFFFFFFF:0,&p_manifest[iofs+16]);
  707. } else if (attrname=="largeScreens") {
  708. encode_uint32(screen_support[SCREEN_LARGE]?0xFFFFFFFF:0,&p_manifest[iofs+16]);
  709. } else if (attrname=="xlargeScreens") {
  710. encode_uint32(screen_support[SCREEN_XLARGE]?0xFFFFFFFF:0,&p_manifest[iofs+16]);
  711. }
  712. }
  713. iofs+=20;
  714. }
  715. } break;
  716. }
  717. //printf("chunk %x: size: %d\n",chunk,size);
  718. ofs+=size;
  719. }
  720. //printf("end\n");
  721. //create new andriodmanifest binary
  722. Vector<uint8_t> ret;
  723. ret.resize(string_table_begins+string_table.size()*4);
  724. for(uint32_t i=0;i<string_table_begins;i++) {
  725. ret[i]=p_manifest[i];
  726. }
  727. ofs=0;
  728. for(int i=0;i<string_table.size();i++) {
  729. encode_uint32(ofs,&ret[string_table_begins+i*4]);
  730. ofs+=string_table[i].length()*2+2+2;
  731. //print_line("ofs: "+itos(i)+": "+itos(ofs));
  732. }
  733. ret.resize(ret.size()+ofs);
  734. uint8_t *chars=&ret[ret.size()-ofs];
  735. for(int i=0;i<string_table.size();i++) {
  736. String s = string_table[i];
  737. //print_line("savint string :"+s);
  738. encode_uint16(s.length(),chars);
  739. chars+=2;
  740. for(int j=0;j<s.length();j++) { //include zero?
  741. encode_uint16(s[j],chars);
  742. chars+=2;
  743. }
  744. encode_uint16(0,chars);
  745. chars+=2;
  746. }
  747. for(int i=0;i<stable_extra.size();i++) {
  748. ret.push_back(stable_extra[i]);
  749. }
  750. while(ret.size()%4)
  751. ret.push_back(0);
  752. uint32_t new_stable_end=ret.size();
  753. uint32_t extra = (p_manifest.size()-string_table_ends);
  754. ret.resize(new_stable_end + extra);
  755. for(uint32_t i=0;i<extra;i++)
  756. ret[new_stable_end+i]=p_manifest[string_table_ends+i];
  757. while(ret.size()%4)
  758. ret.push_back(0);
  759. encode_uint32(ret.size(),&ret[4]); //update new file size
  760. encode_uint32(new_stable_end-8,&ret[12]); //update new string table size
  761. //print_line("file size: "+itos(ret.size()));
  762. p_manifest=ret;
  763. #if 0
  764. uint32_t header[9];
  765. for(int i=0;i<9;i++) {
  766. header[i]=decode_uint32(&p_manifest[i*4]);
  767. }
  768. //print_line("STO: "+itos(header[3]));
  769. uint32_t st_offset=9*4;
  770. //ERR_FAIL_COND(header[3]!=0x24)
  771. uint32_t string_count=header[4];
  772. string_table.resize(string_count);
  773. for(int i=0;i<string_count;i++) {
  774. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  775. string_at+=st_offset+string_count*4;
  776. uint32_t len = decode_uint16(&p_manifest[string_at]);
  777. Vector<CharType> ucstring;
  778. ucstring.resize(len+1);
  779. for(int j=0;j<len;j++) {
  780. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  781. ucstring[j]=c;
  782. }
  783. ucstring[len]=0;
  784. string_table[i]=ucstring.ptr();
  785. }
  786. #endif
  787. }
  788. Error EditorExportPlatformAndroid::save_apk_file(void *p_userdata,const String& p_path, const Vector<uint8_t>& p_data,int p_file,int p_total) {
  789. APKExportData *ed=(APKExportData*)p_userdata;
  790. String dst_path=p_path;
  791. dst_path=dst_path.replace_first("res://","assets/");
  792. zipOpenNewFileInZip(ed->apk,
  793. dst_path.utf8().get_data(),
  794. NULL,
  795. NULL,
  796. 0,
  797. NULL,
  798. 0,
  799. NULL,
  800. _should_compress_asset(p_path,p_data) ? Z_DEFLATED : 0,
  801. Z_DEFAULT_COMPRESSION);
  802. zipWriteInFileInZip(ed->apk,p_data.ptr(),p_data.size());
  803. zipCloseFileInZip(ed->apk);
  804. ed->ep->step("File: "+p_path,3+p_file*100/p_total);
  805. return OK;
  806. }
  807. bool EditorExportPlatformAndroid::_should_compress_asset(const String& p_path, const Vector<uint8_t>& p_data) {
  808. /*
  809. * By not compressing files with little or not benefit in doing so,
  810. * a performance gain is expected at runtime. Moreover, if the APK is
  811. * zip-aligned, assets stored as they are can be efficiently read by
  812. * Android by memory-mapping them.
  813. */
  814. // -- Unconditional uncompress to mimic AAPT plus some other
  815. static const char* unconditional_compress_ext[] = {
  816. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  817. // These formats are already compressed, or don't compress well:
  818. ".jpg", ".jpeg", ".png", ".gif",
  819. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  820. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  821. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  822. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  823. ".amr", ".awb", ".wma", ".wmv",
  824. // Godot-specific:
  825. ".webp", // Same reasoning as .png
  826. ".cfb", // Don't let small config files slow-down startup
  827. // Trailer for easier processing
  828. NULL
  829. };
  830. for (const char** ext=unconditional_compress_ext; *ext; ++ext) {
  831. if (p_path.to_lower().ends_with(String(*ext))) {
  832. return false;
  833. }
  834. }
  835. // -- Compressed resource?
  836. if (p_data.size() >= 4 && p_data[0]=='R' && p_data[1]=='S' && p_data[2]=='C' && p_data[3]=='C') {
  837. // Already compressed
  838. return false;
  839. }
  840. // --- TODO: Decide on texture resources according to their image compression setting
  841. return true;
  842. }
  843. Error EditorExportPlatformAndroid::export_project(const String& p_path, bool p_debug, int p_flags) {
  844. String src_apk;
  845. EditorProgress ep("export","Exporting for Android",105);
  846. if (p_debug)
  847. src_apk=custom_debug_package;
  848. else
  849. src_apk=custom_release_package;
  850. if (src_apk=="") {
  851. String err;
  852. if (p_debug) {
  853. src_apk=find_export_template("android_debug.apk", &err);
  854. } else {
  855. src_apk=find_export_template("android_release.apk", &err);
  856. }
  857. if (src_apk=="") {
  858. EditorNode::add_io_error(err);
  859. return ERR_FILE_NOT_FOUND;
  860. }
  861. }
  862. FileAccess *src_f=NULL;
  863. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  864. ep.step("Creating APK",0);
  865. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  866. if (!pkg) {
  867. EditorNode::add_io_error("Could not find template APK to export:\n"+src_apk);
  868. return ERR_FILE_NOT_FOUND;
  869. }
  870. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  871. int ret = unzGoToFirstFile(pkg);
  872. zlib_filefunc_def io2=io;
  873. FileAccess *dst_f=NULL;
  874. io2.opaque=&dst_f;
  875. String unaligned_path=EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmpexport-unaligned.apk";
  876. zipFile unaligned_apk=zipOpen2(unaligned_path.utf8().get_data(),APPEND_STATUS_CREATE,NULL,&io2);
  877. while(ret==UNZ_OK) {
  878. //get filename
  879. unz_file_info info;
  880. char fname[16384];
  881. ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
  882. bool skip=false;
  883. String file=fname;
  884. Vector<uint8_t> data;
  885. data.resize(info.uncompressed_size);
  886. //read
  887. unzOpenCurrentFile(pkg);
  888. unzReadCurrentFile(pkg,data.ptr(),data.size());
  889. unzCloseCurrentFile(pkg);
  890. //write
  891. if (file=="AndroidManifest.xml") {
  892. _fix_manifest(data,p_flags&(EXPORT_DUMB_CLIENT|EXPORT_REMOTE_DEBUG));
  893. }
  894. if (file=="resources.arsc") {
  895. _fix_resources(data);
  896. }
  897. if (file=="res/drawable/icon.png") {
  898. bool found=false;
  899. if (this->icon!="" && this->icon.ends_with(".png")) {
  900. FileAccess *f = FileAccess::open(this->icon,FileAccess::READ);
  901. if (f) {
  902. data.resize(f->get_len());
  903. f->get_buffer(data.ptr(),data.size());
  904. memdelete(f);
  905. found=true;
  906. }
  907. }
  908. if (!found) {
  909. String appicon = ProjectSettings::get_singleton()->get("application/config/icon");
  910. if (appicon!="" && appicon.ends_with(".png")) {
  911. FileAccess*f = FileAccess::open(appicon,FileAccess::READ);
  912. if (f) {
  913. data.resize(f->get_len());
  914. f->get_buffer(data.ptr(),data.size());
  915. memdelete(f);
  916. }
  917. }
  918. }
  919. }
  920. if (file=="lib/x86/libgodot_android.so" && !export_x86) {
  921. skip=true;
  922. }
  923. if (file.match("lib/armeabi*/libgodot_android.so") && !export_arm) {
  924. skip=true;
  925. }
  926. if (file.begins_with("META-INF") && _signed) {
  927. skip=true;
  928. }
  929. print_line("ADDING: "+file);
  930. if (!skip) {
  931. // Respect decision on compression made by AAPT for the export template
  932. const bool uncompressed = info.compression_method == 0;
  933. zipOpenNewFileInZip(unaligned_apk,
  934. file.utf8().get_data(),
  935. NULL,
  936. NULL,
  937. 0,
  938. NULL,
  939. 0,
  940. NULL,
  941. uncompressed ? 0 : Z_DEFLATED,
  942. Z_DEFAULT_COMPRESSION);
  943. zipWriteInFileInZip(unaligned_apk,data.ptr(),data.size());
  944. zipCloseFileInZip(unaligned_apk);
  945. }
  946. ret = unzGoToNextFile(pkg);
  947. }
  948. ep.step("Adding Files..",1);
  949. Error err=OK;
  950. Vector<String> cl = cmdline.strip_edges().split(" ");
  951. for(int i=0;i<cl.size();i++) {
  952. if (cl[i].strip_edges().length()==0) {
  953. cl.remove(i);
  954. i--;
  955. }
  956. }
  957. gen_export_flags(cl,p_flags);
  958. if (p_flags&EXPORT_DUMB_CLIENT) {
  959. /*String host = EditorSettings::get_singleton()->get("filesystem/file_server/host");
  960. int port = EditorSettings::get_singleton()->get("filesystem/file_server/post");
  961. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  962. cl.push_back("-rfs");
  963. cl.push_back(host+":"+itos(port));
  964. if (passwd!="") {
  965. cl.push_back("-rfs_pass");
  966. cl.push_back(passwd);
  967. }*/
  968. } else {
  969. //all files
  970. if (apk_expansion) {
  971. String apkfname="main."+itos(version_code)+"."+get_package_name()+".obb";
  972. String fullpath=p_path.get_base_dir().plus_file(apkfname);
  973. FileAccess *pf = FileAccess::open(fullpath,FileAccess::WRITE);
  974. if (!pf) {
  975. EditorNode::add_io_error("Could not write expansion package file: "+apkfname);
  976. return OK;
  977. }
  978. err = save_pack(pf);
  979. memdelete(pf);
  980. cl.push_back("-use_apk_expansion");
  981. cl.push_back("-apk_expansion_md5");
  982. cl.push_back(FileAccess::get_md5(fullpath));
  983. cl.push_back("-apk_expansion_key");
  984. cl.push_back(apk_expansion_pkey.strip_edges());
  985. } else {
  986. APKExportData ed;
  987. ed.ep=&ep;
  988. ed.apk=unaligned_apk;
  989. err = export_project_files(save_apk_file,&ed,false);
  990. }
  991. }
  992. if (use_32_fb)
  993. cl.push_back("-use_depth_32");
  994. if (immersive)
  995. cl.push_back("-use_immersive");
  996. if (cl.size()) {
  997. //add comandline
  998. Vector<uint8_t> clf;
  999. clf.resize(4);
  1000. encode_uint32(cl.size(),&clf[0]);
  1001. for(int i=0;i<cl.size();i++) {
  1002. CharString txt = cl[i].utf8();
  1003. int base = clf.size();
  1004. clf.resize(base+4+txt.length());
  1005. encode_uint32(txt.length(),&clf[base]);
  1006. copymem(&clf[base+4],txt.ptr(),txt.length());
  1007. print_line(itos(i)+" param: "+cl[i]);
  1008. }
  1009. zipOpenNewFileInZip(unaligned_apk,
  1010. "assets/_cl_",
  1011. NULL,
  1012. NULL,
  1013. 0,
  1014. NULL,
  1015. 0,
  1016. NULL,
  1017. 0, // No compress (little size gain and potentially slower startup)
  1018. Z_DEFAULT_COMPRESSION);
  1019. zipWriteInFileInZip(unaligned_apk,clf.ptr(),clf.size());
  1020. zipCloseFileInZip(unaligned_apk);
  1021. }
  1022. zipClose(unaligned_apk,NULL);
  1023. unzClose(pkg);
  1024. if (err) {
  1025. return err;
  1026. }
  1027. if (_signed) {
  1028. String jarsigner=EditorSettings::get_singleton()->get("export/android/jarsigner");
  1029. if (!FileAccess::exists(jarsigner)) {
  1030. EditorNode::add_io_error("'jarsigner' could not be found.\nPlease supply a path in the editor settings.\nResulting apk is unsigned.");
  1031. return OK;
  1032. }
  1033. String keystore;
  1034. String password;
  1035. String user;
  1036. if (p_debug) {
  1037. keystore=EditorSettings::get_singleton()->get("export/android/debug_keystore");
  1038. password=EditorSettings::get_singleton()->get("export/android/debug_keystore_pass");
  1039. user=EditorSettings::get_singleton()->get("export/android/debug_keystore_user");
  1040. ep.step("Signing Debug APK..",103);
  1041. } else {
  1042. keystore=release_keystore;
  1043. password=release_password;
  1044. user=release_username;
  1045. ep.step("Signing Release APK..",103);
  1046. }
  1047. if (!FileAccess::exists(keystore)) {
  1048. EditorNode::add_io_error("Could not find keystore, unable to export.");
  1049. return ERR_FILE_CANT_OPEN;
  1050. }
  1051. List<String> args;
  1052. args.push_back("-digestalg");
  1053. args.push_back("SHA1");
  1054. args.push_back("-sigalg");
  1055. args.push_back("MD5withRSA");
  1056. String tsa_url=EditorSettings::get_singleton()->get("export/android/timestamping_authority_url");
  1057. if (tsa_url != "") {
  1058. args.push_back("-tsa");
  1059. args.push_back(tsa_url);
  1060. }
  1061. args.push_back("-verbose");
  1062. args.push_back("-keystore");
  1063. args.push_back(keystore);
  1064. args.push_back("-storepass");
  1065. args.push_back(password);
  1066. args.push_back(unaligned_path);
  1067. args.push_back(user);
  1068. int retval;
  1069. OS::get_singleton()->execute(jarsigner,args,true,NULL,NULL,&retval);
  1070. if (retval) {
  1071. EditorNode::add_io_error("'jarsigner' returned with error #"+itos(retval));
  1072. return ERR_CANT_CREATE;
  1073. }
  1074. ep.step("Verifying APK..",104);
  1075. args.clear();
  1076. args.push_back("-verify");
  1077. args.push_back(unaligned_path);
  1078. args.push_back("-verbose");
  1079. OS::get_singleton()->execute(jarsigner,args,true,NULL,NULL,&retval);
  1080. if (retval) {
  1081. EditorNode::add_io_error("'jarsigner' verification of APK failed. Make sure to use jarsigner from Java 6.");
  1082. return ERR_CANT_CREATE;
  1083. }
  1084. }
  1085. // Let's zip-align (must be done after signing)
  1086. static const int ZIP_ALIGNMENT = 4;
  1087. ep.step("Aligning APK..",105);
  1088. unzFile tmp_unaligned = unzOpen2(unaligned_path.utf8().get_data(), &io);
  1089. if (!tmp_unaligned) {
  1090. EditorNode::add_io_error("Could not find temp unaligned APK.");
  1091. return ERR_FILE_NOT_FOUND;
  1092. }
  1093. ERR_FAIL_COND_V(!tmp_unaligned, ERR_CANT_OPEN);
  1094. ret = unzGoToFirstFile(tmp_unaligned);
  1095. io2=io;
  1096. dst_f=NULL;
  1097. io2.opaque=&dst_f;
  1098. zipFile final_apk=zipOpen2(p_path.utf8().get_data(),APPEND_STATUS_CREATE,NULL,&io2);
  1099. // Take files from the unaligned APK and write them out to the aligned one
  1100. // in raw mode, i.e. not uncompressing and recompressing, aligning them as needed,
  1101. // following what is done in https://github.com/android/platform_build/blob/master/tools/zipalign/ZipAlign.cpp
  1102. int bias = 0;
  1103. while(ret==UNZ_OK) {
  1104. unz_file_info info;
  1105. memset(&info, 0, sizeof(info));
  1106. char fname[16384];
  1107. char extra[16384];
  1108. ret = unzGetCurrentFileInfo(tmp_unaligned,&info,fname,16384,extra,16384-ZIP_ALIGNMENT,NULL,0);
  1109. String file=fname;
  1110. Vector<uint8_t> data;
  1111. data.resize(info.compressed_size);
  1112. // read
  1113. int method, level;
  1114. unzOpenCurrentFile2(tmp_unaligned, &method, &level, 1); // raw read
  1115. long file_offset = unzGetCurrentFileZStreamPos64(tmp_unaligned);
  1116. unzReadCurrentFile(tmp_unaligned,data.ptr(),data.size());
  1117. unzCloseCurrentFile(tmp_unaligned);
  1118. // align
  1119. int padding = 0;
  1120. if (!info.compression_method) {
  1121. // Uncompressed file => Align
  1122. long new_offset = file_offset + bias;
  1123. padding = (ZIP_ALIGNMENT - (new_offset % ZIP_ALIGNMENT)) % ZIP_ALIGNMENT;
  1124. }
  1125. memset(extra + info.size_file_extra, 0, padding);
  1126. // write
  1127. zipOpenNewFileInZip2(final_apk,
  1128. file.utf8().get_data(),
  1129. NULL,
  1130. extra,
  1131. info.size_file_extra + padding,
  1132. NULL,
  1133. 0,
  1134. NULL,
  1135. method,
  1136. level,
  1137. 1); // raw write
  1138. zipWriteInFileInZip(final_apk,data.ptr(),data.size());
  1139. zipCloseFileInZipRaw(final_apk,info.uncompressed_size,info.crc);
  1140. bias += padding;
  1141. ret = unzGoToNextFile(tmp_unaligned);
  1142. }
  1143. zipClose(final_apk,NULL);
  1144. unzClose(tmp_unaligned);
  1145. if (err) {
  1146. return err;
  1147. }
  1148. return OK;
  1149. }
  1150. bool EditorExportPlatformAndroid::poll_devices() {
  1151. bool dc=devices_changed;
  1152. devices_changed=false;
  1153. return dc;
  1154. }
  1155. int EditorExportPlatformAndroid::get_device_count() const {
  1156. device_lock->lock();
  1157. int dc=devices.size();
  1158. device_lock->unlock();
  1159. return dc;
  1160. }
  1161. String EditorExportPlatformAndroid::get_device_name(int p_device) const {
  1162. ERR_FAIL_INDEX_V(p_device,devices.size(),"");
  1163. device_lock->lock();
  1164. String s=devices[p_device].name;
  1165. device_lock->unlock();
  1166. return s;
  1167. }
  1168. String EditorExportPlatformAndroid::get_device_info(int p_device) const {
  1169. ERR_FAIL_INDEX_V(p_device,devices.size(),"");
  1170. device_lock->lock();
  1171. String s=devices[p_device].description;
  1172. device_lock->unlock();
  1173. return s;
  1174. }
  1175. void EditorExportPlatformAndroid::_device_poll_thread(void *ud) {
  1176. EditorExportPlatformAndroid *ea=(EditorExportPlatformAndroid *)ud;
  1177. while(!ea->quit_request) {
  1178. String adb=EditorSettings::get_singleton()->get("export/android/adb");
  1179. if (FileAccess::exists(adb)) {
  1180. String devices;
  1181. List<String> args;
  1182. args.push_back("devices");
  1183. int ec;
  1184. OS::get_singleton()->execute(adb,args,true,NULL,&devices,&ec);
  1185. Vector<String> ds = devices.split("\n");
  1186. Vector<String> ldevices;
  1187. for(int i=1;i<ds.size();i++) {
  1188. String d = ds[i];
  1189. int dpos = d.find("device");
  1190. if (dpos==-1)
  1191. continue;
  1192. d=d.substr(0,dpos).strip_edges();
  1193. //print_line("found devuce: "+d);
  1194. ldevices.push_back(d);
  1195. }
  1196. ea->device_lock->lock();
  1197. bool different=false;
  1198. if (devices.size()!=ldevices.size()) {
  1199. different=true;
  1200. } else {
  1201. for(int i=0;i<ea->devices.size();i++) {
  1202. if (ea->devices[i].id!=ldevices[i]) {
  1203. different=true;
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. if (different) {
  1209. Vector<Device> ndevices;
  1210. for(int i=0;i<ldevices.size();i++) {
  1211. Device d;
  1212. d.id=ldevices[i];
  1213. for(int j=0;j<ea->devices.size();j++) {
  1214. if (ea->devices[j].id==ldevices[i]) {
  1215. d.description=ea->devices[j].description;
  1216. d.name=ea->devices[j].name;
  1217. }
  1218. }
  1219. if (d.description=="") {
  1220. //in the oven, request!
  1221. args.clear();
  1222. args.push_back("-s");
  1223. args.push_back(d.id);
  1224. args.push_back("shell");
  1225. args.push_back("cat");
  1226. args.push_back("/system/build.prop");
  1227. int ec;
  1228. String dp;
  1229. OS::get_singleton()->execute(adb,args,true,NULL,&dp,&ec);
  1230. Vector<String> props = dp.split("\n");
  1231. String vendor;
  1232. String device;
  1233. d.description+"Device ID: "+d.id+"\n";
  1234. for(int j=0;j<props.size();j++) {
  1235. String p = props[j];
  1236. if (p.begins_with("ro.product.model=")) {
  1237. device=p.get_slice("=",1).strip_edges();
  1238. } else if (p.begins_with("ro.product.brand=")) {
  1239. vendor=p.get_slice("=",1).strip_edges().capitalize();
  1240. } else if (p.begins_with("ro.build.display.id=")) {
  1241. d.description+="Build: "+p.get_slice("=",1).strip_edges()+"\n";
  1242. } else if (p.begins_with("ro.build.version.release=")) {
  1243. d.description+="Release: "+p.get_slice("=",1).strip_edges()+"\n";
  1244. } else if (p.begins_with("ro.product.cpu.abi=")) {
  1245. d.description+="CPU: "+p.get_slice("=",1).strip_edges()+"\n";
  1246. } else if (p.begins_with("ro.product.manufacturer=")) {
  1247. d.description+="Manufacturer: "+p.get_slice("=",1).strip_edges()+"\n";
  1248. } else if (p.begins_with("ro.board.platform=")) {
  1249. d.description+="Chipset: "+p.get_slice("=",1).strip_edges()+"\n";
  1250. } else if (p.begins_with("ro.opengles.version=")) {
  1251. uint32_t opengl = p.get_slice("=",1).to_int();
  1252. d.description+="OpenGL: "+itos(opengl>>16)+"."+itos((opengl>>8)&0xFF)+"."+itos((opengl)&0xFF)+"\n";
  1253. }
  1254. }
  1255. d.name=vendor+" "+device;
  1256. //print_line("name: "+d.name);
  1257. //print_line("description: "+d.description);
  1258. }
  1259. ndevices.push_back(d);
  1260. }
  1261. ea->devices=ndevices;
  1262. ea->devices_changed=true;
  1263. }
  1264. ea->device_lock->unlock();
  1265. }
  1266. uint64_t wait = 3000000;
  1267. uint64_t time = OS::get_singleton()->get_ticks_usec();
  1268. while(OS::get_singleton()->get_ticks_usec() - time < wait ) {
  1269. OS::get_singleton()->delay_usec(1000);
  1270. if (ea->quit_request)
  1271. break;
  1272. }
  1273. }
  1274. if (EditorSettings::get_singleton()->get("export/android/shutdown_adb_on_exit")) {
  1275. String adb=EditorSettings::get_singleton()->get("export/android/adb");
  1276. if (!FileAccess::exists(adb)) {
  1277. return; //adb not configured
  1278. }
  1279. List<String> args;
  1280. args.push_back("kill-server");
  1281. OS::get_singleton()->execute(adb,args,true);
  1282. };
  1283. }
  1284. Error EditorExportPlatformAndroid::run(int p_device, int p_flags) {
  1285. ERR_FAIL_INDEX_V(p_device,devices.size(),ERR_INVALID_PARAMETER);
  1286. device_lock->lock();
  1287. EditorProgress ep("run","Running on "+devices[p_device].name,3);
  1288. String adb=EditorSettings::get_singleton()->get("export/android/adb");
  1289. if (adb=="") {
  1290. EditorNode::add_io_error("ADB executable not configured in settings, can't run.");
  1291. device_lock->unlock();
  1292. return ERR_UNCONFIGURED;
  1293. }
  1294. //export_temp
  1295. ep.step("Exporting APK",0);
  1296. bool use_adb_over_usb = bool(EDITOR_DEF("export/android/use_remote_debug_over_adb",true));
  1297. if (use_adb_over_usb) {
  1298. p_flags|=EXPORT_REMOTE_DEBUG_LOCALHOST;
  1299. }
  1300. String export_to=EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmpexport.apk";
  1301. Error err = export_project(export_to,true,p_flags);
  1302. if (err) {
  1303. device_lock->unlock();
  1304. return err;
  1305. }
  1306. List<String> args;
  1307. int rv;
  1308. if (remove_prev) {
  1309. ep.step("Uninstalling..",1);
  1310. print_line("Uninstalling previous version: "+devices[p_device].name);
  1311. args.push_back("-s");
  1312. args.push_back(devices[p_device].id);
  1313. args.push_back("uninstall");
  1314. args.push_back(get_package_name());
  1315. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1316. #if 0
  1317. if (err || rv!=0) {
  1318. EditorNode::add_io_error("Could not install to device.");
  1319. device_lock->unlock();
  1320. return ERR_CANT_CREATE;
  1321. }
  1322. #endif
  1323. }
  1324. print_line("Installing into device (please wait..): "+devices[p_device].name);
  1325. ep.step("Installing to Device (please wait..)..",2);
  1326. args.clear();
  1327. args.push_back("-s");
  1328. args.push_back(devices[p_device].id);
  1329. args.push_back("install");
  1330. args.push_back("-r");
  1331. args.push_back(export_to);
  1332. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1333. if (err || rv!=0) {
  1334. EditorNode::add_io_error("Could not install to device.");
  1335. device_lock->unlock();
  1336. return ERR_CANT_CREATE;
  1337. }
  1338. if (use_adb_over_usb) {
  1339. args.clear();
  1340. args.push_back("reverse");
  1341. args.push_back("--remove-all");
  1342. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1343. int port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  1344. args.clear();
  1345. args.push_back("reverse");
  1346. args.push_back("tcp:"+itos(port));
  1347. args.push_back("tcp:"+itos(port));
  1348. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1349. print_line("Reverse result: "+itos(rv));
  1350. int fs_port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  1351. args.clear();
  1352. args.push_back("reverse");
  1353. args.push_back("tcp:"+itos(fs_port));
  1354. args.push_back("tcp:"+itos(fs_port));
  1355. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1356. print_line("Reverse result2: "+itos(rv));
  1357. }
  1358. ep.step("Running on Device..",3);
  1359. args.clear();
  1360. args.push_back("-s");
  1361. args.push_back(devices[p_device].id);
  1362. args.push_back("shell");
  1363. args.push_back("am");
  1364. args.push_back("start");
  1365. args.push_back("--user 0");
  1366. args.push_back("-a");
  1367. args.push_back("android.intent.action.MAIN");
  1368. args.push_back("-n");
  1369. args.push_back(get_package_name()+"/org.godotengine.godot.Godot");
  1370. err = OS::get_singleton()->execute(adb,args,true,NULL,NULL,&rv);
  1371. if (err || rv!=0) {
  1372. EditorNode::add_io_error("Could not execute ondevice.");
  1373. device_lock->unlock();
  1374. return ERR_CANT_CREATE;
  1375. }
  1376. device_lock->unlock();
  1377. return OK;
  1378. }
  1379. String EditorExportPlatformAndroid::get_package_name() {
  1380. String pname = package;
  1381. String basename = ProjectSettings::get_singleton()->get("application/config/name");
  1382. basename=basename.to_lower();
  1383. String name;
  1384. bool first=true;
  1385. for(int i=0;i<basename.length();i++) {
  1386. CharType c = basename[i];
  1387. if (c>='0' && c<='9' && first) {
  1388. continue;
  1389. }
  1390. if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')) {
  1391. name+=String::chr(c);
  1392. first=false;
  1393. }
  1394. }
  1395. if (name=="")
  1396. name="noname";
  1397. pname=pname.replace("$genname",name);
  1398. return pname;
  1399. }
  1400. EditorExportPlatformAndroid::EditorExportPlatformAndroid() {
  1401. version_code=1;
  1402. version_name="1.0";
  1403. package="org.godotengine.$genname";
  1404. name="";
  1405. _signed=true;
  1406. apk_expansion=false;
  1407. quit_request=false;
  1408. orientation=0;
  1409. remove_prev=true;
  1410. use_32_fb=true;
  1411. immersive=true;
  1412. export_arm=true;
  1413. export_x86=false;
  1414. device_thread=Thread::create(_device_poll_thread,this);
  1415. devices_changed=true;
  1416. Image img( _android_logo );
  1417. logo = Ref<ImageTexture>( memnew( ImageTexture ));
  1418. logo->create_from_image(img);
  1419. for(int i=0;i<4;i++)
  1420. screen_support[i]=true;
  1421. }
  1422. bool EditorExportPlatformAndroid::can_export(String *r_error) const {
  1423. bool valid=true;
  1424. String adb=EditorSettings::get_singleton()->get("export/android/adb");
  1425. String err;
  1426. if (!FileAccess::exists(adb)) {
  1427. valid=false;
  1428. err+="ADB executable not configured in editor settings.\n";
  1429. }
  1430. String js = EditorSettings::get_singleton()->get("export/android/jarsigner");
  1431. if (!FileAccess::exists(js)) {
  1432. valid=false;
  1433. err+="OpenJDK 6 jarsigner not configured in editor settings.\n";
  1434. }
  1435. String dk = EditorSettings::get_singleton()->get("export/android/debug_keystore");
  1436. if (!FileAccess::exists(dk)) {
  1437. valid=false;
  1438. err+="Debug Keystore not configured in editor settings.\n";
  1439. }
  1440. if (!exists_export_template("android_debug.apk") || !exists_export_template("android_release.apk")) {
  1441. valid=false;
  1442. err+="No export templates found.\nDownload and install export templates.\n";
  1443. }
  1444. if (custom_debug_package!="" && !FileAccess::exists(custom_debug_package)) {
  1445. valid=false;
  1446. err+="Custom debug package not found.\n";
  1447. }
  1448. if (custom_release_package!="" && !FileAccess::exists(custom_release_package)) {
  1449. valid=false;
  1450. err+="Custom release package not found.\n";
  1451. }
  1452. if (apk_expansion) {
  1453. /*
  1454. if (apk_expansion_salt=="") {
  1455. valid=false;
  1456. err+="Invalid SALT for apk expansion.\n";
  1457. }
  1458. */
  1459. if (apk_expansion_pkey=="") {
  1460. valid=false;
  1461. err+="Invalid public key for apk expansion.\n";
  1462. }
  1463. }
  1464. if (r_error)
  1465. *r_error=err;
  1466. return valid;
  1467. }
  1468. EditorExportPlatformAndroid::~EditorExportPlatformAndroid() {
  1469. quit_request=true;
  1470. Thread::wait_to_finish(device_thread);
  1471. memdelete(device_lock);
  1472. memdelete(device_thread);
  1473. }
  1474. #endif
  1475. static const char *android_perms[] = {
  1476. "ACCESS_CHECKIN_PROPERTIES",
  1477. "ACCESS_COARSE_LOCATION",
  1478. "ACCESS_FINE_LOCATION",
  1479. "ACCESS_LOCATION_EXTRA_COMMANDS",
  1480. "ACCESS_MOCK_LOCATION",
  1481. "ACCESS_NETWORK_STATE",
  1482. "ACCESS_SURFACE_FLINGER",
  1483. "ACCESS_WIFI_STATE",
  1484. "ACCOUNT_MANAGER",
  1485. "ADD_VOICEMAIL",
  1486. "AUTHENTICATE_ACCOUNTS",
  1487. "BATTERY_STATS",
  1488. "BIND_ACCESSIBILITY_SERVICE",
  1489. "BIND_APPWIDGET",
  1490. "BIND_DEVICE_ADMIN",
  1491. "BIND_INPUT_METHOD",
  1492. "BIND_NFC_SERVICE",
  1493. "BIND_NOTIFICATION_LISTENER_SERVICE",
  1494. "BIND_PRINT_SERVICE",
  1495. "BIND_REMOTEVIEWS",
  1496. "BIND_TEXT_SERVICE",
  1497. "BIND_VPN_SERVICE",
  1498. "BIND_WALLPAPER",
  1499. "BLUETOOTH",
  1500. "BLUETOOTH_ADMIN",
  1501. "BLUETOOTH_PRIVILEGED",
  1502. "BRICK",
  1503. "BROADCAST_PACKAGE_REMOVED",
  1504. "BROADCAST_SMS",
  1505. "BROADCAST_STICKY",
  1506. "BROADCAST_WAP_PUSH",
  1507. "CALL_PHONE",
  1508. "CALL_PRIVILEGED",
  1509. "CAMERA",
  1510. "CAPTURE_AUDIO_OUTPUT",
  1511. "CAPTURE_SECURE_VIDEO_OUTPUT",
  1512. "CAPTURE_VIDEO_OUTPUT",
  1513. "CHANGE_COMPONENT_ENABLED_STATE",
  1514. "CHANGE_CONFIGURATION",
  1515. "CHANGE_NETWORK_STATE",
  1516. "CHANGE_WIFI_MULTICAST_STATE",
  1517. "CHANGE_WIFI_STATE",
  1518. "CLEAR_APP_CACHE",
  1519. "CLEAR_APP_USER_DATA",
  1520. "CONTROL_LOCATION_UPDATES",
  1521. "DELETE_CACHE_FILES",
  1522. "DELETE_PACKAGES",
  1523. "DEVICE_POWER",
  1524. "DIAGNOSTIC",
  1525. "DISABLE_KEYGUARD",
  1526. "DUMP",
  1527. "EXPAND_STATUS_BAR",
  1528. "FACTORY_TEST",
  1529. "FLASHLIGHT",
  1530. "FORCE_BACK",
  1531. "GET_ACCOUNTS",
  1532. "GET_PACKAGE_SIZE",
  1533. "GET_TASKS",
  1534. "GET_TOP_ACTIVITY_INFO",
  1535. "GLOBAL_SEARCH",
  1536. "HARDWARE_TEST",
  1537. "INJECT_EVENTS",
  1538. "INSTALL_LOCATION_PROVIDER",
  1539. "INSTALL_PACKAGES",
  1540. "INSTALL_SHORTCUT",
  1541. "INTERNAL_SYSTEM_WINDOW",
  1542. "INTERNET",
  1543. "KILL_BACKGROUND_PROCESSES",
  1544. "LOCATION_HARDWARE",
  1545. "MANAGE_ACCOUNTS",
  1546. "MANAGE_APP_TOKENS",
  1547. "MANAGE_DOCUMENTS",
  1548. "MASTER_CLEAR",
  1549. "MEDIA_CONTENT_CONTROL",
  1550. "MODIFY_AUDIO_SETTINGS",
  1551. "MODIFY_PHONE_STATE",
  1552. "MOUNT_FORMAT_FILESYSTEMS",
  1553. "MOUNT_UNMOUNT_FILESYSTEMS",
  1554. "NFC",
  1555. "PERSISTENT_ACTIVITY",
  1556. "PROCESS_OUTGOING_CALLS",
  1557. "READ_CALENDAR",
  1558. "READ_CALL_LOG",
  1559. "READ_CONTACTS",
  1560. "READ_EXTERNAL_STORAGE",
  1561. "READ_FRAME_BUFFER",
  1562. "READ_HISTORY_BOOKMARKS",
  1563. "READ_INPUT_STATE",
  1564. "READ_LOGS",
  1565. "READ_PHONE_STATE",
  1566. "READ_PROFILE",
  1567. "READ_SMS",
  1568. "READ_SOCIAL_STREAM",
  1569. "READ_SYNC_SETTINGS",
  1570. "READ_SYNC_STATS",
  1571. "READ_USER_DICTIONARY",
  1572. "REBOOT",
  1573. "RECEIVE_BOOT_COMPLETED",
  1574. "RECEIVE_MMS",
  1575. "RECEIVE_SMS",
  1576. "RECEIVE_WAP_PUSH",
  1577. "RECORD_AUDIO",
  1578. "REORDER_TASKS",
  1579. "RESTART_PACKAGES",
  1580. "SEND_RESPOND_VIA_MESSAGE",
  1581. "SEND_SMS",
  1582. "SET_ACTIVITY_WATCHER",
  1583. "SET_ALARM",
  1584. "SET_ALWAYS_FINISH",
  1585. "SET_ANIMATION_SCALE",
  1586. "SET_DEBUG_APP",
  1587. "SET_ORIENTATION",
  1588. "SET_POINTER_SPEED",
  1589. "SET_PREFERRED_APPLICATIONS",
  1590. "SET_PROCESS_LIMIT",
  1591. "SET_TIME",
  1592. "SET_TIME_ZONE",
  1593. "SET_WALLPAPER",
  1594. "SET_WALLPAPER_HINTS",
  1595. "SIGNAL_PERSISTENT_PROCESSES",
  1596. "STATUS_BAR",
  1597. "SUBSCRIBED_FEEDS_READ",
  1598. "SUBSCRIBED_FEEDS_WRITE",
  1599. "SYSTEM_ALERT_WINDOW",
  1600. "TRANSMIT_IR",
  1601. "UNINSTALL_SHORTCUT",
  1602. "UPDATE_DEVICE_STATS",
  1603. "USE_CREDENTIALS",
  1604. "USE_SIP",
  1605. "VIBRATE",
  1606. "WAKE_LOCK",
  1607. "WRITE_APN_SETTINGS",
  1608. "WRITE_CALENDAR",
  1609. "WRITE_CALL_LOG",
  1610. "WRITE_CONTACTS",
  1611. "WRITE_EXTERNAL_STORAGE",
  1612. "WRITE_GSERVICES",
  1613. "WRITE_HISTORY_BOOKMARKS",
  1614. "WRITE_PROFILE",
  1615. "WRITE_SECURE_SETTINGS",
  1616. "WRITE_SETTINGS",
  1617. "WRITE_SMS",
  1618. "WRITE_SOCIAL_STREAM",
  1619. "WRITE_SYNC_SETTINGS",
  1620. "WRITE_USER_DICTIONARY",
  1621. NULL
  1622. };
  1623. class EditorExportAndroid : public EditorExportPlatform {
  1624. GDCLASS(EditorExportAndroid, EditorExportPlatform)
  1625. Ref<ImageTexture> logo;
  1626. Ref<ImageTexture> run_icon;
  1627. struct Device {
  1628. String id;
  1629. String name;
  1630. String description;
  1631. };
  1632. struct APKExportData {
  1633. zipFile apk;
  1634. EditorProgress *ep;
  1635. };
  1636. Vector<Device> devices;
  1637. bool devices_changed;
  1638. Mutex *device_lock;
  1639. Thread *device_thread;
  1640. volatile bool quit_request;
  1641. static void _device_poll_thread(void *ud) {
  1642. EditorExportAndroid *ea = (EditorExportAndroid *)ud;
  1643. while (!ea->quit_request) {
  1644. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  1645. if (FileAccess::exists(adb)) {
  1646. String devices;
  1647. List<String> args;
  1648. args.push_back("devices");
  1649. int ec;
  1650. OS::get_singleton()->execute(adb, args, true, NULL, &devices, &ec);
  1651. Vector<String> ds = devices.split("\n");
  1652. Vector<String> ldevices;
  1653. for (int i = 1; i < ds.size(); i++) {
  1654. String d = ds[i];
  1655. int dpos = d.find("device");
  1656. if (dpos == -1)
  1657. continue;
  1658. d = d.substr(0, dpos).strip_edges();
  1659. //print_line("found devuce: "+d);
  1660. ldevices.push_back(d);
  1661. }
  1662. ea->device_lock->lock();
  1663. bool different = false;
  1664. if (ea->devices.size() != ldevices.size()) {
  1665. different = true;
  1666. } else {
  1667. for (int i = 0; i < ea->devices.size(); i++) {
  1668. if (ea->devices[i].id != ldevices[i]) {
  1669. different = true;
  1670. break;
  1671. }
  1672. }
  1673. }
  1674. if (different) {
  1675. print_line("DIFFERENT!");
  1676. Vector<Device> ndevices;
  1677. for (int i = 0; i < ldevices.size(); i++) {
  1678. Device d;
  1679. d.id = ldevices[i];
  1680. for (int j = 0; j < ea->devices.size(); j++) {
  1681. if (ea->devices[j].id == ldevices[i]) {
  1682. d.description = ea->devices[j].description;
  1683. d.name = ea->devices[j].name;
  1684. }
  1685. }
  1686. if (d.description == "") {
  1687. //in the oven, request!
  1688. args.clear();
  1689. args.push_back("-s");
  1690. args.push_back(d.id);
  1691. args.push_back("shell");
  1692. args.push_back("cat");
  1693. args.push_back("/system/build.prop");
  1694. int ec;
  1695. String dp;
  1696. OS::get_singleton()->execute(adb, args, true, NULL, &dp, &ec);
  1697. Vector<String> props = dp.split("\n");
  1698. String vendor;
  1699. String device;
  1700. d.description + "Device ID: " + d.id + "\n";
  1701. for (int j = 0; j < props.size(); j++) {
  1702. String p = props[j];
  1703. if (p.begins_with("ro.product.model=")) {
  1704. device = p.get_slice("=", 1).strip_edges();
  1705. } else if (p.begins_with("ro.product.brand=")) {
  1706. vendor = p.get_slice("=", 1).strip_edges().capitalize();
  1707. } else if (p.begins_with("ro.build.display.id=")) {
  1708. d.description += "Build: " + p.get_slice("=", 1).strip_edges() + "\n";
  1709. } else if (p.begins_with("ro.build.version.release=")) {
  1710. d.description += "Release: " + p.get_slice("=", 1).strip_edges() + "\n";
  1711. } else if (p.begins_with("ro.product.cpu.abi=")) {
  1712. d.description += "CPU: " + p.get_slice("=", 1).strip_edges() + "\n";
  1713. } else if (p.begins_with("ro.product.manufacturer=")) {
  1714. d.description += "Manufacturer: " + p.get_slice("=", 1).strip_edges() + "\n";
  1715. } else if (p.begins_with("ro.board.platform=")) {
  1716. d.description += "Chipset: " + p.get_slice("=", 1).strip_edges() + "\n";
  1717. } else if (p.begins_with("ro.opengles.version=")) {
  1718. uint32_t opengl = p.get_slice("=", 1).to_int();
  1719. d.description += "OpenGL: " + itos(opengl >> 16) + "." + itos((opengl >> 8) & 0xFF) + "." + itos((opengl)&0xFF) + "\n";
  1720. }
  1721. }
  1722. d.name = vendor + " " + device;
  1723. //print_line("name: "+d.name);
  1724. //print_line("description: "+d.description);
  1725. }
  1726. ndevices.push_back(d);
  1727. }
  1728. ea->devices = ndevices;
  1729. ea->devices_changed = true;
  1730. }
  1731. ea->device_lock->unlock();
  1732. }
  1733. uint64_t wait = 3000000;
  1734. uint64_t time = OS::get_singleton()->get_ticks_usec();
  1735. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  1736. OS::get_singleton()->delay_usec(1000);
  1737. if (ea->quit_request)
  1738. break;
  1739. }
  1740. }
  1741. if (EditorSettings::get_singleton()->get("export/android/shutdown_adb_on_exit")) {
  1742. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  1743. if (!FileAccess::exists(adb)) {
  1744. return; //adb not configured
  1745. }
  1746. List<String> args;
  1747. args.push_back("kill-server");
  1748. OS::get_singleton()->execute(adb, args, true);
  1749. };
  1750. }
  1751. String get_project_name(const String &p_name) const {
  1752. String aname;
  1753. if (p_name != "") {
  1754. aname = p_name;
  1755. } else {
  1756. aname = ProjectSettings::get_singleton()->get("application/config/name");
  1757. }
  1758. if (aname == "") {
  1759. aname = _MKSTR(VERSION_NAME);
  1760. }
  1761. return aname;
  1762. }
  1763. String get_package_name(const String &p_package) {
  1764. String pname = p_package;
  1765. String basename = ProjectSettings::get_singleton()->get("application/config/name");
  1766. basename = basename.to_lower();
  1767. String name;
  1768. bool first = true;
  1769. for (int i = 0; i < basename.length(); i++) {
  1770. CharType c = basename[i];
  1771. if (c >= '0' && c <= '9' && first) {
  1772. continue;
  1773. }
  1774. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
  1775. name += String::chr(c);
  1776. first = false;
  1777. }
  1778. }
  1779. if (name == "")
  1780. name = "noname";
  1781. pname = pname.replace("$genname", name);
  1782. return pname;
  1783. }
  1784. static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
  1785. /*
  1786. * By not compressing files with little or not benefit in doing so,
  1787. * a performance gain is expected attime. Moreover, if the APK is
  1788. * zip-aligned, assets stored as they are can be efficiently read by
  1789. * Android by memory-mapping them.
  1790. */
  1791. // -- Unconditional uncompress to mimic AAPT plus some other
  1792. static const char *unconditional_compress_ext[] = {
  1793. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  1794. // These formats are already compressed, or don't compress well:
  1795. ".jpg", ".jpeg", ".png", ".gif",
  1796. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  1797. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  1798. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  1799. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  1800. ".amr", ".awb", ".wma", ".wmv",
  1801. // Godot-specific:
  1802. ".webp", // Same reasoning as .png
  1803. ".cfb", // Don't let small config files slow-down startup
  1804. ".scn", // Binary scenes are usually already compressed
  1805. ".stex", // Streamable textures are usually already compressed
  1806. // Trailer for easier processing
  1807. NULL
  1808. };
  1809. for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
  1810. if (p_path.to_lower().ends_with(String(*ext))) {
  1811. return false;
  1812. }
  1813. }
  1814. // -- Compressed resource?
  1815. if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
  1816. // Already compressed
  1817. return false;
  1818. }
  1819. // --- TODO: Decide on texture resources according to their image compression setting
  1820. return true;
  1821. }
  1822. static zip_fileinfo get_zip_fileinfo() {
  1823. OS::Time time = OS::get_singleton()->get_time();
  1824. OS::Date date = OS::get_singleton()->get_date();
  1825. zip_fileinfo zipfi;
  1826. zipfi.tmz_date.tm_hour = time.hour;
  1827. zipfi.tmz_date.tm_mday = date.day;
  1828. zipfi.tmz_date.tm_min = time.min;
  1829. zipfi.tmz_date.tm_mon = date.month;
  1830. zipfi.tmz_date.tm_sec = time.sec;
  1831. zipfi.tmz_date.tm_year = date.year;
  1832. zipfi.dosDate = 0;
  1833. zipfi.external_fa = 0;
  1834. zipfi.internal_fa = 0;
  1835. return zipfi;
  1836. }
  1837. static Error save_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  1838. APKExportData *ed = (APKExportData *)p_userdata;
  1839. String dst_path = p_path;
  1840. dst_path = dst_path.replace_first("res://", "assets/");
  1841. zip_fileinfo zipfi = get_zip_fileinfo();
  1842. zipOpenNewFileInZip(ed->apk,
  1843. dst_path.utf8().get_data(),
  1844. &zipfi,
  1845. NULL,
  1846. 0,
  1847. NULL,
  1848. 0,
  1849. NULL,
  1850. _should_compress_asset(p_path, p_data) ? Z_DEFLATED : 0,
  1851. Z_DEFAULT_COMPRESSION);
  1852. zipWriteInFileInZip(ed->apk, p_data.ptr(), p_data.size());
  1853. zipCloseFileInZip(ed->apk);
  1854. ed->ep->step("File: " + p_path, 3 + p_file * 100 / p_total);
  1855. return OK;
  1856. }
  1857. void _fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet) {
  1858. const int CHUNK_AXML_FILE = 0x00080003;
  1859. const int CHUNK_RESOURCEIDS = 0x00080180;
  1860. const int CHUNK_STRINGS = 0x001C0001;
  1861. const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  1862. const int CHUNK_XML_END_TAG = 0x00100103;
  1863. const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  1864. const int CHUNK_XML_START_TAG = 0x00100102;
  1865. const int CHUNK_XML_TEXT = 0x00100104;
  1866. const int UTF8_FLAG = 0x00000100;
  1867. Vector<String> string_table;
  1868. uint32_t ofs = 0;
  1869. uint32_t header = decode_uint32(&p_manifest[ofs]);
  1870. uint32_t filesize = decode_uint32(&p_manifest[ofs + 4]);
  1871. ofs += 8;
  1872. //print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));
  1873. uint32_t string_count;
  1874. uint32_t styles_count;
  1875. uint32_t string_flags;
  1876. uint32_t string_data_offset;
  1877. uint32_t styles_offset;
  1878. uint32_t string_table_begins;
  1879. uint32_t string_table_ends;
  1880. Vector<uint8_t> stable_extra;
  1881. String version_name = p_preset->get("version/name");
  1882. int version_code = p_preset->get("version/code");
  1883. String package_name = p_preset->get("package/unique_name");
  1884. int orientation = p_preset->get("screen/orientation");
  1885. bool screen_support_small = p_preset->get("screen/support_small");
  1886. bool screen_support_normal = p_preset->get("screen/support_normal");
  1887. bool screen_support_large = p_preset->get("screen/support_large");
  1888. bool screen_support_xlarge = p_preset->get("screen/support_xlarge");
  1889. String user_perms[MAX_USER_PERMISSIONS];
  1890. for (int i = 0; i < MAX_USER_PERMISSIONS; i++) {
  1891. user_perms[i] = p_preset->get("user_permissions/" + itos(i));
  1892. }
  1893. Set<String> perms;
  1894. const char **aperms = android_perms;
  1895. while (*aperms) {
  1896. bool enabled = p_preset->get("permissions/" + String(*aperms).to_lower());
  1897. if (enabled)
  1898. perms.insert(String(*aperms));
  1899. aperms++;
  1900. }
  1901. while (ofs < (uint32_t)p_manifest.size()) {
  1902. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  1903. uint32_t size = decode_uint32(&p_manifest[ofs + 4]);
  1904. switch (chunk) {
  1905. case CHUNK_STRINGS: {
  1906. int iofs = ofs + 8;
  1907. string_count = decode_uint32(&p_manifest[iofs]);
  1908. styles_count = decode_uint32(&p_manifest[iofs + 4]);
  1909. string_flags = decode_uint32(&p_manifest[iofs + 8]);
  1910. string_data_offset = decode_uint32(&p_manifest[iofs + 12]);
  1911. styles_offset = decode_uint32(&p_manifest[iofs + 16]);
  1912. /*
  1913. printf("string count: %i\n",string_count);
  1914. printf("flags: %i\n",string_flags);
  1915. printf("sdata ofs: %i\n",string_data_offset);
  1916. printf("styles ofs: %i\n",styles_offset);
  1917. */
  1918. uint32_t st_offset = iofs + 20;
  1919. string_table.resize(string_count);
  1920. uint32_t string_end = 0;
  1921. string_table_begins = st_offset;
  1922. for (uint32_t i = 0; i < string_count; i++) {
  1923. uint32_t string_at = decode_uint32(&p_manifest[st_offset + i * 4]);
  1924. string_at += st_offset + string_count * 4;
  1925. ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
  1926. ERR_FAIL_COND(string_flags & UTF8_FLAG);
  1927. if (string_flags & UTF8_FLAG) {
  1928. } else {
  1929. uint32_t len = decode_uint16(&p_manifest[string_at]);
  1930. Vector<CharType> ucstring;
  1931. ucstring.resize(len + 1);
  1932. for (uint32_t j = 0; j < len; j++) {
  1933. uint16_t c = decode_uint16(&p_manifest[string_at + 2 + 2 * j]);
  1934. ucstring[j] = c;
  1935. }
  1936. string_end = MAX(string_at + 2 + 2 * len, string_end);
  1937. ucstring[len] = 0;
  1938. string_table[i] = ucstring.ptr();
  1939. }
  1940. //print_line("String "+itos(i)+": "+string_table[i]);
  1941. }
  1942. for (uint32_t i = string_end; i < (ofs + size); i++) {
  1943. stable_extra.push_back(p_manifest[i]);
  1944. }
  1945. //printf("stable extra: %i\n",int(stable_extra.size()));
  1946. string_table_ends = ofs + size;
  1947. //print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));
  1948. } break;
  1949. case CHUNK_XML_START_TAG: {
  1950. int iofs = ofs + 8;
  1951. uint32_t line = decode_uint32(&p_manifest[iofs]);
  1952. uint32_t nspace = decode_uint32(&p_manifest[iofs + 8]);
  1953. uint32_t name = decode_uint32(&p_manifest[iofs + 12]);
  1954. uint32_t check = decode_uint32(&p_manifest[iofs + 16]);
  1955. String tname = string_table[name];
  1956. //printf("NSPACE: %i\n",nspace);
  1957. //printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
  1958. //printf("CHECK: %x\n",check);
  1959. uint32_t attrcount = decode_uint32(&p_manifest[iofs + 20]);
  1960. iofs += 28;
  1961. //printf("ATTRCOUNT: %x\n",attrcount);
  1962. for (uint32_t i = 0; i < attrcount; i++) {
  1963. uint32_t attr_nspace = decode_uint32(&p_manifest[iofs]);
  1964. uint32_t attr_name = decode_uint32(&p_manifest[iofs + 4]);
  1965. uint32_t attr_value = decode_uint32(&p_manifest[iofs + 8]);
  1966. uint32_t attr_flags = decode_uint32(&p_manifest[iofs + 12]);
  1967. uint32_t attr_resid = decode_uint32(&p_manifest[iofs + 16]);
  1968. String value;
  1969. if (attr_value != 0xFFFFFFFF)
  1970. value = string_table[attr_value];
  1971. else
  1972. value = "Res #" + itos(attr_resid);
  1973. String attrname = string_table[attr_name];
  1974. String nspace;
  1975. if (attr_nspace != 0xFFFFFFFF)
  1976. nspace = string_table[attr_nspace];
  1977. else
  1978. nspace = "";
  1979. //printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
  1980. //printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
  1981. //printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
  1982. //printf("ATTR %i FLAGS: %x\n",i,attr_flags);
  1983. //printf("ATTR %i RESID: %x\n",i,attr_resid);
  1984. //replace project information
  1985. if (tname == "manifest" && attrname == "package") {
  1986. print_line("FOUND package");
  1987. string_table[attr_value] = get_package_name(package_name);
  1988. }
  1989. //print_line("tname: "+tname);
  1990. //print_line("nspace: "+nspace);
  1991. //print_line("attrname: "+attrname);
  1992. if (tname == "manifest" && /*nspace=="android" &&*/ attrname == "versionCode") {
  1993. print_line("FOUND versionCode");
  1994. encode_uint32(version_code, &p_manifest[iofs + 16]);
  1995. }
  1996. if (tname == "manifest" && /*nspace=="android" &&*/ attrname == "versionName") {
  1997. print_line("FOUND versionName");
  1998. if (attr_value == 0xFFFFFFFF) {
  1999. WARN_PRINT("Version name in a resource, should be plaintext")
  2000. } else
  2001. string_table[attr_value] = version_name;
  2002. }
  2003. if (tname == "activity" && /*nspace=="android" &&*/ attrname == "screenOrientation") {
  2004. encode_uint32(orientation == 0 ? 0 : 1, &p_manifest[iofs + 16]);
  2005. /*
  2006. print_line("FOUND screen orientation");
  2007. if (attr_value==0xFFFFFFFF) {
  2008. WARN_PRINT("Version name in a resource, should be plaintext")
  2009. } else {
  2010. string_table[attr_value]=(orientation==0?"landscape":"portrait");
  2011. }*/
  2012. }
  2013. if (tname == "uses-feature" && /*nspace=="android" &&*/ attrname == "glEsVersion") {
  2014. print_line("version number: " + itos(decode_uint32(&p_manifest[iofs + 16])));
  2015. }
  2016. if (tname == "uses-permission" && /*nspace=="android" &&*/ attrname == "name") {
  2017. if (value.begins_with("godot.custom")) {
  2018. int which = value.get_slice(".", 2).to_int();
  2019. if (which >= 0 && which < MAX_USER_PERMISSIONS && user_perms[which].strip_edges() != "") {
  2020. string_table[attr_value] = user_perms[which].strip_edges();
  2021. }
  2022. } else if (value.begins_with("godot.")) {
  2023. String perm = value.get_slice(".", 1);
  2024. if (perms.has(perm) || (p_give_internet && perm == "INTERNET")) {
  2025. print_line("PERM: " + perm);
  2026. string_table[attr_value] = "android.permission." + perm;
  2027. }
  2028. }
  2029. }
  2030. if (tname == "supports-screens") {
  2031. if (attrname == "smallScreens") {
  2032. encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  2033. } else if (attrname == "normalScreens") {
  2034. encode_uint32(screen_support_normal ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  2035. } else if (attrname == "largeScreens") {
  2036. encode_uint32(screen_support_large ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  2037. } else if (attrname == "xlargeScreens") {
  2038. encode_uint32(screen_support_xlarge ? 0xFFFFFFFF : 0, &p_manifest[iofs + 16]);
  2039. }
  2040. }
  2041. iofs += 20;
  2042. }
  2043. } break;
  2044. }
  2045. //printf("chunk %x: size: %d\n",chunk,size);
  2046. ofs += size;
  2047. }
  2048. //printf("end\n");
  2049. //create new andriodmanifest binary
  2050. Vector<uint8_t> ret;
  2051. ret.resize(string_table_begins + string_table.size() * 4);
  2052. for (uint32_t i = 0; i < string_table_begins; i++) {
  2053. ret[i] = p_manifest[i];
  2054. }
  2055. ofs = 0;
  2056. for (int i = 0; i < string_table.size(); i++) {
  2057. encode_uint32(ofs, &ret[string_table_begins + i * 4]);
  2058. ofs += string_table[i].length() * 2 + 2 + 2;
  2059. //print_line("ofs: "+itos(i)+": "+itos(ofs));
  2060. }
  2061. ret.resize(ret.size() + ofs);
  2062. uint8_t *chars = &ret[ret.size() - ofs];
  2063. for (int i = 0; i < string_table.size(); i++) {
  2064. String s = string_table[i];
  2065. //print_line("savint string :"+s);
  2066. encode_uint16(s.length(), chars);
  2067. chars += 2;
  2068. for (int j = 0; j < s.length(); j++) { //include zero?
  2069. encode_uint16(s[j], chars);
  2070. chars += 2;
  2071. }
  2072. encode_uint16(0, chars);
  2073. chars += 2;
  2074. }
  2075. for (int i = 0; i < stable_extra.size(); i++) {
  2076. ret.push_back(stable_extra[i]);
  2077. }
  2078. while (ret.size() % 4)
  2079. ret.push_back(0);
  2080. uint32_t new_stable_end = ret.size();
  2081. uint32_t extra = (p_manifest.size() - string_table_ends);
  2082. ret.resize(new_stable_end + extra);
  2083. for (uint32_t i = 0; i < extra; i++)
  2084. ret[new_stable_end + i] = p_manifest[string_table_ends + i];
  2085. while (ret.size() % 4)
  2086. ret.push_back(0);
  2087. encode_uint32(ret.size(), &ret[4]); //update new file size
  2088. encode_uint32(new_stable_end - 8, &ret[12]); //update new string table size
  2089. //print_line("file size: "+itos(ret.size()));
  2090. p_manifest = ret;
  2091. #if 0
  2092. uint32_t header[9];
  2093. for(int i=0;i<9;i++) {
  2094. header[i]=decode_uint32(&p_manifest[i*4]);
  2095. }
  2096. //print_line("STO: "+itos(header[3]));
  2097. uint32_t st_offset=9*4;
  2098. //ERR_FAIL_COND(header[3]!=0x24)
  2099. uint32_t string_count=header[4];
  2100. string_table.resize(string_count);
  2101. for(int i=0;i<string_count;i++) {
  2102. uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
  2103. string_at+=st_offset+string_count*4;
  2104. uint32_t len = decode_uint16(&p_manifest[string_at]);
  2105. Vector<CharType> ucstring;
  2106. ucstring.resize(len+1);
  2107. for(int j=0;j<len;j++) {
  2108. uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
  2109. ucstring[j]=c;
  2110. }
  2111. ucstring[len]=0;
  2112. string_table[i]=ucstring.ptr();
  2113. }
  2114. #endif
  2115. }
  2116. static String _parse_string(const uint8_t *p_bytes, bool p_utf8) {
  2117. uint32_t offset = 0;
  2118. uint32_t len = decode_uint16(&p_bytes[offset]);
  2119. if (p_utf8) {
  2120. //don't know how to read extended utf8, this will have to be for now
  2121. len >>= 8;
  2122. }
  2123. offset += 2;
  2124. //printf("len %i, unicode: %i\n",len,int(p_utf8));
  2125. if (p_utf8) {
  2126. Vector<uint8_t> str8;
  2127. str8.resize(len + 1);
  2128. for (uint32_t i = 0; i < len; i++) {
  2129. str8[i] = p_bytes[offset + i];
  2130. }
  2131. str8[len] = 0;
  2132. String str;
  2133. str.parse_utf8((const char *)str8.ptr());
  2134. return str;
  2135. } else {
  2136. String str;
  2137. for (uint32_t i = 0; i < len; i++) {
  2138. CharType c = decode_uint16(&p_bytes[offset + i * 2]);
  2139. if (c == 0)
  2140. break;
  2141. str += String::chr(c);
  2142. }
  2143. return str;
  2144. }
  2145. }
  2146. void _fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest) {
  2147. const int UTF8_FLAG = 0x00000100;
  2148. print_line("*******************GORRRGLE***********************");
  2149. uint32_t header = decode_uint32(&p_manifest[0]);
  2150. uint32_t filesize = decode_uint32(&p_manifest[4]);
  2151. uint32_t string_block_len = decode_uint32(&p_manifest[16]);
  2152. uint32_t string_count = decode_uint32(&p_manifest[20]);
  2153. uint32_t string_flags = decode_uint32(&p_manifest[28]);
  2154. const uint32_t string_table_begins = 40;
  2155. Vector<String> string_table;
  2156. String package_name = p_preset->get("package/name");
  2157. //printf("stirng block len: %i\n",string_block_len);
  2158. //printf("stirng count: %i\n",string_count);
  2159. //printf("flags: %x\n",string_flags);
  2160. for (uint32_t i = 0; i < string_count; i++) {
  2161. uint32_t offset = decode_uint32(&p_manifest[string_table_begins + i * 4]);
  2162. offset += string_table_begins + string_count * 4;
  2163. String str = _parse_string(&p_manifest[offset], string_flags & UTF8_FLAG);
  2164. if (str.begins_with("godot-project-name")) {
  2165. if (str == "godot-project-name") {
  2166. //project name
  2167. str = get_project_name(package_name);
  2168. } else {
  2169. String lang = str.substr(str.find_last("-") + 1, str.length()).replace("-", "_");
  2170. String prop = "application/config/name_" + lang;
  2171. if (ProjectSettings::get_singleton()->has(prop)) {
  2172. str = ProjectSettings::get_singleton()->get(prop);
  2173. } else {
  2174. str = get_project_name(package_name);
  2175. }
  2176. }
  2177. }
  2178. string_table.push_back(str);
  2179. }
  2180. //write a new string table, but use 16 bits
  2181. Vector<uint8_t> ret;
  2182. ret.resize(string_table_begins + string_table.size() * 4);
  2183. for (uint32_t i = 0; i < string_table_begins; i++) {
  2184. ret[i] = p_manifest[i];
  2185. }
  2186. int ofs = 0;
  2187. for (int i = 0; i < string_table.size(); i++) {
  2188. encode_uint32(ofs, &ret[string_table_begins + i * 4]);
  2189. ofs += string_table[i].length() * 2 + 2 + 2;
  2190. }
  2191. ret.resize(ret.size() + ofs);
  2192. uint8_t *chars = &ret[ret.size() - ofs];
  2193. for (int i = 0; i < string_table.size(); i++) {
  2194. String s = string_table[i];
  2195. encode_uint16(s.length(), chars);
  2196. chars += 2;
  2197. for (int j = 0; j < s.length(); j++) {
  2198. encode_uint16(s[j], chars);
  2199. chars += 2;
  2200. }
  2201. encode_uint16(0, chars);
  2202. chars += 2;
  2203. }
  2204. //pad
  2205. while (ret.size() % 4)
  2206. ret.push_back(0);
  2207. //change flags to not use utf8
  2208. encode_uint32(string_flags & ~0x100, &ret[28]);
  2209. //change length
  2210. encode_uint32(ret.size() - 12, &ret[16]);
  2211. //append the rest...
  2212. int rest_from = 12 + string_block_len;
  2213. int rest_to = ret.size();
  2214. int rest_len = (p_manifest.size() - rest_from);
  2215. ret.resize(ret.size() + (p_manifest.size() - rest_from));
  2216. for (int i = 0; i < rest_len; i++) {
  2217. ret[rest_to + i] = p_manifest[rest_from + i];
  2218. }
  2219. //finally update the size
  2220. encode_uint32(ret.size(), &ret[4]);
  2221. p_manifest = ret;
  2222. //printf("end\n");
  2223. }
  2224. public:
  2225. enum {
  2226. MAX_USER_PERMISSIONS = 20
  2227. };
  2228. typedef Error (*EditorExportSaveFunction)(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total);
  2229. public:
  2230. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  2231. r_features->push_back("etc");
  2232. }
  2233. virtual void get_export_options(List<ExportOption> *r_options) {
  2234. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "graphics/api", PROPERTY_HINT_ENUM, "OpenGL ES 2.0,OpenGL ES 3.0"), 1));
  2235. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "graphics/32_bits_framebuffer"), true));
  2236. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "one_click_deploy/clear_previous_install"), true));
  2237. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE, "apk"), ""));
  2238. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE, "apk"), ""));
  2239. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));
  2240. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/code", PROPERTY_HINT_RANGE, "1,65535,1"), 1));
  2241. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "version/name"), "1.0"));
  2242. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name"), "org.godotengine.$genname"));
  2243. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/name"), ""));
  2244. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/icon", PROPERTY_HINT_FILE, "png"), ""));
  2245. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/signed"), true));
  2246. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architecture/arm"), true));
  2247. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "architecture/x86"), false));
  2248. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/immersive_mode"), true));
  2249. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "screen/orientation", PROPERTY_HINT_ENUM, "Landscape,Portrait"), 0));
  2250. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_small"), true));
  2251. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_normal"), true));
  2252. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_large"), true));
  2253. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_xlarge"), true));
  2254. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release", PROPERTY_HINT_GLOBAL_FILE, "keystore"), ""));
  2255. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_user"), ""));
  2256. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_password"), ""));
  2257. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "apk_expansion/enable"), false));
  2258. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/SALT"), ""));
  2259. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/public_key", PROPERTY_HINT_MULTILINE_TEXT), ""));
  2260. const char **perms = android_perms;
  2261. while (*perms) {
  2262. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "permissions/" + String(*perms).to_lower()), false));
  2263. perms++;
  2264. }
  2265. for (int i = 0; i < MAX_USER_PERMISSIONS; i++) {
  2266. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "user_permissions/" + itos(i)), false));
  2267. }
  2268. //r_options->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)"));
  2269. }
  2270. virtual String get_name() const {
  2271. return "Android";
  2272. }
  2273. virtual String get_os_name() const {
  2274. return "Android";
  2275. }
  2276. virtual Ref<Texture> get_logo() const {
  2277. return logo;
  2278. }
  2279. virtual bool poll_devices() {
  2280. bool dc = devices_changed;
  2281. devices_changed = false;
  2282. return dc;
  2283. }
  2284. virtual int get_device_count() const {
  2285. device_lock->lock();
  2286. int dc = devices.size();
  2287. device_lock->unlock();
  2288. return dc;
  2289. }
  2290. virtual String get_device_name(int p_device) const {
  2291. ERR_FAIL_INDEX_V(p_device, devices.size(), "");
  2292. device_lock->lock();
  2293. String s = devices[p_device].name;
  2294. device_lock->unlock();
  2295. return s;
  2296. }
  2297. virtual String get_device_info(int p_device) const {
  2298. ERR_FAIL_INDEX_V(p_device, devices.size(), "");
  2299. device_lock->lock();
  2300. String s = devices[p_device].description;
  2301. device_lock->unlock();
  2302. return s;
  2303. }
  2304. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  2305. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  2306. device_lock->lock();
  2307. EditorProgress ep("run", "Running on " + devices[p_device].name, 3);
  2308. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  2309. if (adb == "") {
  2310. EditorNode::add_io_error("ADB executable not configured in settings, can't run.");
  2311. device_lock->unlock();
  2312. return ERR_UNCONFIGURED;
  2313. }
  2314. //export_temp
  2315. ep.step("Exporting APK", 0);
  2316. bool use_adb_over_usb = bool(EDITOR_DEF("export/android/use_remote_debug_over_adb", true));
  2317. if (use_adb_over_usb) {
  2318. p_debug_flags |= DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST;
  2319. }
  2320. String export_to = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpexport.apk";
  2321. Error err = export_project(p_preset, true, export_to, p_debug_flags);
  2322. if (err) {
  2323. device_lock->unlock();
  2324. return err;
  2325. }
  2326. List<String> args;
  2327. int rv;
  2328. bool remove_prev = p_preset->get("one_click_deploy/clear_previous_install");
  2329. String version_name = p_preset->get("version/name");
  2330. String package_name = p_preset->get("package/unique_name");
  2331. if (remove_prev) {
  2332. ep.step("Uninstalling..", 1);
  2333. print_line("Uninstalling previous version: " + devices[p_device].name);
  2334. args.push_back("-s");
  2335. args.push_back(devices[p_device].id);
  2336. args.push_back("uninstall");
  2337. args.push_back(get_package_name(package_name));
  2338. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  2339. #if 0
  2340. if (err || rv!=0) {
  2341. EditorNode::add_io_error("Could not install to device.");
  2342. device_lock->unlock();
  2343. return ERR_CANT_CREATE;
  2344. }
  2345. #endif
  2346. }
  2347. print_line("Installing into device (please wait..): " + devices[p_device].name);
  2348. ep.step("Installing to Device (please wait..)..", 2);
  2349. args.clear();
  2350. args.push_back("-s");
  2351. args.push_back(devices[p_device].id);
  2352. args.push_back("install");
  2353. args.push_back("-r");
  2354. args.push_back(export_to);
  2355. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  2356. if (err || rv != 0) {
  2357. EditorNode::add_io_error("Could not install to device.");
  2358. device_lock->unlock();
  2359. return ERR_CANT_CREATE;
  2360. }
  2361. if (use_adb_over_usb) {
  2362. args.clear();
  2363. args.push_back("reverse");
  2364. args.push_back("--remove-all");
  2365. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  2366. int port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  2367. args.clear();
  2368. args.push_back("reverse");
  2369. args.push_back("tcp:" + itos(port));
  2370. args.push_back("tcp:" + itos(port));
  2371. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  2372. print_line("Reverse result: " + itos(rv));
  2373. int fs_port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  2374. args.clear();
  2375. args.push_back("reverse");
  2376. args.push_back("tcp:" + itos(fs_port));
  2377. args.push_back("tcp:" + itos(fs_port));
  2378. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  2379. print_line("Reverse result2: " + itos(rv));
  2380. }
  2381. ep.step("Running on Device..", 3);
  2382. args.clear();
  2383. args.push_back("-s");
  2384. args.push_back(devices[p_device].id);
  2385. args.push_back("shell");
  2386. args.push_back("am");
  2387. args.push_back("start");
  2388. args.push_back("--user 0");
  2389. args.push_back("-a");
  2390. args.push_back("android.intent.action.MAIN");
  2391. args.push_back("-n");
  2392. args.push_back(get_package_name(package_name) + "/org.godotengine.godot.Godot");
  2393. err = OS::get_singleton()->execute(adb, args, true, NULL, NULL, &rv);
  2394. if (err || rv != 0) {
  2395. EditorNode::add_io_error("Could not execute ondevice.");
  2396. device_lock->unlock();
  2397. return ERR_CANT_CREATE;
  2398. }
  2399. device_lock->unlock();
  2400. return OK;
  2401. }
  2402. virtual Ref<Texture> get_run_icon() const {
  2403. return run_icon;
  2404. }
  2405. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  2406. r_missing_templates = find_export_template("android_debug.apk") == String() || find_export_template("android_release.apk") == String();
  2407. bool valid = !r_missing_templates;
  2408. String adb = EditorSettings::get_singleton()->get("export/android/adb");
  2409. String err;
  2410. if (!FileAccess::exists(adb)) {
  2411. valid = false;
  2412. err += "ADB executable not configured in editor settings.\n";
  2413. }
  2414. String js = EditorSettings::get_singleton()->get("export/android/jarsigner");
  2415. if (!FileAccess::exists(js)) {
  2416. valid = false;
  2417. err += "OpenJDK 6 jarsigner not configured in editor settings.\n";
  2418. }
  2419. String dk = EditorSettings::get_singleton()->get("export/android/debug_keystore");
  2420. if (!FileAccess::exists(dk)) {
  2421. valid = false;
  2422. err += "Debug Keystore not configured in editor settings.\n";
  2423. }
  2424. bool apk_expansion = p_preset->get("apk_expansion/enable");
  2425. if (apk_expansion) {
  2426. /*
  2427. if (apk_expansion_salt=="") {
  2428. valid=false;
  2429. err+="Invalid SALT for apk expansion.\n";
  2430. }
  2431. */
  2432. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  2433. if (apk_expansion_pkey == "") {
  2434. valid = false;
  2435. err += "Invalid public key for apk expansion.\n";
  2436. }
  2437. }
  2438. r_error = err;
  2439. return valid;
  2440. }
  2441. virtual String get_binary_extension() const {
  2442. return "apk";
  2443. }
  2444. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) {
  2445. String src_apk;
  2446. EditorProgress ep("export", "Exporting for Android", 105);
  2447. if (p_debug)
  2448. src_apk = p_preset->get("custom_package/debug");
  2449. else
  2450. src_apk = p_preset->get("custom_package/release");
  2451. src_apk = src_apk.strip_edges();
  2452. if (src_apk == "") {
  2453. if (p_debug) {
  2454. src_apk = find_export_template("android_debug.apk");
  2455. } else {
  2456. src_apk = find_export_template("android_release.apk");
  2457. }
  2458. if (src_apk == "") {
  2459. EditorNode::add_io_error("Package not found: " + src_apk);
  2460. return ERR_FILE_NOT_FOUND;
  2461. }
  2462. }
  2463. FileAccess *src_f = NULL;
  2464. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  2465. ep.step("Creating APK", 0);
  2466. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  2467. if (!pkg) {
  2468. EditorNode::add_io_error("Could not find template APK to export:\n" + src_apk);
  2469. return ERR_FILE_NOT_FOUND;
  2470. }
  2471. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  2472. int ret = unzGoToFirstFile(pkg);
  2473. zlib_filefunc_def io2 = io;
  2474. FileAccess *dst_f = NULL;
  2475. io2.opaque = &dst_f;
  2476. String unaligned_path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpexport-unaligned.apk";
  2477. zipFile unaligned_apk = zipOpen2(unaligned_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  2478. bool export_x86 = p_preset->get("architecture/x86");
  2479. bool export_arm = p_preset->get("architecture/arm");
  2480. bool use_32_fb = p_preset->get("screen/use_32_bits_view");
  2481. bool immersive = p_preset->get("screen/immersive_mode");
  2482. bool _signed = p_preset->get("package/signed");
  2483. bool apk_expansion = p_preset->get("apk_expansion/enable");
  2484. String cmdline = p_preset->get("command_line/extra_args");
  2485. int version_code = p_preset->get("version/code");
  2486. String version_name = p_preset->get("version/name");
  2487. String package_name = p_preset->get("package/unique_name");
  2488. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  2489. String release_keystore = p_preset->get("keystore/release");
  2490. String release_username = p_preset->get("keystore/release_user");
  2491. String release_password = p_preset->get("keystore/release_password");
  2492. while (ret == UNZ_OK) {
  2493. //get filename
  2494. unz_file_info info;
  2495. char fname[16384];
  2496. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  2497. bool skip = false;
  2498. String file = fname;
  2499. Vector<uint8_t> data;
  2500. data.resize(info.uncompressed_size);
  2501. //read
  2502. unzOpenCurrentFile(pkg);
  2503. unzReadCurrentFile(pkg, data.ptr(), data.size());
  2504. unzCloseCurrentFile(pkg);
  2505. //write
  2506. if (file == "AndroidManifest.xml") {
  2507. _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG));
  2508. }
  2509. if (file == "resources.arsc") {
  2510. _fix_resources(p_preset, data);
  2511. }
  2512. if (file == "res/drawable/icon.png") {
  2513. String icon = p_preset->get("package/icon");
  2514. icon = icon.strip_edges();
  2515. bool found = false;
  2516. if (icon != "" && icon.ends_with(".png")) {
  2517. FileAccess *f = FileAccess::open(icon, FileAccess::READ);
  2518. if (f) {
  2519. data.resize(f->get_len());
  2520. f->get_buffer(data.ptr(), data.size());
  2521. memdelete(f);
  2522. found = true;
  2523. }
  2524. }
  2525. if (!found) {
  2526. String appicon = ProjectSettings::get_singleton()->get("application/config/icon");
  2527. if (appicon != "" && appicon.ends_with(".png")) {
  2528. FileAccess *f = FileAccess::open(appicon, FileAccess::READ);
  2529. if (f) {
  2530. data.resize(f->get_len());
  2531. f->get_buffer(data.ptr(), data.size());
  2532. memdelete(f);
  2533. }
  2534. }
  2535. }
  2536. }
  2537. if (file == "lib/x86/libgodot_android.so" && !export_x86) {
  2538. skip = true;
  2539. }
  2540. if (file.match("lib/armeabi*/libgodot_android.so") && !export_arm) {
  2541. skip = true;
  2542. }
  2543. if (file.begins_with("META-INF") && _signed) {
  2544. skip = true;
  2545. }
  2546. print_line("ADDING: " + file);
  2547. if (!skip) {
  2548. // Respect decision on compression made by AAPT for the export template
  2549. const bool uncompressed = info.compression_method == 0;
  2550. zip_fileinfo zipfi = get_zip_fileinfo();
  2551. zipOpenNewFileInZip(unaligned_apk,
  2552. file.utf8().get_data(),
  2553. &zipfi,
  2554. NULL,
  2555. 0,
  2556. NULL,
  2557. 0,
  2558. NULL,
  2559. uncompressed ? 0 : Z_DEFLATED,
  2560. Z_DEFAULT_COMPRESSION);
  2561. zipWriteInFileInZip(unaligned_apk, data.ptr(), data.size());
  2562. zipCloseFileInZip(unaligned_apk);
  2563. }
  2564. ret = unzGoToNextFile(pkg);
  2565. }
  2566. ep.step("Adding Files..", 1);
  2567. Error err = OK;
  2568. Vector<String> cl = cmdline.strip_edges().split(" ");
  2569. for (int i = 0; i < cl.size(); i++) {
  2570. if (cl[i].strip_edges().length() == 0) {
  2571. cl.remove(i);
  2572. i--;
  2573. }
  2574. }
  2575. gen_export_flags(cl, p_flags);
  2576. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  2577. /*String host = EditorSettings::get_singleton()->get("filesystem/file_server/host");
  2578. int port = EditorSettings::get_singleton()->get("filesystem/file_server/post");
  2579. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  2580. cl.push_back("-rfs");
  2581. cl.push_back(host+":"+itos(port));
  2582. if (passwd!="") {
  2583. cl.push_back("-rfs_pass");
  2584. cl.push_back(passwd);
  2585. }*/
  2586. } else {
  2587. //all files
  2588. if (apk_expansion) {
  2589. String apkfname = "main." + itos(version_code) + "." + get_package_name(package_name) + ".obb";
  2590. String fullpath = p_path.get_base_dir().plus_file(apkfname);
  2591. err = save_pack(p_preset, fullpath);
  2592. if (err != OK) {
  2593. EditorNode::add_io_error("Could not write expansion package file: " + apkfname);
  2594. return OK;
  2595. }
  2596. cl.push_back("-use_apk_expansion");
  2597. cl.push_back("-apk_expansion_md5");
  2598. cl.push_back(FileAccess::get_md5(fullpath));
  2599. cl.push_back("-apk_expansion_key");
  2600. cl.push_back(apk_expansion_pkey.strip_edges());
  2601. } else {
  2602. APKExportData ed;
  2603. ed.ep = &ep;
  2604. ed.apk = unaligned_apk;
  2605. err = export_project_files(p_preset, save_apk_file, &ed);
  2606. }
  2607. }
  2608. if (use_32_fb)
  2609. cl.push_back("-use_depth_32");
  2610. if (immersive)
  2611. cl.push_back("-use_immersive");
  2612. if (cl.size()) {
  2613. //add comandline
  2614. Vector<uint8_t> clf;
  2615. clf.resize(4);
  2616. encode_uint32(cl.size(), &clf[0]);
  2617. for (int i = 0; i < cl.size(); i++) {
  2618. CharString txt = cl[i].utf8();
  2619. int base = clf.size();
  2620. clf.resize(base + 4 + txt.length());
  2621. encode_uint32(txt.length(), &clf[base]);
  2622. copymem(&clf[base + 4], txt.ptr(), txt.length());
  2623. print_line(itos(i) + " param: " + cl[i]);
  2624. }
  2625. zip_fileinfo zipfi = get_zip_fileinfo();
  2626. zipOpenNewFileInZip(unaligned_apk,
  2627. "assets/_cl_",
  2628. &zipfi,
  2629. NULL,
  2630. 0,
  2631. NULL,
  2632. 0,
  2633. NULL,
  2634. 0, // No compress (little size gain and potentially slower startup)
  2635. Z_DEFAULT_COMPRESSION);
  2636. zipWriteInFileInZip(unaligned_apk, clf.ptr(), clf.size());
  2637. zipCloseFileInZip(unaligned_apk);
  2638. }
  2639. zipClose(unaligned_apk, NULL);
  2640. unzClose(pkg);
  2641. if (err) {
  2642. return err;
  2643. }
  2644. if (_signed) {
  2645. String jarsigner = EditorSettings::get_singleton()->get("export/android/jarsigner");
  2646. if (!FileAccess::exists(jarsigner)) {
  2647. EditorNode::add_io_error("'jarsigner' could not be found.\nPlease supply a path in the editor settings.\nResulting apk is unsigned.");
  2648. return OK;
  2649. }
  2650. String keystore;
  2651. String password;
  2652. String user;
  2653. if (p_debug) {
  2654. keystore = EditorSettings::get_singleton()->get("export/android/debug_keystore");
  2655. password = EditorSettings::get_singleton()->get("export/android/debug_keystore_pass");
  2656. user = EditorSettings::get_singleton()->get("export/android/debug_keystore_user");
  2657. ep.step("Signing Debug APK..", 103);
  2658. } else {
  2659. keystore = release_keystore;
  2660. password = release_password;
  2661. user = release_username;
  2662. ep.step("Signing Release APK..", 103);
  2663. }
  2664. if (!FileAccess::exists(keystore)) {
  2665. EditorNode::add_io_error("Could not find keystore, unable to export.");
  2666. return ERR_FILE_CANT_OPEN;
  2667. }
  2668. List<String> args;
  2669. args.push_back("-digestalg");
  2670. args.push_back("SHA1");
  2671. args.push_back("-sigalg");
  2672. args.push_back("MD5withRSA");
  2673. String tsa_url = EditorSettings::get_singleton()->get("export/android/timestamping_authority_url");
  2674. if (tsa_url != "") {
  2675. args.push_back("-tsa");
  2676. args.push_back(tsa_url);
  2677. }
  2678. args.push_back("-verbose");
  2679. args.push_back("-keystore");
  2680. args.push_back(keystore);
  2681. args.push_back("-storepass");
  2682. args.push_back(password);
  2683. args.push_back(unaligned_path);
  2684. args.push_back(user);
  2685. int retval;
  2686. OS::get_singleton()->execute(jarsigner, args, true, NULL, NULL, &retval);
  2687. if (retval) {
  2688. EditorNode::add_io_error("'jarsigner' returned with error #" + itos(retval));
  2689. return ERR_CANT_CREATE;
  2690. }
  2691. ep.step("Verifying APK..", 104);
  2692. args.clear();
  2693. args.push_back("-verify");
  2694. args.push_back("-keystore");
  2695. args.push_back(keystore);
  2696. args.push_back(unaligned_path);
  2697. args.push_back("-verbose");
  2698. OS::get_singleton()->execute(jarsigner, args, true, NULL, NULL, &retval);
  2699. if (retval) {
  2700. EditorNode::add_io_error("'jarsigner' verification of APK failed. Make sure to use jarsigner from Java 6.");
  2701. return ERR_CANT_CREATE;
  2702. }
  2703. }
  2704. // Let's zip-align (must be done after signing)
  2705. static const int ZIP_ALIGNMENT = 4;
  2706. ep.step("Aligning APK..", 105);
  2707. unzFile tmp_unaligned = unzOpen2(unaligned_path.utf8().get_data(), &io);
  2708. if (!tmp_unaligned) {
  2709. EditorNode::add_io_error("Could not find temp unaligned APK.");
  2710. return ERR_FILE_NOT_FOUND;
  2711. }
  2712. ERR_FAIL_COND_V(!tmp_unaligned, ERR_CANT_OPEN);
  2713. ret = unzGoToFirstFile(tmp_unaligned);
  2714. io2 = io;
  2715. dst_f = NULL;
  2716. io2.opaque = &dst_f;
  2717. zipFile final_apk = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io2);
  2718. // Take files from the unaligned APK and write them out to the aligned one
  2719. // in raw mode, i.e. not uncompressing and recompressing, aligning them as needed,
  2720. // following what is done in https://github.com/android/platform_build/blob/master/tools/zipalign/ZipAlign.cpp
  2721. int bias = 0;
  2722. while (ret == UNZ_OK) {
  2723. unz_file_info info;
  2724. memset(&info, 0, sizeof(info));
  2725. char fname[16384];
  2726. char extra[16384];
  2727. ret = unzGetCurrentFileInfo(tmp_unaligned, &info, fname, 16384, extra, 16384 - ZIP_ALIGNMENT, NULL, 0);
  2728. String file = fname;
  2729. Vector<uint8_t> data;
  2730. data.resize(info.compressed_size);
  2731. // read
  2732. int method, level;
  2733. unzOpenCurrentFile2(tmp_unaligned, &method, &level, 1); // raw read
  2734. long file_offset = unzGetCurrentFileZStreamPos64(tmp_unaligned);
  2735. unzReadCurrentFile(tmp_unaligned, data.ptr(), data.size());
  2736. unzCloseCurrentFile(tmp_unaligned);
  2737. // align
  2738. int padding = 0;
  2739. if (!info.compression_method) {
  2740. // Uncompressed file => Align
  2741. long new_offset = file_offset + bias;
  2742. padding = (ZIP_ALIGNMENT - (new_offset % ZIP_ALIGNMENT)) % ZIP_ALIGNMENT;
  2743. }
  2744. memset(extra + info.size_file_extra, 0, padding);
  2745. // write
  2746. zip_fileinfo zipfi = get_zip_fileinfo();
  2747. zipOpenNewFileInZip2(final_apk,
  2748. file.utf8().get_data(),
  2749. &zipfi,
  2750. extra,
  2751. info.size_file_extra + padding,
  2752. NULL,
  2753. 0,
  2754. NULL,
  2755. method,
  2756. level,
  2757. 1); // raw write
  2758. zipWriteInFileInZip(final_apk, data.ptr(), data.size());
  2759. zipCloseFileInZipRaw(final_apk, info.uncompressed_size, info.crc);
  2760. bias += padding;
  2761. ret = unzGoToNextFile(tmp_unaligned);
  2762. }
  2763. zipClose(final_apk, NULL);
  2764. unzClose(tmp_unaligned);
  2765. if (err) {
  2766. return err;
  2767. }
  2768. return OK;
  2769. }
  2770. virtual void get_platform_features(List<String> *r_features) {
  2771. r_features->push_back("mobile");
  2772. r_features->push_back("Android");
  2773. }
  2774. EditorExportAndroid() {
  2775. Ref<Image> img = memnew(Image(_android_logo));
  2776. logo.instance();
  2777. logo->create_from_image(img);
  2778. img = Ref<Image>(memnew(Image(_android_run_icon)));
  2779. run_icon.instance();
  2780. run_icon->create_from_image(img);
  2781. device_lock = Mutex::create();
  2782. device_thread = Thread::create(_device_poll_thread, this);
  2783. devices_changed = true;
  2784. quit_request = false;
  2785. }
  2786. ~EditorExportAndroid() {
  2787. quit_request = true;
  2788. Thread::wait_to_finish(device_thread);
  2789. memdelete(device_lock);
  2790. memdelete(device_thread);
  2791. }
  2792. };
  2793. void register_android_exporter() {
  2794. String exe_ext;
  2795. if (OS::get_singleton()->get_name() == "Windows") {
  2796. exe_ext = "*.exe";
  2797. }
  2798. EDITOR_DEF("export/android/adb", "");
  2799. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/adb", PROPERTY_HINT_GLOBAL_FILE, exe_ext));
  2800. EDITOR_DEF("export/android/jarsigner", "");
  2801. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/jarsigner", PROPERTY_HINT_GLOBAL_FILE, exe_ext));
  2802. EDITOR_DEF("export/android/debug_keystore", "");
  2803. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/debug_keystore", PROPERTY_HINT_GLOBAL_FILE, "keystore"));
  2804. EDITOR_DEF("export/android/debug_keystore_user", "androiddebugkey");
  2805. EDITOR_DEF("export/android/debug_keystore_pass", "android");
  2806. EDITOR_DEF("export/android/timestamping_authority_url", "");
  2807. EDITOR_DEF("export/android/use_remote_debug_over_adb", false);
  2808. EDITOR_DEF("export/android/shutdown_adb_on_exit", true);
  2809. Ref<EditorExportAndroid> exporter = Ref<EditorExportAndroid>(memnew(EditorExportAndroid));
  2810. EditorExport::get_singleton()->add_export_platform(exporter);
  2811. }