GradleSummit2015 Gradle for Android

this is a talk on primarily about the Gradle build the build system as applied to android i’ve been using Gradle for years I’ve spent a lot of time with it on my groovy related projects and of course now that includes Grails related projects as well I hope that all right well alright everyone sorry about that any rate one of the big changes to Android is that they moved away from what you used to call an IDE build and moved into an actual build system now from the beginning android has supported aunt builds which gives you an idea how old that is in fact if you’ve done a lot of Android development it feels very much like when it first came out like we’d gone back in time back to the late 90s or something the metaphors the idioms everything the way it was done all felt like it was back from then and they’ve been gradually bringing things up to date and one of the big changes what in at Google i/o and 2013 was the announcement of the move to the Gradle build system for the build itself so there were efforts along the way to do something that was you know that was maven related and eventually but nothing was ever officially supported on that even though there were various plugins the first official move to a separate build tool was the one for Gradle and that’s we have now and that has been evolving over the past couple of years rather intensively at the same time that was announced they also announced the move to the Android studio ide prior to that Android development had all been pretty much done under an eclipse variation that had a plug-in called adt the android developer tools and i have to admit that frankly i think it was a better eclipse than eclipse in many ways as Eclipse has gotten larger and more awkward unstable buggy or whatever now I’m saying that and I’m being a little bit harsh eclipse is still widely used throughout the Java industry and it’s very good but as I say I spent a lot of time in the groovy and grails ecosystems and the Eclipse support in there has gotten progressively worse over the past couple years for a variety of understandable reasons but nevertheless that’s colored my perceptions of it whereas the IntelliJ people the JetBrains people working on IntelliJ have done a wonderful job with the groovy and grails support and that includes Gradle so when when they finally move the IDE from eclipse ADT into Android studio which is a free scaled-down version of IntelliJ it turns out to have been a very very good thing I I didn’t know at the time how good it was going to be although most of my friends who used IntelliJ kept saying no no you’re going to be very happy pardon me I don one but I’d been an eclipse user for like 13 years or something you know so when it failed i generally knew where and there’s no denying there’s a pretty substantial learning curve in moving into IntelliJ but whence that happened that was the final trigger for me so I’ve been an IntelliJ pretty much pretty steadily for a little maybe a year and a half something like that so I still get into not swear I don’t know how to fix something but at least I’m productive using it and I agree i’m very happy i never want to go back from that i’m going to show you both of those things now last year i made a very similar presentation and covered a lot of the same topics i’m going to try to update that and talk about more recent developments here but for the people who were not did not see last year’s talk or we’re not interested in last year’s talk and who happened to be interested now i do want to start with the basics but i’m not going to spend too much time on the fundamentals of Android itself I’ll show you the basics in there but then I want to move on to the Gradle build system and that’s where I want to focus primarily the big points I want to hit before the end just so I have them in my own head and hopefully I won’t forget them as well for you is that I want to show you how the basic build structure looks I want to show you what needs to be done in order to make a release build and how the Gradle plug-in handles that i want to show you testing both unit testing and what we would pretty much typically call integration testing with two different mechanisms one is the classic robot IAM library which is still very nice but not again officially supported it’s a separate plug-in and then i have updated my test to use something that is from google and will be pretty much the de

facto in the future is the espresso library although I have to say I’m not real good at it but I can show you the basics on that one how that works and then I also want to show you an example with product flavors in it which is a really neat feature I have some things about build types but honestly build types don’t feel all that different when you use them I mean it’s basically the same app just whether it’s being a debug version or release version so that’s where i’ll talk about the signing key and everything but the testing is really cool ok so just i have some slides the slides that were uploaded the website or the old ones unfortunately i’m going to upload this as soon as we’re done this talk my own marketing i have to do my name’s Ken cousin its cousin like the relative just doesn’t look like it we think it’s an Ellis Island job or something you know nobody knows what it was in the old country any rate there’s my email address my company’s cousin I t-dot-com our cousin ITA wife says cousin it like The Addams Family you know was her idea there’s my twitter handle i spent a lot of time in the groovy world writing this book called making java groovy as if nothing else let me give you some benefit for coming to this talk for any book at manning if you get it at manning calm use the coupon code my last name kou scn and the number 37 after it and that will give you a thirty-seven percent discount on anything at manning as long as you buy my book at no I can’t no that’s not true actually you don’t have to buy mine but it anything else meeps any of them the manager and pardon me the early access program all of them pardon me all those will be a thirty-seven percent discount you can try cousin 85 and see if that will work I don’t but if you really think it through you try cousin 105 and see if they pay you to take it away right yeah yeah also i’m working on a book called gradle for android for o’reilly that will be about this the Gradle plug-in for android that book I’ve had to rip apart and restart half a dozen times because Android keeps changing and android studio keeps changing and Gradle plug-in keeps changing and Gradle keeps changing so at some point I’m just going to go all the heck with it and just write it and it is what it is in the meantime I’ve recorded some videos for O’Reilly video courses these three are now available as of actually today groovy programming fundamentals practical groovy and mastering groovy if you have a safari account they are available through safari if not now then soon they’ll all be out and this week I actually recorded a couple of video courses on Android development itself expect those within the next month or so see how that goes turns out that when you record six to ten hours of video to my surprise it you know in order to review all that takes at least six to ten hours really annoying I I don’t know why that didn’t occur to me but nevertheless okay so any rate all the stuff you want to know about Android comes from the home page developer android com and i believe i have that page open already Wow think I have enough windows open this is the let me actually go to developer.android.com of course they’re talking about the M Developer Preview and if you open up the SDK manager it actually is labeled MNC leading most people to speculate that MN c stands for macadamia nut cookie and i was just talking to zob about that Xavi a Duke Rohit I know I’m not pronouncing that well but I’m trying I mean he’s the head of the android plug-in project he’s talked here already and he’s also the head of the android studio project he informs me that the the M preview the preview versions also have names so that while they’re referring to it as macadamia nut cookie in the M preview when they actually release em in the fall it probably will be called something else I don’t know melon mmm I you know I’m looking for funny alternatives to that you know I thought macadamia nut cookie was the funny one any rate this is the developer android com site and if you go to the develop link then of course the first thing they bring up is Android studio and that is the definitive tool now for building Android applications so there’s a lot of information here there’s even something about the Gradle build tools as well the best thing about this site by the way by far is this little magnifying glass up here because by golly if there’s one thing Google knows how to do its search okay so if I was to say you know studio or actually why don’t I say Gradle you see they already talk right here about the android plug-in for Gradle and have some discussions about the entries and signing and proguard and testing and on and on and on the only problem with this

site is that it is not updated nearly as often as the plug-in is updated there is also a site if you google android new build system okay this is the site that has the latest information in it although again if you were to go into the plug-in user guide that’s seriously out of date course that’s why i have a contract to write a book probably but there is a link here called recent changes and that one will tell you the latest and greatest of what’s going on with the tool and the plug-in and everything else but so I very much recommend those sites in order to keep track of things if you don’t watch those sites then believe it or not you’ll actually have to go to and I know this is hard to imagine google+ you’ll actually have a reason to go to Google Plus now I know that was a little overly sarcastic but nevertheless that’s the idea all right now let me actually go build something here is Android studio let me close this particular project because I’m going to run through two or three of them and but I want to show you the process of building a brand new project and I’m then I’m going to abandon it pretty quickly but I want to show you the generated Gradle build file so you have an idea what they look like so by the way if you have been working with the Eclipse ADT tools and it’s time to move over they have an excellent import wizard you don’t have to see what used to have to happen is you would go to your Eclipse project and then there’s under an export menu there was generate Gradle build file and it would generate the Gradle build file based on the dependencies you’d set up and the the mappings and everything but the project structure changes when you move to Android studio and now this understands how to read the existing Eclipse projects and we’ll do all the conversions for you and move the directories around and everything the only downside to this or something to be aware of is it’s a one-way change you’re not going back you know if the either the whole team has to move to the project structure or I wouldn’t do it at all and frankly i’d say that the stability is such that it’s definitely worth moving at this point a year ago you could argue that we weren’t quite ready yet now most of the serious Android developers I know are quite happy with Android studio or let’s say at least as happy with Android studio as they are with Eclipse plus android studio understands the Gradle build and does all these extra features that Eclipse still does not really support now they’re going to be changing that they’re going to be working on the Eclipse part two but I still think it’s time to move over so I have imported a few dozen projects under this import wizard and with very minor exceptions everything has gone immediately without a problem it’s gone beautifully so i have not had any major issues of that i haven’t tried things that used in depth google play services and odd stuff but if I just have a list of dependencies and basic projects everything is imported without a problem so let me start with a new Android studio project and i’ll call it then you know hello android and let’s change the package here to Gradle calm even though I don’t work for Gradle I’m still want to call them Gradle where I desperately want to say that but i’ll call it Gradle I do not yet have a picture of the elephant in my slides you know does that elephant have a name yet I mean I’ve heard people referring to it as an angry Gradle fan or whatever I mean it looks angry doesn’t even though it’s build happiness you know and it’s the bird looks happy I’ll give him that right so I was thinking you can’t use the name Horton right that’s kind of taken for Elephants but you could therefore use Tim is that the Canadians wouldn’t would enjoy that joke Tim ordinance kind of it no all right any rate it was just a thought all right moving on so when I hit next here then you choose which platform you’re targeting and notice that over these years we no longer really make a distinction between phone and tablet anymore in the honeycomb release then that was the first one designed specifically for tablets and we had the action bar added and we had fragments added and everything like that nowadays the phones are sufficiently big there’s not really a significant distinction there android where I imagine that they do support the development in Android studio and I think that’s going to get bigger over the next year so I do know one person with an Android TV but there’s only one Android auto I also expect maybe the next year to might become much bigger but right now you could probably wait a little bit and if you’re developing for google glass well sorry you know although my thought about Google glass is that Google Glasses version one version 1000 is an Iron Man suit right and who doesn’t want an Iron Man suit so but any rate I think you can not worried about that at this point so if I click on next and then it wants to generate some kind of activity for your application when you first build it and

I’ll just pick blank activity any of these work but again i’m going to create this app and abandon it so it doesn’t really matter i’ll say next and they wanted by default call it main activity and that’s perfectly fine the xml layout name will be activity underscore main the title I’ve never actually figured out what that’s used for they don’t really care and then there’s a menu resource as well and now I’ll just hit finish and it’s going to generate the project and then we’ll start taking a look at the pieces here now the first thing to notice i’m going to assume all of you are reasonably comfortable with Gradle I mean you’re at least familiar with that if you’re at a Gradle conference you probably have some familiarity so I don’t need to explain anything stuff to you so the first thing I want to point out is that Android apps come up as a multi-project Gradle build right from the beginning now it’s not easy to see it here because this particular layout is in an Android form and that’s restricting it in a nice way that’s helpful when you know where all the pieces are but I’m going to switch to the project view and that way I can see everything the way it maps to the disk so if I want to see this now here is a build Gradle file and there’s also a setting stock Gradle file the normal way you make a Gradle multi-project build so they refer to an app folder by default and that’s where the actual app is so they have a top-level project that defines what we’ll see is the where to get the plug-in and then this folder pardon me this file which will tell you which individual subfolders are sub-projects now they start with this one if you use any Android library projects and you there’s a way to add a module here it’ll automatically add the library project as another subfolder underneath this one if you use an AAR ever heard of those are right like those jars and Wars and ears and our AAR as an android archive that too is a sub-project in this space so any rate that’s enough with the settings file here is the top level dependency you make that full screen I can make that a bit bigger too if necessary and you see that they start off with a build script tag so the build script tag again for those who don’t use it a lot the purpose of that is to be able to download the plugin for Gradle now Gradle itself comes with a wide variety of plugins but it’s very common now to have a build script tag to download additional plugins when needed so in other words the user doesn’t have to go and download one and add it to Gradle you just have a build script which downloads it automatically where is it downloaded by default they pick J center used to be maven central now it’s jay Center maven central still works the dependency the group ID is calm Android tools build what maven would call the artifact ID is Gradle and what Gradle will call his name and then the version number the latest 1i have is one two three android studio will highlight this and tell you if you have an older version number it will offer to update it automatically now this is the first thing that I personally have a little quibble with shouldn’t it be called the android plug-in for Gradle not the Gradle plug-in for Android but that’s because we have people coming at this from both sides so I would have called it android here but nevertheless that’s what we’re doing now there is the build script to say where to get the plugin the other thing they put in here is in all projects setting up the j central repository so that it when you add your dependencies you don’t need to do anything in particular to if you can get your dependencies from j center that’ll work just fine okay so let me close this oh well here is the app folder and in the app folder again we have our build da cradle file so if i open this guy up you’ll see the first line is the actual plug-in being applied so the plug-in is called calmed android application and that one makes sense to me okay I get that so this is largely a you know just a copy and paste but they start off with this and I’ve never had to change that although the plugins evolved over time one time I had to change to the new coordinates but now it seems fine now down at the bottom they put in a dependencies block and because I think logically because with eclipse there was no way to put in dependencies as a maven style of download in Eclipse if you wanted to use additional jar files you physically copied them into the libs folder yourself there was no automatic way to do that so here they’re saying if we have I’m sorry didn’t realize those off the bottom there let me know if I go down too far below see if I can resize this to make sure I stay on the

screen okay any rate there is a file tree dependency on the libs folder including any jars in it the only change i ever make to this is sometimes i make that star star rather than just a single star so in case for some weird reason i had nested folders or something it would go down i would sometimes go star star / start out jar and that way it’s like a expansion for however deeply they’re nested and it would find all the jars in there I have to say though since I’ve moved to this system I don’t think I’ve had to do this at all I mean it’s good to know that it’s there and I generally don’t remove it because it doesn’t seem to hurt anything as long as the libs folder is empty but I could remove this compile file tree and remove the libs folder and I’d still be fine now here they also have added in the support library for app compat version 7 one thing for those of you are not as familiar with android when they say v7 here they don’t mean version 7 of the app compat library they mean this has compatibility classes that take you back to android version 7 see there’s also an app compat for version 13 i believe right and you don’t use both use one or the other and they don’t just have compatibility classes they have other classes that Google for one reason or another has decided not to include in the base SDK but you may be interested in so that’s automatically including the app compat library here with the latest version now what the plug-in does is it introduces the dsl that starts with the word android now as as Luke daily was talking about yesterday with the new configuration system this whole block is eventually going to be inside a model block but right now that’s not going to happen and my sense is is that we’ve got time on this is that I would bet that a year from now when we meet again then it might be something that we use on a regular basis but that they’re expecting significant modifications and changes of the new plug-in for the better part of this year so I’m going to stick with this process and I think we’re good for a little while you know so any rate the root element here is Android and this is where you wind up putting in the compile SDK version now you may have noticed on the wizard they didn’t even ask me the compile SDK version the older Wizards used to ask that now they don’t anymore they always assume you want the latest version of the SDK for the compiler and you in fact you do actually because the compiler is backward compatible that doesn’t restrict what you can develop you always want the latest bug fixes on the compiler itself the build tools version this again is something that is a string and it will warn you if you’re a little out of date it’s always good to have the latest version of this doesn’t hurt anything where things start to get interesting is in the default config this is where you pick your min SDK version now one thing that has changed over the last year is that when we defined a package for our project and you use the reverse URL notation nowadays of it back then we always called it package and in the manifest in the xml file it was always package now it’s called application ID now this string must be unique at the google play store if you deploy to there in the old Eclipse tools it used to default the comm Datuk sample dot whatever and that’s kind of annoying because you can’t deploy anything from calm down example to the google play store that’s actually restricted to android samples you know which are good so you notice on this tool they prompt you for your domain name they reverse the domain name and then they add the name of the project at the end and that hope is that that will be unique for you if you try to release a new version of a nap then this is one of the things are going to be matching on then the min SDK we mentioned the target SDK they also used to prompt you for and they don’t anymore this is the SDK version that you have basically promised you’ve tested on and are preparing for and here they said 22 which again is the latest one this one I might consider doing an older version and on occasion now the version code and the version name are largely used for your own internal tracking just a number and a version name but the version code is something that the Google Play Store notices they expect that to change when you deploy a new version of the same app and they’ll check that I don’t know that they do much with the version name but the version code they do expect you to increment that now here is our build type section and by default there are two build types there’s a release type and there’s a debug type and you can make up your own just by putting in words you know just say the name of a word and it will automatically create it if it doesn’t exist already that too apparently is something that’s going to change in the new model but we’re not there yet now what this one is saying is that in our current release version we are not enabling minification and we but

if we were the ProGuard files we would be using are the ones here proguard android text and proguard dash rules pro and if you get comfortable with groovy at all you realize this is basically a method call with two arguments in it with optional parentheses so you see you don’t really have to know much groovy at all to follow what’s going on in this thing when you get into radio regular Gradle builds and Java projects people have a tendency to start customizing things which of course is what Gradle is all about right i mean Gradle kind of assumes every build is ultimately a custom build and you wind up seeing a lot of groovy code in most Android projects pretty much the configuration stage is what we live on you know we just tell it at this config stage we were you were configuring objects here Gradle involves a project class and a series of task class instances and we’re setting them all up and hear everything that’s being done is configuring those objects we’re not defining any new ones now when it comes time to do that then you’ll see people go android dot all variants dot each or whatever and start making changes but you can actually do a lot just with this baseline build okay all right now I’m going to leave this and go to an already constructed couple of applications that I have because I want to show you some examples actually before I do that let me show you how nicely android studio handles when you add a new dependency okay so if I open one of the projects I plan to open my good old icnd be Android studio app I’ll open it a new window at the moment and again I’m going to have to remember to bring that up okay here is one of my dependencies let’s say that I wanted to use Google’s jisan library okay and again I’ll come back in a minute to why I’m using it here but I just wanted a sample so say I want to use that it’s as soon as i hit enter in this file android studio goes wait a minute the Gradle files have changed since the last projects think another projects think may be necessary for the ide to work properly well let me paste in my dependency so there’s the Google jisan library for parsing converting JSON objects into java classes and back again as soon as i hit sync now then a build process happens and one of the interesting things about android studio and this is different from eclipse if that basically this is one giant IDE one giant user interface on top of Gradle everything that’s happening here from compilation to build a test a deployment is actually calling Gradle tasks under the hood so everything you do here can you do it the command line absolutely this just has a nice little gooey on top of it but there is no separation between what can happen at the Gradle level and what’s happening at the IDE level so you saw a sink happened and now if I go back to my project and look under external libraries there’s the JSON library built right in automatically and that is a nice nice feature in my opinion I mean all you have to do is modify this file in any way and androids through is say you want to sync now and say absolutely and if you forget there’s a sink now button you know sync project with Gradle build files you know so you can always get at that functionality all right let me close this project and explain what the next one is going to be like now this one right here a sink job test yes okay this is basically a front end on a website so let’s see where’s my website here called icnd be calm the internet Chuck Norris database as in chuck norris once ordered a big mac at Burger King and got one you know and there’s an API here to give you chuck norris jokes but of course at the reason I’m using it partly is for the humor and partly because the size of the return javascript object notation object is very small making it easy to map to now I’ve mapped to very complex ones but I want to in here I want to keep it very simple to show you how the example works here is the the structure type of success value equals there’s an ID and a joke time waits for no man unless that man is Chuck Norris right and they have an API and you could specify the first and last name which is what i’m going to do i’ll put solve in there or I’ll put Hans doctor or whatever maybe I should put Cedric in except I can’t get the e with the little accent on it you know so any rate you don’t want to just get a random joke in a public forum ok they’re not all really suitable for public consumption but what i can do is limit them to nerdy ok and that’s much better

so that to give you an idea what this looks like if i grab this just by itself and i paste it in my browser and also put in and first-name equals i’ll use Cedric I’ll just have to leave off that special ampersand and lastname equals oh great alright CH ampe a you aha i want brownie points for that band ok now i see that’s a chuck norris joke we met we got to fix that it can instantiate interfaces okay that’s pretty good see but that’s the structure of the of the JSON object I’m getting back very simple but at least I can map to it alright so what I want to do is I want to access this website from a Google App pull down the JSON object parse it extract the joke from it and then update my user interface to show that that’s the plan and one of the reasons I want to do this is partly because it’s funny but also because it lets me use a third-party library in my Gradle build that doesn’t seem to get enough press from my point of view I don’t see it being used often enough and that library is this thing called spring for android now almost everybody has probably a some familiarity with the spring framework right I mean probably the most widely used open source library in the Java ecosystem this is not trying to put a spring container on a handheld device that’s not its goal if you want that there’s Google juice there are ways to do it but what this has in it if you look in the reference docs let me just go to the reference talks here for the release version is it has a rest template and if you’ve ever used spring MVC or done spring with rest web services you know about this rest template it’s a wonderful little client from restful web services and it has I’ll show you the method I’m going to call in a minute it’s got the same message converters as those that come from spring MVC and the only other part of this thing is an authentication or authorization module built on the court that’s it that’s the whole spring for Android and it’s designed for Android so it’s incredibly easy to use so the piece I want is here if I go to the get requests then what it says is if you call a method called get for object and you provide a URI with those little URI templates in it the parts with the braces around it and I supply values in there I also have to supply a class that has a structure that maps to what the Jason structure looks like and then when i call this method then the rest template will download the JSON response and parse it and provide an instance of that class for me right away so no monkeying around with Jason parsers no working with the HTTP connection objects you know all that painful stuff is gone and that to me is really worth it and I don’t know why people don’t use it all the time of course maybe they do I just haven’t run into those people but it’s possible so with that in mind here’s my app and in my app at the bottom you’ll see that i have added the spring android rest template in its release version and the spring android core now i’m not sure whether it would have picked up the core is a transitive dependency or not but they recommended adding it so i added it also when you use springs rest template it asks you which library you want to use to do the conversion from the JavaScript into the Java and they support Jackson you know the Jackson Jason library but I’d still have to add that as a dependency so instead here’s where I use Google’s jisan parser again to convert from one to the other and this will come up again when I show you about testing how we doing here okay I got about what am I going to we started sorry what time so I know ten minutes great all right well then I’ll show you all these things pretty quickly I’ll just run through them as a demonstration so any rate there is the bottom line and I also have the testing stuff which I’ll show you in a minute now if I execute this application here let me switch this to the app itself and I will run it then it asks for a emulator or a connected device I have two emulators running at the moment for reasons I’ll show you in a minute here and it’s not the chuck norris database did you know that chuck norris’s real name is carlos ray norris it’s just chuck is his nickname I found that out when the lawyers contacted me about using the name Chuck Norris so any rate here it’s the Carlos ray database now and if I click on get joke there you go i’m using hans as you can see Han stock can access the database from the UI sure there you go writes code that optimizes itself that’s a good one alright so

that’s how the app works is I’m actually just going to that site downloading the string parsing it and putting it up here and here is the code to actually do it the code i’m using is under source main java and here is my main activity and here is the class that maps to that JSON structure so this is just type and joke and inside joke is ID joking categories that’s it the rest is getters and setters ok so I’ll close that here’s my rest there’s the URL i’m using and then inside an asynchronous task in the doing background so that i get off the UI thread there’s my get for object passing in first name and last name gives me back an instance and i pull that out that’s all there is to it very very simple exercise here so there’s that much now let me switch and show you some of the tests for this sort of thing so i’m going to switch to my women i have tests in this one already so here’s my tests under source android test java here is a class called a main activity test and in this case i’m using a different dependency cauldre bhatia ok Robottom allows you to drive the user interface with clicks and things like that and it has a class in it called solo I always want to make the reference you know hon or hope hope might not be a good choice right now or soprano something like that but I use just solo here you set it up with the instrumentation the activity and they see this is what happens you go click on button and then you search the text you can find out what’s going on now here’s what the cool part is assuming I have all the dependencies set up and I do I want to run a Gradle test that runs this test on both of those emulators simultaneously it will run on all connected devices at the same time over here is a Gradle window which shows all the Gradle tasks there are I think saab mention about a hundred and ten of them tons of these things and I don’t even have any variance or other than the build types I don’t have any special flavors going on the connected check one is the one that I want to run in order to do this instrumentation or the the integration test here so I’m going to double click on that and then you will see that it’s cranking its way through and once it gets to the debug test which it looks like we’re there let me go to the emulators and watch how it’s going to run both emulators and all the tests simultaneously and then when I come back here I can actually look at the output under build outputs women build reports Android tests and here’s index dot HTML I will open in a browser and you can see the generated report ran all four well two tests on two devices if I had it plugged in device that would work as well this is organized by test this is organized by device and all that happened automatically really nice okay because time is short let me now switch and show you the espresso version of the same thing oh I’ve got a local branch code espresso this is all going to be in my github repository as soon as I update it I’ll have to do a new sink because that’s using different dependencies to do espresso here and what you’ll see here and by the way you could see it running through all these pieces with the assembled debug and everything the build looks different now the build has the android support annotations calmed android support test runner and espresso and then also i had to say use the android j unit runner for the test instrumentation and that means now I can run my main activity test as simply on view with ID for the button perform a click and then on view with idea the textview check that it matches with text could contain string Hans see now this one I can do basically the same thing the only difference is and then let me run it is that this one does not seem to be running simultaneously although I can’t prove that I don’t know whether it’s doing them consecutively or not did it run yeah we’re up to it now I’m probably going to miss it any rate it seems to run one and then the other but it’s not like these are exactly complicated or difficult test see there was the first one and then there should be something that flashes by that’ll be the second one as well yep there it goes and again I can go back to my test here and refresh and everything this time I only had the one test in there that works fine as well okay the last thing i want to show you and then i can hang out

for questions or whatever is i want to show you something about flavors as well so first of all let me switch to a different oh I’m sorry one more thing before I leave that the new thing they have in the Gradle build now is the ability to actually handles true unit tests this is a lot less cool than it sounds though they do not emulate the android system that’s robolectric okay instead they just say if you just want to do a regular old j unit tests on something that does not require android in any way you can do that and you’ll see here I have a source test job a folder with a test of that joke class so that I hardwired in a json string and i just ran through jisan to catch it and what you’ll notice and really the reason I’m bringing this up is that down here in my build variant section if i was to switch to unit tests then that would become available and i could run it as a regular old j unit test and that works as well it’s still completely separate from the rest though okay so it’s got some utility but if i needed to do anything inside the android system I’d have to mock that myself in order to use it okay so I want to say that is in there now but I’m not notice it’s not even acting as though it’s a source folder yet until I switch this to unit tests at which point it goes oh this must be a test folder and now the old ones aren’t anymore the Android test anymore so that’s the sort of thing you deal with okay now what about the flavors here is a project called hello world android studio I’ll just use this window again and in this one I have a block called product flavors where I have arrogant friendly and obsequious this one you’re going to put in a good word I wanted to use that for years you know you put in a name and then you’re going to get a greeting like Hello name ok so here I have variants and in the variance I have a whole series of them based on the combination of the build types and the product flavors arrogant debug friendly obsequious etc so i’ll pick the will start with the arrogant debug and let me run this and this app has a lot more meat to it I this is the mother of all hello world app so I like to think you know because I’ve really stuffed a bunch of stuff in here here’s the arrogant one and I’ll put it in Andre right there you go and I’ll say hello and it says his or her royal highness we condescend to acknowledge your presence if just barely Andre and there is my image see and then I’ve got this nice little thing down here and I can click on something and I’ve got alerts and I could register positive and negative i’m telling you this is the mother of all hello world apps you know but i’ll tell you about it later any rate that’s the first one i even got a notification in there and that will take me back to the root here ok now let me switch to the friendly debug and run that one on the same emulator i guess come on you can do it okay I was just waiting for it to pop up which emulator to run on and then that will come up and well put in Cedric again because you’re friendly with Cedric and there we go see we’re BFFs there’s Cedric okay again I’ve got this thing working where I can just switch back on the notification and finally on the obsequious one and then I could run this again I could have done this from the Gradle build and in and there we go so let’s should I put it well put in saab for that right you know say hi and you know I had to put in da be right oh great saw please accept this pathetic greeting from my unworthy self i grovel in your general direction ok now the parts you don’t know and what’s interesting is that how does this actually work inside my project structure I have a separate folder for arrogant resource values and there’s the animal PNG and the same thing under friendly let’s get this out of the way res and there’s my drawable and I’d have the same thing for obsequious that’s just down at the bottom see if so let me close up the main one here and all i’m doing is that each of these animals is it is the different picture just with the same name and all of the strings are the same file just with a different greeting in them see there’s one and there’s another and there’s the third see that sort of thing so whichever build type I pick that’s the resource it’s using now they say flavors are for things like paid versus free which implies you can also change Java versions and you can but you have to be careful of that because you can get

conflicts Java you have to basically not have one in Maine and have the same class in each of the flavors and make sure that you have the same methods available it’s a little bit more work but you can merge that but the strings and the images this is easy so I think of flavors as an easy way to say put a skin on an app for a different client you know I’ve one for one client or one for another one from a third that seems to be a very useful mechanism that you might want to advantage of you can merge the manifest to again there’s complex rules in how that works having additional activities or services and so on okay so those are all the major things I wanted to show you let me at least run through what’s here to give you an idea what’s in the slides here and then I can call it it and there’s the link for the Android studio that’s the business about the build script section here is the latest version of the dependency we specify version code and name we have our multi project build with the build Gradle in the settings we have a debug and release build they both go in the build types and you could modify it now one thing I didn’t do is if I if I put some customization on the package names like put a suffix on them I could have had the Gradle build built all of them all six of them or eight of them actually because I had three flavors and to build types I could have had to generate all APK simultaneously all of them then you have your source main source debug source release you have different folders based on their build types and folders based on same thing here so the resource types just take their counterparts but the job of classes conflict it be careful and then flavors you again you have a separate source tree on all of those to make that work to do the release one of the things you would notice if we had time but i’ll just tell you now is that you don’t have an install release task until you have a signing configuration because it doesn’t know how to sign it so i actually use java’s key tool and generated a signature I’ve got my little wizard right here and showed everything with the password and all of that and then pasted it right in here for my you know Gradle rules and carlos ray AKA chuck and that’s once i pasted that into my Gradle build file now i have an install release as well as an install debug on that one you don’t have to put the passwords there they can be on the command line they could be in a separate file but here for this I just wired it in you specify where the release config is in the build file and you’re all set you could do a signing report as a Gradle task find out what the configuration is a-and that’s enough of that and then the testing I mentioned with Android test and you run oh the one I ran was connected check will run simultaneously on all connected devices and flavors and variants i mentioned i put in my three you get all these apks out of it so that’s enough and then here’s some links on all these different things so that you can get to them and I will update this slides on the website with these slides again that after the presentation I’m sorry around a little bit over I will hang out if there’s any questions otherwise thank you very much for coming any questions comments of there was one there and then I’ll come back to it yes sir yeah the question is is Android studio that different from the android plug-in for IntelliJ and the answer is eventually they wind up syncing up but the Android studio one is always the latest and greatest that’s the one they’re working on all the time so all the changes go in there first when IntelliJ releases a new version eventually Android studio release a new version that takes all the changes and wraps it in and eventually the stuff from Android studio will make it back into the android plug-in I would recommend downloading and using Android studio for Android even if you already have an intelligent license because it keeps them nicely separate and you’ll always have the latest and greatest but you can do almost everything the same way in intelligent okay yes sir you had a question connected android test runs fewer things that connect to check i don’t remember the actual details connected check will ultimately call connected android test it just does some additional setup work ok that was the difference we connected check and connected android test just mention for the video any other questions yes can you localize android apps with flavors you don’t generally need to use flavors for that they already have localization built right in in fact if when you saw when i opened up one of the string files here just string stout xml it says edit translations for all locales in the translations editor i could simply add values underscore you know are you or de or whatever and wind up putting in my strings file on all of

those and then the device will detect which locale we’re in and use that automatically so you don’t need to do that with flavors that’s a fundamental feature of android from the beginning ok any other questions well great thank you very much again and up they’ll see you around in the conference okay yeah we got it okay take care you