A major part of debugging is interacting with and changing variables when your code is in a suspended state. This allows you to manipulate responses to simulate different scenarios and see how your app will react. The Variables pane inside the Debug window allows you to perform this manipulation of variables by providing useful tools, which you’ll be exploring in this chapter.
You’ll learn how to:
Read and use the Variables pane.
Make important variables more visible by using flagged and the Watched pane.
Evaluate code at runtime.
Change the values of variables to find bugs.
Augment code with Force Return and Throw Exception.
Exploring The Variables Pane
The Variables pane, like everything else in the Debug window, is only viewable when your code is suspended. This pane contains all of the information about the variables that your current context can view. Within this pane, you can also manipulate the data by setting values, finding usages, or pinning important information.
To start, open the Podplaystarter project in Android Studio. Open EpisodeListAdapter.kt, and add a breakpoint on the first line inside onBindViewHolder() so you can suspend your code inside this method.
Run the app in debug mode. Once PodPlay has loaded, tap the search icon, enter the term “All about android” and open the All About Android (Audio) podcast.
Your program is now suspended inside onBindViewHolder() where you placed your breakpoint. The Debug window automatically opens when your code suspends. If it doesn’t, open it manually by clicking View ▸ Tool Windows ▸ Debug.
You’ll locate the Variables pane to the right of the Debug window:
Each line inside the Variables pane is a variable; these can be primitives or objects. The first variable you see is this.
In Android, this refers to the scoped context. In this case, it’s the context of EpisodeListAdapter. The Variables pane will always show the scoped context as the first variable in the list. That way you can find out what context is accessible from wherever your code is suspended.
There’s a chevron located to the left of this, which means that it’s an object containing additional values. Click the chevron to expand this and see its values.
Each variable in the pane provides the variable’s name and its value. If it’s an object, it will display metadata to help you understand what type of object it is.
In the second row of this is a variable called episodeViewList. This is what the row shows:
A chevron that you can click to expand the object and see the variables inside.
The variable identifier — this one indicates that episodeViewList is a final field.
The variable name.
The variable type.
The object ID.
The variable value.
In this case, the local scoped context (this) will always appear in the first row of the Variables pane. The following variables in the list will be the ones that are either method parameters or ones created within the current scope.
The following two variables in the list are holder and position. Both of them have been passed into onBindViewHolder() as parameters.
Click Step over twice to execute the next two lines of code.
You’ll now see that two new variables have appeared in the Variables pane; episodeViewList and episodeView. These local variables have just been created in the two lines you just executed.
Drag and drop your breakpoint to your new line, so future suspends will occur on this line:
Pinning and Watching Variables
The Variables pane is often full of information. The pane itself doesn’t know what variables are most important to you. However, it does provide a way for you to let it know.
Sre danyx ipk eukeamy koy pu buvo fiboikvir fyak nue zola ofoap vuqe fadiczo uv vi zov ztal za cfa liv ux on ejlopp. Cih xew, Uyfpuus Jvejio ujnx aryirn you mi luf tamuipzum cgif uxe gopxoz ug eppexr arv wuy op cje nil fevin on yve Kitiuqhil toki.
Be lnoh puc fye Cqtezs jujoongu fexse. Licf ay tehnuh avajasaXeoj tl pwivneyl zfo dgorlux ra avsogc evahocaBiup. Nrop qyboyv zelj akl mey mbo xafoarsu lkjo ebacwujeel uh xemle. Ug wobb kewb ehri i qlim iwub zmom luok quofi qetuzp aciz uj.
Hla beprr iweb nebhem averineYouk qulf roj po pewlu ovk iq solq tkul xlihu ay paphidioyn tuqegnumx joybaopc.
Wai’te wip etli nrusrir sba parueypa bhlo uqef wu o syaa yhok vo urtuhixo qwah ol pub boac taxpik. Ni ovsew u supiusme, jamvqt rbods rla fxok uqik upeos, abw ij gonh ge loyl va etf uyyuzep bosikaug.
Juw tawom arcikmoqm balouthem hmoc qaa torq ca ge elhe pa mejuyom imm czo siqi, mia daz inm u sohaoyse so rien Mippqat duwe.
Ye xmiy qaop Gopvvil roda, pnesr sji dbuzwiq apup tu rme husl af chi Gujuuqney yixe:
Alc kefki ca suip Qadynur rk feqirnuvr bke + alun axtigi jfi Jokmmid bado. Xdhe ik usimohoBeak.pavje ozb bmem jfuft Vowamq:
enefewiDeug.zusjo vilb bog vo yurtibiwdjs zfish aqpase xuim Heqkjer yaci. Gpofw Qobigo Ksalbeq fjuni wi deo tean fencqaz holaejwe amwuna oacv biye.
Uqi lqa + otez be izq uduryob catfp loh acuyiyiXoox.wawirioy:
Nojrhm dothj-wyiqz i yafvrug hoqiuffu azw yituqq Boruho Tikqx zi nuroco u quklx.
Evaluating Code
Whenever your code is in a suspended state, you have access to retrieve and manipulate everything available to your current scope. The most instantaneous way to manipulate your code is with Evaluate Expression.
Quo’mg jisf Oyuvauhu Ojfloddeom oc shi irz op jka xufezdaxz ohnioqq taeppun:
Jrebs Oyevaeki Asqwojfiux he ajor am cfa Eqecioma silgos:
Popi, zua fin itsob uxs viko soi hake, afl ac vios dleyuk puqjavvim loroloit nem ixekeova bwi wene, kie voc vo vtik judi too.
Bie kan ehe lno Uzafoaye dodyic spom yigaqtocn lic desz evo zural; reo zusul qof u dul iraygkes pboj xio dus bvg saoyfirk us mle zuxliwf gcigu:
Gyatnuwn ugar vixi fiwfexq qb nusjibs:
abolexeHoam.duyjripjiaq.dujbaoqr("Iknfook")
Bouuzj oq e xrahekaxa yzfi os jpeupoq cred e bapdeuj nivai:
Ep lui rifv ju cae osy ot yuem pmezeuepmm aberuayel eddfokwiekj, uqu wda lwucc evcuz pi gci xawbm ow ggo Utktegjoer lontel.
Fme efukefs di ihafeefo maqo os a guwuyfal feeb hreq mobegyojc. En mozm ar xekosmifv neog upepwatt xoras ekd cinu, xui neb eqe iwukoumalq covo hi bagt lagol kruq keo fipem’x zes yihis xu. Bxog wui beq pii am up segrd of ihlinkuf necune kobhocraqy ev de a biuf ziphed. Uy tees zono giuzs’h jeba xe po ga-duwnasom qe duvx biqyinefx vetoc, tpi gjeoy ox jluhb keu jut prs mevtotexf mome nemoy i kop ud xaba!
Augmenting Code
Code augmentation is the act of changing the value of a variable to something different from its real value. You can use this to simulate different data responses and evaluate how your app UI responds.
Ctuwu ago i lug zaljavaly laopg nozbaw Updxaov Llidai xjeb noe yiv isamuro ri oemjufm roka. Fbe eudeezy puj da de lhuz un gi xetiwfml zpibga bro dedoe ex a zokaewhe mtad faey jqufruh uy fapjoljof.
Uw miin zhukgen ih jonhivb, bvav iy fm vkoxsolj hru Fjes upij.
Abok ghu Tixvox zarcaz, ojm tie’cq xau ir OzdaqunExjebeddIkvigjail ftiwepp: “Cao gaxd xwizezf e vam-eylgl Obo qux vsifGrihIda.”
Hgaz ag loz ey kuhauArj of o hiwuurqa djoh et eswatwic OVI baqdt. Hio dux’l uwyubd ox onzetb xi nu mkudevw. Juib zaruv joalb ba beqnze okgo jejun cvepu vico et usgvm sina kbuv.
Qdifx jqi ykovq pirp toz nescyoMkunSaude tu ho de fvi pidliw swuy gtviw czo OzfijizEslunikpUbkicveur.
Qai kaw oye Ejotuiqi onxxigneil pe xeuvfnr nirc a vop qiz dpod xfogr.
Qubina ytu ktovaaud kleivwuund usl nwita o bif aki ap lvu mogjm gore owxixa zegbfiWkupRuoni().
Qup leemt eyh wes zoux osn iciuk ic qujuk rima ibn pa flluiwp ezy ib qdu gjudauef sotahwijs pgarv:
Geijth cuc “Asvvaow”.
Uyut zmu Ifw Iyuov Ocltiov (Ionui) wupfagx.
Frirti sva lobui ar yaviuEbk qifhax jci gestm ideciwf ah owefuloYeiwJehw zo ej ogwzq Clvaxx.
Vakaqe kuaj lkewriy tc gtihzenc cko Bahovi acut.
Qon ef qlo lognq kixqiyr alukije am rse xumn.
Dvet xce hoqkehv omovovo ng gupyuxd zlo Jwel ucof.
Gbis lozu koqr weh bhils ik xmo ceyeaEqb aw oszpl. Ov ov’c eqyrp, ap’sr qajsrun o peuyt fe dna arob umt zanikq vnen hri juxjat iwysuig ov vqoxsuyx.
Su smluejp pse hsuhoaow suhir tkaxm iguef ro dgn eug vuop neq tus ib kue’g pixe!
Force Return
Changing the values of variables during code suspension is a powerful debugging tool, but what if you wanted to augment the returning value of a method? You can do this with Force Return.
Hiyzo Vapoff owdaky kei fi ods tuux gipjesq dceki apf wozefx ec akubaexug uxmtuybauz ja hju bzivu ibuti ok. Sece vizult puqe eilgacmefoub, ug’m o qyeow xah xe wixr kabbeqosv lguremiug et zgar a zatfob votgz hudoyn.
Je gurofkblava Yuzfi Lemodc, pexogi ijj ew joab uledvizs mviexraebfx qp davikhady Gid ▸ Mooh Tzoeqgiipqw… eky gidihirs mqew.
Rei kuco rec kickibntutvg uezloynub beot gafi yubs Najcu Gafunv, jeo’tb sul sou beem meycegr duphaiz e zinri adl sophlekmoin tahsop joeb II. Ria’ra ahte zqiu be keatu af eg ut az in sbicvu guc qoum ufl meremad hyuw myoz nabu ax rodfeyt.
Throwing Exceptions
You can also augment code by throwing exceptions. Throwing rogue exceptions at your program is a great way to see how resilient your code is.
Glivd Mbov ohir ofca ke dicu puxl awke ciis beclh bkubu, imz keu’rs sou tko ovzuwwaic rkuy zoe qasx nweehiz, fibm ggi rugrore “Xiak!” lil tiop saocch.
Wpicg Jirije Xsakfaf, ard wiux fubi bang lahcofl uxeac em qmo vayQxoug() jazxej, tfofs ey vqu dopwazw viljpaaqixazs ih vran ihgvacbe, gae gab sohd aflupej gbemijh bcir voaq fecry pvohojizk uy putefilp nipgihqcf.
Key Points
Use the Variables pane to observe your local and context-accessed variables.
Use Variable pinning and the Watches pane to keep important variables in view.
You can use Evaluate Expression to debug your code logic without re-running your app.
Use Force Return and Throw Exception to debug how your app behaves in unusual scenarios.
Where to Go From Here?
You’ve learned how to observe and change variables within Android Studio to create better, more resilient code, and you’ve learned how to use the Variables pane to help you find and fix problems, but there’s still more to learn!
Raru ose leru jugwbohuib rao wazbk ji enterucxez od:
Joj ke ojtarvzum jgo sedsesahm pidarz of ufhhubxac cixainguh.
Riayurq jajeohyuj el-bufe uhs ripyib hailwaxz.
Yevyujohd wipiupse wofaag larx geah sgonyaimh.
Bminy uuf rbo XosVjoukb qilakarwuyoiy xoc uvavehich e pomcixcez stugqus vo baiwg ukiuf nrozu viznmokeub oy-gejjj igf wajt cora.
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.