2
0

export_plugin.cpp 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440
  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 "gradle_export_util.h"
  32. #include "logo_svg.gen.h"
  33. #include "run_icon_svg.gen.h"
  34. #include "core/config/project_settings.h"
  35. #include "core/io/dir_access.h"
  36. #include "core/io/file_access.h"
  37. #include "core/io/image_loader.h"
  38. #include "core/io/json.h"
  39. #include "core/io/marshalls.h"
  40. #include "core/version.h"
  41. #include "drivers/png/png_driver_common.h"
  42. #include "editor/editor_log.h"
  43. #include "editor/editor_node.h"
  44. #include "editor/editor_paths.h"
  45. #include "editor/editor_scale.h"
  46. #include "editor/editor_settings.h"
  47. #include "editor/import/resource_importer_texture_settings.h"
  48. #include "main/splash.gen.h"
  49. #include "scene/resources/image_texture.h"
  50. #include "modules/modules_enabled.gen.h" // For mono and svg.
  51. #ifdef MODULE_SVG_ENABLED
  52. #include "modules/svg/image_loader_svg.h"
  53. #endif
  54. #include <string.h>
  55. static const char *android_perms[] = {
  56. "ACCESS_CHECKIN_PROPERTIES",
  57. "ACCESS_COARSE_LOCATION",
  58. "ACCESS_FINE_LOCATION",
  59. "ACCESS_LOCATION_EXTRA_COMMANDS",
  60. "ACCESS_MOCK_LOCATION",
  61. "ACCESS_NETWORK_STATE",
  62. "ACCESS_SURFACE_FLINGER",
  63. "ACCESS_WIFI_STATE",
  64. "ACCOUNT_MANAGER",
  65. "ADD_VOICEMAIL",
  66. "AUTHENTICATE_ACCOUNTS",
  67. "BATTERY_STATS",
  68. "BIND_ACCESSIBILITY_SERVICE",
  69. "BIND_APPWIDGET",
  70. "BIND_DEVICE_ADMIN",
  71. "BIND_INPUT_METHOD",
  72. "BIND_NFC_SERVICE",
  73. "BIND_NOTIFICATION_LISTENER_SERVICE",
  74. "BIND_PRINT_SERVICE",
  75. "BIND_REMOTEVIEWS",
  76. "BIND_TEXT_SERVICE",
  77. "BIND_VPN_SERVICE",
  78. "BIND_WALLPAPER",
  79. "BLUETOOTH",
  80. "BLUETOOTH_ADMIN",
  81. "BLUETOOTH_PRIVILEGED",
  82. "BRICK",
  83. "BROADCAST_PACKAGE_REMOVED",
  84. "BROADCAST_SMS",
  85. "BROADCAST_STICKY",
  86. "BROADCAST_WAP_PUSH",
  87. "CALL_PHONE",
  88. "CALL_PRIVILEGED",
  89. "CAMERA",
  90. "CAPTURE_AUDIO_OUTPUT",
  91. "CAPTURE_SECURE_VIDEO_OUTPUT",
  92. "CAPTURE_VIDEO_OUTPUT",
  93. "CHANGE_COMPONENT_ENABLED_STATE",
  94. "CHANGE_CONFIGURATION",
  95. "CHANGE_NETWORK_STATE",
  96. "CHANGE_WIFI_MULTICAST_STATE",
  97. "CHANGE_WIFI_STATE",
  98. "CLEAR_APP_CACHE",
  99. "CLEAR_APP_USER_DATA",
  100. "CONTROL_LOCATION_UPDATES",
  101. "DELETE_CACHE_FILES",
  102. "DELETE_PACKAGES",
  103. "DEVICE_POWER",
  104. "DIAGNOSTIC",
  105. "DISABLE_KEYGUARD",
  106. "DUMP",
  107. "EXPAND_STATUS_BAR",
  108. "FACTORY_TEST",
  109. "FLASHLIGHT",
  110. "FORCE_BACK",
  111. "GET_ACCOUNTS",
  112. "GET_PACKAGE_SIZE",
  113. "GET_TASKS",
  114. "GET_TOP_ACTIVITY_INFO",
  115. "GLOBAL_SEARCH",
  116. "HARDWARE_TEST",
  117. "INJECT_EVENTS",
  118. "INSTALL_LOCATION_PROVIDER",
  119. "INSTALL_PACKAGES",
  120. "INSTALL_SHORTCUT",
  121. "INTERNAL_SYSTEM_WINDOW",
  122. "INTERNET",
  123. "KILL_BACKGROUND_PROCESSES",
  124. "LOCATION_HARDWARE",
  125. "MANAGE_ACCOUNTS",
  126. "MANAGE_APP_TOKENS",
  127. "MANAGE_DOCUMENTS",
  128. "MANAGE_EXTERNAL_STORAGE",
  129. "MASTER_CLEAR",
  130. "MEDIA_CONTENT_CONTROL",
  131. "MODIFY_AUDIO_SETTINGS",
  132. "MODIFY_PHONE_STATE",
  133. "MOUNT_FORMAT_FILESYSTEMS",
  134. "MOUNT_UNMOUNT_FILESYSTEMS",
  135. "NFC",
  136. "PERSISTENT_ACTIVITY",
  137. "PROCESS_OUTGOING_CALLS",
  138. "READ_CALENDAR",
  139. "READ_CALL_LOG",
  140. "READ_CONTACTS",
  141. "READ_EXTERNAL_STORAGE",
  142. "READ_FRAME_BUFFER",
  143. "READ_HISTORY_BOOKMARKS",
  144. "READ_INPUT_STATE",
  145. "READ_LOGS",
  146. "READ_PHONE_STATE",
  147. "READ_PROFILE",
  148. "READ_SMS",
  149. "READ_SOCIAL_STREAM",
  150. "READ_SYNC_SETTINGS",
  151. "READ_SYNC_STATS",
  152. "READ_USER_DICTIONARY",
  153. "REBOOT",
  154. "RECEIVE_BOOT_COMPLETED",
  155. "RECEIVE_MMS",
  156. "RECEIVE_SMS",
  157. "RECEIVE_WAP_PUSH",
  158. "RECORD_AUDIO",
  159. "REORDER_TASKS",
  160. "RESTART_PACKAGES",
  161. "SEND_RESPOND_VIA_MESSAGE",
  162. "SEND_SMS",
  163. "SET_ACTIVITY_WATCHER",
  164. "SET_ALARM",
  165. "SET_ALWAYS_FINISH",
  166. "SET_ANIMATION_SCALE",
  167. "SET_DEBUG_APP",
  168. "SET_ORIENTATION",
  169. "SET_POINTER_SPEED",
  170. "SET_PREFERRED_APPLICATIONS",
  171. "SET_PROCESS_LIMIT",
  172. "SET_TIME",
  173. "SET_TIME_ZONE",
  174. "SET_WALLPAPER",
  175. "SET_WALLPAPER_HINTS",
  176. "SIGNAL_PERSISTENT_PROCESSES",
  177. "STATUS_BAR",
  178. "SUBSCRIBED_FEEDS_READ",
  179. "SUBSCRIBED_FEEDS_WRITE",
  180. "SYSTEM_ALERT_WINDOW",
  181. "TRANSMIT_IR",
  182. "UNINSTALL_SHORTCUT",
  183. "UPDATE_DEVICE_STATS",
  184. "USE_CREDENTIALS",
  185. "USE_SIP",
  186. "VIBRATE",
  187. "WAKE_LOCK",
  188. "WRITE_APN_SETTINGS",
  189. "WRITE_CALENDAR",
  190. "WRITE_CALL_LOG",
  191. "WRITE_CONTACTS",
  192. "WRITE_EXTERNAL_STORAGE",
  193. "WRITE_GSERVICES",
  194. "WRITE_HISTORY_BOOKMARKS",
  195. "WRITE_PROFILE",
  196. "WRITE_SECURE_SETTINGS",
  197. "WRITE_SETTINGS",
  198. "WRITE_SMS",
  199. "WRITE_SOCIAL_STREAM",
  200. "WRITE_SYNC_SETTINGS",
  201. "WRITE_USER_DICTIONARY",
  202. nullptr
  203. };
  204. static const char *SPLASH_IMAGE_EXPORT_PATH = "res/drawable-nodpi/splash.png";
  205. static const char *LEGACY_BUILD_SPLASH_IMAGE_EXPORT_PATH = "res/drawable-nodpi-v4/splash.png";
  206. static const char *SPLASH_BG_COLOR_PATH = "res/drawable-nodpi/splash_bg_color.png";
  207. static const char *LEGACY_BUILD_SPLASH_BG_COLOR_PATH = "res/drawable-nodpi-v4/splash_bg_color.png";
  208. static const char *SPLASH_CONFIG_PATH = "res://android/build/res/drawable/splash_drawable.xml";
  209. static const char *GDEXTENSION_LIBS_PATH = "res://android/build/libs/gdextensionlibs.json";
  210. static const int icon_densities_count = 6;
  211. static const char *launcher_icon_option = PNAME("launcher_icons/main_192x192");
  212. static const char *launcher_adaptive_icon_foreground_option = PNAME("launcher_icons/adaptive_foreground_432x432");
  213. static const char *launcher_adaptive_icon_background_option = PNAME("launcher_icons/adaptive_background_432x432");
  214. static const LauncherIcon launcher_icons[icon_densities_count] = {
  215. { "res/mipmap-xxxhdpi-v4/icon.png", 192 },
  216. { "res/mipmap-xxhdpi-v4/icon.png", 144 },
  217. { "res/mipmap-xhdpi-v4/icon.png", 96 },
  218. { "res/mipmap-hdpi-v4/icon.png", 72 },
  219. { "res/mipmap-mdpi-v4/icon.png", 48 },
  220. { "res/mipmap/icon.png", 192 }
  221. };
  222. static const LauncherIcon launcher_adaptive_icon_foregrounds[icon_densities_count] = {
  223. { "res/mipmap-xxxhdpi-v4/icon_foreground.png", 432 },
  224. { "res/mipmap-xxhdpi-v4/icon_foreground.png", 324 },
  225. { "res/mipmap-xhdpi-v4/icon_foreground.png", 216 },
  226. { "res/mipmap-hdpi-v4/icon_foreground.png", 162 },
  227. { "res/mipmap-mdpi-v4/icon_foreground.png", 108 },
  228. { "res/mipmap/icon_foreground.png", 432 }
  229. };
  230. static const LauncherIcon launcher_adaptive_icon_backgrounds[icon_densities_count] = {
  231. { "res/mipmap-xxxhdpi-v4/icon_background.png", 432 },
  232. { "res/mipmap-xxhdpi-v4/icon_background.png", 324 },
  233. { "res/mipmap-xhdpi-v4/icon_background.png", 216 },
  234. { "res/mipmap-hdpi-v4/icon_background.png", 162 },
  235. { "res/mipmap-mdpi-v4/icon_background.png", 108 },
  236. { "res/mipmap/icon_background.png", 432 }
  237. };
  238. static const int EXPORT_FORMAT_APK = 0;
  239. static const int EXPORT_FORMAT_AAB = 1;
  240. static const char *APK_ASSETS_DIRECTORY = "res://android/build/assets";
  241. static const char *AAB_ASSETS_DIRECTORY = "res://android/build/assetPacks/installTime/src/main/assets";
  242. static const int OPENGL_MIN_SDK_VERSION = 21; // Should match the value in 'platform/android/java/app/config.gradle#minSdk'
  243. static const int VULKAN_MIN_SDK_VERSION = 24;
  244. static const int DEFAULT_TARGET_SDK_VERSION = 33; // Should match the value in 'platform/android/java/app/config.gradle#targetSdk'
  245. #ifndef ANDROID_ENABLED
  246. void EditorExportPlatformAndroid::_check_for_changes_poll_thread(void *ud) {
  247. EditorExportPlatformAndroid *ea = static_cast<EditorExportPlatformAndroid *>(ud);
  248. while (!ea->quit_request.is_set()) {
  249. #ifndef DISABLE_DEPRECATED
  250. // Check for android plugins updates
  251. {
  252. // Nothing to do if we already know the plugins have changed.
  253. if (!ea->android_plugins_changed.is_set()) {
  254. Vector<PluginConfigAndroid> loaded_plugins = get_plugins();
  255. MutexLock lock(ea->android_plugins_lock);
  256. if (ea->android_plugins.size() != loaded_plugins.size()) {
  257. ea->android_plugins_changed.set();
  258. } else {
  259. for (int i = 0; i < ea->android_plugins.size(); i++) {
  260. if (ea->android_plugins[i].name != loaded_plugins[i].name) {
  261. ea->android_plugins_changed.set();
  262. break;
  263. }
  264. }
  265. }
  266. if (ea->android_plugins_changed.is_set()) {
  267. ea->android_plugins = loaded_plugins;
  268. }
  269. }
  270. }
  271. #endif // DISABLE_DEPRECATED
  272. // Check for devices updates
  273. String adb = get_adb_path();
  274. if (FileAccess::exists(adb)) {
  275. String devices;
  276. List<String> args;
  277. args.push_back("devices");
  278. int ec;
  279. OS::get_singleton()->execute(adb, args, &devices, &ec);
  280. Vector<String> ds = devices.split("\n");
  281. Vector<String> ldevices;
  282. for (int i = 1; i < ds.size(); i++) {
  283. String d = ds[i];
  284. int dpos = d.find("device");
  285. if (dpos == -1) {
  286. continue;
  287. }
  288. d = d.substr(0, dpos).strip_edges();
  289. ldevices.push_back(d);
  290. }
  291. MutexLock lock(ea->device_lock);
  292. bool different = false;
  293. if (ea->devices.size() != ldevices.size()) {
  294. different = true;
  295. } else {
  296. for (int i = 0; i < ea->devices.size(); i++) {
  297. if (ea->devices[i].id != ldevices[i]) {
  298. different = true;
  299. break;
  300. }
  301. }
  302. }
  303. if (different) {
  304. Vector<Device> ndevices;
  305. for (int i = 0; i < ldevices.size(); i++) {
  306. Device d;
  307. d.id = ldevices[i];
  308. for (int j = 0; j < ea->devices.size(); j++) {
  309. if (ea->devices[j].id == ldevices[i]) {
  310. d.description = ea->devices[j].description;
  311. d.name = ea->devices[j].name;
  312. d.api_level = ea->devices[j].api_level;
  313. }
  314. }
  315. if (d.description.is_empty()) {
  316. //in the oven, request!
  317. args.clear();
  318. args.push_back("-s");
  319. args.push_back(d.id);
  320. args.push_back("shell");
  321. args.push_back("getprop");
  322. int ec2;
  323. String dp;
  324. OS::get_singleton()->execute(adb, args, &dp, &ec2);
  325. Vector<String> props = dp.split("\n");
  326. String vendor;
  327. String device;
  328. d.description = "Device ID: " + d.id + "\n";
  329. d.api_level = 0;
  330. for (int j = 0; j < props.size(); j++) {
  331. // got information by `shell cat /system/build.prop` before and its format is "property=value"
  332. // it's now changed to use `shell getporp` because of permission issue with Android 8.0 and above
  333. // its format is "[property]: [value]" so changed it as like build.prop
  334. String p = props[j];
  335. p = p.replace("]: ", "=");
  336. p = p.replace("[", "");
  337. p = p.replace("]", "");
  338. if (p.begins_with("ro.product.model=")) {
  339. device = p.get_slice("=", 1).strip_edges();
  340. } else if (p.begins_with("ro.product.brand=")) {
  341. vendor = p.get_slice("=", 1).strip_edges().capitalize();
  342. } else if (p.begins_with("ro.build.display.id=")) {
  343. d.description += "Build: " + p.get_slice("=", 1).strip_edges() + "\n";
  344. } else if (p.begins_with("ro.build.version.release=")) {
  345. d.description += "Release: " + p.get_slice("=", 1).strip_edges() + "\n";
  346. } else if (p.begins_with("ro.build.version.sdk=")) {
  347. d.api_level = p.get_slice("=", 1).to_int();
  348. } else if (p.begins_with("ro.product.cpu.abi=")) {
  349. d.description += "CPU: " + p.get_slice("=", 1).strip_edges() + "\n";
  350. } else if (p.begins_with("ro.product.manufacturer=")) {
  351. d.description += "Manufacturer: " + p.get_slice("=", 1).strip_edges() + "\n";
  352. } else if (p.begins_with("ro.board.platform=")) {
  353. d.description += "Chipset: " + p.get_slice("=", 1).strip_edges() + "\n";
  354. } else if (p.begins_with("ro.opengles.version=")) {
  355. uint32_t opengl = p.get_slice("=", 1).to_int();
  356. d.description += "OpenGL: " + itos(opengl >> 16) + "." + itos((opengl >> 8) & 0xFF) + "." + itos((opengl)&0xFF) + "\n";
  357. }
  358. }
  359. d.name = vendor + " " + device;
  360. if (device.is_empty()) {
  361. continue;
  362. }
  363. }
  364. ndevices.push_back(d);
  365. }
  366. ea->devices = ndevices;
  367. ea->devices_changed.set();
  368. }
  369. }
  370. uint64_t sleep = 200;
  371. uint64_t wait = 3000000;
  372. uint64_t time = OS::get_singleton()->get_ticks_usec();
  373. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  374. OS::get_singleton()->delay_usec(1000 * sleep);
  375. if (ea->quit_request.is_set()) {
  376. break;
  377. }
  378. }
  379. }
  380. if (EDITOR_GET("export/android/shutdown_adb_on_exit")) {
  381. String adb = get_adb_path();
  382. if (!FileAccess::exists(adb)) {
  383. return; //adb not configured
  384. }
  385. List<String> args;
  386. args.push_back("kill-server");
  387. OS::get_singleton()->execute(adb, args);
  388. }
  389. }
  390. #endif
  391. String EditorExportPlatformAndroid::get_project_name(const String &p_name) const {
  392. String aname;
  393. if (!p_name.is_empty()) {
  394. aname = p_name;
  395. } else {
  396. aname = GLOBAL_GET("application/config/name");
  397. }
  398. if (aname.is_empty()) {
  399. aname = VERSION_NAME;
  400. }
  401. return aname;
  402. }
  403. String EditorExportPlatformAndroid::get_package_name(const String &p_package) const {
  404. String pname = p_package;
  405. String name = get_valid_basename();
  406. pname = pname.replace("$genname", name);
  407. return pname;
  408. }
  409. // Returns the project name without invalid characters
  410. // or the "noname" string if all characters are invalid.
  411. String EditorExportPlatformAndroid::get_valid_basename() const {
  412. String basename = GLOBAL_GET("application/config/name");
  413. basename = basename.to_lower();
  414. String name;
  415. bool first = true;
  416. for (int i = 0; i < basename.length(); i++) {
  417. char32_t c = basename[i];
  418. if (is_digit(c) && first) {
  419. continue;
  420. }
  421. if (is_ascii_alphanumeric_char(c)) {
  422. name += String::chr(c);
  423. first = false;
  424. }
  425. }
  426. if (name.is_empty()) {
  427. name = "noname";
  428. }
  429. return name;
  430. }
  431. String EditorExportPlatformAndroid::get_assets_directory(const Ref<EditorExportPreset> &p_preset, int p_export_format) const {
  432. return p_export_format == EXPORT_FORMAT_AAB ? AAB_ASSETS_DIRECTORY : APK_ASSETS_DIRECTORY;
  433. }
  434. bool EditorExportPlatformAndroid::is_package_name_valid(const String &p_package, String *r_error) const {
  435. String pname = get_package_name(p_package);
  436. if (pname.length() == 0) {
  437. if (r_error) {
  438. *r_error = TTR("Package name is missing.");
  439. }
  440. return false;
  441. }
  442. int segments = 0;
  443. bool first = true;
  444. for (int i = 0; i < pname.length(); i++) {
  445. char32_t c = pname[i];
  446. if (first && c == '.') {
  447. if (r_error) {
  448. *r_error = TTR("Package segments must be of non-zero length.");
  449. }
  450. return false;
  451. }
  452. if (c == '.') {
  453. segments++;
  454. first = true;
  455. continue;
  456. }
  457. if (!is_ascii_identifier_char(c)) {
  458. if (r_error) {
  459. *r_error = vformat(TTR("The character '%s' is not allowed in Android application package names."), String::chr(c));
  460. }
  461. return false;
  462. }
  463. if (first && is_digit(c)) {
  464. if (r_error) {
  465. *r_error = TTR("A digit cannot be the first character in a package segment.");
  466. }
  467. return false;
  468. }
  469. if (first && is_underscore(c)) {
  470. if (r_error) {
  471. *r_error = vformat(TTR("The character '%s' cannot be the first character in a package segment."), String::chr(c));
  472. }
  473. return false;
  474. }
  475. first = false;
  476. }
  477. if (segments == 0) {
  478. if (r_error) {
  479. *r_error = TTR("The package must have at least one '.' separator.");
  480. }
  481. return false;
  482. }
  483. if (first) {
  484. if (r_error) {
  485. *r_error = TTR("Package segments must be of non-zero length.");
  486. }
  487. return false;
  488. }
  489. if (p_package.find("$genname") >= 0 && !is_project_name_valid()) {
  490. if (r_error) {
  491. *r_error = TTR("The project name does not meet the requirement for the package name format. Please explicitly specify the package name.");
  492. }
  493. return false;
  494. }
  495. return true;
  496. }
  497. bool EditorExportPlatformAndroid::is_project_name_valid() const {
  498. // Get the original project name and convert to lowercase.
  499. String basename = GLOBAL_GET("application/config/name");
  500. basename = basename.to_lower();
  501. // Check if there are invalid characters.
  502. if (basename != get_valid_basename()) {
  503. return false;
  504. }
  505. return true;
  506. }
  507. bool EditorExportPlatformAndroid::_should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
  508. /*
  509. * By not compressing files with little or no benefit in doing so,
  510. * a performance gain is expected at runtime. Moreover, if the APK is
  511. * zip-aligned, assets stored as they are can be efficiently read by
  512. * Android by memory-mapping them.
  513. */
  514. // -- Unconditional uncompress to mimic AAPT plus some other
  515. static const char *unconditional_compress_ext[] = {
  516. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  517. // These formats are already compressed, or don't compress well:
  518. ".jpg", ".jpeg", ".png", ".gif",
  519. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  520. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  521. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  522. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  523. ".amr", ".awb", ".wma", ".wmv",
  524. // Godot-specific:
  525. ".webp", // Same reasoning as .png
  526. ".cfb", // Don't let small config files slow-down startup
  527. ".scn", // Binary scenes are usually already compressed
  528. ".ctex", // Streamable textures are usually already compressed
  529. // Trailer for easier processing
  530. nullptr
  531. };
  532. for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
  533. if (p_path.to_lower().ends_with(String(*ext))) {
  534. return false;
  535. }
  536. }
  537. // -- Compressed resource?
  538. if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
  539. // Already compressed
  540. return false;
  541. }
  542. // --- TODO: Decide on texture resources according to their image compression setting
  543. return true;
  544. }
  545. zip_fileinfo EditorExportPlatformAndroid::get_zip_fileinfo() {
  546. OS::DateTime dt = OS::get_singleton()->get_datetime();
  547. zip_fileinfo zipfi;
  548. zipfi.tmz_date.tm_year = dt.year;
  549. zipfi.tmz_date.tm_mon = dt.month - 1; // tm_mon is zero indexed
  550. zipfi.tmz_date.tm_mday = dt.day;
  551. zipfi.tmz_date.tm_hour = dt.hour;
  552. zipfi.tmz_date.tm_min = dt.minute;
  553. zipfi.tmz_date.tm_sec = dt.second;
  554. zipfi.dosDate = 0;
  555. zipfi.external_fa = 0;
  556. zipfi.internal_fa = 0;
  557. return zipfi;
  558. }
  559. Vector<EditorExportPlatformAndroid::ABI> EditorExportPlatformAndroid::get_abis() {
  560. // Should have the same order and size as get_archs.
  561. Vector<ABI> abis;
  562. abis.push_back(ABI("armeabi-v7a", "arm32"));
  563. abis.push_back(ABI("arm64-v8a", "arm64"));
  564. abis.push_back(ABI("x86", "x86_32"));
  565. abis.push_back(ABI("x86_64", "x86_64"));
  566. return abis;
  567. }
  568. #ifndef DISABLE_DEPRECATED
  569. /// List the gdap files in the directory specified by the p_path parameter.
  570. Vector<String> EditorExportPlatformAndroid::list_gdap_files(const String &p_path) {
  571. Vector<String> dir_files;
  572. Ref<DirAccess> da = DirAccess::open(p_path);
  573. if (da.is_valid()) {
  574. da->list_dir_begin();
  575. while (true) {
  576. String file = da->get_next();
  577. if (file.is_empty()) {
  578. break;
  579. }
  580. if (da->current_is_dir() || da->current_is_hidden()) {
  581. continue;
  582. }
  583. if (file.ends_with(PluginConfigAndroid::PLUGIN_CONFIG_EXT)) {
  584. dir_files.push_back(file);
  585. }
  586. }
  587. da->list_dir_end();
  588. }
  589. return dir_files;
  590. }
  591. Vector<PluginConfigAndroid> EditorExportPlatformAndroid::get_plugins() {
  592. Vector<PluginConfigAndroid> loaded_plugins;
  593. String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().path_join("android/plugins");
  594. // Add the prebuilt plugins
  595. loaded_plugins.append_array(PluginConfigAndroid::get_prebuilt_plugins(plugins_dir));
  596. if (DirAccess::exists(plugins_dir)) {
  597. Vector<String> plugins_filenames = list_gdap_files(plugins_dir);
  598. if (!plugins_filenames.is_empty()) {
  599. Ref<ConfigFile> config_file = memnew(ConfigFile);
  600. for (int i = 0; i < plugins_filenames.size(); i++) {
  601. PluginConfigAndroid config = PluginConfigAndroid::load_plugin_config(config_file, plugins_dir.path_join(plugins_filenames[i]));
  602. if (config.valid_config) {
  603. loaded_plugins.push_back(config);
  604. } else {
  605. print_error("Invalid plugin config file " + plugins_filenames[i]);
  606. }
  607. }
  608. }
  609. }
  610. return loaded_plugins;
  611. }
  612. Vector<PluginConfigAndroid> EditorExportPlatformAndroid::get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) {
  613. Vector<PluginConfigAndroid> enabled_plugins;
  614. Vector<PluginConfigAndroid> all_plugins = get_plugins();
  615. for (int i = 0; i < all_plugins.size(); i++) {
  616. PluginConfigAndroid plugin = all_plugins[i];
  617. bool enabled = p_presets->get("plugins/" + plugin.name);
  618. if (enabled) {
  619. enabled_plugins.push_back(plugin);
  620. }
  621. }
  622. return enabled_plugins;
  623. }
  624. #endif // DISABLE_DEPRECATED
  625. Error EditorExportPlatformAndroid::store_in_apk(APKExportData *ed, const String &p_path, const Vector<uint8_t> &p_data, int compression_method) {
  626. zip_fileinfo zipfi = get_zip_fileinfo();
  627. zipOpenNewFileInZip(ed->apk,
  628. p_path.utf8().get_data(),
  629. &zipfi,
  630. nullptr,
  631. 0,
  632. nullptr,
  633. 0,
  634. nullptr,
  635. compression_method,
  636. Z_DEFAULT_COMPRESSION);
  637. zipWriteInFileInZip(ed->apk, p_data.ptr(), p_data.size());
  638. zipCloseFileInZip(ed->apk);
  639. return OK;
  640. }
  641. Error EditorExportPlatformAndroid::save_apk_so(void *p_userdata, const SharedObject &p_so) {
  642. if (!p_so.path.get_file().begins_with("lib")) {
  643. String err = "Android .so file names must start with \"lib\", but got: " + p_so.path;
  644. ERR_PRINT(err);
  645. return FAILED;
  646. }
  647. APKExportData *ed = static_cast<APKExportData *>(p_userdata);
  648. Vector<ABI> abis = get_abis();
  649. bool exported = false;
  650. for (int i = 0; i < p_so.tags.size(); ++i) {
  651. // shared objects can be fat (compatible with multiple ABIs)
  652. int abi_index = -1;
  653. for (int j = 0; j < abis.size(); ++j) {
  654. if (abis[j].abi == p_so.tags[i] || abis[j].arch == p_so.tags[i]) {
  655. abi_index = j;
  656. break;
  657. }
  658. }
  659. if (abi_index != -1) {
  660. exported = true;
  661. String abi = abis[abi_index].abi;
  662. String dst_path = String("lib").path_join(abi).path_join(p_so.path.get_file());
  663. Vector<uint8_t> array = FileAccess::get_file_as_bytes(p_so.path);
  664. Error store_err = store_in_apk(ed, dst_path, array);
  665. ERR_FAIL_COND_V_MSG(store_err, store_err, "Cannot store in apk file '" + dst_path + "'.");
  666. }
  667. }
  668. if (!exported) {
  669. 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));
  670. return FAILED;
  671. }
  672. return OK;
  673. }
  674. 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) {
  675. APKExportData *ed = static_cast<APKExportData *>(p_userdata);
  676. String dst_path = p_path.replace_first("res://", "assets/");
  677. store_in_apk(ed, dst_path, p_data, _should_compress_asset(p_path, p_data) ? Z_DEFLATED : 0);
  678. return OK;
  679. }
  680. 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) {
  681. return OK;
  682. }
  683. Error EditorExportPlatformAndroid::copy_gradle_so(void *p_userdata, const SharedObject &p_so) {
  684. ERR_FAIL_COND_V_MSG(!p_so.path.get_file().begins_with("lib"), FAILED,
  685. "Android .so file names must start with \"lib\", but got: " + p_so.path);
  686. Vector<ABI> abis = get_abis();
  687. CustomExportData *export_data = static_cast<CustomExportData *>(p_userdata);
  688. bool exported = false;
  689. for (int i = 0; i < p_so.tags.size(); ++i) {
  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 base = "res://android/build/libs";
  700. String type = export_data->debug ? "debug" : "release";
  701. String abi = abis[abi_index].abi;
  702. String filename = p_so.path.get_file();
  703. String dst_path = base.path_join(type).path_join(abi).path_join(filename);
  704. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_so.path);
  705. print_verbose("Copying .so file from " + p_so.path + " to " + dst_path);
  706. Error err = store_file_at_path(dst_path, data);
  707. ERR_FAIL_COND_V_MSG(err, err, "Failed to copy .so file from " + p_so.path + " to " + dst_path);
  708. export_data->libs.push_back(dst_path);
  709. }
  710. }
  711. ERR_FAIL_COND_V_MSG(!exported, FAILED,
  712. "Cannot determine architecture for library \"" + p_so.path + "\". One of the supported architectures must be used as a tag:" + join_abis(abis, " ", true));
  713. return OK;
  714. }
  715. bool EditorExportPlatformAndroid::_has_read_write_storage_permission(const Vector<String> &p_permissions) {
  716. return p_permissions.find("android.permission.READ_EXTERNAL_STORAGE") != -1 || p_permissions.find("android.permission.WRITE_EXTERNAL_STORAGE") != -1;
  717. }
  718. bool EditorExportPlatformAndroid::_has_manage_external_storage_permission(const Vector<String> &p_permissions) {
  719. return p_permissions.find("android.permission.MANAGE_EXTERNAL_STORAGE") != -1;
  720. }
  721. bool EditorExportPlatformAndroid::_uses_vulkan() {
  722. String current_renderer = GLOBAL_GET("rendering/renderer/rendering_method.mobile");
  723. bool uses_vulkan = (current_renderer == "forward_plus" || current_renderer == "mobile") && GLOBAL_GET("rendering/rendering_device/driver.android") == "vulkan";
  724. return uses_vulkan;
  725. }
  726. void EditorExportPlatformAndroid::_get_permissions(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, Vector<String> &r_permissions) {
  727. const char **aperms = android_perms;
  728. while (*aperms) {
  729. bool enabled = p_preset->get("permissions/" + String(*aperms).to_lower());
  730. if (enabled) {
  731. r_permissions.push_back("android.permission." + String(*aperms));
  732. }
  733. aperms++;
  734. }
  735. PackedStringArray user_perms = p_preset->get("permissions/custom_permissions");
  736. for (int i = 0; i < user_perms.size(); i++) {
  737. String user_perm = user_perms[i].strip_edges();
  738. if (!user_perm.is_empty()) {
  739. r_permissions.push_back(user_perm);
  740. }
  741. }
  742. if (p_give_internet) {
  743. if (r_permissions.find("android.permission.INTERNET") == -1) {
  744. r_permissions.push_back("android.permission.INTERNET");
  745. }
  746. }
  747. }
  748. void EditorExportPlatformAndroid::_write_tmp_manifest(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, bool p_debug) {
  749. print_verbose("Building temporary manifest...");
  750. String manifest_text =
  751. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  752. "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
  753. " xmlns:tools=\"http://schemas.android.com/tools\">\n";
  754. manifest_text += _get_screen_sizes_tag(p_preset);
  755. manifest_text += _get_gles_tag();
  756. Vector<String> perms;
  757. _get_permissions(p_preset, p_give_internet, perms);
  758. for (int i = 0; i < perms.size(); i++) {
  759. String permission = perms.get(i);
  760. if (permission == "android.permission.WRITE_EXTERNAL_STORAGE" || (permission == "android.permission.READ_EXTERNAL_STORAGE" && _has_manage_external_storage_permission(perms))) {
  761. manifest_text += vformat(" <uses-permission android:name=\"%s\" android:maxSdkVersion=\"29\" />\n", permission);
  762. } else {
  763. manifest_text += vformat(" <uses-permission android:name=\"%s\" />\n", permission);
  764. }
  765. }
  766. if (_uses_vulkan()) {
  767. manifest_text += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vulkan.level\" android:required=\"false\" android:version=\"1\" />\n";
  768. manifest_text += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vulkan.version\" android:required=\"true\" android:version=\"0x400003\" />\n";
  769. }
  770. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  771. for (int i = 0; i < export_plugins.size(); i++) {
  772. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  773. const String contents = export_plugins[i]->get_android_manifest_element_contents(Ref<EditorExportPlatform>(this), p_debug);
  774. if (!contents.is_empty()) {
  775. manifest_text += contents;
  776. manifest_text += "\n";
  777. }
  778. }
  779. }
  780. manifest_text += _get_application_tag(Ref<EditorExportPlatform>(this), p_preset, _has_read_write_storage_permission(perms), p_debug);
  781. manifest_text += "</manifest>\n";
  782. String manifest_path = vformat("res://android/build/src/%s/AndroidManifest.xml", (p_debug ? "debug" : "release"));
  783. print_verbose("Storing manifest into " + manifest_path + ": " + "\n" + manifest_text);
  784. store_string_at_path(manifest_path, manifest_text);
  785. }
  786. void EditorExportPlatformAndroid::_fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet) {
  787. // Leaving the unused types commented because looking these constants up
  788. // again later would be annoying
  789. // const int CHUNK_AXML_FILE = 0x00080003;
  790. // const int CHUNK_RESOURCEIDS = 0x00080180;
  791. const int CHUNK_STRINGS = 0x001C0001;
  792. // const int CHUNK_XML_END_NAMESPACE = 0x00100101;
  793. const int CHUNK_XML_END_TAG = 0x00100103;
  794. // const int CHUNK_XML_START_NAMESPACE = 0x00100100;
  795. const int CHUNK_XML_START_TAG = 0x00100102;
  796. // const int CHUNK_XML_TEXT = 0x00100104;
  797. const int UTF8_FLAG = 0x00000100;
  798. Vector<String> string_table;
  799. uint32_t ofs = 8;
  800. uint32_t string_count = 0;
  801. uint32_t string_flags = 0;
  802. uint32_t string_data_offset = 0;
  803. uint32_t string_table_begins = 0;
  804. uint32_t string_table_ends = 0;
  805. Vector<uint8_t> stable_extra;
  806. String version_name = p_preset->get_version("version/name");
  807. int version_code = p_preset->get("version/code");
  808. String package_name = p_preset->get("package/unique_name");
  809. const int screen_orientation =
  810. _get_android_orientation_value(DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation"))));
  811. bool screen_support_small = p_preset->get("screen/support_small");
  812. bool screen_support_normal = p_preset->get("screen/support_normal");
  813. bool screen_support_large = p_preset->get("screen/support_large");
  814. bool screen_support_xlarge = p_preset->get("screen/support_xlarge");
  815. bool backup_allowed = p_preset->get("user_data_backup/allow");
  816. int app_category = p_preset->get("package/app_category");
  817. bool retain_data_on_uninstall = p_preset->get("package/retain_data_on_uninstall");
  818. bool exclude_from_recents = p_preset->get("package/exclude_from_recents");
  819. bool is_resizeable = bool(GLOBAL_GET("display/window/size/resizable"));
  820. Vector<String> perms;
  821. // Write permissions into the perms variable.
  822. _get_permissions(p_preset, p_give_internet, perms);
  823. bool has_read_write_storage_permission = _has_read_write_storage_permission(perms);
  824. while (ofs < (uint32_t)p_manifest.size()) {
  825. uint32_t chunk = decode_uint32(&p_manifest[ofs]);
  826. uint32_t size = decode_uint32(&p_manifest[ofs + 4]);
  827. switch (chunk) {
  828. case CHUNK_STRINGS: {
  829. int iofs = ofs + 8;
  830. string_count = decode_uint32(&p_manifest[iofs]);
  831. string_flags = decode_uint32(&p_manifest[iofs + 8]);
  832. string_data_offset = decode_uint32(&p_manifest[iofs + 12]);
  833. uint32_t st_offset = iofs + 20;
  834. string_table.resize(string_count);
  835. uint32_t string_end = 0;
  836. string_table_begins = st_offset;
  837. for (uint32_t i = 0; i < string_count; i++) {
  838. uint32_t string_at = decode_uint32(&p_manifest[st_offset + i * 4]);
  839. string_at += st_offset + string_count * 4;
  840. ERR_FAIL_COND_MSG(string_flags & UTF8_FLAG, "Unimplemented, can't read UTF-8 string table.");
  841. if (string_flags & UTF8_FLAG) {
  842. } else {
  843. uint32_t len = decode_uint16(&p_manifest[string_at]);
  844. Vector<char32_t> ucstring;
  845. ucstring.resize(len + 1);
  846. for (uint32_t j = 0; j < len; j++) {
  847. uint16_t c = decode_uint16(&p_manifest[string_at + 2 + 2 * j]);
  848. ucstring.write[j] = c;
  849. }
  850. string_end = MAX(string_at + 2 + 2 * len, string_end);
  851. ucstring.write[len] = 0;
  852. string_table.write[i] = ucstring.ptr();
  853. }
  854. }
  855. for (uint32_t i = string_end; i < (ofs + size); i++) {
  856. stable_extra.push_back(p_manifest[i]);
  857. }
  858. string_table_ends = ofs + size;
  859. } break;
  860. case CHUNK_XML_START_TAG: {
  861. int iofs = ofs + 8;
  862. uint32_t name = decode_uint32(&p_manifest[iofs + 12]);
  863. String tname = string_table[name];
  864. uint32_t attrcount = decode_uint32(&p_manifest[iofs + 20]);
  865. iofs += 28;
  866. for (uint32_t i = 0; i < attrcount; i++) {
  867. uint32_t attr_nspace = decode_uint32(&p_manifest[iofs]);
  868. uint32_t attr_name = decode_uint32(&p_manifest[iofs + 4]);
  869. uint32_t attr_value = decode_uint32(&p_manifest[iofs + 8]);
  870. uint32_t attr_resid = decode_uint32(&p_manifest[iofs + 16]);
  871. const String value = (attr_value != 0xFFFFFFFF) ? string_table[attr_value] : "Res #" + itos(attr_resid);
  872. String attrname = string_table[attr_name];
  873. const String nspace = (attr_nspace != 0xFFFFFFFF) ? string_table[attr_nspace] : "";
  874. //replace project information
  875. if (tname == "manifest" && attrname == "package") {
  876. string_table.write[attr_value] = get_package_name(package_name);
  877. }
  878. if (tname == "manifest" && attrname == "versionCode") {
  879. encode_uint32(version_code, &p_manifest.write[iofs + 16]);
  880. }
  881. if (tname == "manifest" && attrname == "versionName") {
  882. if (attr_value == 0xFFFFFFFF) {
  883. WARN_PRINT("Version name in a resource, should be plain text");
  884. } else {
  885. string_table.write[attr_value] = version_name;
  886. }
  887. }
  888. if (tname == "application" && attrname == "requestLegacyExternalStorage") {
  889. encode_uint32(has_read_write_storage_permission ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  890. }
  891. if (tname == "application" && attrname == "allowBackup") {
  892. encode_uint32(backup_allowed, &p_manifest.write[iofs + 16]);
  893. }
  894. if (tname == "application" && attrname == "appCategory") {
  895. encode_uint32(_get_app_category_value(app_category), &p_manifest.write[iofs + 16]);
  896. }
  897. if (tname == "application" && attrname == "isGame") {
  898. encode_uint32(app_category == APP_CATEGORY_GAME, &p_manifest.write[iofs + 16]);
  899. }
  900. if (tname == "application" && attrname == "hasFragileUserData") {
  901. encode_uint32(retain_data_on_uninstall, &p_manifest.write[iofs + 16]);
  902. }
  903. if (tname == "activity" && attrname == "screenOrientation") {
  904. encode_uint32(screen_orientation, &p_manifest.write[iofs + 16]);
  905. }
  906. if (tname == "activity" && attrname == "excludeFromRecents") {
  907. encode_uint32(exclude_from_recents, &p_manifest.write[iofs + 16]);
  908. }
  909. if (tname == "activity" && attrname == "resizeableActivity") {
  910. encode_uint32(is_resizeable, &p_manifest.write[iofs + 16]);
  911. }
  912. if (tname == "provider" && attrname == "authorities") {
  913. string_table.write[attr_value] = get_package_name(package_name) + String(".fileprovider");
  914. }
  915. if (tname == "supports-screens") {
  916. if (attrname == "smallScreens") {
  917. encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  918. } else if (attrname == "normalScreens") {
  919. encode_uint32(screen_support_normal ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  920. } else if (attrname == "largeScreens") {
  921. encode_uint32(screen_support_large ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  922. } else if (attrname == "xlargeScreens") {
  923. encode_uint32(screen_support_xlarge ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
  924. }
  925. }
  926. iofs += 20;
  927. }
  928. } break;
  929. case CHUNK_XML_END_TAG: {
  930. int iofs = ofs + 8;
  931. uint32_t name = decode_uint32(&p_manifest[iofs + 12]);
  932. String tname = string_table[name];
  933. if (tname == "uses-feature") {
  934. Vector<String> feature_names;
  935. Vector<bool> feature_required_list;
  936. Vector<int> feature_versions;
  937. if (_uses_vulkan()) {
  938. // Require vulkan hardware level 1 support
  939. feature_names.push_back("android.hardware.vulkan.level");
  940. feature_required_list.push_back(false);
  941. feature_versions.push_back(1);
  942. // Require vulkan version 1.0
  943. feature_names.push_back("android.hardware.vulkan.version");
  944. feature_required_list.push_back(true);
  945. feature_versions.push_back(0x400003); // Encoded value for api version 1.0
  946. }
  947. if (feature_names.size() > 0) {
  948. ofs += 24; // skip over end tag
  949. // save manifest ending so we can restore it
  950. Vector<uint8_t> manifest_end;
  951. uint32_t manifest_cur_size = p_manifest.size();
  952. manifest_end.resize(p_manifest.size() - ofs);
  953. memcpy(manifest_end.ptrw(), &p_manifest[ofs], manifest_end.size());
  954. int32_t attr_name_string = string_table.find("name");
  955. ERR_FAIL_COND_MSG(attr_name_string == -1, "Template does not have 'name' attribute.");
  956. int32_t ns_android_string = string_table.find("http://schemas.android.com/apk/res/android");
  957. if (ns_android_string == -1) {
  958. string_table.push_back("http://schemas.android.com/apk/res/android");
  959. ns_android_string = string_table.size() - 1;
  960. }
  961. int32_t attr_uses_feature_string = string_table.find("uses-feature");
  962. if (attr_uses_feature_string == -1) {
  963. string_table.push_back("uses-feature");
  964. attr_uses_feature_string = string_table.size() - 1;
  965. }
  966. int32_t attr_required_string = string_table.find("required");
  967. if (attr_required_string == -1) {
  968. string_table.push_back("required");
  969. attr_required_string = string_table.size() - 1;
  970. }
  971. for (int i = 0; i < feature_names.size(); i++) {
  972. String feature_name = feature_names[i];
  973. bool feature_required = feature_required_list[i];
  974. int feature_version = feature_versions[i];
  975. bool has_version_attribute = feature_version != -1;
  976. print_line("Adding feature " + feature_name);
  977. int32_t feature_string = string_table.find(feature_name);
  978. if (feature_string == -1) {
  979. string_table.push_back(feature_name);
  980. feature_string = string_table.size() - 1;
  981. }
  982. String required_value_string = feature_required ? "true" : "false";
  983. int32_t required_value = string_table.find(required_value_string);
  984. if (required_value == -1) {
  985. string_table.push_back(required_value_string);
  986. required_value = string_table.size() - 1;
  987. }
  988. int32_t attr_version_string = -1;
  989. int32_t version_value = -1;
  990. int tag_size;
  991. int attr_count;
  992. if (has_version_attribute) {
  993. attr_version_string = string_table.find("version");
  994. if (attr_version_string == -1) {
  995. string_table.push_back("version");
  996. attr_version_string = string_table.size() - 1;
  997. }
  998. version_value = string_table.find(itos(feature_version));
  999. if (version_value == -1) {
  1000. string_table.push_back(itos(feature_version));
  1001. version_value = string_table.size() - 1;
  1002. }
  1003. tag_size = 96; // node and three attrs + end node
  1004. attr_count = 3;
  1005. } else {
  1006. tag_size = 76; // node and two attrs + end node
  1007. attr_count = 2;
  1008. }
  1009. manifest_cur_size += tag_size + 24;
  1010. p_manifest.resize(manifest_cur_size);
  1011. // start tag
  1012. encode_uint16(0x102, &p_manifest.write[ofs]); // type
  1013. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1014. encode_uint32(tag_size, &p_manifest.write[ofs + 4]); // size
  1015. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1016. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1017. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1018. encode_uint32(attr_uses_feature_string, &p_manifest.write[ofs + 20]); // name
  1019. encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start
  1020. encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size
  1021. encode_uint16(attr_count, &p_manifest.write[ofs + 28]); // num_attrs
  1022. encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index
  1023. encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index
  1024. encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index
  1025. // android:name attribute
  1026. encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns
  1027. encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name'
  1028. encode_uint32(feature_string, &p_manifest.write[ofs + 44]); // raw_value
  1029. encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size
  1030. p_manifest.write[ofs + 50] = 0; // typedvalue_always0
  1031. p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string)
  1032. encode_uint32(feature_string, &p_manifest.write[ofs + 52]); // typedvalue reference
  1033. // android:required attribute
  1034. encode_uint32(ns_android_string, &p_manifest.write[ofs + 56]); // ns
  1035. encode_uint32(attr_required_string, &p_manifest.write[ofs + 60]); // 'name'
  1036. encode_uint32(required_value, &p_manifest.write[ofs + 64]); // raw_value
  1037. encode_uint16(8, &p_manifest.write[ofs + 68]); // typedvalue_size
  1038. p_manifest.write[ofs + 70] = 0; // typedvalue_always0
  1039. p_manifest.write[ofs + 71] = 0x03; // typedvalue_type (string)
  1040. encode_uint32(required_value, &p_manifest.write[ofs + 72]); // typedvalue reference
  1041. ofs += 76;
  1042. if (has_version_attribute) {
  1043. // android:version attribute
  1044. encode_uint32(ns_android_string, &p_manifest.write[ofs]); // ns
  1045. encode_uint32(attr_version_string, &p_manifest.write[ofs + 4]); // 'name'
  1046. encode_uint32(version_value, &p_manifest.write[ofs + 8]); // raw_value
  1047. encode_uint16(8, &p_manifest.write[ofs + 12]); // typedvalue_size
  1048. p_manifest.write[ofs + 14] = 0; // typedvalue_always0
  1049. p_manifest.write[ofs + 15] = 0x03; // typedvalue_type (string)
  1050. encode_uint32(version_value, &p_manifest.write[ofs + 16]); // typedvalue reference
  1051. ofs += 20;
  1052. }
  1053. // end tag
  1054. encode_uint16(0x103, &p_manifest.write[ofs]); // type
  1055. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1056. encode_uint32(24, &p_manifest.write[ofs + 4]); // size
  1057. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1058. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1059. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1060. encode_uint32(attr_uses_feature_string, &p_manifest.write[ofs + 20]); // name
  1061. ofs += 24;
  1062. }
  1063. memcpy(&p_manifest.write[ofs], manifest_end.ptr(), manifest_end.size());
  1064. ofs -= 24; // go back over back end
  1065. }
  1066. }
  1067. if (tname == "manifest") {
  1068. // save manifest ending so we can restore it
  1069. Vector<uint8_t> manifest_end;
  1070. uint32_t manifest_cur_size = p_manifest.size();
  1071. manifest_end.resize(p_manifest.size() - ofs);
  1072. memcpy(manifest_end.ptrw(), &p_manifest[ofs], manifest_end.size());
  1073. int32_t attr_name_string = string_table.find("name");
  1074. ERR_FAIL_COND_MSG(attr_name_string == -1, "Template does not have 'name' attribute.");
  1075. int32_t ns_android_string = string_table.find("android");
  1076. ERR_FAIL_COND_MSG(ns_android_string == -1, "Template does not have 'android' namespace.");
  1077. int32_t attr_uses_permission_string = string_table.find("uses-permission");
  1078. if (attr_uses_permission_string == -1) {
  1079. string_table.push_back("uses-permission");
  1080. attr_uses_permission_string = string_table.size() - 1;
  1081. }
  1082. for (int i = 0; i < perms.size(); ++i) {
  1083. print_line("Adding permission " + perms[i]);
  1084. manifest_cur_size += 56 + 24; // node + end node
  1085. p_manifest.resize(manifest_cur_size);
  1086. // Add permission to the string pool
  1087. int32_t perm_string = string_table.find(perms[i]);
  1088. if (perm_string == -1) {
  1089. string_table.push_back(perms[i]);
  1090. perm_string = string_table.size() - 1;
  1091. }
  1092. // start tag
  1093. encode_uint16(0x102, &p_manifest.write[ofs]); // type
  1094. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1095. encode_uint32(56, &p_manifest.write[ofs + 4]); // size
  1096. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1097. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1098. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1099. encode_uint32(attr_uses_permission_string, &p_manifest.write[ofs + 20]); // name
  1100. encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start
  1101. encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size
  1102. encode_uint16(1, &p_manifest.write[ofs + 28]); // num_attrs
  1103. encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index
  1104. encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index
  1105. encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index
  1106. // attribute
  1107. encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns
  1108. encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name'
  1109. encode_uint32(perm_string, &p_manifest.write[ofs + 44]); // raw_value
  1110. encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size
  1111. p_manifest.write[ofs + 50] = 0; // typedvalue_always0
  1112. p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string)
  1113. encode_uint32(perm_string, &p_manifest.write[ofs + 52]); // typedvalue reference
  1114. ofs += 56;
  1115. // end tag
  1116. encode_uint16(0x103, &p_manifest.write[ofs]); // type
  1117. encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize
  1118. encode_uint32(24, &p_manifest.write[ofs + 4]); // size
  1119. encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno
  1120. encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment
  1121. encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns
  1122. encode_uint32(attr_uses_permission_string, &p_manifest.write[ofs + 20]); // name
  1123. ofs += 24;
  1124. }
  1125. // copy footer back in
  1126. memcpy(&p_manifest.write[ofs], manifest_end.ptr(), manifest_end.size());
  1127. }
  1128. } break;
  1129. }
  1130. ofs += size;
  1131. }
  1132. //create new andriodmanifest binary
  1133. Vector<uint8_t> ret;
  1134. ret.resize(string_table_begins + string_table.size() * 4);
  1135. for (uint32_t i = 0; i < string_table_begins; i++) {
  1136. ret.write[i] = p_manifest[i];
  1137. }
  1138. ofs = 0;
  1139. for (int i = 0; i < string_table.size(); i++) {
  1140. encode_uint32(ofs, &ret.write[string_table_begins + i * 4]);
  1141. ofs += string_table[i].length() * 2 + 2 + 2;
  1142. }
  1143. ret.resize(ret.size() + ofs);
  1144. string_data_offset = ret.size() - ofs;
  1145. uint8_t *chars = &ret.write[string_data_offset];
  1146. for (int i = 0; i < string_table.size(); i++) {
  1147. String s = string_table[i];
  1148. encode_uint16(s.length(), chars);
  1149. chars += 2;
  1150. for (int j = 0; j < s.length(); j++) {
  1151. encode_uint16(s[j], chars);
  1152. chars += 2;
  1153. }
  1154. encode_uint16(0, chars);
  1155. chars += 2;
  1156. }
  1157. for (int i = 0; i < stable_extra.size(); i++) {
  1158. ret.push_back(stable_extra[i]);
  1159. }
  1160. //pad
  1161. while (ret.size() % 4) {
  1162. ret.push_back(0);
  1163. }
  1164. uint32_t new_stable_end = ret.size();
  1165. uint32_t extra = (p_manifest.size() - string_table_ends);
  1166. ret.resize(new_stable_end + extra);
  1167. for (uint32_t i = 0; i < extra; i++) {
  1168. ret.write[new_stable_end + i] = p_manifest[string_table_ends + i];
  1169. }
  1170. while (ret.size() % 4) {
  1171. ret.push_back(0);
  1172. }
  1173. encode_uint32(ret.size(), &ret.write[4]); //update new file size
  1174. encode_uint32(new_stable_end - 8, &ret.write[12]); //update new string table size
  1175. encode_uint32(string_table.size(), &ret.write[16]); //update new number of strings
  1176. encode_uint32(string_data_offset - 8, &ret.write[28]); //update new string data offset
  1177. p_manifest = ret;
  1178. }
  1179. String EditorExportPlatformAndroid::_parse_string(const uint8_t *p_bytes, bool p_utf8) {
  1180. uint32_t offset = 0;
  1181. uint32_t len = 0;
  1182. if (p_utf8) {
  1183. uint8_t byte = p_bytes[offset];
  1184. if (byte & 0x80) {
  1185. offset += 2;
  1186. } else {
  1187. offset += 1;
  1188. }
  1189. byte = p_bytes[offset];
  1190. offset++;
  1191. if (byte & 0x80) {
  1192. len = byte & 0x7F;
  1193. len = (len << 8) + p_bytes[offset];
  1194. offset++;
  1195. } else {
  1196. len = byte;
  1197. }
  1198. } else {
  1199. len = decode_uint16(&p_bytes[offset]);
  1200. offset += 2;
  1201. if (len & 0x8000) {
  1202. len &= 0x7FFF;
  1203. len = (len << 16) + decode_uint16(&p_bytes[offset]);
  1204. offset += 2;
  1205. }
  1206. }
  1207. if (p_utf8) {
  1208. Vector<uint8_t> str8;
  1209. str8.resize(len + 1);
  1210. for (uint32_t i = 0; i < len; i++) {
  1211. str8.write[i] = p_bytes[offset + i];
  1212. }
  1213. str8.write[len] = 0;
  1214. String str;
  1215. str.parse_utf8((const char *)str8.ptr());
  1216. return str;
  1217. } else {
  1218. String str;
  1219. for (uint32_t i = 0; i < len; i++) {
  1220. char32_t c = decode_uint16(&p_bytes[offset + i * 2]);
  1221. if (c == 0) {
  1222. break;
  1223. }
  1224. str += String::chr(c);
  1225. }
  1226. return str;
  1227. }
  1228. }
  1229. void EditorExportPlatformAndroid::_fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &r_manifest) {
  1230. const int UTF8_FLAG = 0x00000100;
  1231. uint32_t string_block_len = decode_uint32(&r_manifest[16]);
  1232. uint32_t string_count = decode_uint32(&r_manifest[20]);
  1233. uint32_t string_flags = decode_uint32(&r_manifest[28]);
  1234. const uint32_t string_table_begins = 40;
  1235. Vector<String> string_table;
  1236. String package_name = p_preset->get("package/name");
  1237. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  1238. for (uint32_t i = 0; i < string_count; i++) {
  1239. uint32_t offset = decode_uint32(&r_manifest[string_table_begins + i * 4]);
  1240. offset += string_table_begins + string_count * 4;
  1241. String str = _parse_string(&r_manifest[offset], string_flags & UTF8_FLAG);
  1242. if (str.begins_with("godot-project-name")) {
  1243. if (str == "godot-project-name") {
  1244. //project name
  1245. str = get_project_name(package_name);
  1246. } else {
  1247. String lang = str.substr(str.rfind("-") + 1, str.length()).replace("-", "_");
  1248. if (appnames.has(lang)) {
  1249. str = appnames[lang];
  1250. } else {
  1251. str = get_project_name(package_name);
  1252. }
  1253. }
  1254. }
  1255. string_table.push_back(str);
  1256. }
  1257. //write a new string table, but use 16 bits
  1258. Vector<uint8_t> ret;
  1259. ret.resize(string_table_begins + string_table.size() * 4);
  1260. for (uint32_t i = 0; i < string_table_begins; i++) {
  1261. ret.write[i] = r_manifest[i];
  1262. }
  1263. int ofs = 0;
  1264. for (int i = 0; i < string_table.size(); i++) {
  1265. encode_uint32(ofs, &ret.write[string_table_begins + i * 4]);
  1266. ofs += string_table[i].length() * 2 + 2 + 2;
  1267. }
  1268. ret.resize(ret.size() + ofs);
  1269. uint8_t *chars = &ret.write[ret.size() - ofs];
  1270. for (int i = 0; i < string_table.size(); i++) {
  1271. String s = string_table[i];
  1272. encode_uint16(s.length(), chars);
  1273. chars += 2;
  1274. for (int j = 0; j < s.length(); j++) {
  1275. encode_uint16(s[j], chars);
  1276. chars += 2;
  1277. }
  1278. encode_uint16(0, chars);
  1279. chars += 2;
  1280. }
  1281. //pad
  1282. while (ret.size() % 4) {
  1283. ret.push_back(0);
  1284. }
  1285. //change flags to not use utf8
  1286. encode_uint32(string_flags & ~0x100, &ret.write[28]);
  1287. //change length
  1288. encode_uint32(ret.size() - 12, &ret.write[16]);
  1289. //append the rest...
  1290. int rest_from = 12 + string_block_len;
  1291. int rest_to = ret.size();
  1292. int rest_len = (r_manifest.size() - rest_from);
  1293. ret.resize(ret.size() + (r_manifest.size() - rest_from));
  1294. for (int i = 0; i < rest_len; i++) {
  1295. ret.write[rest_to + i] = r_manifest[rest_from + i];
  1296. }
  1297. //finally update the size
  1298. encode_uint32(ret.size(), &ret.write[4]);
  1299. r_manifest = ret;
  1300. //printf("end\n");
  1301. }
  1302. void EditorExportPlatformAndroid::_load_image_data(const Ref<Image> &p_splash_image, Vector<uint8_t> &p_data) {
  1303. Vector<uint8_t> png_buffer;
  1304. Error err = PNGDriverCommon::image_to_png(p_splash_image, png_buffer);
  1305. if (err == OK) {
  1306. p_data.resize(png_buffer.size());
  1307. memcpy(p_data.ptrw(), png_buffer.ptr(), p_data.size());
  1308. } else {
  1309. String err_str = String("Failed to convert splash image to png.");
  1310. WARN_PRINT(err_str.utf8().get_data());
  1311. }
  1312. }
  1313. void EditorExportPlatformAndroid::_process_launcher_icons(const String &p_file_name, const Ref<Image> &p_source_image, int dimension, Vector<uint8_t> &p_data) {
  1314. Ref<Image> working_image = p_source_image;
  1315. if (p_source_image->get_width() != dimension || p_source_image->get_height() != dimension) {
  1316. working_image = p_source_image->duplicate();
  1317. working_image->resize(dimension, dimension, Image::Interpolation::INTERPOLATE_LANCZOS);
  1318. }
  1319. Vector<uint8_t> png_buffer;
  1320. Error err = PNGDriverCommon::image_to_png(working_image, png_buffer);
  1321. if (err == OK) {
  1322. p_data.resize(png_buffer.size());
  1323. memcpy(p_data.ptrw(), png_buffer.ptr(), p_data.size());
  1324. } else {
  1325. String err_str = String("Failed to convert resized icon (") + p_file_name + ") to png.";
  1326. WARN_PRINT(err_str.utf8().get_data());
  1327. }
  1328. }
  1329. String EditorExportPlatformAndroid::load_splash_refs(Ref<Image> &splash_image, Ref<Image> &splash_bg_color_image) {
  1330. bool scale_splash = GLOBAL_GET("application/boot_splash/fullsize");
  1331. bool apply_filter = GLOBAL_GET("application/boot_splash/use_filter");
  1332. bool show_splash_image = GLOBAL_GET("application/boot_splash/show_image");
  1333. String project_splash_path = GLOBAL_GET("application/boot_splash/image");
  1334. // Setup the splash bg color.
  1335. bool bg_color_valid = false;
  1336. Color bg_color = ProjectSettings::get_singleton()->get("application/boot_splash/bg_color", &bg_color_valid);
  1337. if (!bg_color_valid) {
  1338. bg_color = boot_splash_bg_color;
  1339. }
  1340. if (show_splash_image) {
  1341. if (!project_splash_path.is_empty()) {
  1342. splash_image.instantiate();
  1343. print_verbose("Loading splash image: " + project_splash_path);
  1344. const Error err = ImageLoader::load_image(project_splash_path, splash_image);
  1345. if (err) {
  1346. if (OS::get_singleton()->is_stdout_verbose()) {
  1347. print_error("- unable to load splash image from " + project_splash_path + " (" + itos(err) + ")");
  1348. }
  1349. splash_image.unref();
  1350. }
  1351. }
  1352. } else {
  1353. splash_image.instantiate();
  1354. splash_image->initialize_data(1, 1, false, Image::FORMAT_RGBA8);
  1355. splash_image->set_pixel(0, 0, bg_color);
  1356. }
  1357. if (splash_image.is_null()) {
  1358. // Use the default
  1359. print_verbose("Using default splash image.");
  1360. splash_image = Ref<Image>(memnew(Image(boot_splash_png)));
  1361. }
  1362. if (scale_splash) {
  1363. Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  1364. int width, height;
  1365. if (screen_size.width > screen_size.height) {
  1366. // scale horizontally
  1367. height = screen_size.height;
  1368. width = splash_image->get_width() * screen_size.height / splash_image->get_height();
  1369. } else {
  1370. // scale vertically
  1371. width = screen_size.width;
  1372. height = splash_image->get_height() * screen_size.width / splash_image->get_width();
  1373. }
  1374. splash_image->resize(width, height);
  1375. }
  1376. print_verbose("Creating splash background color image.");
  1377. splash_bg_color_image.instantiate();
  1378. splash_bg_color_image->initialize_data(splash_image->get_width(), splash_image->get_height(), false, splash_image->get_format());
  1379. splash_bg_color_image->fill(bg_color);
  1380. String processed_splash_config_xml = vformat(SPLASH_CONFIG_XML_CONTENT, bool_to_string(apply_filter));
  1381. return processed_splash_config_xml;
  1382. }
  1383. void EditorExportPlatformAndroid::load_icon_refs(const Ref<EditorExportPreset> &p_preset, Ref<Image> &icon, Ref<Image> &foreground, Ref<Image> &background) {
  1384. String project_icon_path = GLOBAL_GET("application/config/icon");
  1385. icon.instantiate();
  1386. foreground.instantiate();
  1387. background.instantiate();
  1388. // Regular icon: user selection -> project icon -> default.
  1389. String path = static_cast<String>(p_preset->get(launcher_icon_option)).strip_edges();
  1390. print_verbose("Loading regular icon from " + path);
  1391. if (path.is_empty() || ImageLoader::load_image(path, icon) != OK) {
  1392. print_verbose("- falling back to project icon: " + project_icon_path);
  1393. ImageLoader::load_image(project_icon_path, icon);
  1394. }
  1395. // Adaptive foreground: user selection -> regular icon (user selection -> project icon -> default).
  1396. path = static_cast<String>(p_preset->get(launcher_adaptive_icon_foreground_option)).strip_edges();
  1397. print_verbose("Loading adaptive foreground icon from " + path);
  1398. if (path.is_empty() || ImageLoader::load_image(path, foreground) != OK) {
  1399. print_verbose("- falling back to using the regular icon");
  1400. foreground = icon;
  1401. }
  1402. // Adaptive background: user selection -> default.
  1403. path = static_cast<String>(p_preset->get(launcher_adaptive_icon_background_option)).strip_edges();
  1404. if (!path.is_empty()) {
  1405. print_verbose("Loading adaptive background icon from " + path);
  1406. ImageLoader::load_image(path, background);
  1407. }
  1408. }
  1409. void EditorExportPlatformAndroid::store_image(const LauncherIcon launcher_icon, const Vector<uint8_t> &data) {
  1410. store_image(launcher_icon.export_path, data);
  1411. }
  1412. void EditorExportPlatformAndroid::store_image(const String &export_path, const Vector<uint8_t> &data) {
  1413. String img_path = export_path.insert(0, "res://android/build/");
  1414. store_file_at_path(img_path, data);
  1415. }
  1416. void EditorExportPlatformAndroid::_copy_icons_to_gradle_project(const Ref<EditorExportPreset> &p_preset,
  1417. const String &processed_splash_config_xml,
  1418. const Ref<Image> &splash_image,
  1419. const Ref<Image> &splash_bg_color_image,
  1420. const Ref<Image> &main_image,
  1421. const Ref<Image> &foreground,
  1422. const Ref<Image> &background) {
  1423. // Store the splash configuration
  1424. if (!processed_splash_config_xml.is_empty()) {
  1425. print_verbose("Storing processed splash configuration: " + String("\n") + processed_splash_config_xml);
  1426. store_string_at_path(SPLASH_CONFIG_PATH, processed_splash_config_xml);
  1427. }
  1428. // Store the splash image
  1429. if (splash_image.is_valid() && !splash_image->is_empty()) {
  1430. print_verbose("Storing splash image in " + String(SPLASH_IMAGE_EXPORT_PATH));
  1431. Vector<uint8_t> data;
  1432. _load_image_data(splash_image, data);
  1433. store_image(SPLASH_IMAGE_EXPORT_PATH, data);
  1434. }
  1435. // Store the splash bg color image
  1436. if (splash_bg_color_image.is_valid() && !splash_bg_color_image->is_empty()) {
  1437. print_verbose("Storing splash background image in " + String(SPLASH_BG_COLOR_PATH));
  1438. Vector<uint8_t> data;
  1439. _load_image_data(splash_bg_color_image, data);
  1440. store_image(SPLASH_BG_COLOR_PATH, data);
  1441. }
  1442. // Prepare images to be resized for the icons. If some image ends up being uninitialized,
  1443. // the default image from the export template will be used.
  1444. for (int i = 0; i < icon_densities_count; ++i) {
  1445. if (main_image.is_valid() && !main_image->is_empty()) {
  1446. print_verbose("Processing launcher icon for dimension " + itos(launcher_icons[i].dimensions) + " into " + launcher_icons[i].export_path);
  1447. Vector<uint8_t> data;
  1448. _process_launcher_icons(launcher_icons[i].export_path, main_image, launcher_icons[i].dimensions, data);
  1449. store_image(launcher_icons[i], data);
  1450. }
  1451. if (foreground.is_valid() && !foreground->is_empty()) {
  1452. print_verbose("Processing launcher adaptive icon foreground for dimension " + itos(launcher_adaptive_icon_foregrounds[i].dimensions) + " into " + launcher_adaptive_icon_foregrounds[i].export_path);
  1453. Vector<uint8_t> data;
  1454. _process_launcher_icons(launcher_adaptive_icon_foregrounds[i].export_path, foreground,
  1455. launcher_adaptive_icon_foregrounds[i].dimensions, data);
  1456. store_image(launcher_adaptive_icon_foregrounds[i], data);
  1457. }
  1458. if (background.is_valid() && !background->is_empty()) {
  1459. print_verbose("Processing launcher adaptive icon background for dimension " + itos(launcher_adaptive_icon_backgrounds[i].dimensions) + " into " + launcher_adaptive_icon_backgrounds[i].export_path);
  1460. Vector<uint8_t> data;
  1461. _process_launcher_icons(launcher_adaptive_icon_backgrounds[i].export_path, background,
  1462. launcher_adaptive_icon_backgrounds[i].dimensions, data);
  1463. store_image(launcher_adaptive_icon_backgrounds[i], data);
  1464. }
  1465. }
  1466. }
  1467. Vector<EditorExportPlatformAndroid::ABI> EditorExportPlatformAndroid::get_enabled_abis(const Ref<EditorExportPreset> &p_preset) {
  1468. Vector<ABI> abis = get_abis();
  1469. Vector<ABI> enabled_abis;
  1470. for (int i = 0; i < abis.size(); ++i) {
  1471. bool is_enabled = p_preset->get("architectures/" + abis[i].abi);
  1472. if (is_enabled) {
  1473. enabled_abis.push_back(abis[i]);
  1474. }
  1475. }
  1476. return enabled_abis;
  1477. }
  1478. void EditorExportPlatformAndroid::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
  1479. r_features->push_back("etc2");
  1480. r_features->push_back("astc");
  1481. Vector<ABI> abis = get_enabled_abis(p_preset);
  1482. for (int i = 0; i < abis.size(); ++i) {
  1483. r_features->push_back(abis[i].arch);
  1484. }
  1485. }
  1486. String EditorExportPlatformAndroid::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const {
  1487. if (p_preset) {
  1488. if (p_name == ("apk_expansion/public_key")) {
  1489. bool apk_expansion = p_preset->get("apk_expansion/enable");
  1490. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  1491. if (apk_expansion && apk_expansion_pkey.is_empty()) {
  1492. return TTR("Invalid public key for APK expansion.");
  1493. }
  1494. } else if (p_name == "package/unique_name") {
  1495. String pn = p_preset->get("package/unique_name");
  1496. String pn_err;
  1497. if (!is_package_name_valid(pn, &pn_err)) {
  1498. return TTR("Invalid package name:") + " " + pn_err;
  1499. }
  1500. } else if (p_name == "gradle_build/use_gradle_build") {
  1501. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1502. String enabled_plugins_names = _get_plugins_names(Ref<EditorExportPreset>(p_preset));
  1503. if (!enabled_plugins_names.is_empty() && !gradle_build_enabled) {
  1504. return TTR("\"Use Gradle Build\" must be enabled to use the plugins.");
  1505. }
  1506. } else if (p_name == "xr_features/xr_mode") {
  1507. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1508. int xr_mode_index = p_preset->get("xr_features/xr_mode");
  1509. if (xr_mode_index == XR_MODE_OPENXR && !gradle_build_enabled) {
  1510. return TTR("OpenXR requires \"Use Gradle Build\" to be enabled");
  1511. }
  1512. } else if (p_name == "gradle_build/export_format") {
  1513. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1514. if (int(p_preset->get("gradle_build/export_format")) == EXPORT_FORMAT_AAB && !gradle_build_enabled) {
  1515. return TTR("\"Export AAB\" is only valid when \"Use Gradle Build\" is enabled.");
  1516. }
  1517. } else if (p_name == "gradle_build/min_sdk") {
  1518. String min_sdk_str = p_preset->get("gradle_build/min_sdk");
  1519. int min_sdk_int = VULKAN_MIN_SDK_VERSION;
  1520. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1521. if (!min_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  1522. if (!gradle_build_enabled) {
  1523. return TTR("\"Min SDK\" can only be overridden when \"Use Gradle Build\" is enabled.");
  1524. }
  1525. if (!min_sdk_str.is_valid_int()) {
  1526. return vformat(TTR("\"Min SDK\" should be a valid integer, but got \"%s\" which is invalid."), min_sdk_str);
  1527. } else {
  1528. min_sdk_int = min_sdk_str.to_int();
  1529. if (min_sdk_int < OPENGL_MIN_SDK_VERSION) {
  1530. return vformat(TTR("\"Min SDK\" cannot be lower than %d, which is the version needed by the Godot library."), OPENGL_MIN_SDK_VERSION);
  1531. }
  1532. }
  1533. }
  1534. } else if (p_name == "gradle_build/target_sdk") {
  1535. String target_sdk_str = p_preset->get("gradle_build/target_sdk");
  1536. int target_sdk_int = DEFAULT_TARGET_SDK_VERSION;
  1537. String min_sdk_str = p_preset->get("gradle_build/min_sdk");
  1538. int min_sdk_int = VULKAN_MIN_SDK_VERSION;
  1539. if (min_sdk_str.is_valid_int()) {
  1540. min_sdk_int = min_sdk_str.to_int();
  1541. }
  1542. bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1543. if (!target_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  1544. if (!gradle_build_enabled) {
  1545. return TTR("\"Target SDK\" can only be overridden when \"Use Gradle Build\" is enabled.");
  1546. }
  1547. if (!target_sdk_str.is_valid_int()) {
  1548. return vformat(TTR("\"Target SDK\" should be a valid integer, but got \"%s\" which is invalid."), target_sdk_str);
  1549. } else {
  1550. target_sdk_int = target_sdk_str.to_int();
  1551. if (target_sdk_int < min_sdk_int) {
  1552. return TTR("\"Target SDK\" version must be greater or equal to \"Min SDK\" version.");
  1553. }
  1554. }
  1555. }
  1556. }
  1557. }
  1558. return String();
  1559. }
  1560. void EditorExportPlatformAndroid::get_export_options(List<ExportOption> *r_options) const {
  1561. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), ""));
  1562. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), ""));
  1563. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gradle_build/use_gradle_build"), false, false, true));
  1564. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "gradle_build/export_format", PROPERTY_HINT_ENUM, "Export APK,Export AAB"), EXPORT_FORMAT_APK, false, true));
  1565. // Using String instead of int to default to an empty string (no override) with placeholder for instructions (see GH-62465).
  1566. // This implies doing validation that the string is a proper int.
  1567. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/min_sdk", PROPERTY_HINT_PLACEHOLDER_TEXT, vformat("%d (default)", VULKAN_MIN_SDK_VERSION)), "", false, true));
  1568. 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));
  1569. #ifndef DISABLE_DEPRECATED
  1570. Vector<PluginConfigAndroid> plugins_configs = get_plugins();
  1571. for (int i = 0; i < plugins_configs.size(); i++) {
  1572. print_verbose("Found Android plugin " + plugins_configs[i].name);
  1573. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), plugins_configs[i].name)), false));
  1574. }
  1575. android_plugins_changed.clear();
  1576. #endif // DISABLE_DEPRECATED
  1577. // Android supports multiple architectures in an app bundle, so
  1578. // we expose each option as a checkbox in the export dialog.
  1579. const Vector<ABI> abis = get_abis();
  1580. for (int i = 0; i < abis.size(); ++i) {
  1581. const String abi = abis[i].abi;
  1582. // All Android devices supporting Vulkan run 64-bit Android,
  1583. // so there is usually no point in exporting for 32-bit Android.
  1584. const bool is_default = abi == "arm64-v8a";
  1585. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), abi)), is_default));
  1586. }
  1587. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug", PROPERTY_HINT_GLOBAL_FILE, "*.keystore,*.jks", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1588. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_user", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1589. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_password", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1590. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release", PROPERTY_HINT_GLOBAL_FILE, "*.keystore,*.jks", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1591. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_user", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1592. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_password", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), ""));
  1593. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/code", PROPERTY_HINT_RANGE, "1,4096,1,or_greater"), 1));
  1594. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "version/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), ""));
  1595. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "ext.domain.name"), "com.example.$genname", false, true));
  1596. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name [default if blank]"), ""));
  1597. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/signed"), true));
  1598. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "package/app_category", PROPERTY_HINT_ENUM, "Accessibility,Audio,Game,Image,Maps,News,Productivity,Social,Video"), APP_CATEGORY_GAME));
  1599. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/retain_data_on_uninstall"), false));
  1600. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/exclude_from_recents"), false));
  1601. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_in_android_tv"), false));
  1602. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_in_app_library"), true));
  1603. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_as_launcher_app"), false));
  1604. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_icon_option, PROPERTY_HINT_FILE, "*.png"), ""));
  1605. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_adaptive_icon_foreground_option, PROPERTY_HINT_FILE, "*.png"), ""));
  1606. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_adaptive_icon_background_option, PROPERTY_HINT_FILE, "*.png"), ""));
  1607. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "graphics/opengl_debug"), false));
  1608. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "xr_features/xr_mode", PROPERTY_HINT_ENUM, "Regular,OpenXR"), XR_MODE_REGULAR, false, true));
  1609. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/immersive_mode"), true));
  1610. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_small"), true));
  1611. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_normal"), true));
  1612. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_large"), true));
  1613. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_xlarge"), true));
  1614. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data_backup/allow"), false));
  1615. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));
  1616. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "apk_expansion/enable"), false, false, true));
  1617. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/SALT"), ""));
  1618. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/public_key", PROPERTY_HINT_MULTILINE_TEXT), "", false, true));
  1619. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "permissions/custom_permissions"), PackedStringArray()));
  1620. const char **perms = android_perms;
  1621. while (*perms) {
  1622. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("permissions"), String(*perms).to_lower())), false));
  1623. perms++;
  1624. }
  1625. }
  1626. String EditorExportPlatformAndroid::get_name() const {
  1627. return "Android";
  1628. }
  1629. String EditorExportPlatformAndroid::get_os_name() const {
  1630. return "Android";
  1631. }
  1632. Ref<Texture2D> EditorExportPlatformAndroid::get_logo() const {
  1633. return logo;
  1634. }
  1635. bool EditorExportPlatformAndroid::should_update_export_options() {
  1636. #ifndef DISABLE_DEPRECATED
  1637. if (android_plugins_changed.is_set()) {
  1638. // don't clear unless we're reporting true, to avoid race
  1639. android_plugins_changed.clear();
  1640. return true;
  1641. }
  1642. #endif // DISABLE_DEPRECATED
  1643. return false;
  1644. }
  1645. bool EditorExportPlatformAndroid::poll_export() {
  1646. bool dc = devices_changed.is_set();
  1647. if (dc) {
  1648. // don't clear unless we're reporting true, to avoid race
  1649. devices_changed.clear();
  1650. }
  1651. return dc;
  1652. }
  1653. int EditorExportPlatformAndroid::get_options_count() const {
  1654. MutexLock lock(device_lock);
  1655. return devices.size();
  1656. }
  1657. String EditorExportPlatformAndroid::get_options_tooltip() const {
  1658. return TTR("Select device from the list");
  1659. }
  1660. String EditorExportPlatformAndroid::get_option_label(int p_index) const {
  1661. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1662. MutexLock lock(device_lock);
  1663. return devices[p_index].name;
  1664. }
  1665. String EditorExportPlatformAndroid::get_option_tooltip(int p_index) const {
  1666. ERR_FAIL_INDEX_V(p_index, devices.size(), "");
  1667. MutexLock lock(device_lock);
  1668. String s = devices[p_index].description;
  1669. if (devices.size() == 1) {
  1670. // Tooltip will be:
  1671. // Name
  1672. // Description
  1673. s = devices[p_index].name + "\n\n" + s;
  1674. }
  1675. return s;
  1676. }
  1677. Error EditorExportPlatformAndroid::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  1678. ERR_FAIL_INDEX_V(p_device, devices.size(), ERR_INVALID_PARAMETER);
  1679. String can_export_error;
  1680. bool can_export_missing_templates;
  1681. if (!can_export(p_preset, can_export_error, can_export_missing_templates)) {
  1682. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), can_export_error);
  1683. return ERR_UNCONFIGURED;
  1684. }
  1685. MutexLock lock(device_lock);
  1686. EditorProgress ep("run", vformat(TTR("Running on %s"), devices[p_device].name), 3);
  1687. String adb = get_adb_path();
  1688. // Export_temp APK.
  1689. if (ep.step(TTR("Exporting APK..."), 0)) {
  1690. return ERR_SKIP;
  1691. }
  1692. const bool use_wifi_for_remote_debug = EDITOR_GET("export/android/use_wifi_for_remote_debug");
  1693. const bool use_remote = (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) || (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT);
  1694. const bool use_reverse = devices[p_device].api_level >= 21 && !use_wifi_for_remote_debug;
  1695. if (use_reverse) {
  1696. p_debug_flags |= DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST;
  1697. }
  1698. String tmp_export_path = EditorPaths::get_singleton()->get_cache_dir().path_join("tmpexport." + uitos(OS::get_singleton()->get_unix_time()) + ".apk");
  1699. #define CLEANUP_AND_RETURN(m_err) \
  1700. { \
  1701. DirAccess::remove_file_or_error(tmp_export_path); \
  1702. return m_err; \
  1703. } \
  1704. ((void)0)
  1705. // Export to temporary APK before sending to device.
  1706. Error err = export_project_helper(p_preset, true, tmp_export_path, EXPORT_FORMAT_APK, true, p_debug_flags);
  1707. if (err != OK) {
  1708. CLEANUP_AND_RETURN(err);
  1709. }
  1710. List<String> args;
  1711. int rv;
  1712. String output;
  1713. bool remove_prev = EDITOR_GET("export/android/one_click_deploy_clear_previous_install");
  1714. String version_name = p_preset->get_version("version/name");
  1715. String package_name = p_preset->get("package/unique_name");
  1716. if (remove_prev) {
  1717. if (ep.step(TTR("Uninstalling..."), 1)) {
  1718. CLEANUP_AND_RETURN(ERR_SKIP);
  1719. }
  1720. print_line("Uninstalling previous version: " + devices[p_device].name);
  1721. args.push_back("-s");
  1722. args.push_back(devices[p_device].id);
  1723. args.push_back("uninstall");
  1724. args.push_back(get_package_name(package_name));
  1725. output.clear();
  1726. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  1727. print_verbose(output);
  1728. }
  1729. print_line("Installing to device (please wait...): " + devices[p_device].name);
  1730. if (ep.step(TTR("Installing to device, please wait..."), 2)) {
  1731. CLEANUP_AND_RETURN(ERR_SKIP);
  1732. }
  1733. args.clear();
  1734. args.push_back("-s");
  1735. args.push_back(devices[p_device].id);
  1736. args.push_back("install");
  1737. args.push_back("-r");
  1738. args.push_back(tmp_export_path);
  1739. output.clear();
  1740. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  1741. print_verbose(output);
  1742. if (err || rv != 0) {
  1743. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Could not install to device: %s"), output));
  1744. CLEANUP_AND_RETURN(ERR_CANT_CREATE);
  1745. }
  1746. if (use_remote) {
  1747. if (use_reverse) {
  1748. static const char *const msg = "--- Device API >= 21; debugging over USB ---";
  1749. EditorNode::get_singleton()->get_log()->add_message(msg, EditorLog::MSG_TYPE_EDITOR);
  1750. print_line(String(msg).to_upper());
  1751. args.clear();
  1752. args.push_back("-s");
  1753. args.push_back(devices[p_device].id);
  1754. args.push_back("reverse");
  1755. args.push_back("--remove-all");
  1756. output.clear();
  1757. OS::get_singleton()->execute(adb, args, &output, &rv, true);
  1758. print_verbose(output);
  1759. if (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  1760. int dbg_port = EDITOR_GET("network/debug/remote_port");
  1761. args.clear();
  1762. args.push_back("-s");
  1763. args.push_back(devices[p_device].id);
  1764. args.push_back("reverse");
  1765. args.push_back("tcp:" + itos(dbg_port));
  1766. args.push_back("tcp:" + itos(dbg_port));
  1767. output.clear();
  1768. OS::get_singleton()->execute(adb, args, &output, &rv, true);
  1769. print_verbose(output);
  1770. print_line("Reverse result: " + itos(rv));
  1771. }
  1772. if (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT) {
  1773. int fs_port = EDITOR_GET("filesystem/file_server/port");
  1774. args.clear();
  1775. args.push_back("-s");
  1776. args.push_back(devices[p_device].id);
  1777. args.push_back("reverse");
  1778. args.push_back("tcp:" + itos(fs_port));
  1779. args.push_back("tcp:" + itos(fs_port));
  1780. output.clear();
  1781. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  1782. print_verbose(output);
  1783. print_line("Reverse result2: " + itos(rv));
  1784. }
  1785. } else {
  1786. static const char *const api_version_msg = "--- Device API < 21; debugging over Wi-Fi ---";
  1787. static const char *const manual_override_msg = "--- Wi-Fi remote debug enabled in project settings; debugging over Wi-Fi ---";
  1788. const char *const msg = use_wifi_for_remote_debug ? manual_override_msg : api_version_msg;
  1789. EditorNode::get_singleton()->get_log()->add_message(msg, EditorLog::MSG_TYPE_EDITOR);
  1790. print_line(String(msg).to_upper());
  1791. }
  1792. }
  1793. if (ep.step(TTR("Running on device..."), 3)) {
  1794. CLEANUP_AND_RETURN(ERR_SKIP);
  1795. }
  1796. args.clear();
  1797. args.push_back("-s");
  1798. args.push_back(devices[p_device].id);
  1799. args.push_back("shell");
  1800. args.push_back("am");
  1801. args.push_back("start");
  1802. if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device].api_level >= 17) { // Multi-user introduced in Android 17
  1803. args.push_back("--user");
  1804. args.push_back("0");
  1805. }
  1806. args.push_back("-a");
  1807. args.push_back("android.intent.action.MAIN");
  1808. args.push_back("-n");
  1809. args.push_back(get_package_name(package_name) + "/com.godot.game.GodotApp");
  1810. output.clear();
  1811. err = OS::get_singleton()->execute(adb, args, &output, &rv, true);
  1812. print_verbose(output);
  1813. if (err || rv != 0) {
  1814. add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Could not execute on device."));
  1815. CLEANUP_AND_RETURN(ERR_CANT_CREATE);
  1816. }
  1817. CLEANUP_AND_RETURN(OK);
  1818. #undef CLEANUP_AND_RETURN
  1819. }
  1820. Ref<Texture2D> EditorExportPlatformAndroid::get_run_icon() const {
  1821. return run_icon;
  1822. }
  1823. String EditorExportPlatformAndroid::get_adb_path() {
  1824. String exe_ext = "";
  1825. if (OS::get_singleton()->get_name() == "Windows") {
  1826. exe_ext = ".exe";
  1827. }
  1828. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  1829. return sdk_path.path_join("platform-tools/adb" + exe_ext);
  1830. }
  1831. String EditorExportPlatformAndroid::get_apksigner_path(int p_target_sdk, bool p_check_executes) {
  1832. if (p_target_sdk == -1) {
  1833. p_target_sdk = DEFAULT_TARGET_SDK_VERSION;
  1834. }
  1835. String exe_ext = "";
  1836. if (OS::get_singleton()->get_name() == "Windows") {
  1837. exe_ext = ".bat";
  1838. }
  1839. String apksigner_command_name = "apksigner" + exe_ext;
  1840. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  1841. String apksigner_path = "";
  1842. Error errn;
  1843. String build_tools_dir = sdk_path.path_join("build-tools");
  1844. Ref<DirAccess> da = DirAccess::open(build_tools_dir, &errn);
  1845. if (errn != OK) {
  1846. print_error("Unable to open Android 'build-tools' directory.");
  1847. return apksigner_path;
  1848. }
  1849. // There are additional versions directories we need to go through.
  1850. Vector<String> dir_list = da->get_directories();
  1851. // We need to use the version of build_tools that matches the Target SDK
  1852. // If somehow we can't find that, we see if a version between 28 and the default target SDK exists.
  1853. // We need to avoid versions <= 27 because they fail on Java versions >9
  1854. // If we can't find that, we just use the first valid version.
  1855. Vector<String> ideal_versions;
  1856. Vector<String> other_versions;
  1857. Vector<String> versions;
  1858. bool found_target_sdk = false;
  1859. // We only allow for versions <= 27 if specifically set
  1860. int min_version = p_target_sdk <= 27 ? p_target_sdk : 28;
  1861. for (String sub_dir : dir_list) {
  1862. if (!sub_dir.begins_with(".")) {
  1863. Vector<String> ver_numbers = sub_dir.split(".");
  1864. // Dir not a version number, will use as last resort
  1865. if (!ver_numbers.size() || !ver_numbers[0].is_valid_int()) {
  1866. other_versions.push_back(sub_dir);
  1867. continue;
  1868. }
  1869. int ver_number = ver_numbers[0].to_int();
  1870. if (ver_number == p_target_sdk) {
  1871. found_target_sdk = true;
  1872. //ensure this is in front of the ones we check
  1873. versions.push_back(sub_dir);
  1874. } else {
  1875. if (ver_number >= min_version && ver_number <= DEFAULT_TARGET_SDK_VERSION) {
  1876. ideal_versions.push_back(sub_dir);
  1877. } else {
  1878. other_versions.push_back(sub_dir);
  1879. }
  1880. }
  1881. }
  1882. }
  1883. // we will check ideal versions first, then other versions.
  1884. versions.append_array(ideal_versions);
  1885. versions.append_array(other_versions);
  1886. if (!versions.size()) {
  1887. print_error("Unable to find the 'apksigner' tool.");
  1888. return apksigner_path;
  1889. }
  1890. int i;
  1891. bool failed = false;
  1892. String version_to_use;
  1893. List<String> args;
  1894. args.push_back("--version");
  1895. String output;
  1896. int retval;
  1897. Error err;
  1898. for (i = 0; i < versions.size(); i++) {
  1899. // Check if the tool is here.
  1900. apksigner_path = build_tools_dir.path_join(versions[i]).path_join(apksigner_command_name);
  1901. if (FileAccess::exists(apksigner_path)) {
  1902. version_to_use = versions[i];
  1903. // If we aren't exporting, just break here.
  1904. if (!p_check_executes) {
  1905. break;
  1906. }
  1907. // we only check to see if it executes on export because it is slow to load
  1908. err = OS::get_singleton()->execute(apksigner_path, args, &output, &retval, false);
  1909. if (err || retval) {
  1910. failed = true;
  1911. } else {
  1912. break;
  1913. }
  1914. }
  1915. }
  1916. if (i == versions.size()) {
  1917. if (failed) {
  1918. print_error("All located 'apksigner' tools in " + build_tools_dir + " failed to execute");
  1919. return "<FAILED>";
  1920. } else {
  1921. print_error("Unable to find the 'apksigner' tool.");
  1922. return "";
  1923. }
  1924. }
  1925. if (!found_target_sdk) {
  1926. print_line("Could not find version of build tools that matches Target SDK, using " + version_to_use);
  1927. } else if (failed && found_target_sdk) {
  1928. print_line("Version of build tools that matches Target SDK failed to execute, using " + version_to_use);
  1929. }
  1930. return apksigner_path;
  1931. }
  1932. bool EditorExportPlatformAndroid::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  1933. String err;
  1934. bool valid = false;
  1935. const bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");
  1936. #ifdef MODULE_MONO_ENABLED
  1937. // Android export is still a work in progress, keep a message as a warning.
  1938. err += TTR("Exporting to Android when using C#/.NET is experimental.") + "\n";
  1939. bool unsupported_arch = false;
  1940. Vector<ABI> enabled_abis = get_enabled_abis(p_preset);
  1941. for (ABI abi : enabled_abis) {
  1942. if (abi.arch != "arm64" && abi.arch != "x86_64") {
  1943. err += vformat(TTR("Android architecture %s not supported in C# projects."), abi.arch) + "\n";
  1944. unsupported_arch = true;
  1945. }
  1946. }
  1947. if (unsupported_arch) {
  1948. r_error = err;
  1949. return false;
  1950. }
  1951. #endif
  1952. // Look for export templates (first official, and if defined custom templates).
  1953. if (!gradle_build_enabled) {
  1954. String template_err;
  1955. bool dvalid = false;
  1956. bool rvalid = false;
  1957. bool has_export_templates = false;
  1958. if (p_preset->get("custom_template/debug") != "") {
  1959. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1960. if (!dvalid) {
  1961. template_err += TTR("Custom debug template not found.") + "\n";
  1962. }
  1963. } else {
  1964. has_export_templates |= exists_export_template("android_debug.apk", &template_err);
  1965. }
  1966. if (p_preset->get("custom_template/release") != "") {
  1967. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1968. if (!rvalid) {
  1969. template_err += TTR("Custom release template not found.") + "\n";
  1970. }
  1971. } else {
  1972. has_export_templates |= exists_export_template("android_release.apk", &template_err);
  1973. }
  1974. r_missing_templates = !has_export_templates;
  1975. valid = dvalid || rvalid || has_export_templates;
  1976. if (!valid) {
  1977. err += template_err;
  1978. }
  1979. } else {
  1980. bool installed_android_build_template = FileAccess::exists("res://android/build/build.gradle");
  1981. if (!installed_android_build_template) {
  1982. r_missing_templates = !exists_export_template("android_source.zip", &err);
  1983. err += TTR("Android build template not installed in the project. Install it from the Project menu.") + "\n";
  1984. } else {
  1985. r_missing_templates = false;
  1986. }
  1987. valid = installed_android_build_template && !r_missing_templates;
  1988. }
  1989. // Validate the rest of the export configuration.
  1990. String dk = p_preset->get_or_env("keystore/debug", ENV_ANDROID_KEYSTORE_DEBUG_PATH);
  1991. String dk_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  1992. String dk_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  1993. if ((dk.is_empty() || dk_user.is_empty() || dk_password.is_empty()) && (!dk.is_empty() || !dk_user.is_empty() || !dk_password.is_empty())) {
  1994. valid = false;
  1995. err += TTR("Either Debug Keystore, Debug User AND Debug Password settings must be configured OR none of them.") + "\n";
  1996. }
  1997. // Use OR to make the export UI able to show this error.
  1998. if ((p_debug || !dk.is_empty()) && !FileAccess::exists(dk)) {
  1999. dk = EDITOR_GET("export/android/debug_keystore");
  2000. if (!FileAccess::exists(dk)) {
  2001. valid = false;
  2002. err += TTR("Debug keystore not configured in the Editor Settings nor in the preset.") + "\n";
  2003. }
  2004. }
  2005. String rk = p_preset->get_or_env("keystore/release", ENV_ANDROID_KEYSTORE_RELEASE_PATH);
  2006. String rk_user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  2007. String rk_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  2008. if ((rk.is_empty() || rk_user.is_empty() || rk_password.is_empty()) && (!rk.is_empty() || !rk_user.is_empty() || !rk_password.is_empty())) {
  2009. valid = false;
  2010. err += TTR("Either Release Keystore, Release User AND Release Password settings must be configured OR none of them.") + "\n";
  2011. }
  2012. if (!p_debug && !rk.is_empty() && !FileAccess::exists(rk)) {
  2013. valid = false;
  2014. err += TTR("Release keystore incorrectly configured in the export preset.") + "\n";
  2015. }
  2016. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  2017. if (sdk_path.is_empty()) {
  2018. err += TTR("A valid Android SDK path is required in Editor Settings.") + "\n";
  2019. valid = false;
  2020. } else {
  2021. Error errn;
  2022. // Check for the platform-tools directory.
  2023. Ref<DirAccess> da = DirAccess::open(sdk_path.path_join("platform-tools"), &errn);
  2024. if (errn != OK) {
  2025. err += TTR("Invalid Android SDK path in Editor Settings.");
  2026. err += TTR("Missing 'platform-tools' directory!");
  2027. err += "\n";
  2028. valid = false;
  2029. }
  2030. // Validate that adb is available.
  2031. String adb_path = get_adb_path();
  2032. if (!FileAccess::exists(adb_path)) {
  2033. err += TTR("Unable to find Android SDK platform-tools' adb command.");
  2034. err += TTR("Please check in the Android SDK directory specified in Editor Settings.");
  2035. err += "\n";
  2036. valid = false;
  2037. }
  2038. // Check for the build-tools directory.
  2039. Ref<DirAccess> build_tools_da = DirAccess::open(sdk_path.path_join("build-tools"), &errn);
  2040. if (errn != OK) {
  2041. err += TTR("Invalid Android SDK path in Editor Settings.");
  2042. err += TTR("Missing 'build-tools' directory!");
  2043. err += "\n";
  2044. valid = false;
  2045. }
  2046. String target_sdk_version = p_preset->get("gradle_build/target_sdk");
  2047. if (!target_sdk_version.is_valid_int()) {
  2048. target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION);
  2049. }
  2050. // Validate that apksigner is available.
  2051. String apksigner_path = get_apksigner_path(target_sdk_version.to_int());
  2052. if (!FileAccess::exists(apksigner_path)) {
  2053. err += TTR("Unable to find Android SDK build-tools' apksigner command.");
  2054. err += TTR("Please check in the Android SDK directory specified in Editor Settings.");
  2055. err += "\n";
  2056. valid = false;
  2057. }
  2058. }
  2059. if (!err.is_empty()) {
  2060. r_error = err;
  2061. }
  2062. return valid;
  2063. }
  2064. bool EditorExportPlatformAndroid::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  2065. String err;
  2066. bool valid = true;
  2067. List<ExportOption> options;
  2068. get_export_options(&options);
  2069. for (const EditorExportPlatform::ExportOption &E : options) {
  2070. if (get_export_option_visibility(p_preset.ptr(), E.option.name)) {
  2071. String warn = get_export_option_warning(p_preset.ptr(), E.option.name);
  2072. if (!warn.is_empty()) {
  2073. err += warn + "\n";
  2074. if (E.required) {
  2075. valid = false;
  2076. }
  2077. }
  2078. }
  2079. }
  2080. if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
  2081. valid = false;
  2082. }
  2083. String min_sdk_str = p_preset->get("gradle_build/min_sdk");
  2084. int min_sdk_int = VULKAN_MIN_SDK_VERSION;
  2085. if (!min_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  2086. if (min_sdk_str.is_valid_int()) {
  2087. min_sdk_int = min_sdk_str.to_int();
  2088. }
  2089. }
  2090. String target_sdk_str = p_preset->get("gradle_build/target_sdk");
  2091. int target_sdk_int = DEFAULT_TARGET_SDK_VERSION;
  2092. if (!target_sdk_str.is_empty()) { // Empty means no override, nothing to do.
  2093. if (target_sdk_str.is_valid_int()) {
  2094. target_sdk_int = target_sdk_str.to_int();
  2095. if (target_sdk_int > DEFAULT_TARGET_SDK_VERSION) {
  2096. // Warning only, so don't override `valid`.
  2097. 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);
  2098. err += "\n";
  2099. }
  2100. }
  2101. }
  2102. String current_renderer = GLOBAL_GET("rendering/renderer/rendering_method.mobile");
  2103. if (current_renderer == "forward_plus") {
  2104. // Warning only, so don't override `valid`.
  2105. err += vformat(TTR("The \"%s\" renderer is designed for Desktop devices, and is not suitable for Android devices."), current_renderer);
  2106. err += "\n";
  2107. }
  2108. if (_uses_vulkan() && min_sdk_int < VULKAN_MIN_SDK_VERSION) {
  2109. // Warning only, so don't override `valid`.
  2110. err += vformat(TTR("\"Min SDK\" should be greater or equal to %d for the \"%s\" renderer."), VULKAN_MIN_SDK_VERSION, current_renderer);
  2111. err += "\n";
  2112. }
  2113. r_error = err;
  2114. return valid;
  2115. }
  2116. List<String> EditorExportPlatformAndroid::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  2117. List<String> list;
  2118. list.push_back("apk");
  2119. list.push_back("aab");
  2120. return list;
  2121. }
  2122. String EditorExportPlatformAndroid::get_apk_expansion_fullpath(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  2123. int version_code = p_preset->get("version/code");
  2124. String package_name = p_preset->get("package/unique_name");
  2125. String apk_file_name = "main." + itos(version_code) + "." + get_package_name(package_name) + ".obb";
  2126. String fullpath = p_path.get_base_dir().path_join(apk_file_name);
  2127. return fullpath;
  2128. }
  2129. Error EditorExportPlatformAndroid::save_apk_expansion_file(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  2130. String fullpath = get_apk_expansion_fullpath(p_preset, p_path);
  2131. Error err = save_pack(p_preset, p_debug, fullpath);
  2132. return err;
  2133. }
  2134. void EditorExportPlatformAndroid::get_command_line_flags(const Ref<EditorExportPreset> &p_preset, const String &p_path, int p_flags, Vector<uint8_t> &r_command_line_flags) {
  2135. String cmdline = p_preset->get("command_line/extra_args");
  2136. Vector<String> command_line_strings = cmdline.strip_edges().split(" ");
  2137. for (int i = 0; i < command_line_strings.size(); i++) {
  2138. if (command_line_strings[i].strip_edges().length() == 0) {
  2139. command_line_strings.remove_at(i);
  2140. i--;
  2141. }
  2142. }
  2143. gen_export_flags(command_line_strings, p_flags);
  2144. bool apk_expansion = p_preset->get("apk_expansion/enable");
  2145. if (apk_expansion) {
  2146. String fullpath = get_apk_expansion_fullpath(p_preset, p_path);
  2147. String apk_expansion_public_key = p_preset->get("apk_expansion/public_key");
  2148. command_line_strings.push_back("--use_apk_expansion");
  2149. command_line_strings.push_back("--apk_expansion_md5");
  2150. command_line_strings.push_back(FileAccess::get_md5(fullpath));
  2151. command_line_strings.push_back("--apk_expansion_key");
  2152. command_line_strings.push_back(apk_expansion_public_key.strip_edges());
  2153. }
  2154. int xr_mode_index = p_preset->get("xr_features/xr_mode");
  2155. if (xr_mode_index == XR_MODE_OPENXR) {
  2156. command_line_strings.push_back("--xr_mode_openxr");
  2157. } else { // XRMode.REGULAR is the default.
  2158. command_line_strings.push_back("--xr_mode_regular");
  2159. }
  2160. bool immersive = p_preset->get("screen/immersive_mode");
  2161. if (immersive) {
  2162. command_line_strings.push_back("--use_immersive");
  2163. }
  2164. bool debug_opengl = p_preset->get("graphics/opengl_debug");
  2165. if (debug_opengl) {
  2166. command_line_strings.push_back("--debug_opengl");
  2167. }
  2168. if (command_line_strings.size()) {
  2169. r_command_line_flags.resize(4);
  2170. encode_uint32(command_line_strings.size(), &r_command_line_flags.write[0]);
  2171. for (int i = 0; i < command_line_strings.size(); i++) {
  2172. print_line(itos(i) + " param: " + command_line_strings[i]);
  2173. CharString command_line_argument = command_line_strings[i].utf8();
  2174. int base = r_command_line_flags.size();
  2175. int length = command_line_argument.length();
  2176. if (length == 0) {
  2177. continue;
  2178. }
  2179. r_command_line_flags.resize(base + 4 + length);
  2180. encode_uint32(length, &r_command_line_flags.write[base]);
  2181. memcpy(&r_command_line_flags.write[base + 4], command_line_argument.ptr(), length);
  2182. }
  2183. }
  2184. }
  2185. Error EditorExportPlatformAndroid::sign_apk(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &export_path, EditorProgress &ep) {
  2186. int export_format = int(p_preset->get("gradle_build/export_format"));
  2187. String export_label = export_format == EXPORT_FORMAT_AAB ? "AAB" : "APK";
  2188. String release_keystore = p_preset->get_or_env("keystore/release", ENV_ANDROID_KEYSTORE_RELEASE_PATH);
  2189. String release_username = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  2190. String release_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  2191. String target_sdk_version = p_preset->get("gradle_build/target_sdk");
  2192. if (!target_sdk_version.is_valid_int()) {
  2193. target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION);
  2194. }
  2195. String apksigner = get_apksigner_path(target_sdk_version.to_int(), true);
  2196. print_verbose("Starting signing of the " + export_label + " binary using " + apksigner);
  2197. if (apksigner == "<FAILED>") {
  2198. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(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 %s is unsigned."), export_label));
  2199. return OK;
  2200. }
  2201. if (!FileAccess::exists(apksigner)) {
  2202. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("'apksigner' could not be found. Please check that the command is available in the Android SDK build-tools directory. The resulting %s is unsigned."), export_label));
  2203. return OK;
  2204. }
  2205. String keystore;
  2206. String password;
  2207. String user;
  2208. if (p_debug) {
  2209. keystore = p_preset->get_or_env("keystore/debug", ENV_ANDROID_KEYSTORE_DEBUG_PATH);
  2210. password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  2211. user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  2212. if (keystore.is_empty()) {
  2213. keystore = EDITOR_GET("export/android/debug_keystore");
  2214. password = EDITOR_GET("export/android/debug_keystore_pass");
  2215. user = EDITOR_GET("export/android/debug_keystore_user");
  2216. }
  2217. if (ep.step(vformat(TTR("Signing debug %s..."), export_label), 104)) {
  2218. return ERR_SKIP;
  2219. }
  2220. } else {
  2221. keystore = release_keystore;
  2222. password = release_password;
  2223. user = release_username;
  2224. if (ep.step(vformat(TTR("Signing release %s..."), export_label), 104)) {
  2225. return ERR_SKIP;
  2226. }
  2227. }
  2228. if (!FileAccess::exists(keystore)) {
  2229. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not find keystore, unable to export."));
  2230. return ERR_FILE_CANT_OPEN;
  2231. }
  2232. String output;
  2233. List<String> args;
  2234. args.push_back("sign");
  2235. args.push_back("--verbose");
  2236. args.push_back("--ks");
  2237. args.push_back(keystore);
  2238. args.push_back("--ks-pass");
  2239. args.push_back("pass:" + password);
  2240. args.push_back("--ks-key-alias");
  2241. args.push_back(user);
  2242. args.push_back(export_path);
  2243. if (OS::get_singleton()->is_stdout_verbose() && p_debug) {
  2244. // We only print verbose logs with credentials for debug builds to avoid leaking release keystore credentials.
  2245. print_verbose("Signing debug binary using: " + String("\n") + apksigner + " " + join_list(args, String(" ")));
  2246. } else {
  2247. List<String> redacted_args = List<String>(args);
  2248. redacted_args.find(keystore)->set("<REDACTED>");
  2249. redacted_args.find("pass:" + password)->set("pass:<REDACTED>");
  2250. redacted_args.find(user)->set("<REDACTED>");
  2251. print_line("Signing binary using: " + String("\n") + apksigner + " " + join_list(redacted_args, String(" ")));
  2252. }
  2253. int retval;
  2254. Error err = OS::get_singleton()->execute(apksigner, args, &output, &retval, true);
  2255. if (err != OK) {
  2256. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start apksigner executable."));
  2257. return err;
  2258. }
  2259. // By design, apksigner does not output credentials in its output unless --verbose is used
  2260. print_line(output);
  2261. if (retval) {
  2262. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("'apksigner' returned with error #%d"), retval));
  2263. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("output: \n%s"), output));
  2264. return ERR_CANT_CREATE;
  2265. }
  2266. if (ep.step(vformat(TTR("Verifying %s..."), export_label), 105)) {
  2267. return ERR_SKIP;
  2268. }
  2269. args.clear();
  2270. args.push_back("verify");
  2271. args.push_back("--verbose");
  2272. args.push_back(export_path);
  2273. if (p_debug) {
  2274. print_verbose("Verifying signed build using: " + String("\n") + apksigner + " " + join_list(args, String(" ")));
  2275. }
  2276. output.clear();
  2277. err = OS::get_singleton()->execute(apksigner, args, &output, &retval, true);
  2278. if (err != OK) {
  2279. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start apksigner executable."));
  2280. return err;
  2281. }
  2282. print_verbose(output);
  2283. if (retval) {
  2284. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("'apksigner' verification of %s failed."), export_label));
  2285. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("output: \n%s"), output));
  2286. return ERR_CANT_CREATE;
  2287. }
  2288. print_verbose("Successfully completed signing build.");
  2289. return OK;
  2290. }
  2291. void EditorExportPlatformAndroid::_clear_assets_directory() {
  2292. Ref<DirAccess> da_res = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2293. // Clear the APK assets directory
  2294. if (da_res->dir_exists(APK_ASSETS_DIRECTORY)) {
  2295. print_verbose("Clearing APK assets directory...");
  2296. Ref<DirAccess> da_assets = DirAccess::open(APK_ASSETS_DIRECTORY);
  2297. ERR_FAIL_COND(da_assets.is_null());
  2298. da_assets->erase_contents_recursive();
  2299. da_res->remove(APK_ASSETS_DIRECTORY);
  2300. }
  2301. // Clear the AAB assets directory
  2302. if (da_res->dir_exists(AAB_ASSETS_DIRECTORY)) {
  2303. print_verbose("Clearing AAB assets directory...");
  2304. Ref<DirAccess> da_assets = DirAccess::open(AAB_ASSETS_DIRECTORY);
  2305. ERR_FAIL_COND(da_assets.is_null());
  2306. da_assets->erase_contents_recursive();
  2307. da_res->remove(AAB_ASSETS_DIRECTORY);
  2308. }
  2309. }
  2310. void EditorExportPlatformAndroid::_remove_copied_libs() {
  2311. print_verbose("Removing previously installed libraries...");
  2312. Error error;
  2313. String libs_json = FileAccess::get_file_as_string(GDEXTENSION_LIBS_PATH, &error);
  2314. if (error || libs_json.is_empty()) {
  2315. print_verbose("No previously installed libraries found");
  2316. return;
  2317. }
  2318. JSON json;
  2319. error = json.parse(libs_json);
  2320. ERR_FAIL_COND_MSG(error, "Error parsing \"" + libs_json + "\" on line " + itos(json.get_error_line()) + ": " + json.get_error_message());
  2321. Vector<String> libs = json.get_data();
  2322. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2323. for (int i = 0; i < libs.size(); i++) {
  2324. print_verbose("Removing previously installed library " + libs[i]);
  2325. da->remove(libs[i]);
  2326. }
  2327. da->remove(GDEXTENSION_LIBS_PATH);
  2328. }
  2329. String EditorExportPlatformAndroid::join_list(const List<String> &p_parts, const String &p_separator) {
  2330. String ret;
  2331. for (int i = 0; i < p_parts.size(); ++i) {
  2332. if (i > 0) {
  2333. ret += p_separator;
  2334. }
  2335. ret += p_parts[i];
  2336. }
  2337. return ret;
  2338. }
  2339. String EditorExportPlatformAndroid::join_abis(const Vector<EditorExportPlatformAndroid::ABI> &p_parts, const String &p_separator, bool p_use_arch) {
  2340. String ret;
  2341. for (int i = 0; i < p_parts.size(); ++i) {
  2342. if (i > 0) {
  2343. ret += p_separator;
  2344. }
  2345. ret += (p_use_arch) ? p_parts[i].arch : p_parts[i].abi;
  2346. }
  2347. return ret;
  2348. }
  2349. String EditorExportPlatformAndroid::_get_plugins_names(const Ref<EditorExportPreset> &p_preset) const {
  2350. Vector<String> names;
  2351. #ifndef DISABLE_DEPRECATED
  2352. PluginConfigAndroid::get_plugins_names(get_enabled_plugins(p_preset), names);
  2353. #endif // DISABLE_DEPRECATED
  2354. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  2355. for (int i = 0; i < export_plugins.size(); i++) {
  2356. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  2357. names.push_back(export_plugins[i]->get_name());
  2358. }
  2359. }
  2360. String plugins_names = String("|").join(names);
  2361. return plugins_names;
  2362. }
  2363. String EditorExportPlatformAndroid::_resolve_export_plugin_android_library_path(const String &p_android_library_path) const {
  2364. String absolute_path;
  2365. if (!p_android_library_path.is_empty()) {
  2366. if (p_android_library_path.is_absolute_path()) {
  2367. absolute_path = ProjectSettings::get_singleton()->globalize_path(p_android_library_path);
  2368. } else {
  2369. const String export_plugin_absolute_path = String("res://addons/").path_join(p_android_library_path);
  2370. absolute_path = ProjectSettings::get_singleton()->globalize_path(export_plugin_absolute_path);
  2371. }
  2372. }
  2373. return absolute_path;
  2374. }
  2375. bool EditorExportPlatformAndroid::_is_clean_build_required(const Ref<EditorExportPreset> &p_preset) {
  2376. bool first_build = last_gradle_build_time == 0;
  2377. bool have_plugins_changed = false;
  2378. String plugin_names = _get_plugins_names(p_preset);
  2379. if (!first_build) {
  2380. have_plugins_changed = plugin_names != last_plugin_names;
  2381. #ifndef DISABLE_DEPRECATED
  2382. if (!have_plugins_changed) {
  2383. Vector<PluginConfigAndroid> enabled_plugins = get_enabled_plugins(p_preset);
  2384. for (int i = 0; i < enabled_plugins.size(); i++) {
  2385. if (enabled_plugins.get(i).last_updated > last_gradle_build_time) {
  2386. have_plugins_changed = true;
  2387. break;
  2388. }
  2389. }
  2390. }
  2391. #endif // DISABLE_DEPRECATED
  2392. }
  2393. last_gradle_build_time = OS::get_singleton()->get_unix_time();
  2394. last_plugin_names = plugin_names;
  2395. return have_plugins_changed || first_build;
  2396. }
  2397. Error EditorExportPlatformAndroid::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  2398. int export_format = int(p_preset->get("gradle_build/export_format"));
  2399. bool should_sign = p_preset->get("package/signed");
  2400. return export_project_helper(p_preset, p_debug, p_path, export_format, should_sign, p_flags);
  2401. }
  2402. Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int export_format, bool should_sign, int p_flags) {
  2403. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  2404. String src_apk;
  2405. Error err;
  2406. EditorProgress ep("export", TTR("Exporting for Android"), 105, true);
  2407. bool use_gradle_build = bool(p_preset->get("gradle_build/use_gradle_build"));
  2408. bool p_give_internet = p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG);
  2409. bool apk_expansion = p_preset->get("apk_expansion/enable");
  2410. Vector<ABI> enabled_abis = get_enabled_abis(p_preset);
  2411. print_verbose("Exporting for Android...");
  2412. print_verbose("- debug build: " + bool_to_string(p_debug));
  2413. print_verbose("- export path: " + p_path);
  2414. print_verbose("- export format: " + itos(export_format));
  2415. print_verbose("- sign build: " + bool_to_string(should_sign));
  2416. print_verbose("- gradle build enabled: " + bool_to_string(use_gradle_build));
  2417. print_verbose("- apk expansion enabled: " + bool_to_string(apk_expansion));
  2418. print_verbose("- enabled abis: " + join_abis(enabled_abis, ",", false));
  2419. print_verbose("- export filter: " + itos(p_preset->get_export_filter()));
  2420. print_verbose("- include filter: " + p_preset->get_include_filter());
  2421. print_verbose("- exclude filter: " + p_preset->get_exclude_filter());
  2422. Ref<Image> splash_image;
  2423. Ref<Image> splash_bg_color_image;
  2424. String processed_splash_config_xml = load_splash_refs(splash_image, splash_bg_color_image);
  2425. Ref<Image> main_image;
  2426. Ref<Image> foreground;
  2427. Ref<Image> background;
  2428. load_icon_refs(p_preset, main_image, foreground, background);
  2429. Vector<uint8_t> command_line_flags;
  2430. // Write command line flags into the command_line_flags variable.
  2431. get_command_line_flags(p_preset, p_path, p_flags, command_line_flags);
  2432. if (export_format == EXPORT_FORMAT_AAB) {
  2433. if (!p_path.ends_with(".aab")) {
  2434. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid filename! Android App Bundle requires the *.aab extension."));
  2435. return ERR_UNCONFIGURED;
  2436. }
  2437. if (apk_expansion) {
  2438. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("APK Expansion not compatible with Android App Bundle."));
  2439. return ERR_UNCONFIGURED;
  2440. }
  2441. }
  2442. if (export_format == EXPORT_FORMAT_APK && !p_path.ends_with(".apk")) {
  2443. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid filename! Android APK requires the *.apk extension."));
  2444. return ERR_UNCONFIGURED;
  2445. }
  2446. if (export_format > EXPORT_FORMAT_AAB || export_format < EXPORT_FORMAT_APK) {
  2447. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unsupported export format!"));
  2448. return ERR_UNCONFIGURED;
  2449. }
  2450. if (use_gradle_build) {
  2451. print_verbose("Starting gradle build...");
  2452. //test that installed build version is alright
  2453. {
  2454. print_verbose("Checking build version...");
  2455. Ref<FileAccess> f = FileAccess::open("res://android/.build_version", FileAccess::READ);
  2456. if (f.is_null()) {
  2457. 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."));
  2458. return ERR_UNCONFIGURED;
  2459. }
  2460. String version = f->get_line().strip_edges();
  2461. print_verbose("- build version: " + version);
  2462. if (version != VERSION_FULL_CONFIG) {
  2463. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Android build version mismatch: Template installed: %s, Godot version: %s. Please reinstall Android build template from 'Project' menu."), version, VERSION_FULL_CONFIG));
  2464. return ERR_UNCONFIGURED;
  2465. }
  2466. }
  2467. const String assets_directory = get_assets_directory(p_preset, export_format);
  2468. String sdk_path = EDITOR_GET("export/android/android_sdk_path");
  2469. ERR_FAIL_COND_V_MSG(sdk_path.is_empty(), ERR_UNCONFIGURED, "Android SDK path must be configured in Editor Settings at 'export/android/android_sdk_path'.");
  2470. print_verbose("Android sdk path: " + sdk_path);
  2471. // TODO: should we use "package/name" or "application/config/name"?
  2472. String project_name = get_project_name(p_preset->get("package/name"));
  2473. err = _create_project_name_strings_files(p_preset, project_name); //project name localization.
  2474. if (err != OK) {
  2475. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unable to overwrite res://android/build/res/*.xml files with project name."));
  2476. }
  2477. // Copies the project icon files into the appropriate Gradle project directory.
  2478. _copy_icons_to_gradle_project(p_preset, processed_splash_config_xml, splash_image, splash_bg_color_image, main_image, foreground, background);
  2479. // Write an AndroidManifest.xml file into the Gradle project directory.
  2480. _write_tmp_manifest(p_preset, p_give_internet, p_debug);
  2481. //stores all the project files inside the Gradle project directory. Also includes all ABIs
  2482. _clear_assets_directory();
  2483. _remove_copied_libs();
  2484. if (!apk_expansion) {
  2485. print_verbose("Exporting project files...");
  2486. CustomExportData user_data;
  2487. user_data.assets_directory = assets_directory;
  2488. user_data.debug = p_debug;
  2489. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  2490. err = export_project_files(p_preset, p_debug, ignore_apk_file, &user_data, copy_gradle_so);
  2491. } else {
  2492. err = export_project_files(p_preset, p_debug, rename_and_store_file_in_gradle_project, &user_data, copy_gradle_so);
  2493. }
  2494. if (err != OK) {
  2495. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not export project files to gradle project."));
  2496. return err;
  2497. }
  2498. if (user_data.libs.size() > 0) {
  2499. Ref<FileAccess> fa = FileAccess::open(GDEXTENSION_LIBS_PATH, FileAccess::WRITE);
  2500. fa->store_string(JSON::stringify(user_data.libs, "\t"));
  2501. }
  2502. } else {
  2503. print_verbose("Saving apk expansion file...");
  2504. err = save_apk_expansion_file(p_preset, p_debug, p_path);
  2505. if (err != OK) {
  2506. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not write expansion package file!"));
  2507. return err;
  2508. }
  2509. }
  2510. print_verbose("Storing command line flags...");
  2511. store_file_at_path(assets_directory + "/_cl_", command_line_flags);
  2512. print_verbose("Updating ANDROID_HOME environment to " + sdk_path);
  2513. OS::get_singleton()->set_environment("ANDROID_HOME", sdk_path); //set and overwrite if required
  2514. String build_command;
  2515. #ifdef WINDOWS_ENABLED
  2516. build_command = "gradlew.bat";
  2517. #else
  2518. build_command = "gradlew";
  2519. #endif
  2520. String build_path = ProjectSettings::get_singleton()->get_resource_path().path_join("android/build");
  2521. build_command = build_path.path_join(build_command);
  2522. String package_name = get_package_name(p_preset->get("package/unique_name"));
  2523. String version_code = itos(p_preset->get("version/code"));
  2524. String version_name = p_preset->get_version("version/name");
  2525. String min_sdk_version = p_preset->get("gradle_build/min_sdk");
  2526. if (!min_sdk_version.is_valid_int()) {
  2527. min_sdk_version = itos(VULKAN_MIN_SDK_VERSION);
  2528. }
  2529. String target_sdk_version = p_preset->get("gradle_build/target_sdk");
  2530. if (!target_sdk_version.is_valid_int()) {
  2531. target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION);
  2532. }
  2533. String enabled_abi_string = join_abis(enabled_abis, "|", false);
  2534. String sign_flag = should_sign ? "true" : "false";
  2535. String zipalign_flag = "true";
  2536. Vector<String> android_libraries;
  2537. Vector<String> android_dependencies;
  2538. Vector<String> android_dependencies_maven_repos;
  2539. #ifndef DISABLE_DEPRECATED
  2540. Vector<PluginConfigAndroid> enabled_plugins = get_enabled_plugins(p_preset);
  2541. PluginConfigAndroid::get_plugins_binaries(PluginConfigAndroid::BINARY_TYPE_LOCAL, enabled_plugins, android_libraries);
  2542. PluginConfigAndroid::get_plugins_binaries(PluginConfigAndroid::BINARY_TYPE_REMOTE, enabled_plugins, android_dependencies);
  2543. PluginConfigAndroid::get_plugins_custom_maven_repos(enabled_plugins, android_dependencies_maven_repos);
  2544. #endif // DISABLE_DEPRECATED
  2545. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  2546. for (int i = 0; i < export_plugins.size(); i++) {
  2547. if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) {
  2548. PackedStringArray export_plugin_android_libraries = export_plugins[i]->get_android_libraries(Ref<EditorExportPlatform>(this), p_debug);
  2549. for (int k = 0; k < export_plugin_android_libraries.size(); k++) {
  2550. const String resolved_android_library_path = _resolve_export_plugin_android_library_path(export_plugin_android_libraries[k]);
  2551. if (!resolved_android_library_path.is_empty()) {
  2552. android_libraries.push_back(resolved_android_library_path);
  2553. }
  2554. }
  2555. PackedStringArray export_plugin_android_dependencies = export_plugins[i]->get_android_dependencies(Ref<EditorExportPlatform>(this), p_debug);
  2556. android_dependencies.append_array(export_plugin_android_dependencies);
  2557. PackedStringArray export_plugin_android_dependencies_maven_repos = export_plugins[i]->get_android_dependencies_maven_repos(Ref<EditorExportPlatform>(this), p_debug);
  2558. android_dependencies_maven_repos.append_array(export_plugin_android_dependencies_maven_repos);
  2559. }
  2560. }
  2561. bool clean_build_required = _is_clean_build_required(p_preset);
  2562. String combined_android_libraries = String("|").join(android_libraries);
  2563. String combined_android_dependencies = String("|").join(android_dependencies);
  2564. String combined_android_dependencies_maven_repos = String("|").join(android_dependencies_maven_repos);
  2565. List<String> cmdline;
  2566. if (clean_build_required) {
  2567. cmdline.push_back("clean");
  2568. }
  2569. String build_type = p_debug ? "Debug" : "Release";
  2570. if (export_format == EXPORT_FORMAT_AAB) {
  2571. String bundle_build_command = vformat("bundle%s", build_type);
  2572. cmdline.push_back(bundle_build_command);
  2573. } else if (export_format == EXPORT_FORMAT_APK) {
  2574. String apk_build_command = vformat("assemble%s", build_type);
  2575. cmdline.push_back(apk_build_command);
  2576. }
  2577. cmdline.push_back("-p"); // argument to specify the start directory.
  2578. cmdline.push_back(build_path); // start directory.
  2579. cmdline.push_back("-Pexport_package_name=" + package_name); // argument to specify the package name.
  2580. cmdline.push_back("-Pexport_version_code=" + version_code); // argument to specify the version code.
  2581. cmdline.push_back("-Pexport_version_name=" + version_name); // argument to specify the version name.
  2582. cmdline.push_back("-Pexport_version_min_sdk=" + min_sdk_version); // argument to specify the min sdk.
  2583. cmdline.push_back("-Pexport_version_target_sdk=" + target_sdk_version); // argument to specify the target sdk.
  2584. cmdline.push_back("-Pexport_enabled_abis=" + enabled_abi_string); // argument to specify enabled ABIs.
  2585. cmdline.push_back("-Pplugins_local_binaries=" + combined_android_libraries); // argument to specify the list of android libraries provided by plugins.
  2586. cmdline.push_back("-Pplugins_remote_binaries=" + combined_android_dependencies); // argument to specify the list of android dependencies provided by plugins.
  2587. 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.
  2588. cmdline.push_back("-Pperform_zipalign=" + zipalign_flag); // argument to specify whether the build should be zipaligned.
  2589. cmdline.push_back("-Pperform_signing=" + sign_flag); // argument to specify whether the build should be signed.
  2590. cmdline.push_back("-Pgodot_editor_version=" + String(VERSION_FULL_CONFIG));
  2591. // NOTE: The release keystore is not included in the verbose logging
  2592. // to avoid accidentally leaking sensitive information when sharing verbose logs for troubleshooting.
  2593. // Any non-sensitive additions to the command line arguments must be done above this section.
  2594. // Sensitive additions must be done below the logging statement.
  2595. print_verbose("Build Android project using gradle command: " + String("\n") + build_command + " " + join_list(cmdline, String(" ")));
  2596. if (should_sign) {
  2597. if (p_debug) {
  2598. String debug_keystore = p_preset->get_or_env("keystore/debug", ENV_ANDROID_KEYSTORE_DEBUG_PATH);
  2599. String debug_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS);
  2600. String debug_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER);
  2601. if (debug_keystore.is_empty()) {
  2602. debug_keystore = EDITOR_GET("export/android/debug_keystore");
  2603. debug_password = EDITOR_GET("export/android/debug_keystore_pass");
  2604. debug_user = EDITOR_GET("export/android/debug_keystore_user");
  2605. }
  2606. if (debug_keystore.is_relative_path()) {
  2607. debug_keystore = OS::get_singleton()->get_resource_dir().path_join(debug_keystore).simplify_path();
  2608. }
  2609. if (!FileAccess::exists(debug_keystore)) {
  2610. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not find keystore, unable to export."));
  2611. return ERR_FILE_CANT_OPEN;
  2612. }
  2613. cmdline.push_back("-Pdebug_keystore_file=" + debug_keystore); // argument to specify the debug keystore file.
  2614. cmdline.push_back("-Pdebug_keystore_alias=" + debug_user); // argument to specify the debug keystore alias.
  2615. cmdline.push_back("-Pdebug_keystore_password=" + debug_password); // argument to specify the debug keystore password.
  2616. } else {
  2617. // Pass the release keystore info as well
  2618. String release_keystore = p_preset->get_or_env("keystore/release", ENV_ANDROID_KEYSTORE_RELEASE_PATH);
  2619. String release_username = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER);
  2620. String release_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS);
  2621. if (release_keystore.is_relative_path()) {
  2622. release_keystore = OS::get_singleton()->get_resource_dir().path_join(release_keystore).simplify_path();
  2623. }
  2624. if (!FileAccess::exists(release_keystore)) {
  2625. add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not find keystore, unable to export."));
  2626. return ERR_FILE_CANT_OPEN;
  2627. }
  2628. cmdline.push_back("-Prelease_keystore_file=" + release_keystore); // argument to specify the release keystore file.
  2629. cmdline.push_back("-Prelease_keystore_alias=" + release_username); // argument to specify the release keystore alias.
  2630. cmdline.push_back("-Prelease_keystore_password=" + release_password); // argument to specify the release keystore password.
  2631. }
  2632. }
  2633. String build_project_output;
  2634. int result = EditorNode::get_singleton()->execute_and_show_output(TTR("Building Android Project (gradle)"), build_command, cmdline, true, false, &build_project_output);
  2635. if (result != 0) {
  2636. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Building of Android project failed, check output for the error:") + "\n\n" + build_project_output);
  2637. return ERR_CANT_CREATE;
  2638. } else {
  2639. print_verbose(build_project_output);
  2640. }
  2641. List<String> copy_args;
  2642. String copy_command;
  2643. if (export_format == EXPORT_FORMAT_AAB) {
  2644. copy_command = vformat("copyAndRename%sAab", build_type);
  2645. } else if (export_format == EXPORT_FORMAT_APK) {
  2646. copy_command = vformat("copyAndRename%sApk", build_type);
  2647. }
  2648. copy_args.push_back(copy_command);
  2649. copy_args.push_back("-p"); // argument to specify the start directory.
  2650. copy_args.push_back(build_path); // start directory.
  2651. String export_filename = p_path.get_file();
  2652. String export_path = p_path.get_base_dir();
  2653. if (export_path.is_relative_path()) {
  2654. export_path = OS::get_singleton()->get_resource_dir().path_join(export_path);
  2655. }
  2656. export_path = ProjectSettings::get_singleton()->globalize_path(export_path).simplify_path();
  2657. copy_args.push_back("-Pexport_path=file:" + export_path);
  2658. copy_args.push_back("-Pexport_filename=" + export_filename);
  2659. print_verbose("Copying Android binary using gradle command: " + String("\n") + build_command + " " + join_list(copy_args, String(" ")));
  2660. String copy_binary_output;
  2661. int copy_result = EditorNode::get_singleton()->execute_and_show_output(TTR("Moving output"), build_command, copy_args, true, false, &copy_binary_output);
  2662. if (copy_result != 0) {
  2663. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unable to copy and rename export file:") + "\n\n" + copy_binary_output);
  2664. return ERR_CANT_CREATE;
  2665. } else {
  2666. print_verbose(copy_binary_output);
  2667. }
  2668. print_verbose("Successfully completed Android gradle build.");
  2669. return OK;
  2670. }
  2671. // This is the start of the Legacy build system
  2672. print_verbose("Starting legacy build system...");
  2673. if (p_debug) {
  2674. src_apk = p_preset->get("custom_template/debug");
  2675. } else {
  2676. src_apk = p_preset->get("custom_template/release");
  2677. }
  2678. src_apk = src_apk.strip_edges();
  2679. if (src_apk.is_empty()) {
  2680. if (p_debug) {
  2681. src_apk = find_export_template("android_debug.apk");
  2682. } else {
  2683. src_apk = find_export_template("android_release.apk");
  2684. }
  2685. if (src_apk.is_empty()) {
  2686. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Package not found: \"%s\"."), src_apk));
  2687. return ERR_FILE_NOT_FOUND;
  2688. }
  2689. }
  2690. if (!DirAccess::exists(p_path.get_base_dir())) {
  2691. return ERR_FILE_BAD_PATH;
  2692. }
  2693. Ref<FileAccess> io_fa;
  2694. zlib_filefunc_def io = zipio_create_io(&io_fa);
  2695. if (ep.step(TTR("Creating APK..."), 0)) {
  2696. return ERR_SKIP;
  2697. }
  2698. unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io);
  2699. if (!pkg) {
  2700. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not find template APK to export: \"%s\"."), src_apk));
  2701. return ERR_FILE_NOT_FOUND;
  2702. }
  2703. int ret = unzGoToFirstFile(pkg);
  2704. Ref<FileAccess> io2_fa;
  2705. zlib_filefunc_def io2 = zipio_create_io(&io2_fa);
  2706. String tmp_unaligned_path = EditorPaths::get_singleton()->get_cache_dir().path_join("tmpexport-unaligned." + uitos(OS::get_singleton()->get_unix_time()) + ".apk");
  2707. #define CLEANUP_AND_RETURN(m_err) \
  2708. { \
  2709. DirAccess::remove_file_or_error(tmp_unaligned_path); \
  2710. return m_err; \
  2711. } \
  2712. ((void)0)
  2713. zipFile unaligned_apk = zipOpen2(tmp_unaligned_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io2);
  2714. String cmdline = p_preset->get("command_line/extra_args");
  2715. String version_name = p_preset->get_version("version/name");
  2716. String package_name = p_preset->get("package/unique_name");
  2717. String apk_expansion_pkey = p_preset->get("apk_expansion/public_key");
  2718. Vector<ABI> invalid_abis(enabled_abis);
  2719. while (ret == UNZ_OK) {
  2720. //get filename
  2721. unz_file_info info;
  2722. char fname[16384];
  2723. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  2724. if (ret != UNZ_OK) {
  2725. break;
  2726. }
  2727. bool skip = false;
  2728. String file = String::utf8(fname);
  2729. Vector<uint8_t> data;
  2730. data.resize(info.uncompressed_size);
  2731. //read
  2732. unzOpenCurrentFile(pkg);
  2733. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  2734. unzCloseCurrentFile(pkg);
  2735. //write
  2736. if (file == "AndroidManifest.xml") {
  2737. _fix_manifest(p_preset, data, p_give_internet);
  2738. }
  2739. if (file == "resources.arsc") {
  2740. _fix_resources(p_preset, data);
  2741. }
  2742. // Process the splash image
  2743. if ((file == SPLASH_IMAGE_EXPORT_PATH || file == LEGACY_BUILD_SPLASH_IMAGE_EXPORT_PATH) && splash_image.is_valid() && !splash_image->is_empty()) {
  2744. _load_image_data(splash_image, data);
  2745. }
  2746. // Process the splash bg color image
  2747. if ((file == SPLASH_BG_COLOR_PATH || file == LEGACY_BUILD_SPLASH_BG_COLOR_PATH) && splash_bg_color_image.is_valid() && !splash_bg_color_image->is_empty()) {
  2748. _load_image_data(splash_bg_color_image, data);
  2749. }
  2750. if (file.ends_with(".png") && file.contains("mipmap")) {
  2751. for (int i = 0; i < icon_densities_count; ++i) {
  2752. if (main_image.is_valid() && !main_image->is_empty()) {
  2753. if (file == launcher_icons[i].export_path) {
  2754. _process_launcher_icons(file, main_image, launcher_icons[i].dimensions, data);
  2755. }
  2756. }
  2757. if (foreground.is_valid() && !foreground->is_empty()) {
  2758. if (file == launcher_adaptive_icon_foregrounds[i].export_path) {
  2759. _process_launcher_icons(file, foreground, launcher_adaptive_icon_foregrounds[i].dimensions, data);
  2760. }
  2761. }
  2762. if (background.is_valid() && !background->is_empty()) {
  2763. if (file == launcher_adaptive_icon_backgrounds[i].export_path) {
  2764. _process_launcher_icons(file, background, launcher_adaptive_icon_backgrounds[i].dimensions, data);
  2765. }
  2766. }
  2767. }
  2768. }
  2769. if (file.ends_with(".so")) {
  2770. bool enabled = false;
  2771. for (int i = 0; i < enabled_abis.size(); ++i) {
  2772. if (file.begins_with("lib/" + enabled_abis[i].abi + "/")) {
  2773. invalid_abis.erase(enabled_abis[i]);
  2774. enabled = true;
  2775. break;
  2776. }
  2777. }
  2778. if (!enabled) {
  2779. skip = true;
  2780. }
  2781. }
  2782. if (file.begins_with("META-INF") && should_sign) {
  2783. skip = true;
  2784. }
  2785. if (!skip) {
  2786. print_line("ADDING: " + file);
  2787. // Respect decision on compression made by AAPT for the export template
  2788. const bool uncompressed = info.compression_method == 0;
  2789. zip_fileinfo zipfi = get_zip_fileinfo();
  2790. zipOpenNewFileInZip(unaligned_apk,
  2791. file.utf8().get_data(),
  2792. &zipfi,
  2793. nullptr,
  2794. 0,
  2795. nullptr,
  2796. 0,
  2797. nullptr,
  2798. uncompressed ? 0 : Z_DEFLATED,
  2799. Z_DEFAULT_COMPRESSION);
  2800. zipWriteInFileInZip(unaligned_apk, data.ptr(), data.size());
  2801. zipCloseFileInZip(unaligned_apk);
  2802. }
  2803. ret = unzGoToNextFile(pkg);
  2804. }
  2805. if (!invalid_abis.is_empty()) {
  2806. 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)));
  2807. CLEANUP_AND_RETURN(ERR_FILE_NOT_FOUND);
  2808. }
  2809. if (ep.step(TTR("Adding files..."), 1)) {
  2810. CLEANUP_AND_RETURN(ERR_SKIP);
  2811. }
  2812. err = OK;
  2813. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  2814. APKExportData ed;
  2815. ed.ep = &ep;
  2816. ed.apk = unaligned_apk;
  2817. err = export_project_files(p_preset, p_debug, ignore_apk_file, &ed, save_apk_so);
  2818. } else {
  2819. if (apk_expansion) {
  2820. err = save_apk_expansion_file(p_preset, p_debug, p_path);
  2821. if (err != OK) {
  2822. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not write expansion package file!"));
  2823. return err;
  2824. }
  2825. } else {
  2826. APKExportData ed;
  2827. ed.ep = &ep;
  2828. ed.apk = unaligned_apk;
  2829. err = export_project_files(p_preset, p_debug, save_apk_file, &ed, save_apk_so);
  2830. }
  2831. }
  2832. if (err != OK) {
  2833. unzClose(pkg);
  2834. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not export project files.")));
  2835. CLEANUP_AND_RETURN(ERR_SKIP);
  2836. }
  2837. zip_fileinfo zipfi = get_zip_fileinfo();
  2838. zipOpenNewFileInZip(unaligned_apk,
  2839. "assets/_cl_",
  2840. &zipfi,
  2841. nullptr,
  2842. 0,
  2843. nullptr,
  2844. 0,
  2845. nullptr,
  2846. 0, // No compress (little size gain and potentially slower startup)
  2847. Z_DEFAULT_COMPRESSION);
  2848. zipWriteInFileInZip(unaligned_apk, command_line_flags.ptr(), command_line_flags.size());
  2849. zipCloseFileInZip(unaligned_apk);
  2850. zipClose(unaligned_apk, nullptr);
  2851. unzClose(pkg);
  2852. if (err != OK) {
  2853. CLEANUP_AND_RETURN(err);
  2854. }
  2855. // Let's zip-align (must be done before signing)
  2856. static const int ZIP_ALIGNMENT = 4;
  2857. // If we're not signing the apk, then the next step should be the last.
  2858. const int next_step = should_sign ? 103 : 105;
  2859. if (ep.step(TTR("Aligning APK..."), next_step)) {
  2860. CLEANUP_AND_RETURN(ERR_SKIP);
  2861. }
  2862. unzFile tmp_unaligned = unzOpen2(tmp_unaligned_path.utf8().get_data(), &io);
  2863. if (!tmp_unaligned) {
  2864. add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not unzip temporary unaligned APK.")));
  2865. CLEANUP_AND_RETURN(ERR_FILE_NOT_FOUND);
  2866. }
  2867. ret = unzGoToFirstFile(tmp_unaligned);
  2868. io2 = zipio_create_io(&io2_fa);
  2869. zipFile final_apk = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io2);
  2870. // Take files from the unaligned APK and write them out to the aligned one
  2871. // in raw mode, i.e. not uncompressing and recompressing, aligning them as needed,
  2872. // following what is done in https://github.com/android/platform_build/blob/master/tools/zipalign/ZipAlign.cpp
  2873. int bias = 0;
  2874. while (ret == UNZ_OK) {
  2875. unz_file_info info;
  2876. memset(&info, 0, sizeof(info));
  2877. char fname[16384];
  2878. char extra[16384];
  2879. ret = unzGetCurrentFileInfo(tmp_unaligned, &info, fname, 16384, extra, 16384 - ZIP_ALIGNMENT, nullptr, 0);
  2880. if (ret != UNZ_OK) {
  2881. break;
  2882. }
  2883. String file = String::utf8(fname);
  2884. Vector<uint8_t> data;
  2885. data.resize(info.compressed_size);
  2886. // read
  2887. int method, level;
  2888. unzOpenCurrentFile2(tmp_unaligned, &method, &level, 1); // raw read
  2889. long file_offset = unzGetCurrentFileZStreamPos64(tmp_unaligned);
  2890. unzReadCurrentFile(tmp_unaligned, data.ptrw(), data.size());
  2891. unzCloseCurrentFile(tmp_unaligned);
  2892. // align
  2893. int padding = 0;
  2894. if (!info.compression_method) {
  2895. // Uncompressed file => Align
  2896. long new_offset = file_offset + bias;
  2897. padding = (ZIP_ALIGNMENT - (new_offset % ZIP_ALIGNMENT)) % ZIP_ALIGNMENT;
  2898. }
  2899. memset(extra + info.size_file_extra, 0, padding);
  2900. zip_fileinfo fileinfo = get_zip_fileinfo();
  2901. zipOpenNewFileInZip2(final_apk,
  2902. file.utf8().get_data(),
  2903. &fileinfo,
  2904. extra,
  2905. info.size_file_extra + padding,
  2906. nullptr,
  2907. 0,
  2908. nullptr,
  2909. method,
  2910. level,
  2911. 1); // raw write
  2912. zipWriteInFileInZip(final_apk, data.ptr(), data.size());
  2913. zipCloseFileInZipRaw(final_apk, info.uncompressed_size, info.crc);
  2914. bias += padding;
  2915. ret = unzGoToNextFile(tmp_unaligned);
  2916. }
  2917. zipClose(final_apk, nullptr);
  2918. unzClose(tmp_unaligned);
  2919. if (should_sign) {
  2920. // Signing must be done last as any additional modifications to the
  2921. // file will invalidate the signature.
  2922. err = sign_apk(p_preset, p_debug, p_path, ep);
  2923. if (err != OK) {
  2924. CLEANUP_AND_RETURN(err);
  2925. }
  2926. }
  2927. CLEANUP_AND_RETURN(OK);
  2928. }
  2929. void EditorExportPlatformAndroid::get_platform_features(List<String> *r_features) const {
  2930. r_features->push_back("mobile");
  2931. r_features->push_back("android");
  2932. }
  2933. void EditorExportPlatformAndroid::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) {
  2934. }
  2935. EditorExportPlatformAndroid::EditorExportPlatformAndroid() {
  2936. if (EditorNode::get_singleton()) {
  2937. #ifdef MODULE_SVG_ENABLED
  2938. Ref<Image> img = memnew(Image);
  2939. const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
  2940. ImageLoaderSVG::create_image_from_string(img, _android_logo_svg, EDSCALE, upsample, false);
  2941. logo = ImageTexture::create_from_image(img);
  2942. ImageLoaderSVG::create_image_from_string(img, _android_run_icon_svg, EDSCALE, upsample, false);
  2943. run_icon = ImageTexture::create_from_image(img);
  2944. #endif
  2945. devices_changed.set();
  2946. #ifndef DISABLE_DEPRECATED
  2947. android_plugins_changed.set();
  2948. #endif // DISABLE_DEPRECATED
  2949. #ifndef ANDROID_ENABLED
  2950. check_for_changes_thread.start(_check_for_changes_poll_thread, this);
  2951. #endif
  2952. }
  2953. }
  2954. EditorExportPlatformAndroid::~EditorExportPlatformAndroid() {
  2955. #ifndef ANDROID_ENABLED
  2956. quit_request.set();
  2957. if (check_for_changes_thread.is_started()) {
  2958. check_for_changes_thread.wait_to_finish();
  2959. }
  2960. #endif
  2961. }