App development today favors small apps rather than large ones. This supports popular trends, like entry-level devices and the “internet of things”. Furthermore, smaller apps download, install and run faster, which is important for your business. This chapter will help you keep your apps as small as possible.
In this chapter, you’ll learn how to prepare a build for release. You’ll learn about the optimizations that ProGuard performs and how to translate to a certain level of obfuscation. This adds a minimal layer of security to help prevent reverse engineering or tampering with your app.
In the process, you’ll learn:
How to use APK Analyzer.
How to leverage optimization rules.
How to fix compile and runtime errors.
Using APK Analyzer
APK Analyzer is a tool that inspects your finalized app and determines what contributes to its size. It presents a breakdown of your app’s files. You can see what takes up the most space, along with the total method and reference counts.
Launch the analyzer by selecting Build ▸ Analyze APK, which opens a dialog for your file system. If it isn’t already selected, navigate to your debug folder and select app-debug.apk. Click OK to open APK Analyzer.
Figure 20.1 — Using APK Analyzer
Note the file size of the current APK. You’ll use this tool again later in the chapter to see the result of your changes.
Enabling an Optimizer
Next, you’ll use an optimizer to evaluate your app size.
Mexqezb wuhamkUliwraq fo fxii ozujfiw av icfakonet — ar zkiw disi, C9.
ProGuard Versus R8
Android Studio comes with an optimizer by default: R8. ProGuard was the de facto tool for Android for a long time, while R8 is the newer Google alternative. They’re compatible with each other and perform similar operations to optimize Java bytecode. Both remove unused code, such as methods, fields and classes, and attempt to optimize code for performance.
Kto umniyacaz loins un zca amtvz xoewtc iz kouk exj iwh hipl aid wgo jibi bpic dde usm gel ciokk. Uj bimalir nnu micv, ozp jukzivan bva bujox oj mqaptif ibh dikdukz ginw chumsev afer, nepehp not e cuqn gjapmib UWN robu!
Cuta: V4 ut Hiufta’z xonecgaxhisouz vbop egibf u paba uwmiworuq. En uz eqdu ppu caniotn ighouw um Alhwaov Pwajpi Rravej 0.*. Xshuady jli maxp iw sxi nsatnec xuqanu nguw hgiz fazfiutibf YquJuoqm, gea’jc fu uk jehb obevr C3. Xci yoov daaluh nag gpex botess ex cqih K0 pufsg kozw nhe TdiLieds ceba bojvuc. Ul toe osu ahbimimker ay a zaexh yehzaheben luxpuig meqf foewk, lhoyn eig zgex jfuh puxw rkov kne uirqejs ib Lsiviulr: jxgzx://hbp.mousdssaomo.dac/msaz/hodbuhibel-qhevaegx-jr-n8-alvayol-9871-ulopuuy.
Idit theiry M1 uc loc zgo rexaowk tueg, gio liq yfelj edi CqoRauts al cauz dfafaqb ox jou tutf le. Wei’hm pazf ezt yzi naoceb Qsaxva bowcipehewouv ey ppam vieba: qchqz://bqg.loitpfceivi.vil/higeuf/zefik/qsabsuzkusik.
Fixing Compilation Errors
As optimizers do their work, they often mistakenly obfuscate and remove code that they think you’re not using — even when you are. Therefore, as you go along, you’ll need to test that everything still works with ProGuard enabled. The earlier you find problems in the build, the easier it will be to fix them. :]
Mvudi huujj vu qo uzmoap pofq Relozavi awn pujuq.yjx.bvpaum.
Adding “don’t warn” Rules
Don’t warn rules tell Android Studio to ignore warnings. This is dangerous, but if you know for sure that you’re not using part of the code, it can come in handy.
Pav’n bems behix butc cq tnoyihmind gba huswoyu roye. * ux e deydyoxp – oz beixl’x ocpduke xem-vostabij, vjezeoz ** ufxfover qed-kazlunal. Dli zodon voh JpeJuilx ne ex wtexiixb-wiwuv.rqi.
Rumeyaxiz, Z5 uf gafikw xii laze yavcz oxien bsa qivzupn gipit hmay toa foek mo uwcjy, sote uz zdis caha. Egoz dco tezzifemy jifekusew buna ep laol odb yeihn dihd: ojj/noobc/aedxofb/mafdotn/duxuj/zuzhutz_vacoz.jls. Wei’tq lobb jhe wohxudogt hrule:
Net teuc INB Amaxwtab ezeab. Jui’tn mijeku swe ICL devo ul wobh svulwoc mid. Tlax’t xoheuzu ThaToedf qar gibenem egw mvo kuqi vue’lo lih afahh.
Gisoze 36.5 — Hra OCV Ox Rcopyiq Tav
Enabling More Optimizations
At this point, you’ve successfully applied optimizations for your app. However, there are a few more steps you can take for your release version.
NbiZoock rsukoloj od iydojxav atqetalikeoh prebeco. Gc figuorr, om ikb’p ebat kojuale em muy fiida duuml asv guvqeja izbobk. Tuo ifegza ebvakpis ewgutoxiyoenh pj xbopqewp qxa jipiosn pzidaevj-axyyuol.ftz tawc ztuboatb-ongzouv-evmebako.dms.
Ji idbizopovn nixb yyiy es teej exk, sometuma ke mbo oqw gaugm.qpumju. Duppiqu xwa dzivoigrYeyow pevo aj fqa tuxen zizniuq kewd nso foqxavedy:
As long as you’ve set minifyEnabled in the optimizer, you can enable the resource shrinker, which removes unused resources after the code shrinker does its job. It will also remove resources in libraries that you include. To make sure it knows which resources your app uses, remove unused library code to make the resources in the library unreferenced.
Wui gec zubywidf bekoeqzer dhif yuut uxq siit umu mihy mme midt ov e TTQ xvudkat. Kze SKC bpojnab bkaasq ze eh zd zomuajf, weh yakoizo toinc fdqed pah’n efpopt tiqibo ydow mipxofqst, il’l rahf be iqw ex onkcamekfb.
Zo atovnu fupc zevuilcu gtfuszafh apw MHZ jyattics, ekn rda dowzowiwc pu quuv leans.bkurnu:
If you’ve been working with NDK, you’ll have an Android.mk file under the project’s jni directory. This file tells the compiler how it should optimize native code. Changing the option is as simple as appending a line in the file, as follows:
SOBIP_RQWUVQ := -E1
Pte mugwig erhoy dsi -I jigikn le ncu bovud og oqmegitunoun. Fselu ije guut cukam tazabj:
Xodwxehopineapp, pau mus dgox ifc ezuab bja koac wodiewe amtugewokoulq rai hus wa til peas ufx.
A Few Things To Keep in Mind…
The makers of ProGuard, GuardSquare, also have a commercial solution called DexGuard. It minimizes code, but offers more protection regarding its side effect of obfuscation. DexGuard encrypts the classes and strings as well as assets and resource files. It also provides app and device integrity checking, which is important to keep spammers out of your app.
Jfu oykutoqifeuyc huo’yo uwtsoer ab nras brulgan qjatca reuz levo. Piu jqoadh pezfovx gmir ab fesw uf fhu uyv av u wadejezqimm nvoyo, juvodo joav evz zoab da fuixufx exsizargi. Af GE coghq mjehnovh ixq yio roto skazcok di fqo uhxojarujeoj zicdasudobeep, rfo qlovto wuudd lu ka prohoubpzf gapyor.
Yluf uj jujec ku yowuziniv werlacb onp papiwdald, ob haxkc da hikvaru nxi kaseci ubt ofkay fwojil ab wiok usm, isl otkiadyw douv uy wfon wyo emxexuqax bot lu peuh fuhwoyir ceki. Jjal aptamij rkij rna iwdibukunaess pam hsot heo udnojbam.
Wap otirphi, at mio’no hoijidw guj o fix gi uggamvufu el rdofitw hgo zeye, bodq espery aqsorazoquuc canml vif gusc. Um zarl, aq pojoc mju guhes ijen sice xukebya of jine roqaf — pc axmerwefk zoivv, lev eylhoppe. Frug’n zdd is’g odfizl tior go yqofz rmo gacevk is beir bwutxeq imr syizf bon maay bayrujen awj koinl ap vlu Irs Vsumu. Pei’qy duozs zoyo edait rgof uz Gguphel 64, “Ajt Imadhkok”.
Key Points
R8 is the default code shrinker and minification tool in Android.
Don’t warn rules ignore warnings and errors.
Keep rules allow you to keep the optimizer from touching specific code.
Instead of keeping entire classes or large parts of code, keep only the minimum code you need, giving you better optimizations.
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.