export_plugin.cpp 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272
  1. /**************************************************************************/
  2. /* export_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_plugin.h"
  31. #include "logo_svg.gen.h"
  32. #include "run_icon_svg.gen.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/image_loader.h"
  36. #include "core/io/json.h"
  37. #include "core/io/marshalls.h"
  38. #include "core/version.h"
  39. #include "drivers/png/png_driver_common.h"
  40. #include "editor/editor_log.h"
  41. #include "editor/editor_node.h"
  42. #include "editor/export/export_template_manager.h"
  43. #include "editor/file_system/editor_paths.h"
  44. #include "editor/import/resource_importer_texture_settings.h"
  45. #include "editor/settings/editor_settings.h"
  46. #include "editor/themes/editor_scale.h"
  47. #include "main/splash.gen.h"
  48. #include "scene/resources/image_texture.h"
  49. #include "modules/modules_enabled.gen.h" // For mono.
  50. #include "modules/svg/image_loader_svg.h"
  51. #ifdef MODULE_MONO_ENABLED
  52. #include "modules/mono/utils/path_utils.h"
  53. #endif
  54. #ifdef ANDROID_ENABLED
  55. #include "../os_android.h"
  56. #endif
  57. static const char *ANDROID_PERMS[] = {
  58. "ACCESS_CHECKIN_PROPERTIES",
  59. "ACCESS_COARSE_LOCATION",
  60. "ACCESS_FINE_LOCATION",
  61. "ACCESS_LOCATION_EXTRA_COMMANDS",
  62. "ACCESS_MEDIA_LOCATION",
  63. "ACCESS_MOCK_LOCATION",
  64. "ACCESS_NETWORK_STATE",
  65. "ACCESS_SURFACE_FLINGER",
  66. "ACCESS_WIFI_STATE",
  67. "ACCOUNT_MANAGER",
  68. "ADD_VOICEMAIL",
  69. "AUTHENTICATE_ACCOUNTS",
  70. "BATTERY_STATS",
  71. "BIND_ACCESSIBILITY_SERVICE",
  72. "BIND_APPWIDGET",
  73. "BIND_DEVICE_ADMIN",
  74. "BIND_INPUT_METHOD",
  75. "BIND_NFC_SERVICE",
  76. "BIND_NOTIFICATION_LISTENER_SERVICE",
  77. "BIND_PRINT_SERVICE",
  78. "BIND_REMOTEVIEWS",
  79. "BIND_TEXT_SERVICE",
  80. "BIND_VPN_SERVICE",
  81. "BIND_WALLPAPER",
  82. "BLUETOOTH",
  83. "BLUETOOTH_ADMIN",
  84. "BLUETOOTH_PRIVILEGED",
  85. "BRICK",
  86. "BROADCAST_PACKAGE_REMOVED",
  87. "BROADCAST_SMS",
  88. "BROADCAST_STICKY",
  89. "BROADCAST_WAP_PUSH",
  90. "CALL_PHONE",
  91. "CALL_PRIVILEGED",
  92. "CAMERA",
  93. "CAPTURE_AUDIO_OUTPUT",
  94. "CAPTURE_SECURE_VIDEO_OUTPUT",
  95. "CAPTURE_VIDEO_OUTPUT",
  96. "CHANGE_COMPONENT_ENABLED_STATE",
  97. "CHANGE_CONFIGURATION",
  98. "CHANGE_NETWORK_STATE",
  99. "CHANGE_WIFI_MULTICAST_STATE",
  100. "CHANGE_WIFI_STATE",
  101. "CLEAR_APP_CACHE",
  102. "CLEAR_APP_USER_DATA",
  103. "CONTROL_LOCATION_UPDATES",
  104. "DELETE_CACHE_FILES",
  105. "DELETE_PACKAGES",
  106. "DEVICE_POWER",
  107. "DIAGNOSTIC",
  108. "DISABLE_KEYGUARD",
  109. "DUMP",
  110. "EXPAND_STATUS_BAR",
  111. "FACTORY_TEST",
  112. "FLASHLIGHT",
  113. "FORCE_BACK",
  114. "GET_ACCOUNTS",
  115. "GET_PACKAGE_SIZE",
  116. "GET_TASKS",
  117. "GET_TOP_ACTIVITY_INFO",
  118. "GLOBAL_SEARCH",
  119. "HARDWARE_TEST",
  120. "INJECT_EVENTS",
  121. "INSTALL_LOCATION_PROVIDER",
  122. "INSTALL_PACKAGES",
  123. "INSTALL_SHORTCUT",
  124. "INTERNAL_SYSTEM_WINDOW",
  125. "INTERNET",
  126. "KILL_BACKGROUND_PROCESSES",
  127. "LOCATION_HARDWARE",
  128. "MANAGE_ACCOUNTS",
  129. "MANAGE_APP_TOKENS",
  130. "MANAGE_DOCUMENTS",
  131. "MANAGE_EXTERNAL_STORAGE",
  132. "MASTER_CLEAR",
  133. "MEDIA_CONTENT_CONTROL",
  134. "MODIFY_AUDIO_SETTINGS",
  135. "MODIFY_PHONE_STATE",
  136. "MOUNT_FORMAT_FILESYSTEMS",
  137. "MOUNT_UNMOUNT_FILESYSTEMS",
  138. "NFC",
  139. "PERSISTENT_ACTIVITY",
  140. "POST_NOTIFICATIONS",
  141. "PROCESS_OUTGOING_CALLS",
  142. "READ_CALENDAR",
  143. "READ_CALL_LOG",
  144. "READ_CONTACTS",
  145. "READ_EXTERNAL_STORAGE",
  146. "READ_FRAME_BUFFER",
  147. "READ_HISTORY_BOOKMARKS",
  148. "READ_INPUT_STATE",
  149. "READ_LOGS",
  150. "READ_MEDIA_AUDIO",
  151. "READ_MEDIA_IMAGES",
  152. "READ_MEDIA_VIDEO",
  153. "READ_MEDIA_VISUAL_USER_SELECTED",
  154. "READ_PHONE_STATE",
  155. "READ_PROFILE",
  156. "READ_SMS",
  157. "READ_SOCIAL_STREAM",
  158. "READ_SYNC_SETTINGS",
  159. "READ_SYNC_STATS",
  160. "READ_USER_DICTIONARY",
  161. "REBOOT",
  162. "RECEIVE_BOOT_COMPLETED",
  163. "RECEIVE_MMS",
  164. "RECEIVE_SMS",
  165. "RECEIVE_WAP_PUSH",
  166. "RECORD_AUDIO",
  167. "REORDER_TASKS",
  168. "RESTART_PACKAGES",
  169. "SEND_RESPOND_VIA_MESSAGE",
  170. "SEND_SMS",
  171. "SET_ACTIVITY_WATCHER",
  172. "SET_ALARM",
  173. "SET_ALWAYS_FINISH",
  174. "SET_ANIMATION_SCALE",
  175. "SET_DEBUG_APP",
  176. "SET_ORIENTATION",
  177. "SET_POINTER_SPEED",
  178. "SET_PREFERRED_APPLICATIONS",
  179. "SET_PROCESS_LIMIT",
  180. "SET_TIME",
  181. "SET_TIME_ZONE",
  182. "SET_WALLPAPER",
  183. "SET_WALLPAPER_HINTS",
  184. "SIGNAL_PERSISTENT_PROCESSES",
  185. "STATUS_BAR",
  186. "SUBSCRIBED_FEEDS_READ",
  187. "SUBSCRIBED_FEEDS_WRITE",
  188. "SYSTEM_ALERT_WINDOW",
  189. "TRANSMIT_IR",
  190. "UNINSTALL_SHORTCUT",
  191. "UPDATE_DEVICE_STATS",
  192. "USE_CREDENTIALS",
  193. "USE_SIP",
  194. "VIBRATE",
  195. "WAKE_LOCK",
  196. "WRITE_APN_SETTINGS",
  197. "WRITE_CALENDAR",
  198. "WRITE_CALL_LOG",
  199. "WRITE_CONTACTS",
  200. "WRITE_EXTERNAL_STORAGE",
  201. "WRITE_GSERVICES",
  202. "WRITE_HISTORY_BOOKMARKS",
  203. "WRITE_PROFILE",
  204. "WRITE_SECURE_SETTINGS",
  205. "WRITE_SETTINGS",
  206. "WRITE_SMS",
  207. "WRITE_SOCIAL_STREAM",
  208. "WRITE_SYNC_SETTINGS",
  209. "WRITE_USER_DICTIONARY",
  210. nullptr
  211. };
  212. static const char *MISMATCHED_VERSIONS_MESSAGE = "Android build version mismatch:\n| Template installed: %s\n| Requested version: %s\nPlease reinstall Android build template from 'Project' menu.";
  213. static const char *GDEXTENSION_LIBS_PATH = "libs/gdextensionlibs.json";
  214. // This template string must be in sync with the content of 'platform/android/java/lib/res/mipmap-anydpi-v26/icon.xml'.
  215. static const String ICON_XML_TEMPLATE =
  216. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  217. "<adaptive-icon xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
  218. " <background android:drawable=\"@mipmap/icon_background\"/>\n"
  219. " <foreground android:drawable=\"@mipmap/icon_foreground\"/>\n"
  220. "%s" // Placeholder for the optional monochrome tag.
  221. "</adaptive-icon>";
  222. static const String ICON_XML_PATH = "res/mipmap-anydpi-v26/icon.xml";
  223. static const String THEMED_ICON_XML_PATH = "res/mipmap-anydpi-v26/themed_icon.xml";
  224. static const int ICON_DENSITIES_COUNT = 6;
  225. static const char *LAUNCHER_ICON_OPTION = PNAME("launcher_icons/main_192x192");
  226. static const char *LAUNCHER_ADAPTIVE_ICON_FOREGROUND_OPTION = PNAME("launcher_icons/adaptive_foreground_432x432");
  227. static const char *LAUNCHER_ADAPTIVE_ICON_BACKGROUND_OPTION = PNAME("launcher_icons/adaptive_background_432x432");
  228. static const char *LAUNCHER_ADAPTIVE_ICON_MONOCHROME_OPTION = PNAME("launcher_icons/adaptive_monochrome_432x432");
  229. static const LauncherIcon LAUNCHER_ICONS[ICON_DENSITIES_COUNT] = {
  230. { "res/mipmap-xxxhdpi-v4/icon.png", 192 },
  231. { "res/mipmap-xxhdpi-v4/icon.png", 144 },
  232. { "res/mipmap-xhdpi-v4/icon.png", 96 },
  233. { "res/mipmap-hdpi-v4/icon.png", 72 },
  234. { "res/mipmap-mdpi-v4/icon.png", 48 },
  235. { "res/mipmap/icon.png", 192 }
  236. };
  237. static const LauncherIcon LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[ICON_DENSITIES_COUNT] = {
  238. { "res/mipmap-xxxhdpi-v4/icon_foreground.png", 432 },
  239. { "res/mipmap-xxhdpi-v4/icon_foreground.png", 324 },
  240. { "res/mipmap-xhdpi-v4/icon_foreground.png", 216 },
  241. { "res/mipmap-hdpi-v4/icon_foreground.png", 162 },
  242. { "res/mipmap-mdpi-v4/icon_foreground.png", 108 },
  243. { "res/mipmap/icon_foreground.png", 432 }
  244. };
  245. static const LauncherIcon LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[ICON_DENSITIES_COUNT] = {
  246. { "res/mipmap-xxxhdpi-v4/icon_background.png", 432 },
  247. { "res/mipmap-xxhdpi-v4/icon_background.png", 324 },
  248. { "res/mipmap-xhdpi-v4/icon_background.png", 216 },
  249. { "res/mipmap-hdpi-v4/icon_background.png", 162 },
  250. { "res/mipmap-mdpi-v4/icon_background.png", 108 },
  251. { "res/mipmap/icon_background.png", 432 }
  252. };
  253. static const LauncherIcon LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[ICON_DENSITIES_COUNT] = {
  254. { "res/mipmap-xxxhdpi-v4/icon_monochrome.png", 432 },
  255. { "res/mipmap-xxhdpi-v4/icon_monochrome.png", 324 },
  256. { "res/mipmap-xhdpi-v4/icon_monochrome.png", 216 },
  257. { "res/mipmap-hdpi-v4/icon_monochrome.png", 162 },
  258. { "res/mipmap-mdpi-v4/icon_monochrome.png", 108 },
  259. { "res/mipmap/icon_monochrome.png", 432 }
  260. };
  261. static const int EXPORT_FORMAT_APK = 0;
  262. static const int EXPORT_FORMAT_AAB = 1;
  263. static const char *APK_ASSETS_DIRECTORY = "assets";
  264. static const char *AAB_ASSETS_DIRECTORY = "assetPackInstallTime/src/main/assets";
  265. static const int DEFAULT_MIN_SDK_VERSION = 24; // Should match the value in 'platform/android/java/app/config.gradle#minSdk'
  266. static const int DEFAULT_TARGET_SDK_VERSION = 35; // Should match the value in 'platform/android/java/app/config.gradle#targetSdk'
  267. #ifndef ANDROID_ENABLED
  268. void EditorExportPlatformAndroid::_check_for_changes_poll_thread(void *ud) {
  269. EditorExportPlatformAndroid *ea = static_cast<EditorExportPlatformAndroid *>(ud);
  270. while (!ea->quit_request.is_set()) {
  271. #ifndef DISABLE_DEPRECATED
  272. // Check for android plugins updates
  273. {
  274. // Nothing to do if we already know the plugins have changed.
  275. if (!ea->android_plugins_changed.is_set()) {
  276. Vector<PluginConfigAndroid> loaded_plugins = get_plugins();
  277. MutexLock lock(ea->android_plugins_lock);
  278. if (ea->android_plugins.size() != loaded_plugins.size()) {
  279. ea->android_plugins_changed.set();
  280. } else {
  281. for (int i = 0; i < ea->android_plugins.size(); i++) {
  282. if (ea->android_plugins[i].name != loaded_plugins[i].name) {
  283. ea->android_plugins_changed.set();
  284. break;
  285. }
  286. }
  287. }
  288. if (ea->android_plugins_changed.is_set()) {
  289. ea->android_plugins = loaded_plugins;
  290. }
  291. }
  292. }
  293. #endif // DISABLE_DEPRECATED
  294. // Check for devices updates
  295. String adb = get_adb_path();
  296. // adb.exe was locking the editor_doc_cache file on startup. Adding a check for is_editor_ready provides just enough time
  297. // to regenerate the doc cache.
  298. if (ea->has_runnable_preset.is_set() && FileAccess::exists(adb) && EditorNode::get_singleton()->is_editor_ready()) {
  299. String devices;
  300. List<String> args;
  301. args.push_back("devices");
  302. int ec;
  303. OS::get_singleton()->execute(adb, args, &devices, &ec);
  304. Vector<String> ds = devices.split("\n");
  305. Vector<String> ldevices;
  306. for (int i = 1; i < ds.size(); i++) {
  307. String d = ds[i];
  308. int dpos = d.find("device");
  309. if (dpos == -1) {
  310. continue;
  311. }
  312. d = d.substr(0, dpos).strip_edges();
  313. ldevices.push_back(d);
  314. }
  315. MutexLock lock(ea->device_lock);
  316. bool different = false;
  317. if (ea->devices.size() != ldevices.size()) {
  318. different = true;
  319. } else {
  320. for (int i = 0; i < ea->devices.size(); i++) {
  321. if (ea->devices[i].id != ldevices[i]) {
  322. different = true;
  323. break;
  324. }
  325. }
  326. }
  327. if (different) {
  328. Vector<Device> ndevices;
  329. for (int i = 0; i < ldevices.size(); i++) {
  330. Device d;
  331. d.id = ldevices[i];
  332. for (int j = 0; j < ea->devices.size(); j++) {
  333. if (ea->devices[j].id == ldevices[i]) {
  334. d.description = ea->devices[j].description;
  335. d.name = ea->devices[j].name;
  336. d.api_level = ea->devices[j].api_level;
  337. }
  338. }
  339. if (d.description.is_empty()) {
  340. //in the oven, request!
  341. args.clear();
  342. args.push_back("-s");
  343. args.push_back(d.id);
  344. args.push_back("shell");
  345. args.push_back("getprop");
  346. int ec2;
  347. String dp;
  348. OS::get_singleton()->execute(adb, args, &dp, &ec2);
  349. Vector<String> props = dp.split("\n");
  350. String vendor;
  351. String device;
  352. d.description = "Device ID: " + d.id + "\n";
  353. d.api_level = 0;
  354. for (int j = 0; j < props.size(); j++) {
  355. // got information by `shell cat /system/build.prop` before and its format is "property=value"
  356. // it's now changed to use `shell getporp` because of permission issue with Android 8.0 and above
  357. // its format is "[property]: [value]" so changed it as like build.prop
  358. String p = props[j];
  359. p = p.replace("]: ", "=");
  360. p = p.remove_chars("[]");
  361. if (p.begins_with("ro.product.model=")) {
  362. device = p.get_slicec('=', 1).strip_edges();
  363. } else if (p.begins_with("ro.product.brand=")) {
  364. vendor = p.get_slicec('=', 1).strip_edges().capitalize();
  365. } else if (p.begins_with("ro.build.display.id=")) {
  366. d.description += "Build: " + p.get_slicec('=', 1).strip_edges() + "\n";
  367. } else if (p.begins_with("ro.build.version.release=")) {
  368. d.description += "Release: " + p.get_slicec('=', 1).strip_edges() + "\n";
  369. } else if (p.begins_with("ro.build.version.sdk=")) {
  370. d.api_level = p.get_slicec('=', 1).to_int();
  371. } else if (p.begins_with("ro.product.cpu.abi=")) {
  372. d.architecture = p.get_slicec('=', 1).strip_edges();
  373. d.description += "CPU: " + d.architecture + "\n";
  374. } else if (p.begins_with("ro.product.manufacturer=")) {
  375. d.description += "Manufacturer: " + p.get_slicec('=', 1).strip_edges() + "\n";
  376. } else if (p.begins_with("ro.board.platform=")) {
  377. d.description += "Chipset: " + p.get_slicec('=', 1).strip_edges() + "\n";
  378. } else if (p.begins_with("ro.opengles.version=")) {
  379. uint32_t opengl = p.get_slicec('=', 1).to_int();
  380. d.description += "OpenGL: " + itos(opengl >> 16) + "." + itos((opengl >> 8) & 0xFF) + "." + itos((opengl) & 0xFF) + "\n";
  381. }
  382. }
  383. d.name = vendor + " " + device;
  384. if (device.is_empty()) {
  385. continue;
  386. }
  387. }
  388. ndevices.push_back(d);
  389. }
  390. ea->devices = ndevices;
  391. ea->devices_changed.set();
  392. }
  393. }
  394. uint64_t sleep = 200;
  395. uint64_t wait = 3000000;
  396. uint64_t time = OS::get_singleton()->get_ticks_usec();
  397. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  398. OS::get_singleton()->delay_usec(1000 * sleep);
  399. if (ea->quit_request.is_set()) {
  400. break;
  401. }
  402. }
  403. }
  404. if (ea->has_runnable_preset.is_set() && EDITOR_GET("export/android/shutdown_adb_on_exit")) {
  405. String adb = get_adb_path();
  406. if (!FileAccess::exists(adb)) {
  407. return; //adb not configured
  408. }
  409. List<String> args;
  410. args.push_back("kill-server");
  411. OS::get_singleton()->execute(adb, args);
  412. }
  413. }
  414. void EditorExportPlatformAndroid::_update_preset_status() {
  415. const int preset_count = EditorExport::get_singleton()->get_export_preset_count();
  416. bool has_runnable = false;
  417. for (int i = 0; i < preset_count; i++) {
  418. const Ref<EditorExportPreset> &preset = EditorExport::get_singleton()->get_export_preset(i);
  419. if (preset->get_platform() == this && preset->is_runnable()) {
  420. has_runnable = true;
  421. break;
  422. }
  423. }
  424. if (has_runnable) {
  425. has_runnable_preset.set();
  426. } else {
  427. has_runnable_preset.clear();
  428. }
  429. devices_changed.set();
  430. }
  431. #endif
  432. String EditorExportPlatformAndroid::get_project_name(const Ref<EditorExportPreset> &p_preset, const String &p_name) const {
  433. String aname;
  434. if (!p_name.is_empty()) {
  435. aname = p_name;
  436. } else {
  437. aname = get_project_setting(p_preset, "application/config/name");
  438. }
  439. if (aname.is_empty()) {
  440. aname = GODOT_VERSION_NAME;
  441. }
  442. return aname;
  443. }
  444. String EditorExportPlatformAndroid::get_package_name(const Ref<EditorExportPreset> &p_preset, const String &p_package) const {
  445. String pname = p_package;
  446. String name = get_valid_basename(p_preset);
  447. pname = pname.replace("$genname", name);
  448. return pname;
  449. }
  450. // Returns the project name without invalid characters
  451. // or the "noname" string if all characters are invalid.
  452. String EditorExportPlatformAndroid::get_valid_basename(const Ref<EditorExportPreset> &p_preset) const {
  453. String basename = get_project_setting(p_preset, "application/config/name");
  454. basename = basename.to_lower();
  455. String name;
  456. bool first = true;
  457. for (int i = 0; i < basename.length(); i++) {
  458. char32_t c = basename[i];
  459. if (is_digit(c) && first) {
  460. continue;
  461. }
  462. if (is_ascii_identifier_char(c)) {
  463. name += String::chr(c);
  464. first = false;
  465. }
  466. }
  467. if (name.is_empty()) {
  468. name = "noname";
  469. }
  470. return name;
  471. }
  472. String EditorExportPlatformAndroid::get_assets_directory(const Ref<EditorExportPreset> &p_preset, int p_export_format) const {
  473. String gradle_build_directory = ExportTemplateManager::get_android_build_directory(p_preset);
  474. return gradle_build_directory.path_join(p_export_format == EXPORT_FORMAT_AAB ? AAB_ASSETS_DIRECTORY : APK_ASSETS_DIRECTORY);
  475. }
  476. bool EditorExportPlatformAndroid::is_package_name_valid(const Ref<EditorExportPreset> &p_preset, const String &p_package, String *r_error) const {
  477. String pname = get_package_name(p_preset, p_package);
  478. if (pname.length() == 0) {
  479. if (r_error) {
  480. *r_error = TTR("Package name is missing.");
  481. }
  482. return false;
  483. }
  484. int segments = 0;
  485. bool first = true;
  486. for (int i = 0; i < pname.length(); i++) {
  487. char32_t c = pname[i];
  488. if (first && c == '.') {
  489. if (r_error) {
  490. *r_error = TTR("Package segments must be of non-zero length.");
  491. }
  492. return false;
  493. }
  494. if (c == '.') {
  495. segments++;
  496. first = true;
  497. continue;
  498. }
  499. if (!is_ascii_identifier_char(c)) {
  500. if (r_error) {
  501. *r_error = vformat(TTR("The character '%s' is not allowed in Android application package names."), String::chr(c));
  502. }
  503. return false;
  504. }
  505. if (first && is_digit(c)) {
  506. if (r_error) {
  507. *r_error = TTR("A digit cannot be the first character in a package segment.");
  508. }
  509. return false;
  510. }
  511. if (first && is_underscore(c)) {
  512. if (r_error) {
  513. *r_error = vformat(TTR("The character '%s' cannot be the first character in a package segment."), String::chr(c));
  514. }
  515. return false;
  516. }
  517. first = false;
  518. }
  519. if (segments == 0) {
  520. if (r_error) {
  521. *r_error = TTR("The package must have at least one '.' separator.");
  522. }
  523. return false;
  524. }
  525. if (first) {
  526. if (r_error) {
  527. *r_error = TTR("Package segments must be of non-zero length.");
  528. }
  529. return false;
  530. }
  531. return true;
  532. }
  533. bool EditorExportPlatformAndroid::is_project_name_valid(const Ref<EditorExportPreset> &p_preset) const {
  534. // Get the original project name and convert to lowercase.
  535. String basename = get_project_setting(p_preset, "application/config/name");
  536. basename = basename.to_lower();
  537. // Check if there are invalid characters.
  538. if (basename != get_valid_basename(p_preset)) {
  539. return false;
  540. }
  541. return true;
  542. }
  543. bool EditorExportPlatformAndroid::_should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
  544. /*
  545. * By not compressing files with little or no benefit in doing so,
  546. * a performance gain is expected at runtime. Moreover, if the APK is
  547. * zip-aligned, assets stored as they are can be efficiently read by
  548. * Android by memory-mapping them.
  549. */
  550. // -- Unconditional uncompress to mimic AAPT plus some other
  551. static const char *unconditional_compress_ext[] = {
  552. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  553. // These formats are already compressed, or don't compress well:
  554. ".jpg", ".jpeg", ".png", ".gif",
  555. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  556. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  557. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  558. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  559. ".amr", ".awb", ".wma", ".wmv",
  560. // Godot-specific:
  561. ".webp", // Same reasoning as .png
  562. ".cfb", // Don't let small config files slow-down startup
  563. ".scn", // Binary scenes are usually already compressed
  564. ".ctex", // Streamable textures are usually already compressed
  565. ".pck", // Pack.
  566. // Trailer for easier processing
  567. nullptr
  568. };
  569. for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
  570. if (p_path.to_lower().ends_with(String(*ext))) {
  571. return false;
  572. }
  573. }
  574. // -- Compressed resource?
  575. if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
  576. // Already compressed
  577. return false;
  578. }
  579. // --- TODO: Decide on texture resources according to their image compression setting
  580. return true;
  581. }
  582. zip_fileinfo EditorExportPlatformAndroid::get_zip_fileinfo() {
  583. OS::DateTime dt = OS::get_singleton()->get_datetime();
  584. zip_fileinfo zipfi;
  585. zipfi.tmz_date.tm_year = dt.year;
  586. zipfi.tmz_date.tm_mon = dt.month - 1; // tm_mon is zero indexed
  587. zipfi.tmz_date.tm_mday = dt.day;
  588. zipfi.tmz_date.tm_hour = dt.hour;
  589. zipfi.tmz_date.tm_min = dt.minute;
  590. zipfi.tmz_date.tm_sec = dt.second;
  591. zipfi.dosDate = 0;
  592. zipfi.external_fa = 0;
  593. zipfi.internal_fa = 0;
  594. return zipfi;
  595. }
  596. Vector<EditorExportPlatformAndroid::ABI> EditorExportPlatformAndroid::get_abis() {
  597. // Should have the same order and size as get_archs.
  598. Vector<ABI> abis;
  599. abis.push_back(ABI("armeabi-v7a", "arm32"));
  600. abis.push_back(ABI("arm64-v8a", "arm64"));
  601. abis.push_back(ABI("x86", "x86_32"));
  602. abis.push_back(ABI("x86_64", "x86_64"));
  603. return abis;
  604. }
  605. #ifndef DISABLE_DEPRECATED
  606. /// List the gdap files in the directory specified by the p_path parameter.
  607. Vector<String> EditorExportPlatformAndroid::list_gdap_files(const String &p_path) {
  608. Vector<String> dir_files;
  609. Ref<DirAccess> da = DirAccess::open(p_path);
  610. if (da.is_valid()) {
  611. da->list_dir_begin();
  612. while (true) {
  613. String file = da->get_next();
  614. if (file.is_empty()) {
  615. break;
  616. }
  617. if (da->current_is_dir() || da->current_is_hidden()) {
  618. continue;
  619. }
  620. if (file.ends_with(PluginConfigAndroid::PLUGIN_CONFIG_EXT)) {
  621. dir_files.push_back(file);
  622. }
  623. }
  624. da->list_dir_end();
  625. }
  626. return dir_files;
  627. }
  628. Vector<PluginConfigAndroid> EditorExportPlatformAndroid::get_plugins() {
  629. Vector<PluginConfigAndroid> loaded_plugins;
  630. String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().path_join("android/plugins");
  631. // Add the prebuilt plugins
  632. loaded_plugins.append_array(PluginConfigAndroid::get_prebuilt_plugins(plugins_dir));
  633. if (DirAccess::exists(plugins_dir)) {
  634. Vector<String> plugins_filenames = list_gdap_files(plugins_dir);
  635. if (!plugins_filenames.is_empty()) {
  636. Ref<ConfigFile> config_file;
  637. config_file.instantiate();
  638. for (int i = 0; i < plugins_filenames.size(); i++) {
  639. PluginConfigAndroid config = PluginConfigAndroid::load_plugin_config(config_file, plugins_dir.path_join(plugins_filenames[i]));
  640. if (config.valid_config) {
  641. loaded_plugins.push_back(config);
  642. } else {
  643. print_error("Invalid plugin config file " + plugins_filenames[i]);
  644. }
  645. }
  646. }
  647. }
  648. return loaded_plugins;
  649. }
  650. Vector<PluginConfigAndroid> EditorExportPlatformAndroid::get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) {
  651. Vector<PluginConfigAndroid> enabled_plugins;
  652. Vector<PluginConfigAndroid> all_plugins = get_plugins();
  653. for (int i = 0; i < all_plugins.size(); i++) {
  654. PluginConfigAndroid plugin = all_plugins[i];
  655. bool enabled = p_presets->get("plugins/" + plugin.name);
  656. if (enabled) {
  657. enabled_plugins.push_back(plugin);
  658. }
  659. }
  660. return enabled_plugins;
  661. }
  662. #endif // DISABLE_DEPRECATED
  663. Error EditorExportPlatformAndroid::store_in_apk(APKExportData *ed, const String &p_path, const Vector<uint8_t> &p_data, int compression_method) {
  664. zip_fileinfo zipfi = get_zip_fileinfo();
  665. zipOpenNewFileInZip(ed->apk,
  666. p_path.utf8().get_data(),
  667. &zipfi,
  668. nullptr,
  669. 0,
  670. nullptr,
  671. 0,
  672. nullptr,
  673. compression_method,
  674. Z_DEFAULT_COMPRESSION);
  675. zipWriteInFileInZip(ed->apk, p_data.ptr(), p_data.size());
  676. zipCloseFileInZip(ed->apk);
  677. return OK;
  678. }
  679. Error EditorExportPlatformAndroid::save_apk_so(void *p_userdata, const SharedObject &p_so) {
  680. if (!p_so.path.get_file().begins_with("lib")) {
  681. String err = "Android .so file names must start with \"lib\", but got: " + p_so.path;
  682. ERR_PRINT(err);
  683. return FAILED;
  684. }
  685. APKExportData *ed = static_cast<APKExportData *>(p_userdata);
  686. Vector<ABI> abis = get_abis();
  687. bool exported = false;
  688. for (int i = 0; i < p_so.tags.size(); ++i) {
  689. // shared objects can be fat (compatible with multiple ABIs)
  690. int abi_index = -1;
  691. for (int j = 0; j < abis.size(); ++j) {
  692. if (abis[j].abi == p_so.tags[i] || abis[j].arch == p_so.tags[i]) {
  693. abi_index = j;
  694. break;
  695. }
  696. }
  697. if (abi_index != -1) {
  698. exported = true;
  699. String abi = abis[abi_index].abi;
  700. String dst_path = String("lib").path_join(abi).path_join(p_so.path.get_file());
  701. Vector<uint8_t> array = FileAccess::get_file_as_bytes(p_so.path);
  702. Error store_err = store_in_apk(ed, dst_path, array, Z_NO_COMPRESSION);
  703. ERR_FAIL_COND_V_MSG(store_err, store_err, "Cannot store in apk file '" + dst_path + "'.");
  704. }
  705. }
  706. if (!exported) {
  707. ERR_PRINT("Cannot determine architecture for library \"" + p_so.path + "\". One of the supported architectures must be used as a tag: " + join_abis(abis, " ", true));
  708. return FAILED;
  709. }
  710. return OK;
  711. }
  712. Error EditorExportPlatformAndroid::save_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed) {
  713. APKExportData *ed = static_cast<APKExportData *>(p_userdata);
  714. String simplified_path = p_path.simplify_path();
  715. if (simplified_path.begins_with("uid://")) {
  716. simplified_path = ResourceUID::uid_to_path(simplified_path).simplify_path();
  717. print_verbose(vformat(R"(UID referenced exported file name "%s" was replaced with "%s".)", p_path, simplified_path));
  718. }
  719. Vector<uint8_t> enc_data;
  720. EditorExportPlatform::SavedData sd;
  721. Error err = _store_temp_file(simplified_path, p_data, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, enc_data, sd);
  722. if (err != OK) {
  723. return err;
  724. }
  725. const String dst_path = String("assets/") + simplified_path.trim_prefix("res://");
  726. print_verbose("Saving project files from " + simplified_path + " into " + dst_path);
  727. store_in_apk(ed, dst_path, enc_data, _should_compress_asset(simplified_path, enc_data) ? Z_DEFLATED : 0);
  728. ed->pd.file_ofs.push_back(sd);
  729. return OK;
  730. }
  731. Error EditorExportPlatformAndroid::ignore_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed) {
  732. return OK;
  733. }
  734. Error EditorExportPlatformAndroid::copy_gradle_so(void *p_userdata, const SharedObject &p_so) {
  735. ERR_FAIL_COND_V_MSG(!p_so.path.get_file().begins_with("lib"), FAILED,
  736. "Android .so file names must start with \"lib\", but got: " + p_so.path);
  737. Vector<ABI> abis = get_abis();
  738. CustomExportData *export_data = static_cast<CustomExportData *>(p_userdata);
  739. bool exported = false;
  740. for (int i = 0; i < p_so.tags.size(); ++i) {
  741. int abi_index = -1;
  742. for (int j = 0; j < abis.size(); ++j) {
  743. if (abis[j].abi == p_so.tags[i] || abis[j].arch == p_so.tags[i]) {
  744. abi_index = j;
  745. break;
  746. }
  747. }
  748. if (abi_index != -1) {
  749. exported = true;
  750. String type = export_data->debug ? "debug" : "release";
  751. String abi = abis[abi_index].abi;
  752. String filename = p_so.path.get_file();
  753. String dst_path = export_data->libs_directory.path_join(type).path_join(abi).path_join(filename);
  754. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_so.path);
  755. print_verbose("Copying .so file from " + p_so.path + " to " + dst_path);
  756. Error err = store_file_at_path(dst_path, data);
  757. ERR_FAIL_COND_V_MSG(err, err, "Failed to copy .so file from " + p_so.path + " to " + dst_path);
  758. export_data->libs.push_back(dst_path);
  759. }
  760. }
  761. ERR_FAIL_COND_V_MSG(!exported, FAILED,
  762. "Cannot determine architecture for library \"" + p_so.path + "\". One of the supported architectures must be used as a tag:" + join_abis(abis, " ", true));
  763. return OK;
  764. }
  765. bool EditorExportPlatformAndroid::_has_read_write_storage_permission(const Vector<String> &p_permissions) {
  766. return p_permissions.has("android.permission.READ_EXTERNAL_STORAGE") || p_permissions.has("android.permission.WRITE_EXTERNAL_STORAGE");
  767. }
  768. bool EditorExportPlatformAndroid::_has_manage_external_storage_permission(const Vector<String> &p_permissions) {
  769. return p_permissions.has("android.permission.MANAGE_EXTERNAL_STORAGE");
  770. }
  771. bool EditorExportPlatformAndroid::_uses_vulkan(const Ref<EditorExportPreset> &p_preset) const {
  772. String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");
  773. String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver.android");
  774. return (rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "vulkan";
  775. }
  776. void EditorExportPlatformAndroid::_notification(int p_what) {
  777. #ifndef ANDROID_ENABLED
  778. switch (p_what) {
  779. case NOTIFICATION_POSTINITIALIZE: {
  780. if (EditorExport::get_singleton()) {
  781. EditorExport::get_singleton()->connect_presets_runnable_updated(callable_mp(this, &EditorExportPlatformAndroid::_update_preset_status));
  782. }
  783. } break;
  784. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  785. if (EditorSettings::get_singleton()->check_changed_settings_in_group("export/android")) {
  786. _create_editor_debug_keystore_if_needed();
  787. }
  788. } break;
  789. }
  790. #endif
  791. }
  792. void EditorExportPlatformAndroid::_create_editor_debug_keystore_if_needed() {
  793. // Check if we have a valid keytool path.
  794. String keytool_path = get_keytool_path();
  795. if (!FileAccess::exists(keytool_path)) {
  796. return;
  797. }
  798. // Check if the current editor debug keystore exists.
  799. String editor_debug_keystore = EDITOR_GET("export/android/debug_keystore");
  800. if (FileAccess::exists(editor_debug_keystore)) {
  801. return;
  802. }
  803. // Generate the debug keystore.
  804. String keystore_path = EditorPaths::get_singleton()->get_debug_keystore_path();
  805. String keystores_dir = keystore_path.get_base_dir();
  806. if (!DirAccess::exists(keystores_dir)) {
  807. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  808. Error err = dir_access->make_dir_recursive(keystores_dir);
  809. if (err != OK) {
  810. WARN_PRINT(TTR("Error creating keystores directory:") + "\n" + keystores_dir);
  811. return;
  812. }
  813. }
  814. if (!FileAccess::exists(keystore_path)) {
  815. String output;
  816. List<String> args;
  817. args.push_back("-genkey");
  818. args.push_back("-keystore");
  819. args.push_back(keystore_path);
  820. args.push_back("-storepass");
  821. args.push_back("android");
  822. args.push_back("-alias");
  823. args.push_back(DEFAULT_ANDROID_KEYSTORE_DEBUG_USER);
  824. args.push_back("-keypass");
  825. args.push_back(DEFAULT_ANDROID_KEYSTORE_DEBUG_PASSWORD);
  826. args.push_back("-keyalg");
  827. args.push_back("RSA");
  828. args.push_back("-keysize");
  829. args.push_back("2048");
  830. args.push_back("-validity");
  831. args.push_back("10000");
  832. args.push_back("-dname");
  833. args.push_back("cn=Godot, ou=Godot Engine, o=Stichting Godot, c=NL");
  834. Error error = OS::get_singleton()->execute(keytool_path, args, &output, nullptr, true);
  835. print_verbose(output);
  836. if (error != OK) {
  837. WARN_PRINT("Error: Unable to create debug keystore");
  838. return;
  839. }
  840. }
  841. // Update the editor settings.
  842. EditorSettings::get_singleton()->set("export/android/debug_keystore", keystore_path);
  843. EditorSettings::get_singleton()->set("export/android/debug_keystore_user", DEFAULT_ANDROID_KEYSTORE_DEBUG_USER);
  844. EditorSettings::get_singleton()->set("export/android/debug_keystore_pass", DEFAULT_ANDROID_KEYSTORE_DEBUG_PASSWORD);
  845. print_verbose("Updated editor debug keystore to " + keystore_path);
  846. }
  847. void EditorExportPlatformAndroid::_get_manifest_info(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, Vector<String> &r_permissions, Vector<FeatureInfo> &r_features, Vector<MetadataInfo> &r_metadata) {
  848. const char **aperms = ANDROID_PERMS;
  849. while (*aperms) {
  850. bool enabled = p_preset->get("permissions/" + String(*aperms).to_lower());
  851. if (enabled) {
  852. r_permissions.push_back("android.permission." + String(*aperms));
  853. }
  854. aperms++;
  855. }
  856. PackedStringArray user_perms = p_preset->get("permissions/custom_permissions");
  857. for (int i = 0; i < user_perms.size(); i++) {
  858. String user_perm = user_perms[i].strip_edges();
  859. if (!user_perm.is_empty()) {
  860. r_permissions.push_back(user_perm);
  861. }
  862. }
  863. if (p_give_internet) {
  864. if (!r_permissions.has("android.permission.INTERNET")) {
  865. r_permissions.push_back("android.permission.INTERNET");
  866. }
  867. }
  868. if (_uses_vulkan(p_preset)) {
  869. // Require vulkan hardware level 1 support
  870. FeatureInfo vulkan_level = {
  871. "android.hardware.vulkan.level", // name
  872. false, // required
  873. "1" // version
  874. };
  875. r_features.append(vulkan_level);
  876. // Require vulkan version 1.0
  877. FeatureInfo vulkan_version = {
  878. "android.hardware.vulkan.version", // name
  879. true, // required
  880. "0x400003" // version - Encoded value for api version 1.0
  881. };
  882. r_features.append(vulkan_version);
  883. }
  884. MetadataInfo rendering_method_metadata = {
  885. "org.godotengine.rendering.method",
  886. p_preset->get_project_setting("rendering/renderer/rendering_method.mobile")
  887. };
  888. r_metadata.append(rendering_method_metadata);
  889. MetadataInfo editor_version_metadata = {
  890. "org.godotengine.editor.version",
  891. String(GODOT_VERSION_FULL_CONFIG)
  892. };
  893. r_metadata.append(editor_version_metadata);
  894. }
  895. void EditorExportPlatformAndroid::_write_tmp_manifest(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, bool p_debug) {
  896. print_verbose("Building temporary manifest...");
  897. String manifest_text =
  898. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  899. "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
  900. " xmlns:tools=\"http://schemas.android.com/tools\">\n";
  901. manifest_text += _get_screen_sizes_tag(p_preset);
  902. manifest_text += _get_gles_tag();
  903. Vector<String> perms;
  904. Vector<FeatureInfo> features;
  905. Vector<MetadataInfo> manifest_metadata;
  906. _get_manifest_info(p_preset, p_give_internet, perms, features, manifest_metadata);
  907. for (int i = 0; i < perms.size(); i++) {
  908. String permission = perms.get(i);
  909. if (permission == "android.permission.WRITE_EXTERNAL_STORAGE" || (permission == "android.permission.READ_EXTERNAL_STORAGE" && _has_manage_external_storage_permission(perms))) {
  910. manifest_text += vformat(" <uses-permission android:name=\"%s\" android:maxSdkVersion=\"29\" />\n", permission);
  911. } else {
  912. manifest_text += vformat(" <uses-permission android:name=\"%s\" />\n", permission);
  913. }
  914. }
  915. for (int i = 0; i < features.size(); i++) {
  916. manifest_text += vformat(" <uses-feature tools:node=\"replace\" android:name=\"%s\" android:required=\"%s\" android:version=\"%s\" />\n", features[i].name, features[i].required, features[i].version);
  917. }
  918. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  919. for (int i = 0; i < export_plugins.size(); i++) {
  920. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  921. const String contents = export_plugins[i]->get_android_manifest_element_contents(Ref<EditorExportPlatform>(this), p_debug);
  922. if (!contents.is_empty()) {
  923. manifest_text += contents;
  924. manifest_text += "\n";
  925. }
  926. }
  927. }
  928. manifest_text += _get_application_tag(Ref<EditorExportPlatform>(this), p_preset, _has_read_write_storage_permission(perms), p_debug, manifest_metadata);
  929. manifest_text += "</manifest>\n";
  930. String manifest_path = ExportTemplateManager::get_android_build_directory(p_preset).path_join(vformat("src/%s/AndroidManifest.xml", (p_debug ? "debug" : "release")));
  931. print_verbose("Storing manifest into " + manifest_path + ": " + "\n" + manifest_text);
  932. store_string_at_path(manifest_path, manifest_text);
  933. }
  934. bool EditorExportPlatformAndroid::_is_transparency_allowed(const Ref<EditorExportPreset> &p_preset) const {
  935. return (bool)get_project_setting(p_preset, "display/window/per_pixel_transparency/allowed");
  936. }
  937. void EditorExportPlatformAndroid::_fix_themes_xml(const Ref<EditorExportPreset> &p_preset) {
  938. const String themes_xml_path = ExportTemplateManager::get_android_build_directory(p_preset).path_join("res/values/themes.xml");
  939. if (!FileAccess::exists(themes_xml_path)) {
  940. print_error("res/values/themes.xml does not exist.");
  941. return;
  942. }
  943. bool transparency_allowed = _is_transparency_allowed(p_preset);
  944. // Default/Reserved theme attributes.
  945. Dictionary main_theme_attributes;
  946. main_theme_attributes["android:windowSwipeToDismiss"] = bool_to_string(p_preset->get("gesture/swipe_to_dismiss"));
  947. main_theme_attributes["android:windowIsTranslucent"] = bool_to_string(transparency_allowed);
  948. if (transparency_allowed) {
  949. main_theme_attributes["android:windowBackground"] = "@android:color/transparent";
  950. } else {
  951. main_theme_attributes["android:windowBackground"] = "#" + p_preset->get("screen/background_color").operator Color().to_html(false);
  952. }
  953. Dictionary splash_theme_attributes;
  954. splash_theme_attributes["android:windowSplashScreenBackground"] = "@mipmap/icon_background";
  955. splash_theme_attributes["windowSplashScreenAnimatedIcon"] = "@mipmap/icon_foreground";
  956. splash_theme_attributes["postSplashScreenTheme"] = "@style/GodotAppMainTheme";
  957. splash_theme_attributes["android:windowIsTranslucent"] = bool_to_string(transparency_allowed);
  958. PackedStringArray reserved_splash_keys;
  959. reserved_splash_keys.append("postSplashScreenTheme");
  960. reserved_splash_keys.append("android:windowIsTranslucent");
  961. Dictionary custom_theme_attributes = p_preset->get("gradle_build/custom_theme_attributes");
  962. // Does not override default/reserved theme attributes; skips any duplicates from custom_theme_attributes.
  963. for (const Variant &k : custom_theme_attributes.keys()) {
  964. String key = k;
  965. String value = custom_theme_attributes[k];
  966. if (key.begins_with("[splash]")) {
  967. String splash_key = key.trim_prefix("[splash]");
  968. if (reserved_splash_keys.has(splash_key)) {
  969. WARN_PRINT(vformat("Skipped custom_theme_attribute '%s'; this is a reserved attribute configured via other export options or project settings.", splash_key));
  970. } else {
  971. splash_theme_attributes[splash_key] = value;
  972. }
  973. } else {
  974. if (main_theme_attributes.has(key)) {
  975. WARN_PRINT(vformat("Skipped custom_theme_attribute '%s'; this is a reserved attribute configured via other export options or project settings.", key));
  976. } else {
  977. main_theme_attributes[key] = value;
  978. }
  979. }
  980. }
  981. Ref<FileAccess> file = FileAccess::open(themes_xml_path, FileAccess::READ);
  982. PackedStringArray lines = file->get_as_text().split("\n");
  983. file->close();
  984. PackedStringArray new_lines;
  985. bool inside_main_theme = false;
  986. bool inside_splash_theme = false;
  987. for (int i = 0; i < lines.size(); i++) {
  988. String line = lines[i];
  989. if (line.contains("<style name=\"GodotAppMainTheme\"")) {
  990. inside_main_theme = true;
  991. new_lines.append(line);
  992. continue;
  993. }
  994. if (line.contains("<style name=\"GodotAppSplashTheme\"")) {
  995. inside_splash_theme = true;
  996. new_lines.append(line);
  997. continue;
  998. }
  999. // Inject GodotAppMainTheme attributes.
  1000. if (inside_main_theme && line.contains("</style>")) {
  1001. for (const Variant &attribute : main_theme_attributes.keys()) {
  1002. String value = main_theme_attributes[attribute];
  1003. String item_line = vformat(" <item name=\"%s\">%s</item>", attribute, value);
  1004. new_lines.append(item_line);
  1005. }
  1006. new_lines.append(line); // Add </style> in the end.
  1007. inside_main_theme = false;
  1008. continue;
  1009. }
  1010. // Inject GodotAppSplashTheme attributes.
  1011. if (inside_splash_theme && line.contains("</style>")) {
  1012. for (const Variant &attribute : splash_theme_attributes.keys()) {
  1013. String value = splash_theme_attributes[attribute];
  1014. String item_line = vformat(" <item name=\"%s\">%s</item>", attribute, value);
  1015. new_lines.append(item_line);
  1016. }
  1017. new_lines.append(line); // Add </style> in the end.
  1018. inside_splash_theme = false;
  1019. continue;
  1020. }
  1021. // Add all other lines unchanged.
  1022. if (!inside_main_theme && !inside_splash_theme) {
  1023. new_lines.append(line);
  1024. }
  1025. }
  1026. // Reconstruct the XML content from the modified lines.
  1027. String xml_content = String("\n").join(new_lines);
  1028. store_string_at_path(themes_xml_path, xml_content);
  1029. print_verbose("Successfully modified " + themes_xml_path + ": " + "\n" + xml_content);
  1030. }
  1031. void EditorExportPlatformAndroid::_fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet) {
  1032. // Leaving the unused types commented because looking these constants up
  1033. // again later would be annoying
  1034. // const int CHUNK_AXML_FILE = 0x00080003;
  1035. // const int CHUNK_RESOURCEIDS = 0x00080180;
  1036. const int CHUNK_STRINGS = 0x001C0001;
  1037. // const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  1038. const int CHUNK_XML_END_TAG = 0x00100103;
  1039. // const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  1040. const int CHUNK_XML_START_TAG = 0x00100102;
  1041. // const int CHUNK_XML_TEXT = 0x00100104;
  1042. const int UTF8_FLAG = 0x00000100;
  1043. Vector<String> string_table;
  1044. uint32_t ofs = 8;
  1045. uint32_t string_count = 0;
  1046. uint32_t string_flags = 0;
  1047. uint32_t string_data_offset = 0;
  1048. uint32_t string_table_begins = 0;
  1049. uint32_t string_table_ends = 0;
  1050. Vector<uint8_t> stable_extra;
  1051. String version_name = p_preset->get_version("version/name");
  1052. int version_code = p_preset->get("version/code");
  1053. String package_name = p_preset->get("package/unique_name");
  1054. const int screen_orientation =
  1055. _get_android_orientation_value(DisplayServer::ScreenOrientation(int(get_project_setting(p_preset, "display/window/handheld/orientation"))));
  1056. bool screen_support_small = p_preset->get("screen/support_small");
  1057. bool screen_support_normal = p_preset->get("screen/support_normal");
  1058. bool screen_support_large = p_preset->get("screen/support_large");
  1059. bool screen_support_xlarge = p_preset->get("screen/support_xlarge");
  1060. bool backup_allowed = p_preset->get("user_data_backup/allow");
  1061. int app_category = p_preset->get("package/app_category");
  1062. bool retain_data_on_uninstall = p_preset->get("package/retain_data_on_uninstall");
  1063. bool exclude_from_recents = p_preset->get("package/exclude_from_recents");
  1064. bool is_resizeable = bool(get_project_setting(p_preset, "display/window/size/resizable"));
  1065. Vector<String> perms;
  1066. Vector<FeatureInfo> features;
  1067. Vector<MetadataInfo> manifest_metadata;
  1068. _get_manifest_info(p_preset, p_give_internet, perms, features, manifest_metadata);
  1069. bool has_read_write_storage_permission = _has_read_write_storage_permission(perms);
  1070. while (ofs < (uint32_t)p_manifest.size()) {
  1071. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  1072. uint32_t size = decode_uint32(&p_manifest[ofs + 4]);
  1073. switch (chunk) {
  1074. case CHUNK_STRINGS: {
  1075. int iofs = ofs + 8;
  1076. string_count = decode_uint32(&p_manifest[iofs]);
  1077. string_flags = decode_uint32(&p_manifest[iofs + 8]);
  1078. string_data_offset = decode_uint32(&p_manifest[iofs + 12]);
  1079. uint32_t st_offset = iofs + 20;
  1080. string_table.resize(string_count);
  1081. uint32_t string_end = 0;
  1082. string_table_begins = st_offset;
  1083. for (uint32_t i = 0; i < string_count; i++) {
  1084. uint32_t string_at = decode_uint32(&p_manifest[st_offset + i * 4]);
  1085. string_at += st_offset + string_count * 4;
  1086. ERR_FAIL_COND_MSG(string_flags & UTF8_FLAG, "Unimplemented, can't read UTF-8 string table.");
  1087. if (string_flags & UTF8_FLAG) {
  1088. } else {
  1089. uint32_t len = decode_uint16(&p_manifest[string_at]);
  1090. Vector<char32_t> ucstring;
  1091. ucstring.resize(len + 1);
  1092. for (uint32_t j = 0; j < len; j++) {
  1093. uint16_t c = decode_uint16(&p_manifest[string_at + 2 + 2 * j]);
  1094. ucstring.write[j] = c;
  1095. }
  1096. string_end = MAX(string_at + 2 + 2 * len, string_end);
  1097. ucstring.write[len] = 0;
  1098. string_table.write[i] = ucstring.ptr();
  1099. }
  1100. }
  1101. for (uint32_t i = string_end; i < (ofs + size); i++) {
  1102. stable_extra.push_back(p_manifest[i]);
  1103. }
  1104. string_table_ends = ofs + size;
  1105. } break;
  1106. case CHUNK_XML_START_TAG: {
  1107. int iofs = ofs + 8;
  1108. uint32_t name = decode_uint32(&p_manifest[iofs + 12]);
  1109. String tname = string_table[name];
  1110. uint32_t attrcount = decode_uint32(&p_manifest[iofs + 20]);
  1111. iofs += 28;
  1112. for (uint32_t i = 0; i < attrcount; i++) {
  1113. uint32_t attr_nspace = decode_uint32(&p_manifest[iofs]);
  1114. uint32_t attr_name = decode_uint32(&p_manifest[iofs + 4]);
  1115. uint32_t attr_value = decode_uint32(&p_manifest[iofs + 8]);
  1116. uint32_t attr_resid = decode_uint32(&p_manifest[iofs + 16]);
  1117. const String value = (attr_value != 0xFFFFFFFF) ? string_table[attr_value] : "Res #" + itos(attr_resid);
  1118. String attrname = string_table[attr_name];
  1119. const String nspace = (attr_nspace != 0xFFFFFFFF) ? string_table[attr_nspace] : "";
  1120. //replace project information
  1121. if (tname == "manifest" && attrname == "package") {
  1122. string_table.write[attr_value] = get_package_name(p_preset, package_name);
  1123. }
  1124. if (tname == "manifest" && attrname == "versionCode") {
  1125. encode_uint32(version_code, &p_manifest.write[iofs + 16]);
  1126. }
  1127. if (tname == "manifest" && attrname == "versionName") {
  1128. if (attr_value == 0xFFFFFFFF) {
  1129. WARN_PRINT("Version name in a resource, should be plain text");
  1130. } else {
  1131. string_table.write[attr_value] = version_name;
  1132. }
  1133. }
  1134. if (tname == "application" && attrname == "requestLegacyExternalStorage") {
  1135. encode_uint32(has_read_write_storage_permission ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  1136. }
  1137. if (tname == "application" && attrname == "allowBackup") {
  1138. encode_uint32(backup_allowed, &p_manifest.write[iofs + 16]);
  1139. }
  1140. if (tname == "application" && attrname == "appCategory") {
  1141. encode_uint32(_get_app_category_value(app_category), &p_manifest.write[iofs + 16]);
  1142. }
  1143. if (tname == "application" && attrname == "isGame") {
  1144. encode_uint32(app_category == APP_CATEGORY_GAME, &p_manifest.write[iofs + 16]);
  1145. }
  1146. if (tname == "application" && attrname == "hasFragileUserData") {
  1147. encode_uint32(retain_data_on_uninstall, &p_manifest.write[iofs + 16]);
  1148. }
  1149. if (tname == "activity" && attrname == "screenOrientation") {
  1150. encode_uint32(screen_orientation, &p_manifest.write[iofs + 16]);
  1151. }
  1152. if (tname == "activity" && attrname == "excludeFromRecents") {
  1153. encode_uint32(exclude_from_recents, &p_manifest.write[iofs + 16]);
  1154. }
  1155. if (tname == "activity" && attrname == "resizeableActivity") {
  1156. encode_uint32(is_resizeable, &p_manifest.write[iofs + 16]);
  1157. }
  1158. if (tname == "provider" && attrname == "authorities") {
  1159. string_table.write[attr_value] = get_package_name(p_preset, package_name) + String(".fileprovider");
  1160. }
  1161. if (tname == "supports-screens") {
  1162. if (attrname == "smallScreens") {
  1163. encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  1164. } else if (attrname == "normalScreens") {
  1165. encode_uint32(screen_support_normal ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  1166. } else if (attrname == "largeScreens") {
  1167. encode_uint32(screen_support_large ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  1168. } else if (attrname == "xlargeScreens") {
  1169. encode_uint32(screen_support_xlarge ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  1170. }
  1171. }
  1172. iofs += 20;
  1173. }
  1174. } break;
  1175. case CHUNK_XML_END_TAG: {
  1176. int iofs = ofs + 8;
  1177. uint32_t name = decode_uint32(&p_manifest[iofs + 12]);
  1178. String tname = string_table[name];
  1179. if (tname == "manifest" || tname == "application") {
  1180. // save manifest ending so we can restore it
  1181. Vector<uint8_t> manifest_end;
  1182. uint32_t manifest_cur_size = p_manifest.size();
  1183. manifest_end.resize(p_manifest.size() - ofs);
  1184. memcpy(manifest_end.ptrw(), &p_manifest[ofs], manifest_end.size());
  1185. int32_t attr_name_string = string_table.find("name");
  1186. ERR_FAIL_COND_MSG(attr_name_string == -1, "Template does not have 'name' attribute.");
  1187. int32_t ns_android_string = string_table.find("http://schemas.android.com/apk/res/android");
  1188. if (ns_android_string == -1) {
  1189. string_table.push_back("http://schemas.android.com/apk/res/android");
  1190. ns_android_string = string_table.size() - 1;
  1191. }
  1192. if (tname == "manifest") {
  1193. // Updating manifest features
  1194. int32_t attr_uses_feature_string = string_table.find("uses-feature");
  1195. if (attr_uses_feature_string == -1) {
  1196. string_table.push_back("uses-feature");
  1197. attr_uses_feature_string = string_table.size() - 1;
  1198. }
  1199. int32_t attr_required_string = string_table.find("required");
  1200. if (attr_required_string == -1) {
  1201. string_table.push_back("required");
  1202. attr_required_string = string_table.size() - 1;
  1203. }
  1204. for (int i = 0; i < features.size(); i++) {
  1205. const String &feature_name = features[i].name;
  1206. bool feature_required = features[i].required;
  1207. String feature_version = features[i].version;
  1208. bool has_version_attribute = !feature_version.is_empty();
  1209. print_line("Adding feature " + feature_name);
  1210. int32_t feature_string = string_table.find(feature_name);
  1211. if (feature_string == -1) {
  1212. string_table.push_back(feature_name);
  1213. feature_string = string_table.size() - 1;
  1214. }
  1215. String required_value_string = feature_required ? "true" : "false";
  1216. int32_t required_value = string_table.find(required_value_string);
  1217. if (required_value == -1) {
  1218. string_table.push_back(required_value_string);
  1219. required_value = string_table.size() - 1;
  1220. }
  1221. int32_t attr_version_string = -1;
  1222. int32_t version_value = -1;
  1223. int tag_size;
  1224. int attr_count;
  1225. if (has_version_attribute) {
  1226. attr_version_string = string_table.find("version");
  1227. if (attr_version_string == -1) {
  1228. string_table.push_back("version");
  1229. attr_version_string = string_table.size() - 1;
  1230. }
  1231. version_value = string_table.find(feature_version);
  1232. if (version_value == -1) {
  1233. string_table.push_back(feature_version);
  1234. version_value = string_table.size() - 1;
  1235. }
  1236. tag_size = 96; // node and three attrs + end node
  1237. attr_count = 3;
  1238. } else {
  1239. tag_size = 76; // node and two attrs + end node
  1240. attr_count = 2;
  1241. }
  1242. manifest_cur_size += tag_size + 24;
  1243. p_manifest.resize(manifest_cur_size);
  1244. // start tag
  1245. encode_uint16(0x102, &p_manifest.write[ofs]); // type
  1246. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1247. encode_uint32(tag_size, &p_manifest.write[ofs + 4]); // size
  1248. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1249. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1250. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1251. encode_uint32(attr_uses_feature_string, &p_manifest.write[ofs + 20]); // name
  1252. encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start
  1253. encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size
  1254. encode_uint16(attr_count, &p_manifest.write[ofs + 28]); // num_attrs
  1255. encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index
  1256. encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index
  1257. encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index
  1258. // android:name attribute
  1259. encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns
  1260. encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name'
  1261. encode_uint32(feature_string, &p_manifest.write[ofs + 44]); // raw_value
  1262. encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size
  1263. p_manifest.write[ofs + 50] = 0; // typedvalue_always0
  1264. p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string)
  1265. encode_uint32(feature_string, &p_manifest.write[ofs + 52]); // typedvalue reference
  1266. // android:required attribute
  1267. encode_uint32(ns_android_string, &p_manifest.write[ofs + 56]); // ns
  1268. encode_uint32(attr_required_string, &p_manifest.write[ofs + 60]); // 'name'
  1269. encode_uint32(required_value, &p_manifest.write[ofs + 64]); // raw_value
  1270. encode_uint16(8, &p_manifest.write[ofs + 68]); // typedvalue_size
  1271. p_manifest.write[ofs + 70] = 0; // typedvalue_always0
  1272. p_manifest.write[ofs + 71] = 0x03; // typedvalue_type (string)
  1273. encode_uint32(required_value, &p_manifest.write[ofs + 72]); // typedvalue reference
  1274. ofs += 76;
  1275. if (has_version_attribute) {
  1276. // android:version attribute
  1277. encode_uint32(ns_android_string, &p_manifest.write[ofs]); // ns
  1278. encode_uint32(attr_version_string, &p_manifest.write[ofs + 4]); // 'name'
  1279. encode_uint32(version_value, &p_manifest.write[ofs + 8]); // raw_value
  1280. encode_uint16(8, &p_manifest.write[ofs + 12]); // typedvalue_size
  1281. p_manifest.write[ofs + 14] = 0; // typedvalue_always0
  1282. p_manifest.write[ofs + 15] = 0x03; // typedvalue_type (string)
  1283. encode_uint32(version_value, &p_manifest.write[ofs + 16]); // typedvalue reference
  1284. ofs += 20;
  1285. }
  1286. // end tag
  1287. encode_uint16(0x103, &p_manifest.write[ofs]); // type
  1288. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1289. encode_uint32(24, &p_manifest.write[ofs + 4]); // size
  1290. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1291. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1292. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1293. encode_uint32(attr_uses_feature_string, &p_manifest.write[ofs + 20]); // name
  1294. ofs += 24;
  1295. }
  1296. // Updating manifest permissions
  1297. int32_t attr_uses_permission_string = string_table.find("uses-permission");
  1298. if (attr_uses_permission_string == -1) {
  1299. string_table.push_back("uses-permission");
  1300. attr_uses_permission_string = string_table.size() - 1;
  1301. }
  1302. for (int i = 0; i < perms.size(); ++i) {
  1303. print_line("Adding permission " + perms[i]);
  1304. manifest_cur_size += 56 + 24; // node + end node
  1305. p_manifest.resize(manifest_cur_size);
  1306. // Add permission to the string pool
  1307. int32_t perm_string = string_table.find(perms[i]);
  1308. if (perm_string == -1) {
  1309. string_table.push_back(perms[i]);
  1310. perm_string = string_table.size() - 1;
  1311. }
  1312. // start tag
  1313. encode_uint16(0x102, &p_manifest.write[ofs]); // type
  1314. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1315. encode_uint32(56, &p_manifest.write[ofs + 4]); // size
  1316. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1317. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1318. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1319. encode_uint32(attr_uses_permission_string, &p_manifest.write[ofs + 20]); // name
  1320. encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start
  1321. encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size
  1322. encode_uint16(1, &p_manifest.write[ofs + 28]); // num_attrs
  1323. encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index
  1324. encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index
  1325. encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index
  1326. // attribute
  1327. encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns
  1328. encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name'
  1329. encode_uint32(perm_string, &p_manifest.write[ofs + 44]); // raw_value
  1330. encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size
  1331. p_manifest.write[ofs + 50] = 0; // typedvalue_always0
  1332. p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string)
  1333. encode_uint32(perm_string, &p_manifest.write[ofs + 52]); // typedvalue reference
  1334. ofs += 56;
  1335. // end tag
  1336. encode_uint16(0x103, &p_manifest.write[ofs]); // type
  1337. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1338. encode_uint32(24, &p_manifest.write[ofs + 4]); // size
  1339. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1340. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1341. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1342. encode_uint32(attr_uses_permission_string, &p_manifest.write[ofs + 20]); // name
  1343. ofs += 24;
  1344. }
  1345. }
  1346. if (tname == "application") {
  1347. // Updating application meta-data
  1348. int32_t attr_meta_data_string = string_table.find("meta-data");
  1349. if (attr_meta_data_string == -1) {
  1350. string_table.push_back("meta-data");
  1351. attr_meta_data_string = string_table.size() - 1;
  1352. }
  1353. int32_t attr_value_string = string_table.find("value");
  1354. if (attr_value_string == -1) {
  1355. string_table.push_back("value");
  1356. attr_value_string = string_table.size() - 1;
  1357. }
  1358. for (int i = 0; i < manifest_metadata.size(); i++) {
  1359. String meta_data_name = manifest_metadata[i].name;
  1360. String meta_data_value = manifest_metadata[i].value;
  1361. print_line("Adding application metadata " + meta_data_name);
  1362. int32_t meta_data_name_string = string_table.find(meta_data_name);
  1363. if (meta_data_name_string == -1) {
  1364. string_table.push_back(meta_data_name);
  1365. meta_data_name_string = string_table.size() - 1;
  1366. }
  1367. int32_t meta_data_value_string = string_table.find(meta_data_value);
  1368. if (meta_data_value_string == -1) {
  1369. string_table.push_back(meta_data_value);
  1370. meta_data_value_string = string_table.size() - 1;
  1371. }
  1372. int tag_size = 76; // node and two attrs + end node
  1373. int attr_count = 2;
  1374. manifest_cur_size += tag_size + 24;
  1375. p_manifest.resize(manifest_cur_size);
  1376. // start tag
  1377. encode_uint16(0x102, &p_manifest.write[ofs]); // type
  1378. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1379. encode_uint32(tag_size, &p_manifest.write[ofs + 4]); // size
  1380. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1381. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1382. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1383. encode_uint32(attr_meta_data_string, &p_manifest.write[ofs + 20]); // name
  1384. encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start
  1385. encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size
  1386. encode_uint16(attr_count, &p_manifest.write[ofs + 28]); // num_attrs
  1387. encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index
  1388. encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index
  1389. encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index
  1390. // android:name attribute
  1391. encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns
  1392. encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name'
  1393. encode_uint32(meta_data_name_string, &p_manifest.write[ofs + 44]); // raw_value
  1394. encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size
  1395. p_manifest.write[ofs + 50] = 0; // typedvalue_always0
  1396. p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string)
  1397. encode_uint32(meta_data_name_string, &p_manifest.write[ofs + 52]); // typedvalue reference
  1398. // android:value attribute
  1399. encode_uint32(ns_android_string, &p_manifest.write[ofs + 56]); // ns
  1400. encode_uint32(attr_value_string, &p_manifest.write[ofs + 60]); // 'value'
  1401. encode_uint32(meta_data_value_string, &p_manifest.write[ofs + 64]); // raw_value
  1402. encode_uint16(8, &p_manifest.write[ofs + 68]); // typedvalue_size
  1403. p_manifest.write[ofs + 70] = 0; // typedvalue_always0
  1404. p_manifest.write[ofs + 71] = 0x03; // typedvalue_type (string)
  1405. encode_uint32(meta_data_value_string, &p_manifest.write[ofs + 72]); // typedvalue reference
  1406. ofs += 76;
  1407. // end tag
  1408. encode_uint16(0x103, &p_manifest.write[ofs]); // type
  1409. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1410. encode_uint32(24, &p_manifest.write[ofs + 4]); // size
  1411. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1412. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1413. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1414. encode_uint32(attr_meta_data_string, &p_manifest.write[ofs + 20]); // name
  1415. ofs += 24;
  1416. }
  1417. }
  1418. // copy footer back in
  1419. memcpy(&p_manifest.write[ofs], manifest_end.ptr(), manifest_end.size());
  1420. }
  1421. } break;
  1422. }
  1423. ofs += size;
  1424. }
  1425. // Create new android manifest binary.
  1426. Vector<uint8_t> ret;
  1427. ret.resize(string_table_begins + string_table.size() * 4);
  1428. for (uint32_t i = 0; i < string_table_begins; i++) {
  1429. ret.write[i] = p_manifest[i];
  1430. }
  1431. ofs = 0;
  1432. for (int i = 0; i < string_table.size(); i++) {
  1433. encode_uint32(ofs, &ret.write[string_table_begins + i * 4]);
  1434. ofs += string_table[i].length() * 2 + 2 + 2;
  1435. }
  1436. ret.resize(ret.size() + ofs);
  1437. string_data_offset = ret.size() - ofs;
  1438. uint8_t *chars = &ret.write[string_data_offset];
  1439. for (int i = 0; i < string_table.size(); i++) {
  1440. String s = string_table[i];
  1441. encode_uint16(s.length(), chars);
  1442. chars += 2;
  1443. for (int j = 0; j < s.length(); j++) {
  1444. encode_uint16(s[j], chars);
  1445. chars += 2;
  1446. }
  1447. encode_uint16(0, chars);
  1448. chars += 2;
  1449. }
  1450. for (int i = 0; i < stable_extra.size(); i++) {
  1451. ret.push_back(stable_extra[i]);
  1452. }
  1453. //pad
  1454. while (ret.size() % 4) {
  1455. ret.push_back(0);
  1456. }
  1457. uint32_t new_stable_end = ret.size();
  1458. uint32_t extra = (p_manifest.size() - string_table_ends);
  1459. ret.resize(new_stable_end + extra);
  1460. for (uint32_t i = 0; i < extra; i++) {
  1461. ret.write[new_stable_end + i] = p_manifest[string_table_ends + i];
  1462. }
  1463. while (ret.size() % 4) {
  1464. ret.push_back(0);
  1465. }
  1466. encode_uint32(ret.size(), &ret.write[4]); //update new file size
  1467. encode_uint32(new_stable_end - 8, &ret.write[12]); //update new string table size
  1468. encode_uint32(string_table.size(), &ret.write[16]); //update new number of strings
  1469. encode_uint32(string_data_offset - 8, &ret.write[28]); //update new string data offset
  1470. p_manifest = ret;
  1471. }
  1472. String EditorExportPlatformAndroid::_get_keystore_path(const Ref<EditorExportPreset> &p_preset, bool p_debug) {
  1473. String keystore_preference = p_debug ? "keystore/debug" : "keystore/release";
  1474. String keystore_env_variable = p_debug ? ENV_ANDROID_KEYSTORE_DEBUG_PATH : ENV_ANDROID_KEYSTORE_RELEASE_PATH;
  1475. String keystore_path = p_preset->get_or_env(keystore_preference, keystore_env_variable);
  1476. return ProjectSettings::get_singleton()->globalize_path(keystore_path).simplify_path();
  1477. }
  1478. String EditorExportPlatformAndroid::_parse_string(const uint8_t *p_bytes, bool p_utf8) {
  1479. uint32_t offset = 0;
  1480. uint32_t len = 0;
  1481. if (p_utf8) {
  1482. uint8_t byte = p_bytes[offset];
  1483. if (byte & 0x80) {
  1484. offset += 2;
  1485. } else {
  1486. offset += 1;
  1487. }
  1488. byte = p_bytes[offset];
  1489. offset++;
  1490. if (byte & 0x80) {
  1491. len = byte & 0x7F;
  1492. len = (len << 8) + p_bytes[offset];
  1493. offset++;
  1494. } else {
  1495. len = byte;
  1496. }
  1497. } else {
  1498. len = decode_uint16(&p_bytes[offset]);
  1499. offset += 2;
  1500. if (len & 0x8000) {
  1501. len &= 0x7FFF;
  1502. len = (len << 16) + decode_uint16(&p_bytes[offset]);
  1503. offset += 2;
  1504. }
  1505. }
  1506. if (p_utf8) {
  1507. Vector<uint8_t> str8;
  1508. str8.resize(len + 1);
  1509. for (uint32_t i = 0; i < len; i++) {
  1510. str8.write[i] = p_bytes[offset + i];
  1511. }
  1512. str8.write[len] = 0;
  1513. return String::utf8((const char *)str8.ptr(), len);
  1514. } else {
  1515. String str;
  1516. for (uint32_t i = 0; i < len; i++) {
  1517. char32_t c = decode_uint16(&p_bytes[offset + i * 2]);
  1518. if (c == 0) {
  1519. break;
  1520. }
  1521. str += String::chr(c);
  1522. }
  1523. return str;
  1524. }
  1525. }
  1526. void EditorExportPlatformAndroid::_fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &r_manifest) {
  1527. const int UTF8_FLAG = 0x00000100;
  1528. uint32_t string_block_len = decode_uint32(&r_manifest[16]);
  1529. uint32_t string_count = decode_uint32(&r_manifest[20]);
  1530. uint32_t string_flags = decode_uint32(&r_manifest[28]);
  1531. const uint32_t string_table_begins = 40;
  1532. Vector<String> string_table;
  1533. String package_name = p_preset->get("package/name");
  1534. Dictionary appnames = get_project_setting(p_preset, "application/config/name_localized");
  1535. for (uint32_t i = 0; i < string_count; i++) {
  1536. uint32_t offset = decode_uint32(&r_manifest[string_table_begins + i * 4]);
  1537. offset += string_table_begins + string_count * 4;
  1538. String str = _parse_string(&r_manifest[offset], string_flags & UTF8_FLAG);
  1539. if (str.begins_with("godot-project-name")) {
  1540. if (str == "godot-project-name") {
  1541. //project name
  1542. str = get_project_name(p_preset, package_name);
  1543. } else {
  1544. String lang = str.substr(str.rfind_char('-') + 1).replace_char('-', '_');
  1545. if (appnames.has(lang)) {
  1546. str = appnames[lang];
  1547. } else {
  1548. str = get_project_name(p_preset, package_name);
  1549. }
  1550. }
  1551. }
  1552. string_table.push_back(str);
  1553. }
  1554. //write a new string table, but use 16 bits
  1555. Vector<uint8_t> ret;
  1556. ret.resize(string_table_begins + string_table.size() * 4);
  1557. for (uint32_t i = 0; i < string_table_begins; i++) {
  1558. ret.write[i] = r_manifest[i];
  1559. }
  1560. int ofs = 0;
  1561. for (int i = 0; i < string_table.size(); i++) {
  1562. encode_uint32(ofs, &ret.write[string_table_begins + i * 4]);
  1563. ofs += string_table[i].length() * 2 + 2 + 2;
  1564. }
  1565. ret.resize(ret.size() + ofs);
  1566. uint8_t *chars = &ret.write[ret.size() - ofs];
  1567. for (int i = 0; i < string_table.size(); i++) {
  1568. String s = string_table[i];
  1569. encode_uint16(s.length(), chars);
  1570. chars += 2;
  1571. for (int j = 0; j < s.length(); j++) {
  1572. encode_uint16(s[j], chars);
  1573. chars += 2;
  1574. }
  1575. encode_uint16(0, chars);
  1576. chars += 2;
  1577. }
  1578. //pad
  1579. while (ret.size() % 4) {
  1580. ret.push_back(0);
  1581. }
  1582. //change flags to not use utf8
  1583. encode_uint32(string_flags & ~0x100, &ret.write[28]);
  1584. //change length
  1585. encode_uint32(ret.size() - 12, &ret.write[16]);
  1586. //append the rest...
  1587. int rest_from = 12 + string_block_len;
  1588. int rest_to = ret.size();
  1589. int rest_len = (r_manifest.size() - rest_from);
  1590. ret.resize(ret.size() + (r_manifest.size() - rest_from));
  1591. for (int i = 0; i < rest_len; i++) {
  1592. ret.write[rest_to + i] = r_manifest[rest_from + i];
  1593. }
  1594. //finally update the size
  1595. encode_uint32(ret.size(), &ret.write[4]);
  1596. r_manifest = ret;
  1597. //printf("end\n");
  1598. }
  1599. void EditorExportPlatformAndroid::_load_image_data(const Ref<Image> &p_splash_image, Vector<uint8_t> &p_data) {
  1600. Vector<uint8_t> png_buffer;
  1601. Error err = PNGDriverCommon::image_to_png(p_splash_image, png_buffer);
  1602. if (err == OK) {
  1603. p_data.resize(png_buffer.size());
  1604. memcpy(p_data.ptrw(), png_buffer.ptr(), p_data.size());
  1605. } else {
  1606. String err_str = String("Failed to convert splash image to png.");
  1607. WARN_PRINT(err_str.utf8().get_data());
  1608. }
  1609. }
  1610. void EditorExportPlatformAndroid::_process_launcher_icons(const String &p_file_name, const Ref<Image> &p_source_image, int dimension, Vector<uint8_t> &p_data) {
  1611. Ref<Image> working_image = p_source_image;
  1612. if (p_source_image->get_width() != dimension || p_source_image->get_height() != dimension) {
  1613. working_image = p_source_image->duplicate();
  1614. working_image->resize(dimension, dimension, Image::Interpolation::INTERPOLATE_LANCZOS);
  1615. }
  1616. Vector<uint8_t> png_buffer;
  1617. Error err = PNGDriverCommon::image_to_png(working_image, png_buffer);
  1618. if (err == OK) {
  1619. p_data.resize(png_buffer.size());
  1620. memcpy(p_data.ptrw(), png_buffer.ptr(), p_data.size());
  1621. } else {
  1622. String err_str = String("Failed to convert resized icon (") + p_file_name + ") to png.";
  1623. WARN_PRINT(err_str.utf8().get_data());
  1624. }
  1625. }
  1626. void EditorExportPlatformAndroid::load_icon_refs(const Ref<EditorExportPreset> &p_preset, Ref<Image> &icon, Ref<Image> &foreground, Ref<Image> &background, Ref<Image> &monochrome) {
  1627. String project_icon_path = get_project_setting(p_preset, "application/config/icon");
  1628. Error err = OK;
  1629. // Regular icon: user selection -> project icon -> default.
  1630. String path = static_cast<String>(p_preset->get(LAUNCHER_ICON_OPTION)).strip_edges();
  1631. print_verbose("Loading regular icon from " + path);
  1632. if (!path.is_empty()) {
  1633. icon = _load_icon_or_splash_image(path, &err);
  1634. }
  1635. if (path.is_empty() || err != OK || icon.is_null() || icon->is_empty()) {
  1636. print_verbose("- falling back to project icon: " + project_icon_path);
  1637. if (!project_icon_path.is_empty()) {
  1638. icon = _load_icon_or_splash_image(project_icon_path, &err);
  1639. } else {
  1640. ERR_PRINT("No project icon specified. Please specify one in the Project Settings under Application -> Config -> Icon");
  1641. }
  1642. }
  1643. // Adaptive foreground: user selection -> regular icon (user selection -> project icon -> default).
  1644. path = static_cast<String>(p_preset->get(LAUNCHER_ADAPTIVE_ICON_FOREGROUND_OPTION)).strip_edges();
  1645. print_verbose("Loading adaptive foreground icon from " + path);
  1646. if (!path.is_empty()) {
  1647. foreground = _load_icon_or_splash_image(path, &err);
  1648. }
  1649. if (path.is_empty() || err != OK || foreground.is_null() || foreground->is_empty()) {
  1650. print_verbose("- falling back to using the regular icon");
  1651. foreground = icon;
  1652. }
  1653. // Adaptive background: user selection -> default.
  1654. path = static_cast<String>(p_preset->get(LAUNCHER_ADAPTIVE_ICON_BACKGROUND_OPTION)).strip_edges();
  1655. if (!path.is_empty()) {
  1656. print_verbose("Loading adaptive background icon from " + path);
  1657. background = _load_icon_or_splash_image(path, &err);
  1658. }
  1659. // Adaptive monochrome: user selection -> default.
  1660. path = static_cast<String>(p_preset->get(LAUNCHER_ADAPTIVE_ICON_MONOCHROME_OPTION)).strip_edges();
  1661. if (!path.is_empty()) {
  1662. print_verbose("Loading adaptive monochrome icon from " + path);
  1663. monochrome = _load_icon_or_splash_image(path, &err);
  1664. }
  1665. }
  1666. void EditorExportPlatformAndroid::_copy_icons_to_gradle_project(const Ref<EditorExportPreset> &p_preset,
  1667. const Ref<Image> &p_main_image,
  1668. const Ref<Image> &p_foreground,
  1669. const Ref<Image> &p_background,
  1670. const Ref<Image> &p_monochrome) {
  1671. String gradle_build_dir = ExportTemplateManager::get_android_build_directory(p_preset);
  1672. String monochrome_tag = "";
  1673. // Prepare images to be resized for the icons. If some image ends up being uninitialized,
  1674. // the default image from the export template will be used.
  1675. for (int i = 0; i < ICON_DENSITIES_COUNT; ++i) {
  1676. if (p_main_image.is_valid() && !p_main_image->is_empty()) {
  1677. print_verbose("Processing launcher icon for dimension " + itos(LAUNCHER_ICONS[i].dimensions) + " into " + LAUNCHER_ICONS[i].export_path);
  1678. Vector<uint8_t> data;
  1679. _process_launcher_icons(LAUNCHER_ICONS[i].export_path, p_main_image, LAUNCHER_ICONS[i].dimensions, data);
  1680. store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ICONS[i].export_path), data);
  1681. }
  1682. if (p_foreground.is_valid() && !p_foreground->is_empty()) {
  1683. print_verbose("Processing launcher adaptive icon p_foreground for dimension " + itos(LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].dimensions) + " into " + LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path);
  1684. Vector<uint8_t> data;
  1685. _process_launcher_icons(LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path, p_foreground,
  1686. LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].dimensions, data);
  1687. store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path), data);
  1688. }
  1689. if (p_background.is_valid() && !p_background->is_empty()) {
  1690. print_verbose("Processing launcher adaptive icon p_background for dimension " + itos(LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].dimensions) + " into " + LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path);
  1691. Vector<uint8_t> data;
  1692. _process_launcher_icons(LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path, p_background,
  1693. LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].dimensions, data);
  1694. store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path), data);
  1695. }
  1696. if (p_monochrome.is_valid() && !p_monochrome->is_empty()) {
  1697. print_verbose("Processing launcher adaptive icon p_monochrome for dimension " + itos(LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].dimensions) + " into " + LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path);
  1698. Vector<uint8_t> data;
  1699. _process_launcher_icons(LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path, p_monochrome,
  1700. LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].dimensions, data);
  1701. store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path), data);
  1702. monochrome_tag = " <monochrome android:drawable=\"@mipmap/icon_monochrome\"/>\n";
  1703. }
  1704. }
  1705. // Finalize the icon.xml by formatting the template with the optional monochrome tag.
  1706. store_string_at_path(gradle_build_dir.path_join(ICON_XML_PATH), vformat(ICON_XML_TEMPLATE, monochrome_tag));
  1707. }
  1708. Vector<EditorExportPlatformAndroid::ABI> EditorExportPlatformAndroid::get_enabled_abis(const Ref<EditorExportPreset> &p_preset) {
  1709. Vector<ABI> abis = get_abis();
  1710. Vector<ABI> enabled_abis;
  1711. for (int i = 0; i < abis.size(); ++i) {
  1712. bool is_enabled = p_preset->get("architectures/" + abis[i].abi);
  1713. if (is_enabled) {
  1714. enabled_abis.push_back(abis[i]);
  1715. }
  1716. }
  1717. return enabled_abis;
  1718. }
  1719. void EditorExportPlatformAndroid::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
  1720. r_features->push_back("etc2");
  1721. r_features->push_back("astc");
  1722. if (p_preset->get("shader_baker/enabled")) {
  1723. r_features->push_back("shader_baker");
  1724. }
  1725. Vector<ABI> abis = get_enabled_abis(p_preset);
  1726. for (int i = 0; i < abis.size(); ++i) {
  1727. r_features->push_back(abis[i].arch);
  1728. }
  1729. }
  1730. String EditorExportPlatformAndroid::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const {
  1731. if (p_preset) {
  1732. if (p_name == ("apk_expansion/public_key")) {
  1733. bool apk_expansion = p_preset->get("apk_expansion/enable");
  1734. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  1735. if (apk_expansion && apk_expansion_pkey.is_empty()) {
  1736. return TTR("Invalid public key for APK expansion.");
  1737. }
  1738. } else if (p_name == "package/unique_name") {
  1739. String pn = p_preset->get("package/unique_name");
  1740. String pn_err;
  1741. if (!is_package_name_valid(Ref<EditorExportPreset>(p_preset), pn, &pn_err)) {
  1742. return TTR("Invalid package name:") + " " + pn_err;
  1743. }
  1744. } else if (p_name == "gesture/swipe_to_dismiss") {
  1745. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1746. if (bool(p_preset->get("gesture/swipe_to_dismiss")) && !gradle_build_enabled) {
  1747. return TTR("\"Use Gradle Build\" is required to enable \"Swipe to dismiss\".");
  1748. }
  1749. } else if (p_name == "gradle_build/use_gradle_build") {
  1750. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1751. String enabled_deprecated_plugins_names = _get_deprecated_plugins_names(Ref<EditorExportPreset>(p_preset));
  1752. if (!enabled_deprecated_plugins_names.is_empty() && !gradle_build_enabled) {
  1753. return TTR("\"Use Gradle Build\" must be enabled to use the plugins.");
  1754. }
  1755. } else if (p_name == "gradle_build/export_format") {
  1756. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1757. if (int(p_preset->get("gradle_build/export_format")) == EXPORT_FORMAT_AAB && !gradle_build_enabled) {
  1758. return TTR("\"Export AAB\" is only valid when \"Use Gradle Build\" is enabled.");
  1759. }
  1760. } else if (p_name == "gradle_build/min_sdk") {
  1761. String min_sdk_str = p_preset->get("gradle_build/min_sdk");
  1762. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1763. if (!min_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  1764. if (!gradle_build_enabled) {
  1765. return TTR("\"Min SDK\" can only be overridden when \"Use Gradle Build\" is enabled.");
  1766. }
  1767. if (!min_sdk_str.is_valid_int()) {
  1768. return vformat(TTR("\"Min SDK\" should be a valid integer, but got \"%s\" which is invalid."), min_sdk_str);
  1769. } else {
  1770. int min_sdk_int = min_sdk_str.to_int();
  1771. if (min_sdk_int < DEFAULT_MIN_SDK_VERSION) {
  1772. return vformat(TTR("\"Min SDK\" cannot be lower than %d, which is the version needed by the Godot library."), DEFAULT_MIN_SDK_VERSION);
  1773. }
  1774. }
  1775. }
  1776. } else if (p_name == "gradle_build/target_sdk") {
  1777. String target_sdk_str = p_preset->get("gradle_build/target_sdk");
  1778. int target_sdk_int = DEFAULT_TARGET_SDK_VERSION;
  1779. String min_sdk_str = p_preset->get("gradle_build/min_sdk");
  1780. int min_sdk_int = DEFAULT_MIN_SDK_VERSION;
  1781. if (min_sdk_str.is_valid_int()) {
  1782. min_sdk_int = min_sdk_str.to_int();
  1783. }
  1784. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1785. if (!target_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  1786. if (!gradle_build_enabled) {
  1787. return TTR("\"Target SDK\" can only be overridden when \"Use Gradle Build\" is enabled.");
  1788. }
  1789. if (!target_sdk_str.is_valid_int()) {
  1790. return vformat(TTR("\"Target SDK\" should be a valid integer, but got \"%s\" which is invalid."), target_sdk_str);
  1791. } else {
  1792. target_sdk_int = target_sdk_str.to_int();
  1793. if (target_sdk_int < min_sdk_int) {
  1794. return TTR("\"Target SDK\" version must be greater or equal to \"Min SDK\" version.");
  1795. }
  1796. }
  1797. }
  1798. } else if (p_name == "gradle_build/custom_theme_attributes") {
  1799. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1800. if (bool(p_preset->get("gradle_build/custom_theme_attributes")) && !gradle_build_enabled) {
  1801. return TTR("\"Use Gradle Build\" is required to add custom theme attributes.");
  1802. }
  1803. } else if (p_name == "package/show_in_android_tv") {
  1804. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1805. if (bool(p_preset->get("package/show_in_android_tv")) && !gradle_build_enabled) {
  1806. return TTR("\"Use Gradle Build\" must be enabled to enable \"Show In Android Tv\".");
  1807. }
  1808. } else if (p_name == "package/show_as_launcher_app") {
  1809. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1810. if (bool(p_preset->get("package/show_as_launcher_app")) && !gradle_build_enabled) {
  1811. return TTR("\"Use Gradle Build\" must be enabled to enable \"Show As Launcher App\".");
  1812. }
  1813. } else if (p_name == "package/show_in_app_library") {
  1814. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1815. if (!bool(p_preset->get("package/show_in_app_library")) && !gradle_build_enabled) {
  1816. return TTR("\"Use Gradle Build\" must be enabled to disable \"Show In App Library\".");
  1817. }
  1818. } else if (p_name == "shader_baker/enabled" && bool(p_preset->get("shader_baker/enabled"))) {
  1819. String export_renderer = GLOBAL_GET("rendering/renderer/rendering_method.mobile");
  1820. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  1821. return TTR("\"Shader Baker\" is not supported when using the Compatibility renderer.");
  1822. } else if (OS::get_singleton()->get_current_rendering_method() != export_renderer) {
  1823. return vformat(TTR("The editor is currently using a different renderer than what the target platform will use. \"Shader Baker\" won't be able to include core shaders. Switch to the \"%s\" renderer temporarily to fix this."), export_renderer);
  1824. }
  1825. }
  1826. }
  1827. return String();
  1828. }
  1829. void EditorExportPlatformAndroid::get_export_options(List<ExportOption> *r_options) const {
  1830. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), ""));
  1831. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), ""));
  1832. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gradle_build/use_gradle_build"), false, true, true));
  1833. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/gradle_build_directory", PROPERTY_HINT_PLACEHOLDER_TEXT, "res://android"), "", false, false));
  1834. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/android_source_template", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  1835. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "gradle_build/export_format", PROPERTY_HINT_ENUM, "Export APK,Export AAB"), EXPORT_FORMAT_APK, false, true));
  1836. // Using String instead of int to default to an empty string (no override) with placeholder for instructions (see GH-62465).
  1837. // This implies doing validation that the string is a proper int.
  1838. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/min_sdk", PROPERTY_HINT_PLACEHOLDER_TEXT, vformat("%d (default)", DEFAULT_MIN_SDK_VERSION)), "", false, true));
  1839. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/target_sdk", PROPERTY_HINT_PLACEHOLDER_TEXT, vformat("%d (default)", DEFAULT_TARGET_SDK_VERSION)), "", false, true));
  1840. r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "gradle_build/custom_theme_attributes", PROPERTY_HINT_DICTIONARY_TYPE, "String;String"), Dictionary()));
  1841. #ifndef DISABLE_DEPRECATED
  1842. Vector<PluginConfigAndroid> plugins_configs = get_plugins();
  1843. for (int i = 0; i < plugins_configs.size(); i++) {
  1844. print_verbose("Found Android plugin " + plugins_configs[i].name);
  1845. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), plugins_configs[i].name)), false));
  1846. }
  1847. android_plugins_changed.clear();
  1848. #endif // DISABLE_DEPRECATED
  1849. // Android supports multiple architectures in an app bundle, so
  1850. // we expose each option as a checkbox in the export dialog.
  1851. const Vector<ABI> abis = get_abis();
  1852. for (int i = 0; i < abis.size(); ++i) {
  1853. const String abi = abis[i].abi;
  1854. // All Android devices supporting Vulkan run 64-bit Android,
  1855. // so there is usually no point in exporting for 32-bit Android.
  1856. const bool is_default = abi == "arm64-v8a";
  1857. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), abi)), is_default));
  1858. }
  1859. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug", PROPERTY_HINT_GLOBAL_FILE, "*.keystore,*.jks", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1860. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_user", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1861. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_password", PROPERTY_HINT_PASSWORD, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1862. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release", PROPERTY_HINT_GLOBAL_FILE, "*.keystore,*.jks", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1863. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_user", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1864. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_password", PROPERTY_HINT_PASSWORD, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1865. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/code", PROPERTY_HINT_RANGE, "1,4096,1,or_greater"), 1));
  1866. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "version/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  1867. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "ext.domain.name"), "com.example.$genname", false, true));
  1868. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name [default if blank]"), ""));
  1869. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/signed"), true));
  1870. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "package/app_category", PROPERTY_HINT_ENUM, "Accessibility,Audio,Game,Image,Maps,News,Productivity,Social,Video,Undefined"), APP_CATEGORY_GAME));
  1871. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/retain_data_on_uninstall"), false));
  1872. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/exclude_from_recents"), false));
  1873. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_in_android_tv"), false));
  1874. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_in_app_library"), true));
  1875. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_as_launcher_app"), false));
  1876. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ICON_OPTION, PROPERTY_HINT_FILE, "*.png"), ""));
  1877. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ADAPTIVE_ICON_FOREGROUND_OPTION, PROPERTY_HINT_FILE, "*.png"), ""));
  1878. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ADAPTIVE_ICON_BACKGROUND_OPTION, PROPERTY_HINT_FILE, "*.png"), ""));
  1879. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ADAPTIVE_ICON_MONOCHROME_OPTION, PROPERTY_HINT_FILE, "*.png"), ""));
  1880. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "graphics/opengl_debug"), false));
  1881. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "shader_baker/enabled"), false));
  1882. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "xr_features/xr_mode", PROPERTY_HINT_ENUM, "Regular,OpenXR"), XR_MODE_REGULAR, false, true));
  1883. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gesture/swipe_to_dismiss"), false));
  1884. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/immersive_mode"), true));
  1885. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/edge_to_edge"), false));
  1886. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_small"), true));
  1887. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_normal"), true));
  1888. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_large"), true));
  1889. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_xlarge"), true));
  1890. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "screen/background_color", PROPERTY_HINT_COLOR_NO_ALPHA), Color()));
  1891. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data_backup/allow"), false));
  1892. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));
  1893. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "apk_expansion/enable"), false, false, true));
  1894. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/SALT"), ""));
  1895. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/public_key", PROPERTY_HINT_MULTILINE_TEXT), "", false, true));
  1896. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "permissions/custom_permissions"), PackedStringArray()));
  1897. const char **perms = ANDROID_PERMS;
  1898. while (*perms) {
  1899. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("permissions"), String(*perms).to_lower())), false));
  1900. perms++;
  1901. }
  1902. }
  1903. bool EditorExportPlatformAndroid::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
  1904. if (p_preset == nullptr) {
  1905. return true;
  1906. }
  1907. bool advanced_options_enabled = p_preset->are_advanced_options_enabled();
  1908. if (p_option == "graphics/opengl_debug" ||
  1909. p_option == "gradle_build/custom_theme_attributes" ||
  1910. p_option == "command_line/extra_args" ||
  1911. p_option == "permissions/custom_permissions" ||
  1912. p_option == "keystore/debug" ||
  1913. p_option == "keystore/debug_user" ||
  1914. p_option == "keystore/debug_password" ||
  1915. p_option == "package/retain_data_on_uninstall" ||
  1916. p_option == "package/exclude_from_recents" ||
  1917. p_option == "package/show_in_app_library" ||
  1918. p_option == "package/show_as_launcher_app" ||
  1919. p_option == "gesture/swipe_to_dismiss" ||
  1920. p_option == "apk_expansion/enable" ||
  1921. p_option == "apk_expansion/SALT" ||
  1922. p_option == "apk_expansion/public_key") {
  1923. return advanced_options_enabled;
  1924. }
  1925. if (p_option == "gradle_build/gradle_build_directory" || p_option == "gradle_build/android_source_template") {
  1926. return advanced_options_enabled && bool(p_preset->get("gradle_build/use_gradle_build"));
  1927. }
  1928. if (p_option == "custom_template/debug" || p_option == "custom_template/release") {
  1929. // The APK templates are ignored if Gradle build is enabled.
  1930. return advanced_options_enabled && !bool(p_preset->get("gradle_build/use_gradle_build"));
  1931. }
  1932. // Hide .NET embedding option (always enabled).
  1933. if (p_option == "dotnet/embed_build_outputs") {
  1934. return false;
  1935. }
  1936. if (p_option == "dotnet/android_use_linux_bionic") {
  1937. return advanced_options_enabled;
  1938. }
  1939. return true;
  1940. }
  1941. String EditorExportPlatformAndroid::get_name() const {
  1942. return "Android";
  1943. }
  1944. String EditorExportPlatformAndroid::get_os_name() const {
  1945. return "Android";
  1946. }
  1947. Ref<Texture2D> EditorExportPlatformAndroid::get_logo() const {
  1948. return logo;
  1949. }
  1950. bool EditorExportPlatformAndroid::should_update_export_options() {
  1951. #ifndef DISABLE_DEPRECATED
  1952. if (android_plugins_changed.is_set()) {
  1953. // don't clear unless we're reporting true, to avoid race
  1954. android_plugins_changed.clear();
  1955. return true;
  1956. }
  1957. #endif // DISABLE_DEPRECATED
  1958. return false;
  1959. }
  1960. bool EditorExportPlatformAndroid::poll_export() {
  1961. bool dc = devices_changed.is_set();
  1962. if (dc) {
  1963. // don't clear unless we're reporting true, to avoid race
  1964. devices_changed.clear();
  1965. }
  1966. return dc;
  1967. }
  1968. int EditorExportPlatformAndroid::get_options_count() const {
  1969. MutexLock lock(device_lock);
  1970. return devices.size();
  1971. }
  1972. String EditorExportPlatformAndroid::get_options_tooltip() const {
  1973. return TTR("Select device from the list");
  1974. }
  1975. String EditorExportPlatformAndroid::get_option_label(int p_index) const {
  1976. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1977. MutexLock lock(device_lock);
  1978. return devices[p_index].name;
  1979. }
  1980. String EditorExportPlatformAndroid::get_option_tooltip(int p_index) const {
  1981. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1982. MutexLock lock(device_lock);
  1983. String s = devices[p_index].description;
  1984. if (devices.size() == 1) {
  1985. // Tooltip will be:
  1986. // Name
  1987. // Description
  1988. s = devices[p_index].name + "\n\n" + s;
  1989. }
  1990. return s;
  1991. }
  1992. String EditorExportPlatformAndroid::get_device_architecture(int p_index) const {
  1993. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1994. MutexLock lock(device_lock);
  1995. return devices[p_index].architecture;
  1996. }
  1997. Error EditorExportPlatformAndroid::run(const Ref<EditorExportPreset> &p_preset, int p_device, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) {
  1998. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  1999. String can_export_error;
  2000. bool can_export_missing_templates;
  2001. if (!can_export(p_preset, can_export_error, can_export_missing_templates)) {
  2002. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), can_export_error);
  2003. return ERR_UNCONFIGURED;
  2004. }
  2005. MutexLock lock(device_lock);
  2006. EditorProgress ep("run", vformat(TTR("Running on %s"), devices[p_device].name), 3);
  2007. String adb = get_adb_path();
  2008. // Export_temp APK.
  2009. if (ep.step(TTR("Exporting APK..."), 0)) {
  2010. return ERR_SKIP;
  2011. }
  2012. const bool use_wifi_for_remote_debug = EDITOR_GET("export/android/use_wifi_for_remote_debug");
  2013. const bool use_remote = p_debug_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG) || p_debug_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT);
  2014. const bool use_reverse = devices[p_device].api_level >= 21 && !use_wifi_for_remote_debug;
  2015. if (use_reverse) {
  2016. p_debug_flags.set_flag(DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST);
  2017. }
  2018. String tmp_export_path = EditorPaths::get_singleton()->get_temp_dir().path_join("tmpexport." + uitos(OS::get_singleton()->get_unix_time()) + ".apk");
  2019. #define CLEANUP_AND_RETURN(m_err) \
  2020. { \
  2021. DirAccess::remove_file_or_error(tmp_export_path); \
  2022. if (FileAccess::exists(tmp_export_path + ".idsig")) { \
  2023. DirAccess::remove_file_or_error(tmp_export_path + ".idsig"); \
  2024. } \
  2025. return m_err; \
  2026. } \
  2027. ((void)0)
  2028. // Export to temporary APK before sending to device.
  2029. Error err = export_project_helper(p_preset, true, tmp_export_path, EXPORT_FORMAT_APK, true, p_debug_flags);
  2030. if (err != OK) {
  2031. CLEANUP_AND_RETURN(err);
  2032. }
  2033. List<String> args;
  2034. int rv;
  2035. String output;
  2036. bool remove_prev = EDITOR_GET("export/android/one_click_deploy_clear_previous_install");
  2037. String version_name = p_preset->get_version("version/name");
  2038. String package_name = p_preset->get("package/unique_name");
  2039. if (remove_prev) {
  2040. if (ep.step(TTR("Uninstalling..."), 1)) {
  2041. CLEANUP_AND_RETURN(ERR_SKIP);
  2042. }
  2043. print_line("Uninstalling previous version: " + devices[p_device].name);
  2044. args.push_back("-s");
  2045. args.push_back(devices[p_device].id);
  2046. args.push_back("uninstall");
  2047. if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device].api_level >= 17) {
  2048. args.push_back("--user");
  2049. args.push_back("0");
  2050. }
  2051. args.push_back(get_package_name(p_preset, package_name));
  2052. output.clear();
  2053. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2054. print_verbose(output);
  2055. }
  2056. print_line("Installing to device (please wait...): " + devices[p_device].name);
  2057. if (ep.step(TTR("Installing to device, please wait..."), 2)) {
  2058. CLEANUP_AND_RETURN(ERR_SKIP);
  2059. }
  2060. args.clear();
  2061. args.push_back("-s");
  2062. args.push_back(devices[p_device].id);
  2063. args.push_back("install");
  2064. if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device].api_level >= 17) {
  2065. args.push_back("--user");
  2066. args.push_back("0");
  2067. }
  2068. args.push_back("-r");
  2069. args.push_back(tmp_export_path);
  2070. output.clear();
  2071. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2072. print_verbose(output);
  2073. if (err || rv != 0) {
  2074. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Could not install to device: %s"), output));
  2075. CLEANUP_AND_RETURN(ERR_CANT_CREATE);
  2076. }
  2077. if (use_remote) {
  2078. if (use_reverse) {
  2079. static const char *const msg = "--- Device API >= 21; debugging over USB ---";
  2080. EditorNode::get_singleton()->get_log()->add_message(msg, EditorLog::MSG_TYPE_EDITOR);
  2081. print_line(String(msg).to_upper());
  2082. args.clear();
  2083. args.push_back("-s");
  2084. args.push_back(devices[p_device].id);
  2085. args.push_back("reverse");
  2086. args.push_back("--remove-all");
  2087. output.clear();
  2088. OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2089. print_verbose(output);
  2090. if (p_debug_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG)) {
  2091. int dbg_port = EDITOR_GET("network/debug/remote_port");
  2092. args.clear();
  2093. args.push_back("-s");
  2094. args.push_back(devices[p_device].id);
  2095. args.push_back("reverse");
  2096. args.push_back("tcp:" + itos(dbg_port));
  2097. args.push_back("tcp:" + itos(dbg_port));
  2098. output.clear();
  2099. OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2100. print_verbose(output);
  2101. print_line("Reverse result: " + itos(rv));
  2102. }
  2103. if (p_debug_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) {
  2104. int fs_port = EDITOR_GET("filesystem/file_server/port");
  2105. args.clear();
  2106. args.push_back("-s");
  2107. args.push_back(devices[p_device].id);
  2108. args.push_back("reverse");
  2109. args.push_back("tcp:" + itos(fs_port));
  2110. args.push_back("tcp:" + itos(fs_port));
  2111. output.clear();
  2112. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2113. print_verbose(output);
  2114. print_line("Reverse result2: " + itos(rv));
  2115. }
  2116. } else {
  2117. static const char *const api_version_msg = "--- Device API < 21; debugging over Wi-Fi ---";
  2118. static const char *const manual_override_msg = "--- Wi-Fi remote debug enabled in project settings; debugging over Wi-Fi ---";
  2119. const char *const msg = use_wifi_for_remote_debug ? manual_override_msg : api_version_msg;
  2120. EditorNode::get_singleton()->get_log()->add_message(msg, EditorLog::MSG_TYPE_EDITOR);
  2121. print_line(String(msg).to_upper());
  2122. }
  2123. }
  2124. if (ep.step(TTR("Running on device..."), 3)) {
  2125. CLEANUP_AND_RETURN(ERR_SKIP);
  2126. }
  2127. args.clear();
  2128. args.push_back("-s");
  2129. args.push_back(devices[p_device].id);
  2130. args.push_back("shell");
  2131. args.push_back("am");
  2132. args.push_back("start");
  2133. if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device].api_level >= 17) {
  2134. args.push_back("--user");
  2135. args.push_back("0");
  2136. }
  2137. args.push_back("-a");
  2138. args.push_back("android.intent.action.MAIN");
  2139. // Going with implicit launch first based on the LAUNCHER category and the app's package.
  2140. args.push_back("-c");
  2141. args.push_back("android.intent.category.LAUNCHER");
  2142. args.push_back(get_package_name(p_preset, package_name));
  2143. output.clear();
  2144. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2145. print_verbose(output);
  2146. if (err || rv != 0 || output.contains("Error: Activity not started")) {
  2147. // The implicit launch failed, let's try an explicit launch by specifying the component name before giving up.
  2148. const String component_name = get_package_name(p_preset, package_name) + "/com.godot.game.GodotApp";
  2149. print_line("Implicit launch failed.. Trying explicit launch using", component_name);
  2150. args.erase(get_package_name(p_preset, package_name));
  2151. args.push_back("-n");
  2152. args.push_back(component_name);
  2153. output.clear();
  2154. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  2155. print_verbose(output);
  2156. if (err || rv != 0 || output.begins_with("Error: Activity not started")) {
  2157. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Could not execute on device."));
  2158. CLEANUP_AND_RETURN(ERR_CANT_CREATE);
  2159. }
  2160. }
  2161. CLEANUP_AND_RETURN(OK);
  2162. #undef CLEANUP_AND_RETURN
  2163. }
  2164. Ref<Texture2D> EditorExportPlatformAndroid::get_run_icon() const {
  2165. return run_icon;
  2166. }
  2167. String EditorExportPlatformAndroid::get_java_path() {
  2168. String exe_ext;
  2169. if (OS::get_singleton()->get_name() == "Windows") {
  2170. exe_ext = ".exe";
  2171. }
  2172. String java_sdk_path = EDITOR_GET("export/android/java_sdk_path");
  2173. return java_sdk_path.path_join("bin/java" + exe_ext);
  2174. }
  2175. String EditorExportPlatformAndroid::get_keytool_path() {
  2176. String exe_ext;
  2177. if (OS::get_singleton()->get_name() == "Windows") {
  2178. exe_ext = ".exe";
  2179. }
  2180. String java_sdk_path = EDITOR_GET("export/android/java_sdk_path");
  2181. return java_sdk_path.path_join("bin/keytool" + exe_ext);
  2182. }
  2183. String EditorExportPlatformAndroid::get_adb_path() {
  2184. String exe_ext;
  2185. if (OS::get_singleton()->get_name() == "Windows") {
  2186. exe_ext = ".exe";
  2187. }
  2188. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  2189. return sdk_path.path_join("platform-tools/adb" + exe_ext);
  2190. }
  2191. String EditorExportPlatformAndroid::get_apksigner_path(int p_target_sdk, bool p_check_executes) {
  2192. if (p_target_sdk == -1) {
  2193. p_target_sdk = DEFAULT_TARGET_SDK_VERSION;
  2194. }
  2195. String exe_ext;
  2196. if (OS::get_singleton()->get_name() == "Windows") {
  2197. exe_ext = ".bat";
  2198. }
  2199. String apksigner_command_name = "apksigner" + exe_ext;
  2200. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  2201. String apksigner_path;
  2202. Error errn;
  2203. String build_tools_dir = sdk_path.path_join("build-tools");
  2204. Ref<DirAccess> da = DirAccess::open(build_tools_dir, &errn);
  2205. if (errn != OK) {
  2206. print_error("Unable to open Android 'build-tools' directory.");
  2207. return apksigner_path;
  2208. }
  2209. // There are additional versions directories we need to go through.
  2210. Vector<String> dir_list = da->get_directories();
  2211. // We need to use the version of build_tools that matches the Target SDK
  2212. // If somehow we can't find that, we see if a version between 28 and the default target SDK exists.
  2213. // We need to avoid versions <= 27 because they fail on Java versions >9
  2214. // If we can't find that, we just use the first valid version.
  2215. Vector<String> ideal_versions;
  2216. Vector<String> other_versions;
  2217. Vector<String> versions;
  2218. bool found_target_sdk = false;
  2219. // We only allow for versions <= 27 if specifically set
  2220. int min_version = p_target_sdk <= 27 ? p_target_sdk : 28;
  2221. for (String sub_dir : dir_list) {
  2222. if (!sub_dir.begins_with(".")) {
  2223. Vector<String> ver_numbers = sub_dir.split(".");
  2224. // Dir not a version number, will use as last resort
  2225. if (!ver_numbers.size() || !ver_numbers[0].is_valid_int()) {
  2226. other_versions.push_back(sub_dir);
  2227. continue;
  2228. }
  2229. int ver_number = ver_numbers[0].to_int();
  2230. if (ver_number == p_target_sdk) {
  2231. found_target_sdk = true;
  2232. //ensure this is in front of the ones we check
  2233. versions.push_back(sub_dir);
  2234. } else {
  2235. if (ver_number >= min_version && ver_number <= DEFAULT_TARGET_SDK_VERSION) {
  2236. ideal_versions.push_back(sub_dir);
  2237. } else {
  2238. other_versions.push_back(sub_dir);
  2239. }
  2240. }
  2241. }
  2242. }
  2243. // we will check ideal versions first, then other versions.
  2244. versions.append_array(ideal_versions);
  2245. versions.append_array(other_versions);
  2246. if (!versions.size()) {
  2247. print_error("Unable to find the 'apksigner' tool.");
  2248. return apksigner_path;
  2249. }
  2250. int i;
  2251. bool failed = false;
  2252. String version_to_use;
  2253. String java_sdk_path = EDITOR_GET("export/android/java_sdk_path");
  2254. if (!java_sdk_path.is_empty()) {
  2255. OS::get_singleton()->set_environment("JAVA_HOME", java_sdk_path);
  2256. #ifdef UNIX_ENABLED
  2257. String env_path = OS::get_singleton()->get_environment("PATH");
  2258. if (!env_path.contains(java_sdk_path)) {
  2259. OS::get_singleton()->set_environment("PATH", java_sdk_path + "/bin:" + env_path);
  2260. }
  2261. #endif
  2262. }
  2263. List<String> args;
  2264. args.push_back("--version");
  2265. String output;
  2266. int retval;
  2267. Error err;
  2268. for (i = 0; i < versions.size(); i++) {
  2269. // Check if the tool is here.
  2270. apksigner_path = build_tools_dir.path_join(versions[i]).path_join(apksigner_command_name);
  2271. if (FileAccess::exists(apksigner_path)) {
  2272. version_to_use = versions[i];
  2273. // If we aren't exporting, just break here.
  2274. if (!p_check_executes) {
  2275. break;
  2276. }
  2277. // we only check to see if it executes on export because it is slow to load
  2278. err = OS::get_singleton()->execute(apksigner_path, args, &output, &retval, false);
  2279. if (err || retval) {
  2280. failed = true;
  2281. } else {
  2282. break;
  2283. }
  2284. }
  2285. }
  2286. if (i == versions.size()) {
  2287. if (failed) {
  2288. print_error("All located 'apksigner' tools in " + build_tools_dir + " failed to execute");
  2289. return "<FAILED>";
  2290. } else {
  2291. print_error("Unable to find the 'apksigner' tool.");
  2292. return "";
  2293. }
  2294. }
  2295. if (!found_target_sdk) {
  2296. print_line("Could not find version of build tools that matches Target SDK, using " + version_to_use);
  2297. } else if (failed && found_target_sdk) {
  2298. print_line("Version of build tools that matches Target SDK failed to execute, using " + version_to_use);
  2299. }
  2300. return apksigner_path;
  2301. }
  2302. static bool has_valid_keystore_credentials(String &r_error_str, const String &p_keystore, const String &p_username, const String &p_password, const String &p_type) {
  2303. String output;
  2304. List<String> args;
  2305. args.push_back("-list");
  2306. args.push_back("-keystore");
  2307. args.push_back(p_keystore);
  2308. args.push_back("-storepass");
  2309. args.push_back(p_password);
  2310. args.push_back("-alias");
  2311. args.push_back(p_username);
  2312. String keytool_path = EditorExportPlatformAndroid::get_keytool_path();
  2313. Error error = OS::get_singleton()->execute(keytool_path, args, &output, nullptr, true);
  2314. String keytool_error = "keytool error:";
  2315. bool valid = output.substr(0, keytool_error.length()) != keytool_error;
  2316. if (error != OK) {
  2317. r_error_str = TTR("Error: There was a problem validating the keystore username and password");
  2318. return false;
  2319. }
  2320. if (!valid) {
  2321. r_error_str = TTR(p_type + " Username and/or Password is invalid for the given " + p_type + " Keystore");
  2322. return false;
  2323. }
  2324. r_error_str = "";
  2325. return true;
  2326. }
  2327. bool EditorExportPlatformAndroid::has_valid_username_and_password(const Ref<EditorExportPreset> &p_preset, String &r_error) {
  2328. String dk = _get_keystore_path(p_preset, true);
  2329. String dk_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  2330. String dk_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  2331. String rk = _get_keystore_path(p_preset, false);
  2332. String rk_user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  2333. String rk_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  2334. bool valid = true;
  2335. if (!dk.is_empty() && !dk_user.is_empty() && !dk_password.is_empty()) {
  2336. String err = "";
  2337. valid = has_valid_keystore_credentials(err, dk, dk_user, dk_password, "Debug");
  2338. r_error += err;
  2339. }
  2340. if (!rk.is_empty() && !rk_user.is_empty() && !rk_password.is_empty()) {
  2341. String err = "";
  2342. valid = has_valid_keystore_credentials(err, rk, rk_user, rk_password, "Release");
  2343. r_error += err;
  2344. }
  2345. return valid;
  2346. }
  2347. #ifdef MODULE_MONO_ENABLED
  2348. static uint64_t _last_validate_tfm_time = 0;
  2349. static String _last_validate_tfm = "";
  2350. bool _validate_dotnet_tfm(const String &required_tfm, String &r_error) {
  2351. String assembly_name = Path::get_csharp_project_name();
  2352. String project_path = ProjectSettings::get_singleton()->globalize_path("res://" + assembly_name + ".csproj");
  2353. if (!FileAccess::exists(project_path)) {
  2354. return true;
  2355. }
  2356. uint64_t modified_time = FileAccess::get_modified_time(project_path);
  2357. String tfm;
  2358. if (modified_time == _last_validate_tfm_time) {
  2359. tfm = _last_validate_tfm;
  2360. } else {
  2361. String pipe;
  2362. List<String> args;
  2363. args.push_back("build");
  2364. args.push_back(project_path);
  2365. args.push_back("--getProperty:TargetFramework");
  2366. int exitcode;
  2367. Error err = OS::get_singleton()->execute("dotnet", args, &pipe, &exitcode, true);
  2368. if (err != OK || exitcode != 0) {
  2369. if (err != OK) {
  2370. WARN_PRINT("Failed to execute dotnet command. Error " + String(error_names[err]));
  2371. } else if (exitcode != 0) {
  2372. print_line(pipe);
  2373. WARN_PRINT("dotnet command exited with code " + itos(exitcode) + ". See output above for more details.");
  2374. }
  2375. r_error += vformat(TTR("Unable to determine the C# project's TFM, it may be incompatible. The export template only supports '%s'. Make sure the project targets '%s' or consider using gradle builds instead."), required_tfm, required_tfm) + "\n";
  2376. return true;
  2377. } else {
  2378. tfm = pipe.strip_edges();
  2379. _last_validate_tfm_time = modified_time;
  2380. _last_validate_tfm = tfm;
  2381. }
  2382. }
  2383. if (tfm != required_tfm) {
  2384. r_error += vformat(TTR("C# project targets '%s' but the export template only supports '%s'. Consider using gradle builds instead."), tfm, required_tfm) + "\n";
  2385. return false;
  2386. }
  2387. return true;
  2388. }
  2389. #endif
  2390. bool EditorExportPlatformAndroid::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  2391. String err;
  2392. bool valid = false;
  2393. const bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  2394. #ifdef MODULE_MONO_ENABLED
  2395. // Android export is still a work in progress, keep a message as a warning.
  2396. err += TTR("Exporting to Android when using C#/.NET is experimental.") + "\n";
  2397. if (!gradle_build_enabled) {
  2398. // For template exports we only support .NET 8 because the template
  2399. // includes .jar dependencies that may only be compatible with .NET 8.
  2400. if (!_validate_dotnet_tfm("net8.0", err)) {
  2401. r_error = err;
  2402. return false;
  2403. }
  2404. }
  2405. #endif
  2406. // Look for export templates (first official, and if defined custom templates).
  2407. if (!gradle_build_enabled) {
  2408. String template_err;
  2409. bool dvalid = false;
  2410. bool rvalid = false;
  2411. bool has_export_templates = false;
  2412. if (p_preset->get("custom_template/debug") != "") {
  2413. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  2414. if (!dvalid) {
  2415. template_err += TTR("Custom debug template not found.") + "\n";
  2416. }
  2417. has_export_templates |= dvalid;
  2418. } else {
  2419. has_export_templates |= exists_export_template("android_debug.apk", &template_err);
  2420. }
  2421. if (p_preset->get("custom_template/release") != "") {
  2422. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  2423. if (!rvalid) {
  2424. template_err += TTR("Custom release template not found.") + "\n";
  2425. }
  2426. has_export_templates |= rvalid;
  2427. } else {
  2428. has_export_templates |= exists_export_template("android_release.apk", &template_err);
  2429. }
  2430. r_missing_templates = !has_export_templates;
  2431. valid = dvalid || rvalid || has_export_templates;
  2432. if (!valid) {
  2433. err += template_err;
  2434. }
  2435. } else {
  2436. #ifdef ANDROID_ENABLED
  2437. err += TTR("Gradle build is not supported for the Android editor.") + "\n";
  2438. valid = false;
  2439. #else
  2440. // Validate the custom gradle android source template.
  2441. bool android_source_template_valid = false;
  2442. const String android_source_template = p_preset->get("gradle_build/android_source_template");
  2443. if (!android_source_template.is_empty()) {
  2444. android_source_template_valid = FileAccess::exists(android_source_template);
  2445. if (!android_source_template_valid) {
  2446. err += TTR("Custom Android source template not found.") + "\n";
  2447. }
  2448. }
  2449. // Validate the installed build template.
  2450. bool installed_android_build_template = FileAccess::exists(ExportTemplateManager::get_android_build_directory(p_preset).path_join("build.gradle"));
  2451. if (!installed_android_build_template) {
  2452. if (!android_source_template_valid) {
  2453. r_missing_templates = !exists_export_template("android_source.zip", &err);
  2454. }
  2455. err += TTR("Android build template not installed in the project. Install it from the Project menu.") + "\n";
  2456. } else {
  2457. r_missing_templates = false;
  2458. }
  2459. valid = installed_android_build_template && !r_missing_templates;
  2460. #endif
  2461. }
  2462. // Validate the rest of the export configuration.
  2463. String dk = _get_keystore_path(p_preset, true);
  2464. String dk_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  2465. String dk_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  2466. if ((dk.is_empty() || dk_user.is_empty() || dk_password.is_empty()) && (!dk.is_empty() || !dk_user.is_empty() || !dk_password.is_empty())) {
  2467. valid = false;
  2468. err += TTR("Either Debug Keystore, Debug User AND Debug Password settings must be configured OR none of them.") + "\n";
  2469. }
  2470. // Use OR to make the export UI able to show this error.
  2471. if ((p_debug || !dk.is_empty()) && !FileAccess::exists(dk)) {
  2472. dk = EDITOR_GET("export/android/debug_keystore");
  2473. if (!FileAccess::exists(dk)) {
  2474. valid = false;
  2475. err += TTR("Debug keystore not configured in the Editor Settings nor in the preset.") + "\n";
  2476. }
  2477. }
  2478. String rk = _get_keystore_path(p_preset, false);
  2479. String rk_user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  2480. String rk_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  2481. if ((rk.is_empty() || rk_user.is_empty() || rk_password.is_empty()) && (!rk.is_empty() || !rk_user.is_empty() || !rk_password.is_empty())) {
  2482. valid = false;
  2483. err += TTR("Either Release Keystore, Release User AND Release Password settings must be configured OR none of them.") + "\n";
  2484. }
  2485. if (!p_debug && !rk.is_empty() && !FileAccess::exists(rk)) {
  2486. valid = false;
  2487. err += TTR("Release keystore incorrectly configured in the export preset.") + "\n";
  2488. }
  2489. #ifndef ANDROID_ENABLED
  2490. String java_sdk_path = EDITOR_GET("export/android/java_sdk_path");
  2491. if (java_sdk_path.is_empty()) {
  2492. err += TTR("A valid Java SDK path is required in Editor Settings.") + "\n";
  2493. valid = false;
  2494. } else {
  2495. // Validate the given path by checking that `java` is present under the `bin` directory.
  2496. Error errn;
  2497. // Check for the bin directory.
  2498. Ref<DirAccess> da = DirAccess::open(java_sdk_path.path_join("bin"), &errn);
  2499. if (errn != OK) {
  2500. err += TTR("Invalid Java SDK path in Editor Settings.") + " ";
  2501. err += TTR("Missing 'bin' directory!");
  2502. err += "\n";
  2503. valid = false;
  2504. } else {
  2505. // Check for the `java` command.
  2506. String java_path = get_java_path();
  2507. if (!FileAccess::exists(java_path)) {
  2508. err += TTR("Unable to find 'java' command using the Java SDK path.") + " ";
  2509. err += TTR("Please check the Java SDK directory specified in Editor Settings.");
  2510. err += "\n";
  2511. valid = false;
  2512. }
  2513. }
  2514. }
  2515. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  2516. if (sdk_path.is_empty()) {
  2517. err += TTR("A valid Android SDK path is required in Editor Settings.") + "\n";
  2518. valid = false;
  2519. } else {
  2520. Error errn;
  2521. // Check for the platform-tools directory.
  2522. Ref<DirAccess> da = DirAccess::open(sdk_path.path_join("platform-tools"), &errn);
  2523. if (errn != OK) {
  2524. err += TTR("Invalid Android SDK path in Editor Settings.") + " ";
  2525. err += TTR("Missing 'platform-tools' directory!");
  2526. err += "\n";
  2527. valid = false;
  2528. }
  2529. // Validate that adb is available.
  2530. String adb_path = get_adb_path();
  2531. if (!FileAccess::exists(adb_path)) {
  2532. err += TTR("Unable to find Android SDK platform-tools' adb command.") + " ";
  2533. err += TTR("Please check in the Android SDK directory specified in Editor Settings.");
  2534. err += "\n";
  2535. valid = false;
  2536. }
  2537. // Check for the build-tools directory.
  2538. Ref<DirAccess> build_tools_da = DirAccess::open(sdk_path.path_join("build-tools"), &errn);
  2539. if (errn != OK) {
  2540. err += TTR("Invalid Android SDK path in Editor Settings.") + " ";
  2541. err += TTR("Missing 'build-tools' directory!");
  2542. err += "\n";
  2543. valid = false;
  2544. }
  2545. String target_sdk_version = p_preset->get("gradle_build/target_sdk");
  2546. if (!target_sdk_version.is_valid_int()) {
  2547. target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION);
  2548. }
  2549. // Validate that apksigner is available.
  2550. String apksigner_path = get_apksigner_path(target_sdk_version.to_int());
  2551. if (!FileAccess::exists(apksigner_path)) {
  2552. err += TTR("Unable to find Android SDK build-tools' apksigner command.") + " ";
  2553. err += TTR("Please check in the Android SDK directory specified in Editor Settings.");
  2554. err += "\n";
  2555. valid = false;
  2556. }
  2557. }
  2558. #endif
  2559. if (!err.is_empty()) {
  2560. r_error = err;
  2561. }
  2562. return valid;
  2563. }
  2564. bool EditorExportPlatformAndroid::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  2565. String err;
  2566. bool valid = true;
  2567. List<ExportOption> options;
  2568. get_export_options(&options);
  2569. for (const EditorExportPlatform::ExportOption &E : options) {
  2570. if (get_export_option_visibility(p_preset.ptr(), E.option.name)) {
  2571. String warn = get_export_option_warning(p_preset.ptr(), E.option.name);
  2572. if (!warn.is_empty()) {
  2573. err += warn + "\n";
  2574. if (E.required) {
  2575. valid = false;
  2576. }
  2577. }
  2578. }
  2579. }
  2580. if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
  2581. valid = false;
  2582. }
  2583. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  2584. if (gradle_build_enabled) {
  2585. String build_version_path = ExportTemplateManager::get_android_build_directory(p_preset).get_base_dir().path_join(".build_version");
  2586. Ref<FileAccess> f = FileAccess::open(build_version_path, FileAccess::READ);
  2587. if (f.is_valid()) {
  2588. String current_version = ExportTemplateManager::get_android_template_identifier(p_preset);
  2589. String installed_version = f->get_line().strip_edges();
  2590. if (current_version != installed_version) {
  2591. err += vformat(TTR(MISMATCHED_VERSIONS_MESSAGE), installed_version, current_version);
  2592. err += "\n";
  2593. }
  2594. }
  2595. } else {
  2596. if (_is_transparency_allowed(p_preset)) {
  2597. // Warning only, so don't override `valid`.
  2598. err += vformat(TTR("\"Use Gradle Build\" is required for transparent background on Android"));
  2599. err += "\n";
  2600. }
  2601. }
  2602. String target_sdk_str = p_preset->get("gradle_build/target_sdk");
  2603. int target_sdk_int = DEFAULT_TARGET_SDK_VERSION;
  2604. if (!target_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  2605. if (target_sdk_str.is_valid_int()) {
  2606. target_sdk_int = target_sdk_str.to_int();
  2607. if (target_sdk_int > DEFAULT_TARGET_SDK_VERSION) {
  2608. // Warning only, so don't override `valid`.
  2609. err += vformat(TTR("\"Target SDK\" %d is higher than the default version %d. This may work, but wasn't tested and may be unstable."), target_sdk_int, DEFAULT_TARGET_SDK_VERSION);
  2610. err += "\n";
  2611. }
  2612. }
  2613. }
  2614. String current_renderer = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");
  2615. if (current_renderer == "forward_plus") {
  2616. // Warning only, so don't override `valid`.
  2617. err += vformat(TTR("The \"%s\" renderer is designed for Desktop devices, and is not suitable for Android devices."), current_renderer);
  2618. err += "\n";
  2619. }
  2620. String package_name = p_preset->get("package/unique_name");
  2621. if (package_name.contains("$genname") && !is_project_name_valid(p_preset)) {
  2622. // Warning only, so don't override `valid`.
  2623. err += vformat(TTR("The project name does not meet the requirement for the package name format and will be updated to \"%s\". Please explicitly specify the package name if needed."), get_valid_basename(p_preset));
  2624. err += "\n";
  2625. }
  2626. r_error = err;
  2627. return valid;
  2628. }
  2629. List<String> EditorExportPlatformAndroid::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  2630. List<String> list;
  2631. list.push_back("apk");
  2632. list.push_back("aab");
  2633. return list;
  2634. }
  2635. String EditorExportPlatformAndroid::get_apk_expansion_fullpath(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  2636. int version_code = p_preset->get("version/code");
  2637. String package_name = p_preset->get("package/unique_name");
  2638. String apk_file_name = "main." + itos(version_code) + "." + get_package_name(p_preset, package_name) + ".obb";
  2639. String fullpath = p_path.get_base_dir().path_join(apk_file_name);
  2640. return fullpath;
  2641. }
  2642. Error EditorExportPlatformAndroid::save_apk_expansion_file(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  2643. String fullpath = get_apk_expansion_fullpath(p_preset, p_path);
  2644. Error err = save_pack(p_preset, p_debug, fullpath);
  2645. return err;
  2646. }
  2647. void EditorExportPlatformAndroid::get_command_line_flags(const Ref<EditorExportPreset> &p_preset, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags, Vector<uint8_t> &r_command_line_flags) {
  2648. String cmdline = p_preset->get("command_line/extra_args");
  2649. Vector<String> command_line_strings = cmdline.strip_edges().split(" ");
  2650. for (int i = 0; i < command_line_strings.size(); i++) {
  2651. if (command_line_strings[i].strip_edges().length() == 0) {
  2652. command_line_strings.remove_at(i);
  2653. i--;
  2654. }
  2655. }
  2656. command_line_strings.append_array(gen_export_flags(p_flags));
  2657. bool apk_expansion = p_preset->get("apk_expansion/enable");
  2658. if (apk_expansion) {
  2659. String fullpath = get_apk_expansion_fullpath(p_preset, p_path);
  2660. String apk_expansion_public_key = p_preset->get("apk_expansion/public_key");
  2661. command_line_strings.push_back("--use_apk_expansion");
  2662. command_line_strings.push_back("--apk_expansion_md5");
  2663. command_line_strings.push_back(FileAccess::get_md5(fullpath));
  2664. command_line_strings.push_back("--apk_expansion_key");
  2665. command_line_strings.push_back(apk_expansion_public_key.strip_edges());
  2666. }
  2667. int xr_mode_index = p_preset->get("xr_features/xr_mode");
  2668. if (xr_mode_index == XR_MODE_OPENXR) {
  2669. command_line_strings.push_back("--xr_mode_openxr");
  2670. } else { // XRMode.REGULAR is the default.
  2671. command_line_strings.push_back("--xr_mode_regular");
  2672. // Also override the 'xr/openxr/enabled' project setting.
  2673. // This is useful for multi-platforms projects supporting both XR and non-XR devices. The project would need
  2674. // to enable openxr for development, and would create multiple XR and non-XR export presets.
  2675. // These command line args ensure that the non-XR export presets will have openxr disabled.
  2676. command_line_strings.push_back("--xr-mode");
  2677. command_line_strings.push_back("off");
  2678. }
  2679. bool immersive = p_preset->get("screen/immersive_mode");
  2680. if (immersive) {
  2681. command_line_strings.push_back("--fullscreen");
  2682. }
  2683. bool edge_to_edge = p_preset->get("screen/edge_to_edge");
  2684. if (edge_to_edge) {
  2685. command_line_strings.push_back("--edge_to_edge");
  2686. }
  2687. String background_color = "#" + p_preset->get("screen/background_color").operator Color().to_html(false);
  2688. // For Gradle build, _fix_themes_xml() sets background to transparent if _is_transparency_allowed().
  2689. // Overriding to transparent here too as it's used as fallback for system bar appearance.
  2690. if (_is_transparency_allowed(p_preset) && p_preset->get("gradle_build/use_gradle_build")) {
  2691. background_color = "#00000000";
  2692. }
  2693. command_line_strings.push_back("--background_color");
  2694. command_line_strings.push_back(background_color);
  2695. bool debug_opengl = p_preset->get("graphics/opengl_debug");
  2696. if (debug_opengl) {
  2697. command_line_strings.push_back("--debug_opengl");
  2698. }
  2699. if (command_line_strings.size()) {
  2700. r_command_line_flags.resize(4);
  2701. encode_uint32(command_line_strings.size(), &r_command_line_flags.write[0]);
  2702. for (int i = 0; i < command_line_strings.size(); i++) {
  2703. print_line(itos(i) + " param: " + command_line_strings[i]);
  2704. CharString command_line_argument = command_line_strings[i].utf8();
  2705. int base = r_command_line_flags.size();
  2706. int length = command_line_argument.length();
  2707. if (length == 0) {
  2708. continue;
  2709. }
  2710. r_command_line_flags.resize(base + 4 + length);
  2711. encode_uint32(length, &r_command_line_flags.write[base]);
  2712. memcpy(&r_command_line_flags.write[base + 4], command_line_argument.ptr(), length);
  2713. }
  2714. }
  2715. }
  2716. Error EditorExportPlatformAndroid::sign_apk(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &export_path, EditorProgress &ep) {
  2717. int export_format = int(p_preset->get("gradle_build/export_format"));
  2718. if (export_format == EXPORT_FORMAT_AAB) {
  2719. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("AAB signing is not supported"));
  2720. return FAILED;
  2721. }
  2722. String keystore;
  2723. String password;
  2724. String user;
  2725. if (p_debug) {
  2726. keystore = _get_keystore_path(p_preset, true);
  2727. password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  2728. user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  2729. if (keystore.is_empty()) {
  2730. keystore = EDITOR_GET("export/android/debug_keystore");
  2731. password = EDITOR_GET("export/android/debug_keystore_pass");
  2732. user = EDITOR_GET("export/android/debug_keystore_user");
  2733. }
  2734. if (ep.step(TTR("Signing debug APK..."), 104)) {
  2735. return ERR_SKIP;
  2736. }
  2737. } else {
  2738. keystore = _get_keystore_path(p_preset, false);
  2739. password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  2740. user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  2741. if (ep.step(TTR("Signing release APK..."), 104)) {
  2742. return ERR_SKIP;
  2743. }
  2744. }
  2745. if (!FileAccess::exists(keystore)) {
  2746. if (p_debug) {
  2747. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not find debug keystore, unable to export."));
  2748. } else {
  2749. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not find release keystore, unable to export."));
  2750. }
  2751. return ERR_FILE_CANT_OPEN;
  2752. }
  2753. String apk_path = export_path;
  2754. if (apk_path.is_relative_path()) {
  2755. apk_path = OS::get_singleton()->get_resource_dir().path_join(apk_path);
  2756. }
  2757. apk_path = ProjectSettings::get_singleton()->globalize_path(apk_path).simplify_path();
  2758. Error err;
  2759. #ifdef ANDROID_ENABLED
  2760. err = OS_Android::get_singleton()->sign_apk(apk_path, apk_path, keystore, user, password);
  2761. if (err != OK) {
  2762. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Unable to sign apk."));
  2763. return err;
  2764. }
  2765. #else
  2766. String target_sdk_version = p_preset->get("gradle_build/target_sdk");
  2767. if (!target_sdk_version.is_valid_int()) {
  2768. target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION);
  2769. }
  2770. String apksigner = get_apksigner_path(target_sdk_version.to_int(), true);
  2771. print_verbose("Starting signing of the APK binary using " + apksigner);
  2772. if (apksigner == "<FAILED>") {
  2773. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("All 'apksigner' tools located in Android SDK 'build-tools' directory failed to execute. Please check that you have the correct version installed for your target sdk version. The resulting APK is unsigned."));
  2774. return OK;
  2775. }
  2776. if (!FileAccess::exists(apksigner)) {
  2777. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("'apksigner' could not be found. Please check that the command is available in the Android SDK build-tools directory. The resulting APK is unsigned."));
  2778. return OK;
  2779. }
  2780. String output;
  2781. List<String> args;
  2782. args.push_back("sign");
  2783. args.push_back("--verbose");
  2784. args.push_back("--ks");
  2785. args.push_back(keystore);
  2786. args.push_back("--ks-pass");
  2787. args.push_back("pass:" + password);
  2788. args.push_back("--ks-key-alias");
  2789. args.push_back(user);
  2790. args.push_back(apk_path);
  2791. if (OS::get_singleton()->is_stdout_verbose() && p_debug) {
  2792. // We only print verbose logs with credentials for debug builds to avoid leaking release keystore credentials.
  2793. print_verbose("Signing debug binary using: " + String("\n") + apksigner + " " + join_list(args, String(" ")));
  2794. } else {
  2795. List<String> redacted_args = List<String>(args);
  2796. redacted_args.find(keystore)->set("<REDACTED>");
  2797. redacted_args.find("pass:" + password)->set("pass:<REDACTED>");
  2798. redacted_args.find(user)->set("<REDACTED>");
  2799. print_line("Signing binary using: " + String("\n") + apksigner + " " + join_list(redacted_args, String(" ")));
  2800. }
  2801. int retval;
  2802. err = OS::get_singleton()->execute(apksigner, args, &output, &retval, true);
  2803. if (err != OK) {
  2804. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start apksigner executable."));
  2805. return err;
  2806. }
  2807. // By design, apksigner does not output credentials in its output unless --verbose is used
  2808. print_line(output);
  2809. if (retval) {
  2810. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("'apksigner' returned with error #%d"), retval));
  2811. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("output: \n%s"), output));
  2812. return ERR_CANT_CREATE;
  2813. }
  2814. #endif
  2815. if (ep.step(TTR("Verifying APK..."), 105)) {
  2816. return ERR_SKIP;
  2817. }
  2818. #ifdef ANDROID_ENABLED
  2819. err = OS_Android::get_singleton()->verify_apk(apk_path);
  2820. if (err != OK) {
  2821. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Unable to verify signed apk."));
  2822. return err;
  2823. }
  2824. #else
  2825. args.clear();
  2826. args.push_back("verify");
  2827. args.push_back("--verbose");
  2828. args.push_back(apk_path);
  2829. if (p_debug) {
  2830. print_verbose("Verifying signed build using: " + String("\n") + apksigner + " " + join_list(args, String(" ")));
  2831. }
  2832. output.clear();
  2833. err = OS::get_singleton()->execute(apksigner, args, &output, &retval, true);
  2834. if (err != OK) {
  2835. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start apksigner executable."));
  2836. return err;
  2837. }
  2838. print_verbose(output);
  2839. if (retval) {
  2840. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("'apksigner' verification of APK failed."));
  2841. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("output: \n%s"), output));
  2842. return ERR_CANT_CREATE;
  2843. }
  2844. #endif
  2845. print_verbose("Successfully completed signing build.");
  2846. #ifdef ANDROID_ENABLED
  2847. bool prompt_apk_install = EDITOR_GET("export/android/install_exported_apk");
  2848. if (prompt_apk_install) {
  2849. OS_Android::get_singleton()->shell_open(apk_path);
  2850. }
  2851. #endif
  2852. return OK;
  2853. }
  2854. void EditorExportPlatformAndroid::_clear_assets_directory(const Ref<EditorExportPreset> &p_preset) {
  2855. Ref<DirAccess> da_res = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2856. String gradle_build_directory = ExportTemplateManager::get_android_build_directory(p_preset);
  2857. // Clear the APK assets directory
  2858. String apk_assets_directory = gradle_build_directory.path_join(APK_ASSETS_DIRECTORY);
  2859. if (da_res->dir_exists(apk_assets_directory)) {
  2860. print_verbose("Clearing APK assets directory...");
  2861. Ref<DirAccess> da_assets = DirAccess::open(apk_assets_directory);
  2862. ERR_FAIL_COND(da_assets.is_null());
  2863. da_assets->erase_contents_recursive();
  2864. da_res->remove(apk_assets_directory);
  2865. }
  2866. // Clear the AAB assets directory
  2867. String aab_assets_directory = gradle_build_directory.path_join(AAB_ASSETS_DIRECTORY);
  2868. if (da_res->dir_exists(aab_assets_directory)) {
  2869. print_verbose("Clearing AAB assets directory...");
  2870. Ref<DirAccess> da_assets = DirAccess::open(aab_assets_directory);
  2871. ERR_FAIL_COND(da_assets.is_null());
  2872. da_assets->erase_contents_recursive();
  2873. da_res->remove(aab_assets_directory);
  2874. }
  2875. }
  2876. void EditorExportPlatformAndroid::_remove_copied_libs(String p_gdextension_libs_path) {
  2877. print_verbose("Removing previously installed libraries...");
  2878. Error error;
  2879. String libs_json = FileAccess::get_file_as_string(p_gdextension_libs_path, &error);
  2880. if (error || libs_json.is_empty()) {
  2881. print_verbose("No previously installed libraries found");
  2882. return;
  2883. }
  2884. JSON json;
  2885. error = json.parse(libs_json);
  2886. ERR_FAIL_COND_MSG(error, "Error parsing \"" + libs_json + "\" on line " + itos(json.get_error_line()) + ": " + json.get_error_message());
  2887. Vector<String> libs = json.get_data();
  2888. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2889. for (int i = 0; i < libs.size(); i++) {
  2890. print_verbose("Removing previously installed library " + libs[i]);
  2891. da->remove(libs[i]);
  2892. }
  2893. da->remove(p_gdextension_libs_path);
  2894. }
  2895. String EditorExportPlatformAndroid::join_list(const List<String> &p_parts, const String &p_separator) {
  2896. String ret;
  2897. for (List<String>::ConstIterator itr = p_parts.begin(); itr != p_parts.end(); ++itr) {
  2898. if (itr != p_parts.begin()) {
  2899. ret += p_separator;
  2900. }
  2901. ret += *itr;
  2902. }
  2903. return ret;
  2904. }
  2905. String EditorExportPlatformAndroid::join_abis(const Vector<EditorExportPlatformAndroid::ABI> &p_parts, const String &p_separator, bool p_use_arch) {
  2906. String ret;
  2907. for (int i = 0; i < p_parts.size(); ++i) {
  2908. if (i > 0) {
  2909. ret += p_separator;
  2910. }
  2911. ret += (p_use_arch) ? p_parts[i].arch : p_parts[i].abi;
  2912. }
  2913. return ret;
  2914. }
  2915. String EditorExportPlatformAndroid::_get_deprecated_plugins_names(const Ref<EditorExportPreset> &p_preset) const {
  2916. Vector<String> names;
  2917. #ifndef DISABLE_DEPRECATED
  2918. PluginConfigAndroid::get_plugins_names(get_enabled_plugins(p_preset), names);
  2919. #endif // DISABLE_DEPRECATED
  2920. String plugins_names = String("|").join(names);
  2921. return plugins_names;
  2922. }
  2923. String EditorExportPlatformAndroid::_get_plugins_names(const Ref<EditorExportPreset> &p_preset) const {
  2924. Vector<String> names;
  2925. #ifndef DISABLE_DEPRECATED
  2926. PluginConfigAndroid::get_plugins_names(get_enabled_plugins(p_preset), names);
  2927. #endif // DISABLE_DEPRECATED
  2928. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  2929. for (int i = 0; i < export_plugins.size(); i++) {
  2930. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  2931. names.push_back(export_plugins[i]->get_name());
  2932. }
  2933. }
  2934. String plugins_names = String("|").join(names);
  2935. return plugins_names;
  2936. }
  2937. String EditorExportPlatformAndroid::_resolve_export_plugin_android_library_path(const String &p_android_library_path) const {
  2938. String absolute_path;
  2939. if (!p_android_library_path.is_empty()) {
  2940. if (p_android_library_path.is_absolute_path()) {
  2941. absolute_path = ProjectSettings::get_singleton()->globalize_path(p_android_library_path);
  2942. } else {
  2943. const String export_plugin_absolute_path = String("res://addons/").path_join(p_android_library_path);
  2944. absolute_path = ProjectSettings::get_singleton()->globalize_path(export_plugin_absolute_path);
  2945. }
  2946. }
  2947. return absolute_path;
  2948. }
  2949. bool EditorExportPlatformAndroid::_is_clean_build_required(const Ref<EditorExportPreset> &p_preset) {
  2950. bool first_build = last_gradle_build_time == 0;
  2951. bool have_plugins_changed = false;
  2952. String gradle_build_dir = ExportTemplateManager::get_android_build_directory(p_preset);
  2953. bool has_build_dir_changed = last_gradle_build_dir != gradle_build_dir;
  2954. String plugin_names = _get_plugins_names(p_preset);
  2955. if (!first_build) {
  2956. have_plugins_changed = plugin_names != last_plugin_names;
  2957. #ifndef DISABLE_DEPRECATED
  2958. if (!have_plugins_changed) {
  2959. Vector<PluginConfigAndroid> enabled_plugins = get_enabled_plugins(p_preset);
  2960. for (int i = 0; i < enabled_plugins.size(); i++) {
  2961. if (enabled_plugins.get(i).last_updated > last_gradle_build_time) {
  2962. have_plugins_changed = true;
  2963. break;
  2964. }
  2965. }
  2966. }
  2967. #endif // DISABLE_DEPRECATED
  2968. }
  2969. last_gradle_build_time = OS::get_singleton()->get_unix_time();
  2970. last_gradle_build_dir = gradle_build_dir;
  2971. last_plugin_names = plugin_names;
  2972. return have_plugins_changed || has_build_dir_changed || first_build;
  2973. }
  2974. Error EditorExportPlatformAndroid::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  2975. int export_format = int(p_preset->get("gradle_build/export_format"));
  2976. bool should_sign = p_preset->get("package/signed");
  2977. return export_project_helper(p_preset, p_debug, p_path, export_format, should_sign, p_flags);
  2978. }
  2979. Error EditorExportPlatformAndroid::_generate_sparse_pck_metadata(const Ref<EditorExportPreset> &p_preset, PackData &p_pack_data, Vector<uint8_t> &r_data) {
  2980. Error err;
  2981. Ref<FileAccess> ftmp = FileAccess::create_temp(FileAccess::WRITE_READ, "export_index", "tmp", false, &err);
  2982. if (err != OK) {
  2983. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not create temporary file!"));
  2984. return err;
  2985. }
  2986. int64_t pck_start_pos = ftmp->get_position();
  2987. uint64_t file_base_ofs = 0;
  2988. uint64_t dir_base_ofs = 0;
  2989. EditorExportPlatform::_store_header(ftmp, p_preset->get_enc_pck() && p_preset->get_enc_directory(), true, file_base_ofs, dir_base_ofs);
  2990. // Write directory.
  2991. uint64_t dir_offset = ftmp->get_position();
  2992. ftmp->seek(dir_base_ofs);
  2993. ftmp->store_64(dir_offset - pck_start_pos);
  2994. ftmp->seek(dir_offset);
  2995. Vector<uint8_t> key;
  2996. if (p_preset->get_enc_pck() && p_preset->get_enc_directory()) {
  2997. String script_key = _get_script_encryption_key(p_preset);
  2998. key.resize(32);
  2999. if (script_key.length() == 64) {
  3000. for (int i = 0; i < 32; i++) {
  3001. int v = 0;
  3002. if (i * 2 < script_key.length()) {
  3003. char32_t ct = script_key[i * 2];
  3004. if (is_digit(ct)) {
  3005. ct = ct - '0';
  3006. } else if (ct >= 'a' && ct <= 'f') {
  3007. ct = 10 + ct - 'a';
  3008. }
  3009. v |= ct << 4;
  3010. }
  3011. if (i * 2 + 1 < script_key.length()) {
  3012. char32_t ct = script_key[i * 2 + 1];
  3013. if (is_digit(ct)) {
  3014. ct = ct - '0';
  3015. } else if (ct >= 'a' && ct <= 'f') {
  3016. ct = 10 + ct - 'a';
  3017. }
  3018. v |= ct;
  3019. }
  3020. key.write[i] = v;
  3021. }
  3022. }
  3023. }
  3024. if (!EditorExportPlatform::_encrypt_and_store_directory(ftmp, p_pack_data, key, p_preset->get_seed(), 0)) {
  3025. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Can't create encrypted file."));
  3026. return ERR_CANT_CREATE;
  3027. }
  3028. r_data.resize(ftmp->get_length());
  3029. ftmp->seek(0);
  3030. ftmp->get_buffer(r_data.ptrw(), r_data.size());
  3031. ftmp.unref();
  3032. return OK;
  3033. }
  3034. Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int export_format, bool should_sign, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  3035. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  3036. const String base_dir = p_path.get_base_dir();
  3037. if (!DirAccess::exists(base_dir)) {
  3038. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), base_dir));
  3039. return ERR_FILE_BAD_PATH;
  3040. }
  3041. String src_apk;
  3042. Error err;
  3043. EditorProgress ep("export", TTR("Exporting for Android"), 105, true);
  3044. bool use_gradle_build = bool(p_preset->get("gradle_build/use_gradle_build"));
  3045. String gradle_build_directory = use_gradle_build ? ExportTemplateManager::get_android_build_directory(p_preset) : "";
  3046. bool p_give_internet = p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT) || p_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG);
  3047. bool apk_expansion = p_preset->get("apk_expansion/enable");
  3048. Vector<ABI> enabled_abis = get_enabled_abis(p_preset);
  3049. print_verbose("Exporting for Android...");
  3050. print_verbose("- debug build: " + bool_to_string(p_debug));
  3051. print_verbose("- export path: " + p_path);
  3052. print_verbose("- export format: " + itos(export_format));
  3053. print_verbose("- sign build: " + bool_to_string(should_sign));
  3054. print_verbose("- gradle build enabled: " + bool_to_string(use_gradle_build));
  3055. print_verbose("- apk expansion enabled: " + bool_to_string(apk_expansion));
  3056. print_verbose("- enabled abis: " + join_abis(enabled_abis, ",", false));
  3057. print_verbose("- export filter: " + itos(p_preset->get_export_filter()));
  3058. print_verbose("- include filter: " + p_preset->get_include_filter());
  3059. print_verbose("- exclude filter: " + p_preset->get_exclude_filter());
  3060. Ref<Image> main_image;
  3061. Ref<Image> foreground;
  3062. Ref<Image> background;
  3063. Ref<Image> monochrome;
  3064. load_icon_refs(p_preset, main_image, foreground, background, monochrome);
  3065. Vector<uint8_t> command_line_flags;
  3066. // Write command line flags into the command_line_flags variable.
  3067. get_command_line_flags(p_preset, p_path, p_flags, command_line_flags);
  3068. if (export_format == EXPORT_FORMAT_AAB) {
  3069. if (!p_path.ends_with(".aab")) {
  3070. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid filename! Android App Bundle requires the *.aab extension."));
  3071. return ERR_UNCONFIGURED;
  3072. }
  3073. if (apk_expansion) {
  3074. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("APK Expansion not compatible with Android App Bundle."));
  3075. return ERR_UNCONFIGURED;
  3076. }
  3077. }
  3078. if (export_format == EXPORT_FORMAT_APK && !p_path.ends_with(".apk")) {
  3079. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid filename! Android APK requires the *.apk extension."));
  3080. return ERR_UNCONFIGURED;
  3081. }
  3082. if (export_format > EXPORT_FORMAT_AAB || export_format < EXPORT_FORMAT_APK) {
  3083. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unsupported export format!"));
  3084. return ERR_UNCONFIGURED;
  3085. }
  3086. String err_string;
  3087. if (!has_valid_username_and_password(p_preset, err_string)) {
  3088. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR(err_string));
  3089. return ERR_UNCONFIGURED;
  3090. }
  3091. if (use_gradle_build) {
  3092. print_verbose("Starting gradle build...");
  3093. //test that installed build version is alright
  3094. {
  3095. print_verbose("Checking build version...");
  3096. String gradle_base_directory = gradle_build_directory.get_base_dir();
  3097. Ref<FileAccess> f = FileAccess::open(gradle_base_directory.path_join(".build_version"), FileAccess::READ);
  3098. if (f.is_null()) {
  3099. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Trying to build from a gradle built template, but no version info for it exists. Please reinstall from the 'Project' menu."));
  3100. return ERR_UNCONFIGURED;
  3101. }
  3102. String current_version = ExportTemplateManager::get_android_template_identifier(p_preset);
  3103. String installed_version = f->get_line().strip_edges();
  3104. print_verbose("- build version: " + installed_version);
  3105. if (installed_version != current_version) {
  3106. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR(MISMATCHED_VERSIONS_MESSAGE), installed_version, current_version));
  3107. return ERR_UNCONFIGURED;
  3108. }
  3109. }
  3110. const String assets_directory = get_assets_directory(p_preset, export_format);
  3111. String java_sdk_path = EDITOR_GET("export/android/java_sdk_path");
  3112. if (java_sdk_path.is_empty()) {
  3113. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Java SDK path must be configured in Editor Settings at 'export/android/java_sdk_path'."));
  3114. return ERR_UNCONFIGURED;
  3115. }
  3116. print_verbose("Java sdk path: " + java_sdk_path);
  3117. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  3118. if (sdk_path.is_empty()) {
  3119. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Android SDK path must be configured in Editor Settings at 'export/android/android_sdk_path'."));
  3120. return ERR_UNCONFIGURED;
  3121. }
  3122. print_verbose("Android sdk path: " + sdk_path);
  3123. // TODO: should we use "package/name" or "application/config/name"?
  3124. String project_name = get_project_name(p_preset, p_preset->get("package/name"));
  3125. err = _create_project_name_strings_files(p_preset, project_name, gradle_build_directory, get_project_setting(p_preset, "application/config/name_localized")); //project name localization.
  3126. if (err != OK) {
  3127. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unable to overwrite res/*.xml files with project name."));
  3128. }
  3129. // Copies the project icon files into the appropriate Gradle project directory.
  3130. _copy_icons_to_gradle_project(p_preset, main_image, foreground, background, monochrome);
  3131. // Write an AndroidManifest.xml file into the Gradle project directory.
  3132. _write_tmp_manifest(p_preset, p_give_internet, p_debug);
  3133. // Modify res/values/themes.xml file.
  3134. _fix_themes_xml(p_preset);
  3135. //stores all the project files inside the Gradle project directory. Also includes all ABIs
  3136. _clear_assets_directory(p_preset);
  3137. String gdextension_libs_path = gradle_build_directory.path_join(GDEXTENSION_LIBS_PATH);
  3138. _remove_copied_libs(gdextension_libs_path);
  3139. if (!apk_expansion) {
  3140. print_verbose("Exporting project files...");
  3141. CustomExportData user_data;
  3142. user_data.assets_directory = assets_directory;
  3143. user_data.libs_directory = gradle_build_directory.path_join("libs");
  3144. user_data.debug = p_debug;
  3145. if (p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) {
  3146. err = export_project_files(p_preset, p_debug, ignore_apk_file, nullptr, &user_data, copy_gradle_so);
  3147. } else {
  3148. user_data.pd.path = "assets.sparsepck";
  3149. user_data.pd.use_sparse_pck = true;
  3150. err = export_project_files(p_preset, p_debug, rename_and_store_file_in_gradle_project, nullptr, &user_data, copy_gradle_so);
  3151. Vector<uint8_t> enc_data;
  3152. err = _generate_sparse_pck_metadata(p_preset, user_data.pd, enc_data);
  3153. if (err != OK) {
  3154. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not generate sparse pck metadata!"));
  3155. return err;
  3156. }
  3157. err = store_file_at_path(user_data.assets_directory + "/assets.sparsepck", enc_data);
  3158. if (err != OK) {
  3159. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not write PCK directory!"));
  3160. return err;
  3161. }
  3162. }
  3163. if (err != OK) {
  3164. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not export project files to gradle project."));
  3165. return err;
  3166. }
  3167. if (user_data.libs.size() > 0) {
  3168. Ref<FileAccess> fa = FileAccess::open(gdextension_libs_path, FileAccess::WRITE);
  3169. fa->store_string(JSON::stringify(user_data.libs, "\t"));
  3170. }
  3171. } else {
  3172. print_verbose("Saving apk expansion file...");
  3173. err = save_apk_expansion_file(p_preset, p_debug, p_path);
  3174. if (err != OK) {
  3175. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not write expansion package file!"));
  3176. return err;
  3177. }
  3178. }
  3179. print_verbose("Storing command line flags...");
  3180. store_file_at_path(assets_directory + "/_cl_", command_line_flags);
  3181. print_verbose("Updating JAVA_HOME environment to " + java_sdk_path);
  3182. OS::get_singleton()->set_environment("JAVA_HOME", java_sdk_path);
  3183. print_verbose("Updating ANDROID_HOME environment to " + sdk_path);
  3184. OS::get_singleton()->set_environment("ANDROID_HOME", sdk_path);
  3185. String build_command;
  3186. #ifdef WINDOWS_ENABLED
  3187. build_command = "gradlew.bat";
  3188. #else
  3189. build_command = "gradlew";
  3190. #endif
  3191. String build_path = ProjectSettings::get_singleton()->globalize_path(gradle_build_directory);
  3192. build_command = build_path.path_join(build_command);
  3193. String package_name = get_package_name(p_preset, p_preset->get("package/unique_name"));
  3194. String version_code = itos(p_preset->get("version/code"));
  3195. String version_name = p_preset->get_version("version/name");
  3196. String min_sdk_version = p_preset->get("gradle_build/min_sdk");
  3197. if (!min_sdk_version.is_valid_int()) {
  3198. min_sdk_version = itos(DEFAULT_MIN_SDK_VERSION);
  3199. }
  3200. String target_sdk_version = p_preset->get("gradle_build/target_sdk");
  3201. if (!target_sdk_version.is_valid_int()) {
  3202. target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION);
  3203. }
  3204. String enabled_abi_string = join_abis(enabled_abis, "|", false);
  3205. String sign_flag = should_sign ? "true" : "false";
  3206. String zipalign_flag = "true";
  3207. Vector<String> android_libraries;
  3208. Vector<String> android_dependencies;
  3209. Vector<String> android_dependencies_maven_repos;
  3210. #ifndef DISABLE_DEPRECATED
  3211. Vector<PluginConfigAndroid> enabled_plugins = get_enabled_plugins(p_preset);
  3212. PluginConfigAndroid::get_plugins_binaries(PluginConfigAndroid::BINARY_TYPE_LOCAL, enabled_plugins, android_libraries);
  3213. PluginConfigAndroid::get_plugins_binaries(PluginConfigAndroid::BINARY_TYPE_REMOTE, enabled_plugins, android_dependencies);
  3214. PluginConfigAndroid::get_plugins_custom_maven_repos(enabled_plugins, android_dependencies_maven_repos);
  3215. #endif // DISABLE_DEPRECATED
  3216. bool has_dotnet_project = false;
  3217. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  3218. for (int i = 0; i < export_plugins.size(); i++) {
  3219. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  3220. PackedStringArray export_plugin_android_libraries = export_plugins[i]->get_android_libraries(Ref<EditorExportPlatform>(this), p_debug);
  3221. for (int k = 0; k < export_plugin_android_libraries.size(); k++) {
  3222. const String resolved_android_library_path = _resolve_export_plugin_android_library_path(export_plugin_android_libraries[k]);
  3223. if (!resolved_android_library_path.is_empty()) {
  3224. android_libraries.push_back(resolved_android_library_path);
  3225. }
  3226. }
  3227. PackedStringArray export_plugin_android_dependencies = export_plugins[i]->get_android_dependencies(Ref<EditorExportPlatform>(this), p_debug);
  3228. android_dependencies.append_array(export_plugin_android_dependencies);
  3229. PackedStringArray export_plugin_android_dependencies_maven_repos = export_plugins[i]->get_android_dependencies_maven_repos(Ref<EditorExportPlatform>(this), p_debug);
  3230. android_dependencies_maven_repos.append_array(export_plugin_android_dependencies_maven_repos);
  3231. }
  3232. PackedStringArray features = export_plugins[i]->get_export_features(Ref<EditorExportPlatform>(this), p_debug);
  3233. if (features.has("dotnet")) {
  3234. has_dotnet_project = true;
  3235. }
  3236. }
  3237. bool clean_build_required = _is_clean_build_required(p_preset);
  3238. String combined_android_libraries = String("|").join(android_libraries);
  3239. String combined_android_dependencies = String("|").join(android_dependencies);
  3240. String combined_android_dependencies_maven_repos = String("|").join(android_dependencies_maven_repos);
  3241. List<String> cmdline;
  3242. cmdline.push_back("validateJavaVersion");
  3243. if (clean_build_required) {
  3244. cmdline.push_back("clean");
  3245. }
  3246. String edition = has_dotnet_project ? "Mono" : "Standard";
  3247. String build_type = p_debug ? "Debug" : "Release";
  3248. if (export_format == EXPORT_FORMAT_AAB) {
  3249. String bundle_build_command = vformat("bundle%s", build_type);
  3250. cmdline.push_back(bundle_build_command);
  3251. } else if (export_format == EXPORT_FORMAT_APK) {
  3252. String apk_build_command = vformat("assemble%s%s", edition, build_type);
  3253. cmdline.push_back(apk_build_command);
  3254. }
  3255. String addons_directory = ProjectSettings::get_singleton()->globalize_path("res://addons");
  3256. cmdline.push_back("-p"); // argument to specify the start directory.
  3257. cmdline.push_back(build_path); // start directory.
  3258. cmdline.push_back("-Paddons_directory=" + addons_directory); // path to the addon directory as it may contain jar or aar dependencies
  3259. cmdline.push_back("-Pexport_package_name=" + package_name); // argument to specify the package name.
  3260. cmdline.push_back("-Pexport_version_code=" + version_code); // argument to specify the version code.
  3261. cmdline.push_back("-Pexport_version_name=" + version_name); // argument to specify the version name.
  3262. cmdline.push_back("-Pexport_version_min_sdk=" + min_sdk_version); // argument to specify the min sdk.
  3263. cmdline.push_back("-Pexport_version_target_sdk=" + target_sdk_version); // argument to specify the target sdk.
  3264. cmdline.push_back("-Pexport_enabled_abis=" + enabled_abi_string); // argument to specify enabled ABIs.
  3265. cmdline.push_back("-Pplugins_local_binaries=" + combined_android_libraries); // argument to specify the list of android libraries provided by plugins.
  3266. cmdline.push_back("-Pplugins_remote_binaries=" + combined_android_dependencies); // argument to specify the list of android dependencies provided by plugins.
  3267. cmdline.push_back("-Pplugins_maven_repos=" + combined_android_dependencies_maven_repos); // argument to specify the list of maven repos for android dependencies provided by plugins.
  3268. cmdline.push_back("-Pperform_zipalign=" + zipalign_flag); // argument to specify whether the build should be zipaligned.
  3269. cmdline.push_back("-Pperform_signing=" + sign_flag); // argument to specify whether the build should be signed.
  3270. // NOTE: The release keystore is not included in the verbose logging
  3271. // to avoid accidentally leaking sensitive information when sharing verbose logs for troubleshooting.
  3272. // Any non-sensitive additions to the command line arguments must be done above this section.
  3273. // Sensitive additions must be done below the logging statement.
  3274. print_verbose("Build Android project using gradle command: " + String("\n") + build_command + " " + join_list(cmdline, String(" ")));
  3275. if (should_sign) {
  3276. if (p_debug) {
  3277. String debug_keystore = _get_keystore_path(p_preset, true);
  3278. String debug_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  3279. String debug_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  3280. if (debug_keystore.is_empty()) {
  3281. debug_keystore = EDITOR_GET("export/android/debug_keystore");
  3282. debug_password = EDITOR_GET("export/android/debug_keystore_pass");
  3283. debug_user = EDITOR_GET("export/android/debug_keystore_user");
  3284. }
  3285. if (debug_keystore.is_relative_path()) {
  3286. debug_keystore = OS::get_singleton()->get_resource_dir().path_join(debug_keystore).simplify_path();
  3287. }
  3288. if (!FileAccess::exists(debug_keystore)) {
  3289. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not find debug keystore, unable to export."));
  3290. return ERR_FILE_CANT_OPEN;
  3291. }
  3292. cmdline.push_back("-Pdebug_keystore_file=" + debug_keystore); // argument to specify the debug keystore file.
  3293. cmdline.push_back("-Pdebug_keystore_alias=" + debug_user); // argument to specify the debug keystore alias.
  3294. cmdline.push_back("-Pdebug_keystore_password=" + debug_password); // argument to specify the debug keystore password.
  3295. } else {
  3296. // Pass the release keystore info as well
  3297. String release_keystore = _get_keystore_path(p_preset, false);
  3298. String release_username = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  3299. String release_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  3300. if (release_keystore.is_relative_path()) {
  3301. release_keystore = OS::get_singleton()->get_resource_dir().path_join(release_keystore).simplify_path();
  3302. }
  3303. if (!FileAccess::exists(release_keystore)) {
  3304. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not find release keystore, unable to export."));
  3305. return ERR_FILE_CANT_OPEN;
  3306. }
  3307. cmdline.push_back("-Prelease_keystore_file=" + release_keystore); // argument to specify the release keystore file.
  3308. cmdline.push_back("-Prelease_keystore_alias=" + release_username); // argument to specify the release keystore alias.
  3309. cmdline.push_back("-Prelease_keystore_password=" + release_password); // argument to specify the release keystore password.
  3310. }
  3311. }
  3312. String build_project_output;
  3313. int result = EditorNode::get_singleton()->execute_and_show_output(TTR("Building Android Project (gradle)"), build_command, cmdline, true, false, &build_project_output);
  3314. if (result != 0) {
  3315. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Building of Android project failed, check output for the error:") + "\n\n" + build_project_output);
  3316. return ERR_CANT_CREATE;
  3317. } else {
  3318. print_verbose(build_project_output);
  3319. }
  3320. List<String> copy_args;
  3321. String copy_command = "copyAndRenameBinary";
  3322. copy_args.push_back(copy_command);
  3323. copy_args.push_back("-p"); // argument to specify the start directory.
  3324. copy_args.push_back(build_path); // start directory.
  3325. copy_args.push_back("-Pexport_edition=" + edition.to_lower());
  3326. copy_args.push_back("-Pexport_build_type=" + build_type.to_lower());
  3327. String export_format_arg = export_format == EXPORT_FORMAT_AAB ? "aab" : "apk";
  3328. copy_args.push_back("-Pexport_format=" + export_format_arg);
  3329. String export_filename = p_path.get_file();
  3330. String export_path = p_path.get_base_dir();
  3331. if (export_path.is_relative_path()) {
  3332. export_path = OS::get_singleton()->get_resource_dir().path_join(export_path);
  3333. }
  3334. export_path = ProjectSettings::get_singleton()->globalize_path(export_path).simplify_path();
  3335. copy_args.push_back("-Pexport_path=file:" + export_path);
  3336. copy_args.push_back("-Pexport_filename=" + export_filename);
  3337. print_verbose("Copying Android binary using gradle command: " + String("\n") + build_command + " " + join_list(copy_args, String(" ")));
  3338. String copy_binary_output;
  3339. int copy_result = EditorNode::get_singleton()->execute_and_show_output(TTR("Moving output"), build_command, copy_args, true, false, &copy_binary_output);
  3340. if (copy_result != 0) {
  3341. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unable to copy and rename export file:") + "\n\n" + copy_binary_output);
  3342. return ERR_CANT_CREATE;
  3343. } else {
  3344. print_verbose(copy_binary_output);
  3345. }
  3346. print_verbose("Successfully completed Android gradle build.");
  3347. return OK;
  3348. }
  3349. // This is the start of the Legacy build system
  3350. print_verbose("Starting legacy build system...");
  3351. if (p_debug) {
  3352. src_apk = p_preset->get("custom_template/debug");
  3353. } else {
  3354. src_apk = p_preset->get("custom_template/release");
  3355. }
  3356. src_apk = src_apk.strip_edges();
  3357. if (src_apk.is_empty()) {
  3358. if (p_debug) {
  3359. src_apk = find_export_template("android_debug.apk");
  3360. } else {
  3361. src_apk = find_export_template("android_release.apk");
  3362. }
  3363. if (src_apk.is_empty()) {
  3364. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(p_debug ? TTR("Debug export template not found: \"%s\".") : TTR("Release export template not found: \"%s\"."), src_apk));
  3365. return ERR_FILE_NOT_FOUND;
  3366. }
  3367. }
  3368. Ref<FileAccess> io_fa;
  3369. zlib_filefunc_def io = zipio_create_io(&io_fa);
  3370. if (ep.step(TTR("Creating APK..."), 0)) {
  3371. return ERR_SKIP;
  3372. }
  3373. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  3374. if (!pkg) {
  3375. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not find template APK to export: \"%s\"."), src_apk));
  3376. return ERR_FILE_NOT_FOUND;
  3377. }
  3378. int ret = unzGoToFirstFile(pkg);
  3379. Ref<FileAccess> io2_fa;
  3380. zlib_filefunc_def io2 = zipio_create_io(&io2_fa);
  3381. String tmp_unaligned_path = EditorPaths::get_singleton()->get_temp_dir().path_join("tmpexport-unaligned." + uitos(OS::get_singleton()->get_unix_time()) + ".apk");
  3382. #define CLEANUP_AND_RETURN(m_err) \
  3383. { \
  3384. DirAccess::remove_file_or_error(tmp_unaligned_path); \
  3385. return m_err; \
  3386. } \
  3387. ((void)0)
  3388. zipFile unaligned_apk = zipOpen2(tmp_unaligned_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io2);
  3389. String cmdline = p_preset->get("command_line/extra_args");
  3390. String version_name = p_preset->get_version("version/name");
  3391. String package_name = p_preset->get("package/unique_name");
  3392. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  3393. Vector<ABI> invalid_abis(enabled_abis);
  3394. //To temporarily store icon xml data.
  3395. Vector<uint8_t> themed_icon_xml_data;
  3396. int icon_xml_compression_method = -1;
  3397. while (ret == UNZ_OK) {
  3398. //get filename
  3399. unz_file_info info;
  3400. char fname[16384];
  3401. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  3402. if (ret != UNZ_OK) {
  3403. break;
  3404. }
  3405. bool skip = false;
  3406. String file = String::utf8(fname);
  3407. Vector<uint8_t> data;
  3408. data.resize(info.uncompressed_size);
  3409. //read
  3410. unzOpenCurrentFile(pkg);
  3411. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  3412. unzCloseCurrentFile(pkg);
  3413. //write
  3414. if (file == "AndroidManifest.xml") {
  3415. _fix_manifest(p_preset, data, p_give_internet);
  3416. // Allow editor export plugins to update the prebuilt manifest as needed.
  3417. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  3418. for (int i = 0; i < export_plugins.size(); i++) {
  3419. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  3420. PackedByteArray export_plugin_data = export_plugins[i]->update_android_prebuilt_manifest(Ref<EditorExportPlatform>(this), data);
  3421. if (!export_plugin_data.is_empty()) {
  3422. data = export_plugin_data;
  3423. }
  3424. }
  3425. }
  3426. }
  3427. if (file == "resources.arsc") {
  3428. _fix_resources(p_preset, data);
  3429. }
  3430. if (file == THEMED_ICON_XML_PATH) {
  3431. // Store themed_icon.xml data.
  3432. themed_icon_xml_data = data;
  3433. skip = true;
  3434. }
  3435. if (file == ICON_XML_PATH) {
  3436. if (monochrome.is_valid() && !monochrome->is_empty()) {
  3437. // Defer processing of icon.xml until after themed_icon.xml is read.
  3438. icon_xml_compression_method = info.compression_method;
  3439. skip = true;
  3440. }
  3441. }
  3442. if (file.ends_with(".png") && file.contains("mipmap")) {
  3443. for (int i = 0; i < ICON_DENSITIES_COUNT; ++i) {
  3444. if (main_image.is_valid() && !main_image->is_empty()) {
  3445. if (file == LAUNCHER_ICONS[i].export_path) {
  3446. _process_launcher_icons(file, main_image, LAUNCHER_ICONS[i].dimensions, data);
  3447. }
  3448. }
  3449. if (foreground.is_valid() && !foreground->is_empty()) {
  3450. if (file == LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path) {
  3451. _process_launcher_icons(file, foreground, LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].dimensions, data);
  3452. }
  3453. }
  3454. if (background.is_valid() && !background->is_empty()) {
  3455. if (file == LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path) {
  3456. _process_launcher_icons(file, background, LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].dimensions, data);
  3457. }
  3458. }
  3459. if (monochrome.is_valid() && !monochrome->is_empty()) {
  3460. if (file == LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path) {
  3461. _process_launcher_icons(file, monochrome, LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].dimensions, data);
  3462. }
  3463. }
  3464. }
  3465. }
  3466. if (file.ends_with(".so")) {
  3467. bool enabled = false;
  3468. for (int i = 0; i < enabled_abis.size(); ++i) {
  3469. if (file.begins_with("lib/" + enabled_abis[i].abi + "/")) {
  3470. invalid_abis.erase(enabled_abis[i]);
  3471. enabled = true;
  3472. break;
  3473. }
  3474. }
  3475. if (!enabled) {
  3476. skip = true;
  3477. }
  3478. }
  3479. if (file.begins_with("META-INF") && should_sign) {
  3480. skip = true;
  3481. }
  3482. if (!skip) {
  3483. print_line("ADDING: " + file);
  3484. // Respect decision on compression made by AAPT for the export template
  3485. const bool uncompressed = info.compression_method == 0;
  3486. zip_fileinfo zipfi = get_zip_fileinfo();
  3487. zipOpenNewFileInZip(unaligned_apk,
  3488. file.utf8().get_data(),
  3489. &zipfi,
  3490. nullptr,
  3491. 0,
  3492. nullptr,
  3493. 0,
  3494. nullptr,
  3495. uncompressed ? 0 : Z_DEFLATED,
  3496. Z_DEFAULT_COMPRESSION);
  3497. zipWriteInFileInZip(unaligned_apk, data.ptr(), data.size());
  3498. zipCloseFileInZip(unaligned_apk);
  3499. }
  3500. ret = unzGoToNextFile(pkg);
  3501. }
  3502. // Process deferred icon.xml and replace it's data with themed_icon.xml.
  3503. if (monochrome.is_valid() && !monochrome->is_empty()) {
  3504. print_line("ADDING: " + ICON_XML_PATH + " (replacing with themed_icon.xml data)");
  3505. const bool uncompressed = icon_xml_compression_method == 0;
  3506. zip_fileinfo zipfi = get_zip_fileinfo();
  3507. zipOpenNewFileInZip(unaligned_apk,
  3508. ICON_XML_PATH.utf8().get_data(),
  3509. &zipfi,
  3510. nullptr,
  3511. 0,
  3512. nullptr,
  3513. 0,
  3514. nullptr,
  3515. uncompressed ? 0 : Z_DEFLATED,
  3516. Z_DEFAULT_COMPRESSION);
  3517. zipWriteInFileInZip(unaligned_apk, themed_icon_xml_data.ptr(), themed_icon_xml_data.size());
  3518. zipCloseFileInZip(unaligned_apk);
  3519. }
  3520. if (!invalid_abis.is_empty()) {
  3521. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Missing libraries in the export template for the selected architectures: %s. Please build a template with all required libraries, or uncheck the missing architectures in the export preset."), join_abis(invalid_abis, ", ", false)));
  3522. CLEANUP_AND_RETURN(ERR_FILE_NOT_FOUND);
  3523. }
  3524. if (ep.step(TTR("Adding files..."), 1)) {
  3525. CLEANUP_AND_RETURN(ERR_SKIP);
  3526. }
  3527. err = OK;
  3528. if (p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) {
  3529. APKExportData ed;
  3530. ed.ep = &ep;
  3531. ed.apk = unaligned_apk;
  3532. err = export_project_files(p_preset, p_debug, ignore_apk_file, nullptr, &ed, save_apk_so);
  3533. } else {
  3534. if (apk_expansion) {
  3535. err = save_apk_expansion_file(p_preset, p_debug, p_path);
  3536. if (err != OK) {
  3537. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not write expansion package file!"));
  3538. return err;
  3539. }
  3540. } else {
  3541. APKExportData ed;
  3542. ed.ep = &ep;
  3543. ed.apk = unaligned_apk;
  3544. ed.pd.path = "assets.sparsepck";
  3545. ed.pd.use_sparse_pck = true;
  3546. err = export_project_files(p_preset, p_debug, save_apk_file, nullptr, &ed, save_apk_so);
  3547. Vector<uint8_t> enc_data;
  3548. err = _generate_sparse_pck_metadata(p_preset, ed.pd, enc_data);
  3549. if (err != OK) {
  3550. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not generate sparse pck metadata!"));
  3551. return err;
  3552. }
  3553. store_in_apk(&ed, "assets/assets.sparsepck", enc_data, 0);
  3554. }
  3555. }
  3556. if (err != OK) {
  3557. unzClose(pkg);
  3558. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not export project files.")));
  3559. CLEANUP_AND_RETURN(ERR_SKIP);
  3560. }
  3561. zip_fileinfo zipfi = get_zip_fileinfo();
  3562. zipOpenNewFileInZip(unaligned_apk,
  3563. "assets/_cl_",
  3564. &zipfi,
  3565. nullptr,
  3566. 0,
  3567. nullptr,
  3568. 0,
  3569. nullptr,
  3570. 0, // No compress (little size gain and potentially slower startup)
  3571. Z_DEFAULT_COMPRESSION);
  3572. zipWriteInFileInZip(unaligned_apk, command_line_flags.ptr(), command_line_flags.size());
  3573. zipCloseFileInZip(unaligned_apk);
  3574. zipClose(unaligned_apk, nullptr);
  3575. unzClose(pkg);
  3576. // Let's zip-align (must be done before signing)
  3577. static const int PAGE_SIZE_KB = 16 * 1024;
  3578. static const int ZIP_ALIGNMENT = 4;
  3579. // If we're not signing the apk, then the next step should be the last.
  3580. const int next_step = should_sign ? 103 : 105;
  3581. if (ep.step(TTR("Aligning APK..."), next_step)) {
  3582. CLEANUP_AND_RETURN(ERR_SKIP);
  3583. }
  3584. unzFile tmp_unaligned = unzOpen2(tmp_unaligned_path.utf8().get_data(), &io);
  3585. if (!tmp_unaligned) {
  3586. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not unzip temporary unaligned APK.")));
  3587. CLEANUP_AND_RETURN(ERR_FILE_NOT_FOUND);
  3588. }
  3589. ret = unzGoToFirstFile(tmp_unaligned);
  3590. io2 = zipio_create_io(&io2_fa);
  3591. zipFile final_apk = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io2);
  3592. // Take files from the unaligned APK and write them out to the aligned one
  3593. // in raw mode, i.e. not uncompressing and recompressing, aligning them as needed,
  3594. // following what is done in https://github.com/android/platform_build/blob/master/tools/zipalign/ZipAlign.cpp
  3595. int bias = 0;
  3596. while (ret == UNZ_OK) {
  3597. unz_file_info info;
  3598. memset(&info, 0, sizeof(info));
  3599. char fname[16384];
  3600. char extra[16384];
  3601. ret = unzGetCurrentFileInfo(tmp_unaligned, &info, fname, 16384, extra, 16384 - ZIP_ALIGNMENT, nullptr, 0);
  3602. if (ret != UNZ_OK) {
  3603. break;
  3604. }
  3605. String file = String::utf8(fname);
  3606. Vector<uint8_t> data;
  3607. data.resize(info.compressed_size);
  3608. // read
  3609. int method, level;
  3610. unzOpenCurrentFile2(tmp_unaligned, &method, &level, 1); // raw read
  3611. long file_offset = unzGetCurrentFileZStreamPos64(tmp_unaligned);
  3612. unzReadCurrentFile(tmp_unaligned, data.ptrw(), data.size());
  3613. unzCloseCurrentFile(tmp_unaligned);
  3614. // align
  3615. int padding = 0;
  3616. if (!info.compression_method) {
  3617. // Uncompressed file => Align
  3618. long new_offset = file_offset + bias;
  3619. const char *ext = strrchr(fname, '.');
  3620. if (ext && strcmp(ext, ".so") == 0) {
  3621. padding = (PAGE_SIZE_KB - (new_offset % PAGE_SIZE_KB)) % PAGE_SIZE_KB;
  3622. } else {
  3623. padding = (ZIP_ALIGNMENT - (new_offset % ZIP_ALIGNMENT)) % ZIP_ALIGNMENT;
  3624. }
  3625. }
  3626. memset(extra + info.size_file_extra, 0, padding);
  3627. zip_fileinfo fileinfo = get_zip_fileinfo();
  3628. zipOpenNewFileInZip2(final_apk,
  3629. file.utf8().get_data(),
  3630. &fileinfo,
  3631. extra,
  3632. info.size_file_extra + padding,
  3633. nullptr,
  3634. 0,
  3635. nullptr,
  3636. method,
  3637. level,
  3638. 1); // raw write
  3639. zipWriteInFileInZip(final_apk, data.ptr(), data.size());
  3640. zipCloseFileInZipRaw(final_apk, info.uncompressed_size, info.crc);
  3641. bias += padding;
  3642. ret = unzGoToNextFile(tmp_unaligned);
  3643. }
  3644. zipClose(final_apk, nullptr);
  3645. unzClose(tmp_unaligned);
  3646. if (should_sign) {
  3647. // Signing must be done last as any additional modifications to the
  3648. // file will invalidate the signature.
  3649. err = sign_apk(p_preset, p_debug, p_path, ep);
  3650. if (err != OK) {
  3651. // Message is supplied by the subroutine method.
  3652. CLEANUP_AND_RETURN(err);
  3653. }
  3654. }
  3655. CLEANUP_AND_RETURN(OK);
  3656. }
  3657. void EditorExportPlatformAndroid::get_platform_features(List<String> *r_features) const {
  3658. r_features->push_back("mobile");
  3659. r_features->push_back("android");
  3660. }
  3661. void EditorExportPlatformAndroid::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) {
  3662. }
  3663. EditorExportPlatformAndroid::EditorExportPlatformAndroid() {
  3664. if (EditorNode::get_singleton()) {
  3665. Ref<Image> img = memnew(Image);
  3666. const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
  3667. ImageLoaderSVG::create_image_from_string(img, _android_logo_svg, EDSCALE, upsample, false);
  3668. logo = ImageTexture::create_from_image(img);
  3669. ImageLoaderSVG::create_image_from_string(img, _android_run_icon_svg, EDSCALE, upsample, false);
  3670. run_icon = ImageTexture::create_from_image(img);
  3671. devices_changed.set();
  3672. #ifndef DISABLE_DEPRECATED
  3673. android_plugins_changed.set();
  3674. #endif // DISABLE_DEPRECATED
  3675. #ifndef ANDROID_ENABLED
  3676. _create_editor_debug_keystore_if_needed();
  3677. _update_preset_status();
  3678. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  3679. #endif
  3680. }
  3681. }
  3682. EditorExportPlatformAndroid::~EditorExportPlatformAndroid() {
  3683. #ifndef ANDROID_ENABLED
  3684. quit_request.set();
  3685. if (check_for_changes_thread.is_started()) {
  3686. check_for_changes_thread.wait_to_finish();
  3687. }
  3688. #endif
  3689. }