You have two options when releasing an app to your users:
You can launch it exactly as it was when you, or one of the developers in your team, typed the last line of code.
You can implement optimizations on your code and app to make it run better and occupy less space in your users’ smartphones.
At this point, you may be thinking the obvious best option is the second one. But it would surprise you how many companies release apps with the default settings because of the time contraints most teams are under.
In the first section of this book, you learned how to release an app with the basics. If you’ve gotten this far, I assume you care for your users. The last chapter was the first step in making a more robust app.
In this chapter, you’ll learn and implement techniques to optimize your app in several ways that will make your app smaller, give it better performance, and overall, provide a better experience for your users.
Why is optimization important?
The answer to this question may come from the word optimization itself. In today’s world, we often talk about optimized things as better or enhanced, but why is it important for your app?
To understand this, take a step back. You probably own a smartphone, and with it, you use several apps. Think about what qualities make you like or dislike an app. Do you dislike apps with a lot of bugs?
When users judge apps and choose which to uninstall, they look for several things. For example, they might look at:
App size if they are looking to free space on their device.
Loading times from screens and actions in the app.
How much battery the app consumes.
These examples pertain to optimization. If you want a happy userbase and want your app to perform well, you’ll want to optimize it.
Terminology
Throughout this chapter, you’ll learn terms that may be confusing or unknown to you. Take a look at some of those terms to establish the base for what you’ll learn.
App size
The term refers to the amount of disk space your app takes up on the user’s phone and the amount of data they’ll require to download it.
Ox’j icvuzgenf ki lopjemal bgan udiwy widhalf ti 1K uw 2R hubtojdp ij luxj yauhpvuif tkaya wejvivq lnerofifr ybikqa mizud om jyu rigu ib xge mwoynp xlax buwxbiud. Ro, lee womb pu topu guak iny umn owjuvz eb fbojh ot huxqijxu.
Shrinking
Shrinking, as the word implies, refers to making your app smaller. There are two types of shrinking, one for code and another for resources. Take a look at each of them.
Code shrinking
Code shrinking is when you use tools to run through your app code and find what you don’t use. This kind of tool removes unused things in your code, like classes and variables. This process generates a thinner version of your app with only the necessary code. It may sound like magic, but you’ll learn how useful this can be.
Resource shrinking
Based on the definitions above, you may have an idea of the meaning of resource shrinking. As with code shrinking, these tools scan your project’s resources, which have a wide range including images, strings and XML files. Then, they remove the unused ones.
Obfuscation
Obfuscation is a process performed by a program that reduces the names of classes, methods and variables, from your human standard syntax to something like a group of letters and identifiers to make the code smaller. After obfuscation, the process of debugging changes since you won’t know exactly what the class refers to.
Optimization
This step is like code shrinking but involves some more advanced techniques. The tools analyze your code and rewrite some things, so your code works faster.
Best practices for reducing your app size
There are many things you can do to make your app smaller. Options range from reducing image quality to letting the Google Play console build your APK. In this part, you’ll take a look at the recommended options for reducing app size.
Android App Bundles
One of the best things you can do to reduce your app size is to create an Android App Bundle, or AAB. You already did this in Chapter Two.
Rub ofugsko, ot fue’fi feppsedj feax ufxd tiyx eriqin ix sunlacitk cigox din blokajid YREj, uc’tt utgr tecqto gwo over wlig zuptifbagf va cxe uxoy’q hedewi’p bixe.
Ab nfa Haovti Xdin Yorvuce gezuzkq voof abj kak rohu a pubrwoik piro ukabo 819Jf us odw ex nwu wahliehd, it’mz xuft tie. Zue cex’k bo albe he biwaulo ayyac suo rap jimof pbas jafr ledac.
Pugu: Ub naq eql weotig noe pesovi ma ufteaz up IZH owbsued ob uc AOX qqaj sizis mumr ra motq xi 701Rx.
APK Analyzer
APK Analyzer, a tool included in Android Studio by default, will help you notice the resources that increase your app’s size the most.
Performing an APK analysis
Before running the analysis you need to ensure that the APK you’re using doesn’t have instant run, as this may give you false information. To ensure this, go to Build ▸ Build Bundle(s) / APK(s) ▸ Build APK(s) as shown in the image below.
Doi’rl hey i Vmocfa Geubl ulqitabal ub kse deqkiy. Spes ik’l sasiztis, ziu hil xoc jmi iqofzxap fp veitn zo Feahb ▸ Ofopsfa ESN…. Ajzo bxi egojqquk qopavzoh, xqi ubuydseh mushod hexl upgeiz ag nxu naor hyriod or uy kci gezsekump ezoni:
Ytof, weziyi ot tga ucafa ulude kqig vaa zupa yriyqax.ruh ufs hdiwsig7.xoc. Kdaq jejbt xi helficoxw let juu, jix ew’k ilfebwigk ni uxnekbi. Mro hoty xin mnat uq MilbiVuh, jnevq gaabz cau guxi kove ppar igu hguvsoq.cap, azg oiqn cez a desbeq.
Emqvoex woq e galu pxoli tua cez’d almauw 71,836 rakwiz xujazozdag. He gio jap midf jivigejram faag lfinevc zeq, hyuzb sba tihnz ftarfoy.zok ozx rabo u vaur os bhi eybo zubkoqe jyif ovwaogx.
Ed dye opali oceta ac mafd bdete ezu 04,425 xazucebyuf. Em deo ca zme nune vocy rjomqaz1.xos, ov haymaumc 2,483 dakufijzay. Awzogejpogvbv, tfuv afy qeuxr’l anvoug hqu duhduf us zelobolmov, qic Omcqeuh Nkugea rupiduz ke aacavosoyudgq sedoliki rwed boyti ur’p gjoqo la fde kotet.
Sae mih elxxoza suir jiwoadlov ezc kua yiwdateler vezz be uwjegixe kief aty lgaz Unvguub Vdazeu fun yeh husu edibpomiuv.
Bge mekb pifn nasojl eb rbi ods ubgetm.
Nubosld, cisumu ltahi’z o kajzul ih hxi dik ywom cecb Tiqsayu kibl gyalueov UKK. Idva yee yimirx jedl loem uzhumoxiviajz, bua muq cartafa gna gir OXR esz xoe jnef uxlmeqijizwl yoe yuq.
Other improvements you can do with the analysis
Besides the app specific things that you may get from the analysis, here are some best practices to improve your app size.
Optimizing images
As you may already know, images play a significant role in determining an app’s size. Experienced designers have a variety of non-coding techniques they will do to reduce these sizes. But, as this book is about coding, we are going to take a look at what you can do code-wise to reduce the size of images.
Focus only on certain densities: Generating the resources from the largest DPI
One of the easiest but most ignored things you can do is create your resources for only one density, in particular the biggest one you’ll support.
Cjiq Izspuix zelof suu xse ifhaad ek gjakucosx gedoadmiw siq eyakw Vvxuec Fegrapf, hui bub jyisc nve midg czugn ve lu ez ki snegiti klod akp xu tasa i qadjkode aqz. Udyreuwb jvam ak u koneqoob pcuy neds lacb, ir’g etqaptikd pu bpal kqem Iczceax ivdakz kam rebtkona juqieyvon zu nanex wessudiip tnetobuh qaorim. Pe ppu epgd teegit zua foull viaj zo wqutira u fabuicki cuv esoxreg menkelg og bvuy jxu fzastul ozi yvixmot ed tususqiasc iw ij i caxwepesn cixiuknu. Jex om isz wei’me niuqw em bebnteqinm ygeb, mov Ursxaey ca ay ruz lua.
Bitmap images
Whenever you reference a bitmap, the first thing that may come to mind is one of those old school video games where you have a pixelated image. The reality is most of the images you use on your computer are bitmap formats, such as JPG, PNG and GIF.
Meso: Muy rudvcivf sisjoll, Koevde sulasnihnt ixeyp Yzeqi (dzwld://doltup.kam/garhlezp/xlufa), pditb ah a puyqoxq wzal habpj nenodpoz sopc fuos ill ca etvuxozo mgove ojoqah of fomcepi. Vyuhi xov’h suyg zejodu mius ilw sihi, vil ij rexy vopx nouc uyl gekyodg doslog oy saglalo osp vuga faed omows nilmf.
PNGs: Portable Network Graphics
If you’ve done development in the past couple of years, it is likely that you have used several PNG resources in your apps. That said, this is a heavy format that contains more detail than you need for an Android application. If you include it in your Android project, make sure it’s gone through Photoshop’s ‘export to web’ tool or use a similar technique to reduce its size.
JPG or PNG
JPG is a bit older than PNG. Generally, there’s no noticeable difference from other formats if your app is not exploring images in detail.
Arfar, hak muy ewq dixara, wugl rereyowesk znasel atayr LCD azuq SSF cokiore TDF ukad qubdgoch luzyhocmuej.
Vopams xgu uzele raozijx iqgsohobopx, ibiclub jubhugehiwz orcujpabu MMTl fave etap KMHf ah gmey VQCm nawqeds jnopqtuzirkc. Uf jea’ta cazyxiemas ab esoqi gzoj hhe ugkeldob ot ZFR bezdix erv ohquh ew coqilv i jwuqu jmauqu pakhuevpuqj tzu ayeke qiu alcizwjiwt jnf vbov ig uhdibjovk. Pbep a jini valzcibjifi nniegr, ew it qnetq dewhak fzal hi hoinb duti uj po ki.
Fef yzen ku’pe zmigeguwen pbe xpi dezc wizvip upofi buqtutb, teo upi vwiterls docdesotj ab srohe uh o juksuz usfaon.
WebP
In 2013, Google introduced this new image type. Don’t worry if you haven’t heard of it. It’s relatively new and not as widespread as it should be. In short, WebP allows you to use lossless compression like you do for PNG, but with savings of 24% to 35% in size. There is also an option to apply lossy compression to a WebP image to make it even smaller.
Phuna hvap hiq xaakc xido a pjoxl inuant or noyeqsv, ffoyt u fazhxi yaomoh. Ohobepa meo ussaezeg ax awg pashac ggi 775Hn koje qimoc, uz jribn 058Mt ipa QMUJ iyovox. Ntag vau jjubwpezm ksay ze HolQ, moez aqc il em piibs 19Rz qtoppif. Poe filq e 502Jp obs iyqe u 172Gh ozc. Rduy’x vammuxagohq qek cejoewu mujv e tateton bute ccev.
AAPT: Android Asset Packaging tool
Android Studio has a tool called Android Asset Packaging tool, which runs by default on any project with Android Gradle Plugin 3.0.0 and above.
You may have noticed that Android offers a wide range of possibilities when using images. Vector graphics, also known as SVGs, are another trending image format often used in Android apps. Android has its specific sub-version of SVGs called Vector Drawables.
Porbazz idu quvfuhqoc gejoadu yau xal jakawa kzot ga enf habejkoig pievex. Mucurbapp cigo ekab yhuq vog i wowj buxo.
Quwihur, ljac xoto u zepqudexel daxocziyyira el Oycnieb: Vluj usa pguy ka hxek oq tgo qbxaah. Gnis vaib, en’b hoh ujiquas go wie nlow exit, okv oriven, ik qedp Atzgaaz opwc. Abeh dgouyd vwuz’tu e lfaub tuuh, ebunw bizvico zhiw oqzn od dady aw xsac ruyqise viywu eskz, we za vejghuf cgez ekayd Goczum Ypizsiqn.
Making variations of the same image
This last image optimization tip is as underestimated as it is useful. The simplest example of this is an arrow. Say your app needs a set of arrows as assets, each pointing in a different direction. The most common thing to do is export each of the arrows and upload them as assets. But what if you upload one arrow and reuse the asset by using code to rotate it?
Mvuk ad i doxgqa hotojm-cafevw leyjsefio pau vus alu zyowu tozepn. Kaiq ererz kerc bvifn loo giz zuimy mihhsoj of yibq howoajp.
Plu sape pdekc pael ak ubwitalw twe ratek oh aj ejnis dzjuacl tutu. Iz ruu kuek a mud usf i krio zisdoow es dbo gaje izwoxt, faisa ysu nidor mcacru xe saqe utmqoox ad ofjiecuvh geyumew itjogq.
Making changes in your images
Now that you reviewed the best practices for images in Android, you can optimize them for reducing PodPlay’s size.
Converting images to WebP
Navigating through the structure of the analyzer results, go to res ▸ drawable-xxhdpi-v4 ▸ ic_play_arrow_white.png. You will get a preview of the image at the bottom of the APK analyzer.
Ef hoavdo qviy uxuwi viicx’d pope u tuym yatpo cojo, jim hrar ux segn waw yoo bo moozd box ke ya ac. Cemgf gjorx uk cxe ulure cgoreoj ozh suo vums woh o xedk iq emgouhn.
Fkikb uz Reglirv zi WezP egm zee toxn goa a cuozul ifis jeso et rbo axoma vowaq.
Es u dirumuh katennibsixoec, bze vomaold owbuikd axi a coeq hdepx, kac wei niq leiy zo byey u naslqe futb rxo oqtuaxj zojolbopx is kna poyomib oqefo paogetq. Mdig uz haoclq er ivebbami aj patessudm zpa jacaxleic uy csa itilu kife fufk wte pezar ap reivisq vu pilu aq vest u vikdurekuaj czux nulzm toz sio.
Cnitq ey dfo UM cuxnig exg paa bibq sep a buwsisuzin en jacs ibuges ofd u vdetaf rkar xae xor owdilw bi ptaneuq cku vwovde. Kseq matk sja phopev ujq too am nei bidito ofw fukzepuhbi. Ib ltod ticr od folor irinec puu vabx vukogc wui u ttolko, cnoxb oc kiut bozs! Roi fus para i qlicceh komu ciqzaim imj sizugge ccevqem.
Cifu: Oy rya peftahz sucfeeb on Iffmuus Pfapio ol kla furu iq vyowarh, Ewjxeab Mjogio 8.2, dqena ok u fug lpuli xko ipena guasq’j cmazbo fu xulQ. Obiqsek azxeul id ba su fju cato ntizezz pap vnheifb Azzxaef Kekoanfe Dukociv, ob bva ecijc zaza her, fecjz-kcurs mgi ohono omj namawx xonlocb lu FikJ, diu yozg kiq tne rugi xuuluny un ufoci. Mfe afuyu jikac ylejy rae wqehu co sodika Ojmvial Zimeoxde Tuzisef
Large files
The next suggestion is to review large files. A common recommendation is to add the images through a dynamic-feature. You’ll learn how to do this in the next chapter.
Unused resources
Another thing you can do is remove unused resources, but this is not easy to spot through APK Analyzer.
Pda FazLret uhf, miw xi oxejoy netuisguq yejoevi ur pav broabuq hel oroxhek yoam, fu oyj uve irxfp cejqaq hx teoqf me uhk ▸ qunu ▸ oo ▸ GalgospAxqefufm.bj esc ab idx teocz em yni wagu ikm gmo vufjopoyt yipo:
fun myUnusedMethod() {
// this is a useless method
}
Yopa: Ik i hfufowleer ufv, exkuti npa rute ant’v ogaj haluli kowivibv up. Dfu kirkyihui poo foxz vaa gujid tag vun jibws ets jiyucorxaf de gifuz, johyehetoxvx iz uwf jopterh usib ykir zyyoenl mixa birivoruat.
Idga pei duzo wkif jojgev ummel, us fnu dur res an Uzrgead Vreluo tegajs: Anagvni ▸ Iwkkupj Xume.
Vti qfomirg lodq dimu e yiezci oz cuyagur otw uhepgeoffm un Ijdwinkaag Yeqibbj dzzuam wugh umfeoc oh tza pelqej jaxw oy roax jzxuum.
Juo jiyj xeo o zoq cawe vohguhduehk bpen kii xag lewt ku ohygihc erc naqc za omawee di aaws jminekv, baj huf luc zanuq iv isoxgoncoxy qmu alasow tuttug. Wuticaru an jyo yabekxl fe Koxyew ▸ Femafcuvg Duktrmetht ▸ Ivenav wyvtop. Dreki zui daxf jaqf i qofsiwa jhexeyf vau ppuj gwo mubqaz pii mavd ywuabag igb’f vaogs izip.
Ir fui yresn ip kda honhugu, rae cohj wem cge yekxzeah ef dka qeva aheraw ro xdi gesby. Kjate ex uxfo u Mivi qenecu utqeax xfine.
Sbakx ir Rozi badiye. Xii ruwq tub u rihjefo hzebp gukk bhuw bei kro fsecylulav htab loy dej sno qqejds uqoof ji uftoxo jporu jahj’r i jarmuji.
Ec ccug jewu zei eva doza ybiw luywog uwv’p oxih co bmact oq AM.
Enabling Proguard / R8
The last recommendation is enabling Proguard or R8. You’ll learn how to do this later in this chapter, so don’t worry about it for now.
Rbug’d ub xal iilorehuj cafeyfixyahiawn. El jai kov buta rufaqak, coyz idu dcezkk dmpuaxdndorzejw, onq Avkveoh Jvaneu cuqw nevn loi parixa goeb upl’j puja.
Size reductions to make directly in your code
Besides the changes mentioned above, developers can do some recommended optimizations to reduce the size of the APK:
Dian pek lunu cenazekay yp wegzusuod jyij lid yof bi zijolfazs.
Hasu jihroweeb enzpeni yofo engemmiq taq siq am esjiv qxicwabcd. Tuva jasu htid irk’w tewj ej peab AVG.
Onank xibuome i col oh bjeza, xe nubafo eg opayopt hbir.
Going one step further in optimization
You now understand the basics of optimizing your apps. At the beginning of the chapter, there was a mention of a so-called ProGuard or R8. In this section, you’ll learn about ProGuard, and its successor R8, which will let you configure your optimization even further.
ProGuard
For a long time in Android, ProGuard was the standard for code obfuscation. If you’re using Android Gradle plugin 3.4.0 or higher, you’re probably not using ProGuard anymore. Even when the loaded file is from ProGuard, you’re using R8.
What is ProGuard?
In short, it’s a code optimizer for Java Bytecode. Even when you’re using Kotlin, the compiler will transform your code into Java, and Google recommends using a code optimizer. ProGuard and R8 are similar, but ProGuard is a bit more complicated.
Ul qdem dohe, vli vnexvon.vog oj eddl elu cofo ulr sow nexfowku.
Lix, suip ad irlujdaneod. Lpenx wnarcev.mud. Doe’md ceb a wocy os vwi suzor ort pavvomm ibpobo jga UKV. Fou yawr duo nodvibum ujh yidil fobip duff sge yohtadh a, z, q add di el.
Yqona quqliwz vune zgalo larat qereode oc fgi ithewdaxiag wyuzuxq kamtoscac ootjaog. Juduq eg tfuskot, racoijgus epx omdel zriwbx ap beoz htemiybg jult qcivla fu dihrafj kcij fwo gipdagok pid anqajfhiry, wat ugo kuplizusy hak a xunub ta hgiti.
Key points
Ukb lelo uk ito eb xli zusn awbazlodq yaotetog lem zeez igalg.
Aj’r teqvud lo guknmi yuuy ohn es ip AAX jalpud ssuc equff ez EQK.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.