Jeffrey is the tech lead manager for the D8/R8 Compiler team within Android Studio. He has over 15 year of industry experience building developer tools with a focus on compilers and static analysis. Jeffrey is the Google representative on the Kotlin Language Committee, ensuring the language ages well. Jeffrey studied Computer Science at the University of Utrecht in the Netherlands.View the profile
About the talk
This session will discuss in-depth what is happening to the various compilers used in Android: Java 8 language feature desugaring, the new dexer (D8) and shrinker (R8), and work done on the Kotlin compiler for Android use.
Good afternoon. I'm Jeffrey fingo. I'm a tech lead manager on the Android Studio team. I want to be in charge of the DHHR a compiler project the last year a lot happens in outer space. We had a think I'm addled Xing. We just make sure you talk to build faster. We had a string of David language feature. So you can now use Java 8 features like Landis in older version of Android edit the new Dexter which compiles your Java bytecode to Delta bike code that run some of the Arts run time and date in internet as well.
And then today we also have the Android app bundle. And so I wanted to talk to you about all of these show how it works inside dreidel and give you some tips of things. You need to know when your start using these new tools. so let's first start looking at the incremental texting so Here is you have a very very simplified version of what happens in Gradle compile. It runs. So first we run either Java sea or Clancy to take your Java open source code and generate Java bytecode and then we run DX that takes a Java bytecode and produces delphic
bytecode because of grades do antidepressants work Java sea and Colton trxc incremental and that means that if you change one source file, it only compile said one source file and potentially any sources that has different semantics based on that changed the unfortunately in Android Studio Enbrel before 3.0 DX didn't do any incremental work. So it's still took all your Java class files and compile each one of them to delphic bytecode, even if there's glass Falls hadn't changed. So in Android Studio
3.0 and above we actually change it so that we can be incremental. So we split the action to the two steps one that takes the job I do then compiles it to the Delta by Toto and Weezer. Per class file to be actually generate Now 1 Dex file for class file and that way that step can be incremental and then after that we take all the X-Files and March them into a single or multiple decks files if needed this works is that most of the time spent index thing is actually in the compilations face, very take
the Java bytecode switch is stuck bass machine to delphic Vice code, which is registered base. And then the decks merging is more like a fancy Concord and so that's a lot faster. And so we unable this by default in your V Buck built-in release built. We don't do that. And so Do you pay a little bit extra for the initial build because we need to generate more files, but then eats incremental bills that you do afterwards. It's a lot faster. I just because we have to do less work and there's some things you need to be aware of though. So Java sea and Colton see our
only incremental when you're not using annotation processors anytime you enable annotation processor Bistro sanitation procedures can reach into any part of your source. And so we cannot make that incremental now dreidel is working hard to make that supported and swingrail 4.7. They introduce inflammatory support for independent income in sanitation processors is required to support that because her needs to tell dreidel how incremental it can be and so what I'd like to ask you all as if you are an annotation processor rider
look at the stuff that Gradle is doing and see if you can For that and if you're using annotation processors, please read out to the developers of those that they should look at this because it will really really speed up your builds. Okay, so let's go on to the sugaring. What is the sugaring we hear from old people David many times that they want to use mother and Java features and Java a lack of time does default method in interface is private resources Etc. A lot of these features require a new bike code and language apis
to support them. And of course a lot of Android device out there when all the versions of the delphic p.m. That doesn't get support. He's not sure exactly what these libraries do but they use a lot of cool backs that and would be much nicer if you can use land us there. And so what do we do with the sugar is we take the bike code and poles that are generated by these new features in the Java compiler. And we convert them into something that is supported in the old system.
If you use a Lambda and jaw paid we can take that and replace it with the class as if you heard you had tried it so that you don't have to hand write it then we do it for you. So let's take a look at how that works in practice. And so that's what's the demo. ISO here at Android Studio project that is just raided by following the Wizards and selecting a basic activity. And so if you go to the immortal Immortal settings you see that I've set the source language 21.8 and now it
will suit me to write Java 8 language features and Gretel automatically figured out that it needs to run the sugar for you answer here. If some code action button that I want to hook up some code to add when you click it and I'll because I use Java I don't have to ride New Ulm click listener and implement the hole in face and said I can just ride the lender and have it in your throat. So if I go and look at the output of the Java compiler, there's a main activity of class and I can watch a dka
medical jobs IP, which allows you to take a class file and look at the bike code that's in there. And so I'll run that I am because it's Universal. I'll put all pipe it to a file in and then I'm open to get to my favorites ID and Art Studio. And so if you haven't looked at Java bytecode don't be afraid this is so pretty high-level. It's kind of readable. The only thing that's not there is four lobes if they ate months and so on but we threw it is still pretty understandable. So just a lot of chromosomes that we're just going to skip over and then here we have you don't create method
that we were looking at it for school here. You see that there is a virtual Cole to the south on Craigslist in our that's the thing where we passed on them. To the argument before that. It is invoked Dynamic construction and it tells us that he's going to pass this on click listener. I'm so what does invoke tnemec? It's a nice feature in the Java p.m. That is kind of reflective. So instead of the VM image of the invoking your method. It allows the application to provide a hook in there and
dispatch the method. Anyway you want and so and they have these things called metal factories that they use to implement these features a specific Factory and you see that you're in the bottom of that file and then it passes through this damn the uncreated method which we can see here as well and if you look carefully You see that it actually has the snack bar codes that we had in the body of Orlando. So what's going on here is that the first time the app is run on the jvm. It knows it needs to call his lamp manufacturer you and that thing will actually generate the class
that implements the interface on the Fly and then it will call that for the rest of the program know the price. I said that takes time and run time a task. Remember that one time so we don't do that Android even a newer version of Android and of course an old version of Android, they don't know about this involve Dynamic construction or the metal Factory. So instead we'll take care of his so let's take a look at what's happened in this project me build it. You think the sugar so I'm going to open APK are there stool since in Android
Studio 3.0 a bar called the APK analyzer and allows you to look inside APK both for a file size of your resources at but also to see what's inside the Dex coat that's going to run on a device. So here I see the oldest packages in the decks coats and I'm going to navigate to my main activity and then one create methods and I'm going to say show by code. So you see bytecode that is kind of similar to Java bytecode just a couple differences instead of using stack base machines. We have registers, but if you don't look familiar
with that, don't worry about that at the end of the method we see the same Cole to the set phone. Click listener. But the Victor France is that one line above it doesn't show info. Can I make or info customers would be on Android instead? Of course this magic class that they're still Orlando and then it gets the interface field of that take a look at that class. Do you see that the classes right along there? And so let me see in the class is that it implements the only click listener in space
and then it has to be on click methods for the one click listener and face and all it does is called the generated method that contains Method video for Lambda. And so no, there's no Java 8 features no job site by codes in this code and we can execute a delay any Android version that you can the slowest ice cream sandwich. Play so that switch back to the slides. So this is how that is integrated into the greater build system after the Java C compiler run
we run this effort process called the sugar I want it dusted read the Java bytecode. It takes out all these functions that are not supported admits new buy coats and then we pass it on to DXM. And so the rest of the pipeline doesn't have to know anything about do sugaring. So this nice you can use new Java 8 features. I just a couple things you need to be aware of if you do your own vital Transformations. So just people who do their own bicycle transmissions for the blackcoats injection crash reporting Etc. Love you sugar your bike still Transformations after the
sugar, which means that you see or crazy patterns like the Black Codes have been you doing your own processing. So be aware if that if you're doing your own vital transformations, They so let's move on to DIA. Dhsr new Dexter Android it runs Java bytecode North Java bytecode and Dexter is the tool that takes Java byte codes from the SEC bass machine in comparison to delphic by code is register base. And so we decided to build a new version will be eight. And so do you want that faster complation? Because everybody always wants past the bills that
we wanted to generate smaller codes and gift people better Diagnostics. If I better Diagnostics, I mean both the error messages that you get from your run the computer as well as better be book information that when you're running your app in the debugger that you have a better understanding of what is going on. Okay. So how is the eighth integrated into the greater build system? It's actually quite similar to what the Exodus we just swap out the X 48 the interesting thing there is that in Android you 3.2. We also integrated the sugaring step into the 8th.
So that saves us a round trip between write a reading and writing the class file and so it will proceed provide more speed up the side effects of Depo. He said if you're riding your own by to agree Riders, we not run them before the 8th, which means that your bike providers have to support the Java 8 language. Okay, so let's look at the demo of the 18 action. Okay. So here I have another project that I just created using the project Wizards and it because I'm using Android studio 3.2
theaters already enabled by default. So I ever went into the greater property files and I explicitly disable the 8 because I want to show you a behavior of the ex before Tuesday 8. And I mean application added some code to my own click listener to have the snack bar Prince a custom message. And then I have a method get message and I said a break wine. That's so let's look at that when I run that's in the emulator on the debugger. Okay, so the app is running. I'm going to hit the
button so that I Got hit the break point. So I initialize text to be the length of the empty string. So that should be zero and souvenir statement. I expect to step through the true case. And of course, that's what happens and I'm going to separate her. This is guitar, right? I don't expect my code to evaluate both the true and false case. So that's kind of weird. So, let's see what happens if I run and see what the outfit is. So luckily the output on the screen is hello there what I
expected but there was something weird going on what was going on. Remove this and sink our betrayal builds so that we are switching to DIA and that's stuff that and redeploy it. The weather's going on sewing TX very high starts book report is the the volume very different between stack and register the way the debug information is stored in Java class files in delphic is very different and in Java, it starts with the instructions and in Delta kits a state machine and so we had to translate both from
registered sec to register and the debug information and so sometimes information go plus so it's like it might end up with a single return say friends in the delphic bytecode and then we couldn't map that ended to be a confirmation. NBA retract audiobook information carefully and yes whole system of assertions to make sure that we don't lose the book information. So let's hit the button here. We're hitting the brakes when they're gonna be step through. We getting to the truth branch and we jump out of it. Adele
better let switch back to this lights. So it has better debug information about it's also faster. So here is some data around Bill timer on the Google Nest app. So we ship dates as a preview in address to 3.1 there were always faster but to be a lot more work since 3.3.2. I'll never forget to worry about 16% faster and clean belts. And of course in increments of bills like the Delta smaller because to compile so still 60% This is pretty nice. So V8 is already widely used if you're using Android P beta that was released yesterday. Android p is completely
built with V8 add. The Google Docs app is already built with the 8th and then Google photos is right now in Canary using V8 and many more Google Apps will follow soon. Hey, so let's move over to all right. All right just aren't used Rinker. So why you want to swing her? So most people who build apps that use of all the libraries rxjava then usually don't use that for the library at you might use maybe 10 15% of the library yet. If you ship it as if you would be shipping ellipse code that you don't use application sizes in Fullerton, right people don't want to pay for it and
their benefits if uses disk space on your device. And so the smaller the better, they must have previous solution to this that the programmer tool but we had some people that they had issues with taking a long time to code wasn't as small as they wish it didn't really understand Android and so we invested in building a news anchor make the messages they were and of course we understood that people are already using proguard and so we decided that we wanted to be programs compatible. And so we understand progress keep rules So how does paragard work so
before that's what I read Works before and Gretel we would run proguard between your Java completion and deck generation. And the reason for that is that frog art is a Jaffa to Jaffa Confederation. And so this added more time to your bill. In Android Studio 3.2. You can enable R8. It's just too experimental. You can label it using the setting and what will happen is it will replace broken hearts D sugar VA with one single process. All right. That's all Phil steps in one go.
Sol Beer N All rights on several stops and friendly, this is the nest app data. So but just swapping proguard for R8, we're able to save 100K that on the text file size and save 25% on completion time. Now, the nest app is high V optimized. It has a very specific program peoples and still are able to save quite a bit of space have we also run this on some of the system apps that are shipping with the Android OS able to save 25% of the Dex file size by just swapping proguard with all right.
So, of course last year be announced at Copeland is now a supported language on Android. And so we figured we need to do something for Colton as well soon quotes. Of course, if you right this instant cold and it's so powerful. It needs to generate a lot of bytecode. And so we looked into places with we could drink that further than the sun analysis. And so we found a couple places where we could do things like class emerging especially around Lambda. We do more enormous analysis
take a look at that and a demo. Okay, so Here. I have a couple an application program Copeland. This is a data class, which is a class that it generates letter code for you. So you have a couple of fields here that are really properties. So it generates gather sunsetters equals get hashcode Etc. And then here I have an extension method that sells his if I see a collection of intentions are to the type of card at this extra method that allows me to search for make and model. In the class. I'm using like the sequence operators of Cortland to do a
couple filtration group eyes. Normally, you wouldn't write this many filters in a row you at were just put all the Boolean logic in 1 filter, but they wanted to show you what happens with multiple filters. So normally cost Lynnville compiled each of these lambdas into his own class and eat slime that you use at a new class and that's not what you want in a text file because you always want to keep their methods reference count low. Look at what happened when we do with. All right. So in this project and able to operate by sending his property
And let's take a look what happens in the output APK. So here's my classes Dex again the main activity that message in the on create method. And so here and you don't create method. You see that there is new Ensign Studios Lambda group class. And then the thing is that the class is not defined in a package. It's defines at the top level. So let's take a look at that here in The X-Files. There is just one interface and it has two in since Fields one, which is a type of Jack his name capture. And the other one is ID. And then the Constructor takes both the
ID and the object and sets of variables. And then here in the envelope method. we see that it has his pack switch and so what we're doing is basically the equivalent of a switch statement with switch over the idea that was passed in the Constructor do figure out which piece of code to call and see if you scroll through here you'll see that the code to get year model are all in this piece of codes and then you see here that we have to decide which I don't know
what's going on if we find that we have the same signature implement the same interface and they have the same capture variables and so we can take all the different lamp Glass and Mirror sand into one class which allows you to have some metal data and this message references I saw that switch back to this lights. Cortland one is something we were very important and it's becoming more and more important and so with our aides we're going to keep adding new Altima stations for Cortland. We're doing this at the emerging analysis and we're adding many
more so hopefully, all right will really help you get your cotton code even smaller. Vietnam Android apps bundle yesterday, one of the things that comes with Android app bundle is dynamic features. And so you can not only split your APK by resources languages Etc that you can also split your features into multiple APK the first great because not everybody uses every feature of your Ave and I did it down side and we heard this already with in synapse last year. They said it makes it harder to rent Rogue R2 or R8 for your app because these tools
they're based on doing whole program analysis, and now you don't have a single program anymore. And so what we came up with is the way to take all your different features. Adam I'm passing this one command line into proguard or R8 so that it is effectively a whole program. And I forgot to Rachel spit out the single jar or Dex file. And then we can take that information the proguard mapping file and the original feature jars and get that information. We have enough data to reconstitute the different X-Files ice. We have this new
deck splitter, which is based on the DEA date code base and will spit out that the whole program again in two different modules, and now you can apply shrinking and optimizations to tell your features as well. So this is coming soon. I will be in Android Studio 3.2 by the time it reaches table. So we look to the whole bunch of different computer work that we've done over the last year. Some of it is already stable incremental texting. It was introduced Android 3.0 the sugar stand alone with him Fusion 3.0
in Android Studio 3.22 enabling the 8th and the sugaring as part of that and then we're introducing are Avis and experimental feature. Please use it be announced that the X will be deprecated. As soon as we find no more major issues in NBA before you in the ER so next we'll be gone. That's what we'd like to try it out. File bucks. So if you go in Android Studio to The Help menu dresses submit feedback option which to immediately jump you jump into the issue tracker
and then you can easily file bug. Our team is very responsive these books. There is another station tomorrow has Colt effective program keep rules for smaller applications, and it's basically the how to learn how to start using Pro guide or R8. They are only are a team. Please fill out the survey on Google IO schedule about this talk as well. Thanks everybody.
Buy this talk
Access to all the recordings of the event
Buy this video
With ConferenceCast.tv, you get access to our library of the world's best conference talks.