Тёмный

D8, R8 and enums - Kotlin Vocabulary 

Android Developers
Подписаться 1,3 млн
Просмотров 35 тыс.
50% 1

In this episode, we go over Kotlin enums and when statements, the overhead that they can incur, and how to use the Android R8 compiler to reduce that overhead.
#KotlinVocabulary playlist → goo.gle/2wHr94N
Subscribe to Android Developers → goo.gle/AndroidDevs
#Kotlin #AndroidDev #Featured

Наука

Опубликовано:

 

9 мар 2020

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 58   
@AdnaanAhmedZohran
@AdnaanAhmedZohran 4 года назад
... have I been pronouncing enums wrong all my life!!! o.O
@Blafaselblubb
@Blafaselblubb 4 года назад
It's [enumm] and not [anoom], ight?
@GeneralKazar
@GeneralKazar 4 года назад
It is pronounced enumeration.
@SpaceTimeBeing_
@SpaceTimeBeing_ 4 года назад
@@Blafaselblubb e-num
@SnoopyDoofie
@SnoopyDoofie 3 года назад
Nope. Different languages, different pronounciations.
@goobar
@goobar 4 года назад
Enjoying this series 👍👍. This very specific practical example of R8 is great. Would love more in this style.
@darkknightsds
@darkknightsds 4 года назад
I have been nerding out on enums lately, thanks for sharing!
@guzzledust
@guzzledust 4 года назад
How great to see that Android community becomes more and more shareful, open and performance care!
@harounach
@harounach 4 года назад
Thanks Haas for this explanations
@Salehalanazi-7
@Salehalanazi-7 4 года назад
Oh wow I'm enjoying this way too much
@roshanpisharody6398
@roshanpisharody6398 4 года назад
Chet glossed over one detail that if you have both kotlin code and java code, then initially both kotlin and java files passes through the kotlin compiler and then passes through the java compiler.
@ericksli
@ericksli 4 года назад
What if we define attribute inside the enum? Will the overhead become very big?
@Bliss467
@Bliss467 4 года назад
Is it not possible for the R8 optimizations to be added to the Kotlin complier given that it is open source? Is this something only specific to the Android environment, or does Google wish to keep the optimizations proprietary?
@jerrywang9593
@jerrywang9593 4 года назад
🤔 Interesting
@akdeveloper5219
@akdeveloper5219 4 года назад
Kotlin is taking over.
@TamimProduction
@TamimProduction 4 года назад
I personally never use Enums for such situations, even android studio sometimes warned me about using Enums in switch statements because it adds some overhead and suggested using static integers which is what I did since then. if it's required to use enums switch their values instead of the enums them selves.
@Israeli8103
@Israeli8103 4 года назад
I didn't get it. How can the enum break?
@adityavikramrajawat
@adityavikramrajawat 4 года назад
He didn't say it will break but to avoid breaking the code enums will generate an array which will cause overhead
@jaloveast1k
@jaloveast1k 4 года назад
In java enum can break (not enum itself, the switch statement) if you change the order of constants and recompile the file containing this enum. It can't break in Android tho, so that's why R8 gets rid of it.
@ulmaxy
@ulmaxy 4 года назад
@@jaloveast1k But what if we recompile both the file containing the enum and the file with the switch statement? I can't see any problems here
@SociedadAndroide
@SociedadAndroide 4 года назад
@@ulmaxy The problem is that the generated bytecode of that switch statment will not change the position of the elements in the array, so, in each case scenario you have, if you changed the enum class the array will be still pointing at the same positions but with different values. If you use R8, it will get rid of that array and just get the enums from each case
@roxferesr
@roxferesr 3 года назад
Don't get it either... recompiling both files (library + client) should fix the problem
@alexei3366
@alexei3366 4 года назад
in java 14 you can use Switch like in Kotlin
@SnoopyDoofie
@SnoopyDoofie 3 года назад
Kotlin doesn't support switch. It uses "when".
@Binizh23
@Binizh23 4 года назад
So in simpler terms, does this mean I should enable R8 for my app ?
@AbhinavKulshreshtha
@AbhinavKulshreshtha 4 года назад
Only when you push a release in production. R8 takes its sweet amount of time for optimizing, which will make development slow. Also if your code has a lot of legacy debt, it's better to avoid it until that debt is reduced, in my experience .
@linhtruong9011
@linhtruong9011 4 года назад
Does anyone know the color theme of sample code :)
@SriHarshaChilakapati
@SriHarshaChilakapati 4 года назад
I'm confused on how can an enum break. After all, enums exist in Java since a long time, and Android supported them just fine. So does this mean that Kotlin implemented enums differently compared to how Java does?
@adityavikramrajawat
@adityavikramrajawat 4 года назад
He didn't say it will break but to avoid breaking the code enums will generate an array which will cause overhead
@maalevolent8052
@maalevolent8052 4 года назад
Enum isn't the issue; it's the combination of enum and when that introduces the overhead
@SriHarshaChilakapati
@SriHarshaChilakapati 4 года назад
@@adityavikramrajawat My question was why did the Kotlin compiler has to create a class and an array to generate enums with when expression. As far as I understand, when expression seems like a shorthand to the switch statement. It even reminds me of the Haskell's case statement. Java does switch expressions since Java 13 (although it's a preview feature), so I wonder how they had the bytecode implemented.
@SriHarshaChilakapati
@SriHarshaChilakapati 4 года назад
@@maalevolent8052 I think my wording isn't right. What I don't get is why does Kotlin require a class and an array in the first place. There are two kinds of when expressions, one with a subject to check on, and other variant which has the subject inside the conditions itself. The first case, is like case statement in Haskell. The second case however, can be converted by the compiler into an if-else chain. I don't understand why the enum with when construct break if the class is not generated there.
@jaloveast1k
@jaloveast1k 4 года назад
@@SriHarshaChilakapati video was a bit misleading. Java also generates these additional mappings if you use enums in switch statement, but the naming was different. So basically R8 was optimizing Java mappings, but not Kotlin ones (which are, again, almost exactly the same but with different naming). Now R8 will also take care of Kotlin mappings.
@devanshmaurya9928
@devanshmaurya9928 2 года назад
So basically the gist is that I can use enums when I am using R8?
@ashiqurrahman-nu7ik
@ashiqurrahman-nu7ik 4 года назад
Most of the times i try to set minifyEnabled=true my app crashes with exceptions like classNotFoundException. Would appreciate some insight about this ;_;
@volnoor
@volnoor 4 года назад
You just need to add some rules (for example, not to obfuscate the class that causes classNotFoundException) to your Proguard file (proguard-rules.pro).
@FlorianMeyer1983
@FlorianMeyer1983 4 года назад
you´re not alone here. Turning on code minimization and obfuscation is a tricky thing. In short: To make the compiled app smaller and harder to decompile, R8 (i.e. ProGuard) is changing all your class names and method names to something shorter. So "YourClass.withThisMethod()" may become "a.b()". If there is a place in your app that for some reason still expects the class name to be "YourClass", but now it is called "a", then you get the ClassNotFoundException. That`s where ProGuard rules come into play. Start googling from there.
@ashiqurrahman-nu7ik
@ashiqurrahman-nu7ik 4 года назад
@@FlorianMeyer1983 Thank you for your explanation. And yes, i did get my way around by ignoring some of those classes in my proguard rules. However, its still seems messy to me because i double checked that the code 'i wrote' and it has no reason to be still called by the string matched class name. But, i think its prolly because some of the dependencies im using require that exact same class/method name. i think even some of the core dependencies coming from android require the same class/method names e.g: i think android navigation components, android databinding ? In these case, i find it often difficult to debug the scenario, wish there was some more help idk maybe from the IDE or some plugin. Or Am i missing something useful?
@FlorianMeyer1983
@FlorianMeyer1983 4 года назад
@@ashiqurrahman-nu7ik Tooling could be better for sure. When you are facing runtime exceptions, I would recommend to use the "analyse APK" function in Android Studio to be able to dig into the obfuscated classes. For compile errors I have nothing useful. Try and error, research and gut feeling tbh.
@darshans1190
@darshans1190 4 года назад
you need to exclude a few classes. The crash usually happens from an external library, Please check the documentation of the library which you have used and add an exception for the mentioned classes in Proguard rules file :)
@joaohenriquebusobortolotto1512
@joaohenriquebusobortolotto1512 4 года назад
But, should we use enums on Android?
@GabrielSouza-of7kt
@GabrielSouza-of7kt 4 года назад
Kotlin is a amazing language, is so nice to play it, but, is better then Dota 2?
@MukarrimAdam
@MukarrimAdam 4 года назад
OMG DOTA!!!!! (my day job is kotlin - night job Dota XD)
@AdnaanAhmedZohran
@AdnaanAhmedZohran 4 года назад
lol.... saving dota2 with memes. never thought I'll see it here.
@Aayein_YaYaYa
@Aayein_YaYaYa 4 года назад
Please upload one video on Kotlin Roadmap 2020. How to become master at KOTLIN with industry requirements and resources (open source) to use.
@SnoopyDoofie
@SnoopyDoofie 3 года назад
It's a shame that Kotlin doesn't have a light weight enum.
@swiftkeyyt911
@swiftkeyyt911 4 года назад
TOP
@rushimithagare2598
@rushimithagare2598 4 года назад
Very confusing for amateur coder like me
@aitortilla5128
@aitortilla5128 4 года назад
When you are a beginner sometimes you have to go through many different websites and videos in order to learn it.
@rushimithagare2598
@rushimithagare2598 4 года назад
@@aitortilla5128 Agreed any Recommendations
@kitpeddler
@kitpeddler 4 года назад
For an amateur coder, this stuff doesn't really matter. He's just talking about a small, optional optimization you can run on your code to make your app run just a little bit faster. Really not worth worrying about things like this until you start making large, performance-intensive apps.
@barteksvaberg4403
@barteksvaberg4403 4 года назад
Still, small bits and pieces of this kind of information gets stored somewhere in the back of your head until one day you go: Hey! I remember someone mentioning this. It's happened som many times to me. Enjoy being an amateur, by the way. It is a fantastic state of exploring new stuff all the time. Like being a child again. The more you learn the more you realize there's so much you don't know and that can sometimes be frustrating.
@SociedadAndroide
@SociedadAndroide 4 года назад
@@barteksvaberg4403 I think we are all amateurs every day of our life as programmers haha, we can have years of experience, projects, and so on, but there are always new things that we don't know and we need to explore, I think the word amateur describes a really big part of us since we are amateurs at first when we know how to code, how to use a library, and then when we master it we are confident to keep investigating, and this is why programming is really fun, we are always learning new things and new challenges everyday, is really nice :)
@DeLL116
@DeLL116 4 года назад
I love pork fried rice !
@coroutinedispatcher
@coroutinedispatcher 4 года назад
Gson users don't like this video
Далее
Suspend functions - Kotlin Vocabulary
9:03
Просмотров 50 тыс.
Object - Kotlin Vocabulary
7:53
Просмотров 32 тыс.
I Built 100 Houses And Gave Them Away!
09:36
Просмотров 54 млн
I SIMULATED THIS EURO 2024 GAME.. 🤯
00:13
Просмотров 1,7 млн
Shrinking your app with R8 (Android Dev Summit '19)
18:04
Every Kind of Class in Kotlin
10:44
Просмотров 7 тыс.
Delegating delegates - Kotlin Vocabulary
8:44
Просмотров 25 тыс.
The ABC of Coroutines - Kotlin Vocabulary
9:27
Просмотров 49 тыс.
Kotlin Enum class (with Demo Source Code)
11:05
Просмотров 12 тыс.