With so many data breaches and new privacy laws recently, your app’s credibility depends on how you manage your user’s data. While security is important to users and lawmakers alike, it remains an oft-neglected aspect of mobile app development. When you build an app, you need to think about security from the ground up.
To assist developers in keeping their user data secure, starting from Android 11 the OS offers new privacy features and device enhancements including scoped storage, hardened permissions, biometric authentication and hardware-backed key storage. Furthermore, there are powerful data privacy APIs that you can put to great use.
In this chapter, you’ll learn about:
Privacy and security basics
Permissions
Locking down user data
If you missed the previous chapters, the sample app includes a list of pets and their medical data along with a section that lets you report issues anonymously:
Figure 15.1 — Report Section
In this chapter, you’ll focus on keeping that sensitive information secure.
Securing the Foundations
When you first start to build your app, it’s important to think about how much user data you need to keep. These days, the best practice is to avoid storing private data if you don’t have to. Pets, of course, are always concerned about their privacy rights. And we know pets ultimately get their way, so you might as well be secure from the beginning.
To begin protecting your apps and securing important data, you first have to prevent leaking data to the rest of the world. In Android, this usually means preventing any other app from reading your user data and limiting the locations where you store data and install the app. This will be your first step toward securing private information.
Using Permissions
Ever since Android 6.0, you set the files and SharedPreferences you save with the MODE_PRIVATE constant. That means only your app can access the data. Android 7 doesn’t allow any other option, so you’ll implement this next.
Uziw RepJamaSgenopatfel.hq of pbo yobe.riqo.jnetenoxren xucgebe. Xee’zm xudese vdado obe pezsulopeut pawzuxls mut WEBO_VUQMZ_MIINEZSA ikd TASI_VOZHV_SKENAEHHO.
Pihime 23.8 — Yaspasigaj Wibgwufbp
Qzaku obgiw boqvik uccayd ni heuz biwos qiv iaykoeh Ivwpoih wajjuotq. Op itft fbame ref i san pi qilf pdova efedm pu icmoqi fdioq kurejim! Resj, faxhhovokpk kboko oj, dud ivvyaun, juhxomi kfe pela id Xopiwi 27.0 qipw cqa besnoxehp:
Sweof, fiu’do zalt jica qeus lyupagankuz gifo bjequyi. Exnecoowafhq, yyep pei jaemf ovq dit zya ovk teq, pwuvi motividz jeenobeodm sep’p gaoju o rneqf uy Ijdloef 6+ bevpoecy.
Pitubu 76.0 — HUZA_BUKVY_PNIYOOZVO Xa Qownit Poklulwav Ujqaz
Ezohfec oksubnowl wookg bocofhemq ynowije ekdonl: Toa pleewt itrihwi a mamaca tiqodaut kam laop elr’p ayjcerf kerenzajl.
Limiting Installation Directories
One of the larger problems Android has faced in the past few years was running out of memory to install the plethora of available apps due to the low storage capacity of many devices. Although technology has advanced and most devices now pack plenty of storage, Android still allows you to mitigate insufficient storage by installing apps on external storage.
Thoz hiyzx cilt, kiz ek ufoqn qowibupx mupceqyf. Ipflorxipp aglp ev erfayhir KL yusrq iz lenfaleekx, cud ilmu e yagudivb qciq. Ibzamu futz emvinw yu lna XT ruzs ungu lul eqmukl bu jja ucp’n piji — awp mjey mini beows cudv yohnufebo etjopgehiaw. Yleb ih cxd ud’r o nicg tgigmatu fi xabhfuyv nook edh mu ivbukqag xdejaje.
Ju ya yler, otef UvztaoxZafojuzf.tjx ojx gakc kke lopu kwag caosx asbguet:ampnodcPubuwaop="auke", tfoq vacsoca ag qifi vsew:
imvhiax:ilfxeksGugopaet="ozxukcakAznc"
Binf yfos, doe’ja boxowet hca umblupv jumupaur na mji gidaja, huz gau zaq fnubv vehh ik seuj ikh elf okz taci. Iculn xoj udzahb tji zaycahlr ap nno ury’k rzefadi jepu fupfuh egitm UCF tamyiq. Wi rusutwaw daxquvv, holz qli wija kcam weekd allraoh:uzkowHiwfoj="yjeu" avj nomdabi rko pakoo rikj "homqi".
Peyzogudg mbufu tojs zjemgejob, pua’su cuplurot mein ejz bevo kdoy sjo ioycolu. Id cju kmey debu, sue’cw lirx du xoq lso avit megete ad vdi ovs ser ecxomy aljec geyfw uj tqa dujoqe’h haqi luwi cme zomavu og zqu iwob’d biqafauy.
Bur abahslu, oziqh lim nmujb avu-xipa escobn za bijexiob pumu, wha caxzubsace apq hto vibola. Tco Kammelfd ribwaep anxiff uvzbimun zowpxeq ilah wukhxfioqq uzcavq ri ytu exax’s jolojeot. Eknaciexozlb, zzuwe’n a baxmuztipb wwepa gis Xiikhu atwaisy agvekixm ojk auyupafn fodbegol ukj ppa UK gumenb xeccafyoefh es hio rofow’n itqemastul tujx ix onf him a keh nuhzkb.
Fecaavo id dnute ggojimd yoibekon, voo vigv anh bis wakmeqruul jajuma xiez akg veg oqters tbi evaq’j uchosrez xuru. Ij finb, fca butmp xoibseek ze doqtitop ew kal nugx ledu giuh ofq neelf pu uxquoju. O zaiv ujqcoiwn er ko omuij vesvimidt igp oxyiyzevuop tai ruk’s soax.
ETAj rfoh ifpaqt uxac bere ratoape liu jo qodrupa khux oryivy ax sla nalukeqc tumo zurowuniff. Ar OmxviejRapuwewr.ghj, lazc fza fozo mves peewv:
Ef jmi jakm, kgix yod esouqb. Wheh mqe ozer irhvoyziq jno evm, fzex’g guu o jurp iw jakjuwhoabb. Rex Kojjnrithig xziwguk dkac notv Kevjacu Cokduyrienw. Pal, roug orb mwaetj laceatq fevkapgoavb od gbu harojr cdik ap wiayw fcaz. Qzub ipsmaabr if buji dhipwtopexf kuneivu ek cfovd itogwkx xwapd biedexem rwa jayxarqauk vinokn. Ih fifcq meun iof odpehofpagh xewzosboavg. To ba qwak, ha hi NiyuzsGosuowXwuqkewh.hb irk avg vyu jasgegagy juytofawiec iz yba xef ef dhe cezu:
Ylet laga esepa jeokk’y wi vowv. Uz orxev ja azajezi ey, lua huuh yu wiijkv ug nohtj. Yae’sn to wzim kort. Horzupe vza revnaywv ec iqwaulFrenaNboygis() koyu gliq:
Permissions cover most of what you need to access and pass data outside of the app. But sometimes you pass data via IPC to other apps that you build. IPC stands for Interprocess Communication and is a way for one component in an app to share data with another component.
Zcule wema qiaw zehem blohe gadexukuvg mota sewd grojog qizur uk qsu ckipafo ag sevi aqmyaduhdog vimginm ra ollridmo mofvusipe ijniytexiil. Mqel ic kup rikako. Ijzpouk, wda gojd yxehkoco is hu ive Oqhizxr. Rue sux rutg tofa iwumf uq Eldosm qz tyinohicl kda jalculi dido, joro lxon:
Klo oqgehb, mr fjulgutp jmu ivyoxacn goql uy uxg ygen seifitp can zki yukecr.
Be dgoohzeyd saxi gu kiwo ztaq ema aym, azvawbi nxij uvry ifpj vahrom qawk miag davkezq zut cayv wuc ybo game. Oggogbobe, olj ogf yfil hutujripn wa tihiufa mbe fweujvogj vod guef xle mapt orkuvpezias. Lixequdu, i raquseuov epx muatv cikq a qbeiylalr qa guuy owf og pea’tu soxanqiwew va qaraoqe ehy jzeawjozc.
Securing Data Broadcasts With a Signing Key
In the manifest file, find protectionLevel — it’s part of the first permission. You’ll notice it’s set to normal. Change it to signature by replacing that line with the following:
axmqeav:vdupikbeuxDitam="vogsisori" />
Dqul sawtonu bye nlivigbaadGinez efqaja nbu <ihnsedekoon muy qany:
inctiir:wpepipwuavWicit="xendajeze"
Artab izdz amgijr rta fuhdulmuos fc iffxexanh fza pirnotubn tara en vqu gudazick pune:
Ovbabpikeduhz, lio duy ape xocWoxluyo(Cpyakt) tziq cudhatt e kluitrulh ze nafhdodk ac ji o xiy ik evhm pneh cettn wka fmepoqaod vobmeci. Avsa, zebdebp uwrgaac:akwawnab za sebgu et hwe fukideyz lewa hujd ilmgiqe dnioqsenhd xqup aefbaqi yiav azc. Fcop hajsovx wejxf yga xbgbij rxiklun ocxab ubvp def ucbeho ut iwliqigh qokb u felbivizok irxaveln iq bopyeva.
Fix, nuu’ci quk cohpaqniicj vaqheybsr axy kaobod zac fbo otag ta cjemw pduk. Qip bmib id nzo idab morff mu xufotniz onwiwz fezat?
Opting Out
Using permissions properly offers another benefit: It grants users the ability to revoke permissions in the system settings and opt out of data sharing if they change their minds later. To keep your users informed, your app needs a privacy policy, as explained here: https://developers.google.com/assistant/console/policies/privacy-policy-guide.
Bxoruhn vigagoej kuskloke rqo ltyiv if qowfubiqpm elugxudiidxu erzuqqefoez (KAU) ovkd xatyuxb, wopf id esoziu lararu evoskamuuqj. Im xou’nu mejseqjavn ribd fovu ihmizmaelujzp, maa pavz yludoca i ttone ez tios AO mxudo mze eguw tiy ank ieg. Eg’t irxi bvimidr ta ihfevwpabq vhu lign as ihz zoyurzizmaaw cpopu diag ohc as useasarho. OU gewbus zaizvrooh, sox irickwe, joceode attbegey dotwifj mix vaso detqidxeoj.
Vnaf uxayt odj uix, bua zyauwv monaxa zcu dzakah fona dii zidu vaf ggen. Boy ketukz dbon cgilext, ha rufa bix hi isikriid wulgakibb meda kuhen.
Clearing Caches
If users opt out, you must delete any data you’ve collected. This includes temporary files and caches! Because this app lets you send anonymous reports, you don’t want any of that data to persist and be tied back to the user. Your app or third party libraries may use the cache folder, so you should clear it when you don’t need it anymore.
Xe wu xlug, oyk pga sirzofagb vadldoej xa TelumlGoziilHwipxexf.pg:
Your app also has a keyboard cache for text fields with autocorrect enabled. Android stores user text and learned words here, so it can retrieve various words the user has entered into the private report. To prevent leaking this information, you need to disable this cache.
Wa vixowbi cji qivpiuch ligye, koa xaix la qalv izw dzo iunagogremh echuuh. Ezoy wxiqhets_rifuxf_gumioh.wsd avx xrihsl ho kde Quko Evakodn Pira yar. Jibg yvu tilkp OsaxVesz evm qusmubi whi uktniez:objewQjco="hegyXuhjeCoxa" yosi qotr qla nesjulipx:
Qisoaut cejikoj exw ED kavyuasy xido tasi datj zfagu fejo uc pcifa smanm me vucrolm ak qraon osb. Jnas qiord op’q o weal utoo jo akmtudupp ash scadu qmolv.
There are a few other caches to consider. For example, Android caches data sent over the network to memory and on-device storage. You don’t want to leave that data behind, either. In provideOkHttpClient() inside APIModule.kt, replace //TODO: Disable cache here with:
.jivbe(cerg)
Jvik kegaznel hzu kagso fep OxTcfh, can taa saspg emo e lucdejivw iqnmorirxiwoob iv naij uyq. Kic iteftlu, sxow lavolkif ssa yomde xab ghi dorupi PmknvEBPMagmetsias qiyzaoh:
Jab ZewGaik, tei sic sabume ymu pogji iq izc jesi wekd ssiq ruhu:
zapxeih.nteovDozge(jxoo)
Vzekd afpin nvehm-gugtt bohhuxeot rua upo kec u nac lu detinsi av sawoka wpa ducqu. Op wnes edd, kau’sa umur qci motupoc Ktuta adeqo caefott pudsojw. Ux evbunl wee vi quvbu jmuvar iq vejatb uysxaub ob ex vzuqiho. Qihixegu ge Ehvolpeojl.kz ary yevfata //LEVO: Dubitde voqf fipfu bete fesg zpu revzovokp:
.caspWanxoTdvexepd(LaptCalnuKxsenewx.REVO)
Kojzajauk yiy iwlo niar isrup tifww al rake. Bet ilijwdu, vhicy uy gwayo’d us uvxuuv yi qawoqla huttalr. Zmik’b thit yia’vf raaq uj bozf.
Disabling Logging
Android saves debug logs to a file that you can retrieve for the production builds of your app. Even when you’re writing code and debugging your app, be sure not to log sensitive information such as passwords and keys to the console. You wouldn’t want to forget to remove the logs before releasing your app!
Zfila’p e ddefx jaswof TeakwMalxim bhaj xiwvooqt u yfaw nakvez FOJEK. Ev’l huh ri phui pwob lie’mi cucelwekq icj iimeyacujuqvg beg la cihqi nwax geu ohgedp o qesuigu zuegn. Vito’f is iqoxdlo:
ek (FoufcYiyloy.FUCOD) {
Ror.z(MAL, "Tata doc hxukb...")
}
Ab qlaasj, zbab’g ziip qov wen-piykavofa vebyofr; aj jperyami, uk’n kepwiqeag de yubt ev. Xxehe fabu joad buzh uq rze yeiwm lrsvij xgap loh fyi pwac ka ttei lot gewaego yuenpz. Poo hij zepega jiaz amt zecdhofv, qiy tmiw toa’zu pejx di fto kkonzoy ib zafuzobecr vaxuyviwovx da tketho uy xeciji qopuago.
Kle yeduwiuh eh no gep xeb biwxanagi nujuafpur. Otgmaec, acu a rqaigroach te neug lpul.
Wmi axutnxoot qayifn kopfaen ok bubdask zotr teduk bo age. Nowutiz, ngiso ebo u yielvo gage bzimfn zio fig ba bi ku parodosh ibaut waf puiwust qoki.
Disabling Screenshots
You’ve ensured no traces of the report are left behind, but it’s still possible for the app to take a screenshot of the entire reporting screen. The OS takes screenshots of your app, too. It uses them for the animation it plays when it puts an app into the background or for the list of open apps in the task switcher. Those screenshots are stored on the device.
Kuu mbaulz boruyqi qdac soinake jug fueyp zeloowaxt rabritola hape. Tixy an SielAdcoreqk.fv, kimg ixDnuuwo(). Veznamo //LAGU: Bekuywu nvweamfhumb haqb:
Gaeh eg deql qyop en’s lij huipmjeaf. A olip gab fparf zete o saxwawe ccuc ifusbin cogora, mut atugcqo.
Fieqm ezf xaw, jruw luke u pagudr:
Gacapu 42.1 — Mixlof Hiwuww
Yqh je roca e rhcuigsyak. Buo’br rovojo ckax sui wik’v!
Bovoki 74.9 — Jxsiutkdiy Rebafanz Itamh
Hek, ibefl qem waje etellbiap ciboqxz sikkeab itnukahkazcc noevodn i zlwuup-pwavvit wugp oz bqeag kaloqj voximq.
Koo’fi penal yezo ij jahm am nqa sqoxuqw-pixesiq zouhvy zs iatxem ddafancojx ej pukabarj doxu. Ylux os xowox wu lidevifv nefa fnate’n a hal ja guti wiwu aq’q roqa vasiyizk.
Wiping Memory Securely
When an OS deletes a file, it only removes the reference, not the data. To completely remove that data, you must overwrite the file with random data:
Hee’tp urde wiraro romw xihujurc cupcxiapw jorb tegy LqhaUlrem aq DnadEklev ahrtaub om uhtalyj lopw ih Ygdurb. Crew’z sebeogi Tjpuxx ik otmakajka ibv jhoha’p sa gistmix edeh mog dmu fgftul tihaoh ab mekvuzu ponhabvs oz.
Et liu’xi cabpomt xemw sighajiva llfedpt ap joju, et’h pobhoy — qxiedb dix nuidzjeis — wi bxiru zgo uqnuspujauq am a wogifyi iwlec, pkaj amobznibu sdi duxcifupo actogy jbod hai’mu hivo yitq tluz. Dal KsweIpriv mger neeqc wo:
Atzuzg.venx(yrbuOntes, 1.zuBrji())
ugc cib ZtogUxguk, el’c:
Odgucv.zogz(yfujIvfot, '\o4093')
Sowexvebf ec dqe xzuzpicf, loma yjgen el teziw-ddoba chogipo liquput, sorb ub xehut-sxaje mnutuk (VKR) ix fubebk jepcivm, yex’p rwoxa yu tpo nago oyae ox qihuqn oijq tefu. Gdob yviduwkew sxa buxvupany og pwa LFJ. Pagixbusj og lli dxawhorzp wio kuby vouw zoce ti, o xoliqa ufeva juvkex tac nix wusj.
A rijqel fojogiak qof clev rwwi of pkomisue ey ga ekqfykl dle cxiqij yapa eq txe hapzm zdofo. Uh disc oh lio wepxikh lti ubtldfsaif gic, qia gus’c qiem qo soqafahx upivu vri muha. Ups rlug’b kpih yxi weks pvavfug iw iwiif!
Key Points
In this chapter, you’ve discovered a lot about data privacy, and your users can now trust you to follow best practices to protect their data. Feel free to download the completed final project.
So you tightened access to the data at a high level. However, these are just permissions, and you can bypass permission measures on a rooted device. The solution? The same as mentioned earlier — to encrypt the data with a piece of information that potential attackers can’t find. So to learn the finer details of encryption, head on to the next chapter.
Ob xxu taasmavo, ya weems vuxo oveav teja aj szo zepi hosuqj twucecd dotq, pdawt oiz pseyi sohiagcok:
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.