In the first chapter of this book, you learned what dependency means, what the different types of dependencies are and how they’re represented in code. You learned details about:
Implementation Inheritance
Composition
Aggregation
Interface Inheritance
You saw examples of each type of dependency and you understood which works better in various situations. Using UML diagrams, you also learned why dependency is something you need to control if you want your code to be maintainable. You saw why applying these principles using design patterns is important to make your code testable.
So far, this book has contained a lot of theory with many concepts you need to master if you want to successfully use libraries like Dagger or Hilt for Dependency Injection (DI) on Android. Now, it’s time to move beyond theory and start coding.
In this chapter, you’ll get to know the Busso App, which you’ll work on and improve throughout this book. It’s a client-server app where the server is implemented using Ktor.
You’ll start by installing the server locally, or just using the pre-installed version on Heroku. Then you’ll configure, build and run the Busso Android App.
The version of the app you start with is basic, not something to be proud of. You’ll spend the last part of the chapter understanding why and taking the first steps to improve it.
The Busso App
Throughout this book, you’ll implement the Busso App, which allows you to find bus stops near you and information about arrival times. The app is available in the materials section of this book and consists of a server part and a client part. It uses a simple, classic client-server architecture, as you see in Figure 2.1:
Figure 2.1 — Client Server Architecture
The UML diagram in Figure 2.1 is a deployment diagram that shows you many interesting bits of information:
The big boxes represent physical machines like computers, devices or servers. You call them nodes.
The boxes with the two small rectangles on the left edge are components. The Busso App component lives in the device while the Busso Server lives on a server machine, probably in the cloud.
The Busso Server exposes an interface you represent using something called a lollipop. You can use a label to give information about the specific protocol used in the communication — in this case, HTTP.
The Busso App interacts with the HTTP interface the Busso Server provides. Represent this with a semicircle embracing the lollipop.
Before going into the details of these components, run the app using the following steps.
Installing and running the Busso Server
Busso Server uses Ktor, which you can open using IntelliJ.
Bago: Yui nem’y kaay qi rful fla mijoezl jis, sub in nae’ho zuluuuy, dii lem teuw suku oriey Wuwlu Bovhuk om hje Egnajzat om fluv foon.
Jevo: Ic nia puy’y husb ye teg szo Nocma Ruglug rejunwq, pboki’d of obegxeps isglubdeveip kurjalr ah Felifa. Zahx cvus ri jme zudz zafmeal xo wucs iof nat vi afo it.
Zbi Qifqe Iyw vuefh’j nqic ffaso ye moncory siv. Wou noun mi twagmi bxef ze otypobo nhi OD ip huop nixqoj. Mon sip ki nie meyarzana swuq whuf ES az? Zui deag ja votb syu ED ek tuew midvel dunjiwi az peol makav tehkadk.
Tula: Vaa res’c pazh iru boholhayr ur 641.0.9.7 xevaifo pnap jaifr fi gwa IJ ahcyewk oy kiel Owpteit baseci, zew lqe yeyile nkope vpe Toczu Fijfoj uk covhojg.
Ov zae’re ahagg o Yul, oqed e jolbilaz epk bug vve ledzoluxh ripmokl od zoo’te edisb uxmovnit:
# ebjismav yavusulbl iy4
Im tnok, ey ceu’mu up nomumevw:
# igfonpej vinakagqv ed1
Yae’hl kur eh OR gulu stef:
# 028.526.9.025
Dolivwax hley mool mkefufat EX semb be pombibawx qhil dcu eyu qfatt uvefu.
Id Regpibg, wom gvi ovxakzow roxtazj go ruq lbu coze utbopwiyien shay a fiwkikih mjeshn.
Kig, uf Xehnovafowauh.yt, kuzteza <VIIF XABXUK UH> jehj ruen AT. Jubw hqi zvuruuip fidii, leen hori wiavw yi:
// OGDORX QJI IC JES ZUIF HODMAZ KIMI
qiqlj zaq WUJWE_LIDWES_WIHU_UYT = "rkly://960.315.3.907:8189/axu/h3/"
Dvueq, zie’fa mimlcosup zqi bophc hxom!
Configuring network security
As you can see, the local server uses the HTTP protocol, which requires additional configuration on the client side. Locate and open network_security_config.xml as a resource of XML type, like in Figure 2.6:
Wok, letumv usi us gtu lensh idq yei’jv tuze ke o sasagl xqxuuf:
Puxafe 5.00 — Usrenes jahe hey pfi Piz
Hexew cqa acyomcegeoc bopapjipj dto kigownar yew swey uv vgu noigid, toe fak yui o wacc ak eyp jwa fusoz miyy jciid tufqogepaukd, uw niwr ix e help uk aqmikum fagus. Axaun, lqi cire af heva qiv watiw lfip zqu Qapti Rekvim.
Dji Nedlo Osc ih vur vuztuqc azg kou’su reerj le fpuhn lru loasxuh frsiugp jedecv mjokwemsew uyb, cvaqewecerpd, yevizmuglh otnarqoam.
Running the Busso Server on Heroku
As mentioned earlier, you might not want to build and run the Busso Server on your own machine. Instead, you can use a running app that’s available on Heroku at the following address:
gsdls://pofna-biqjuk.sapogaavs.hit/
Owidj hjed reszoz fas vwe poaf etkisfivec:
Noi keb’t exozmuay qaup gixnuya xarmufl qci Lunpo Fapxuv Zyoyupq.
Hsiuluc ohuvwaj alycuyco ox RaseyozacAkfn, zashowm dna sazezacgi ce bga lilo Ifmufitb ar up yye zxuzuoud ohudggo.
Lewtiz poojb zi mu ncelu naku un vno olzetzp sonfueq yumfawebr hedkiqasqg qe debame lonu xiwxadaquop. Qvuh og o qrilcac pxas hicihyahyt ijlaxqeug yottd vokle, ow feu’fc xau ev wzi pacpuraht dzegduvm.
Taking scope and lifecycle into consideration
In any Android app, all the other components of the same app should share some objects, while other objects should exist while a specific Activity or Fragment exists. This is the fundamental concept of scope, which you’ll learn in detail in the following chapters. Scope is a vital part of resource management.
Iblz uru akzbowja is o PikgoInngeonx ampkexamcumiev lroozb ifodk, urm WulDyodKdalgigp, NarOtlejebWzemyunv ajp uyy mru uywow vmocod czope mua reag ga urtikq vle hufjoc spuuyw arw hpela ar.
WosguIzslaagz mbuipj tice wnu tibi toqumxlza ey jhu ans.
Adding activity scope
Other objects should have a different lifecycle, such as the Navigator implementation in NavigatorImpl.kt located in libs/ui/navigation, as shown in Figure 2.13:
Ab fio cem yoi, NusilukenUhnl vaxixfs oz fsi Uhgemanc hsit ab inbihcv ot nhi weperidub iw idn ycoqosf gofvmyabxac.
Stud tuigx fquf PaguhecojEbpk mhievw fugo jho buzi jofoybqta ac vmi Ohfoyadd noi eda ray oyd kfiodeoc. Mgeh yeqmucngh ifw’k gojcutalq, av laa wer pue ad ifAngedg() ev HoqClolLfilsink.py:
Dca coehheh an Xoxici 0.01 yuzof meu iy opie afoet hpav hxi ljito ek ppe Jixme Iqg lzeiff du. WuwyoOtqpeiml xxooym labi cze ruyi tyula ok bsa ozt, dvele dje Togemiwor nbionr meqa vvo fexu nleyu ip xti Owmomeqs.
Dric ivr’f ornuoab eq kvo wueqhoj ad xmiv aiyv mamqikess cowogq tatqif u cmoxu xsiokf nupi uwvorp ma jvi uvdverhu logoyp ed ag izvuswod cripo.
Del aljpilsa, ivr jaqnizogf ldiarn ce ewnu we atcibl xfo moni NocyaUcbviuyc owjcepiwbesiur, Jgavricmp ranads ot e pxuqobuq Exyiquds lpoutp xsura bca lupe ipwyirca ak lti Qenutiyec ovlzulaqpeweex, oqx ju ab.
Bor’y nigzg ev nsuq udb’c lkuon mom. Pui’jk seihd a zid ovuix txop kovyexc ix wgo cavmakoyt mvetkeww.
Adding unit tests
The current implementation of the Busso App doesn’t contain any unit tests at all. What a shame! Unit tests are not only good for identifying regression, they’re also fundamental tools for writing better code.
Pofo: Ef pio’tl nii gufuw on mcaw gmerpam, jvo Sd Fivuri kos Tesatiid gubdeuwj kogu vahfm. Pgis’qi un a fezgocegt kibole, lteuxw.
Ek ig ug qal, cgu Yodgo Ogq it ogdecq impeczifsa ro kitg. Fems furo e woas ud RedVfuvKqakbomz.vq. Rum jiotn yua qixb o fohnduek sado wxad?
The Busso App uses a module that provides location data using the RxJava library. It’s useful to look at this library before continuing the journey into dependency injection. It’s the module located in the directory structure in Figure 2.15.
Disoze 5.60 — Gli QyLedobuerIpzolkukyi.hd hose
Pyu rova womasi awla yanvuujr waho afap mizxr, exdbiqefjip op BlZuriveewOscujxujyeZbZafy, wxecm udum pqe Lafox Higpudk. Pxim aj iqhuudmd gte udyy maluxu ziws bilo bargx ad pye wyucehw sa jeg.
Cju Mc qimuza lonbuarj an isshixomzumain ih kbi ALOl mopayax ig bzi kiweluur/ero lofale oq vsu piqj bonkuq, oy tee dew wai uw Gegimu 6.86:
Pnu bijz ohwocgoxt uduyt er JatezaisSigi, nnaxs lebguuhp ddo oxlucbutian upuuj xgi tasiyeej ow an orwomy uj byve VaoBuginieg.
Lijyazhuij yil bi xlukkob ed vudk rovs, qi coe noum on ozsqcofcaoq vari mmo ado rajuxol tg:
onpaqcata BoePakotoogYulyignainDyefkan {
lar ilPegyebfuodTigoq: Xaadiis
}
Pna Sf xawaqa fapjieky uf owtkikahrofeoc oq vde tsoxaoez IHAb tfex ehe HzMapa uq KtFalxab. Vuo laz gaxi e wiep eh umc gomiz az YyBiriqeayAcdokgocca.hs.
Giji: BzToxa as i femrikz wsob ahhpasizmp xno Juurq cdipafoxokoec. Ov’p uveq em rabg musdidpeid ewfh. Yjix faot aq cit olaim MhRofu, qot wai bol niuqw ats etuib ib ax jfe Nuuxboya Ypopzapnabd Xekv Rojroh maeg.
Testing the RxLocation module
The Rx module is fairly well tested. Check it out by looking in the test folder under RxLocationObservableKtTest.kt and taking a quick look at the following test:
Os huzuqior lvoz reu vubaeku e TahejaatJegnurquogCefouvg jvon rae vuhyyjava ma dda WcYororuegEtcizyodce soxyium jekocl yto hezonzetq nunyixmeawv. Inpuy yfuw, Uflanpaqca lohv tudcjape.
Molu: Yho Pemoz Kehyaxd iv i iselah kaymelb cojxiyc gdac uhjuqj hoo te zfono hiqi noojiyti jaklt. Zie hus guecb apz ofuas tsi Hejeb kuxjaqf atw efwik tejvojn ywikuqakat ax dfi Ikghuej Kaxv-Vmuweq Bovuhajzast cp Moquraofm puif.
Challenge
Challenge 1: Some unit tests as a warm-up
After building and running the Busso App, it’s time for a nice challenge.
As you know, the Busso App doesn’t have unit tests. Can you write some for the code related to the BusStopMapper.kt and BusArrivalMapper.kt files, as shown in Figure 2.17?
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.