News for hackers. Stuff that matters.
Re:React Native at Instagram by 40acres on Monday February 06, 2017 @04:42PM (#13584397)
I didn't know what single/multi-dex means so i looked it up.

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

Do Android apps regularly encompass over 65.3k methods? I assume a majority of these would be framework type functionality, but it really seems that a multi-dex product would be near impossible to maintain.

Re:React Native at Instagram by bitmapbrother on Monday February 06, 2017 @10:20PM (#13586701)
This is one of those Android design decisions that Google probably wishes they could get to do over again. The DEX header file format allocates only 2 bytes to hold the number of methods the app can reference. This was a very short sighted decision that has come back to bite them repeatedly. It would be interesting to know why they only allocated 2 bytes instead of 4 which would have given them the ability to reference over 4 billion methods. If their reasoning was for cost saving measures than it was a pretty silly decision. There has been some talk about revising the DEX header file format to correct some of the early design mistakes, but I'm not sure when they'll get around to it.
Re:React Native at Instagram by hota_mazi on Tuesday February 07, 2017 @12:14AM (#13587230)
> This is one of those Android design decisions that Google probably wishes they could get to do over again.

Doubtful. This decision is what allowed Android to perform reasonably even in the early days circa 2008. Without such a restriction, it's possible that Android couldn't have competed with iOS on performance and die as a consequence.

It's easy to make fun of technical decisions made ten years ago, try to put things in perspective.

Besides, even today, this 65k limit is barely a minor annoyance that only a very tiny percentage of Android apps ever hit (and there are easy workarounds).

Re:React Native at Instagram by wasyl on Tuesday February 07, 2017 @01:44PM (#13591522)
> this 65k limit is barely a minor annoyance that only a very tiny percentage of Android apps ever hit

This is not true, you can easily hit this limit because of a dependency that you _need_ that pulls e.g. Gauva, of when you use 2 or 3 libraries from Play Services, which are huge

> (and there are easy workarounds)

Easy maybe, but the issue is still more than _a minor annoyance_ imo

Re:React Native at Instagram by crystalPalace on Tuesday February 07, 2017 @05:01PM (#13593293)
I've hit the limit when pulling in all Play services. The key is to only pull in what you will be using. Many tutorials(including one from a third party analytics API) are a bit lazy and just have you import Play in its entirety.
Re:React Native at Instagram by kuschku on Monday February 06, 2017 @10:32PM (#13586765)
It's not that easy, and not just one field.

DEX uses a method table, listing every method with an ID, and using in the rest of the file only that ID to refer to it.

This ID is limited to 2 bytes.

Obviously, thats an issue, but short of using varint or straightaway uint32 there's few alternatives. And those use either more CPU or memory.

Re:React Native at Instagram by bitmapbrother on Tuesday February 07, 2017 @12:09AM (#13587215)
Thanks for the correction. Here's a transcript from the Android Developer podcast, courtesy of http://blog.osom.info/2014/10/multi-dex-to-rescue-from-infam..., talking about the issue with an Android engineer:

>Tor: So [regarding] the infamous 64k method [issue].. I understand that it is a dex file format limitation.. which is a short integer. Do you have plans to address this somehow, by either change the dex format or some other way?

>Anwar: So we've talked about revving DEX, so that limitation doesn't exist anymore. And there's a couple of reasons we haven't done that: one, there're other things we would like to do better as well, including supporting new language features. The other reason is - it doesn't help us with devices still in the field. It's hard to go and say "by the way, we're going to upgrade your runtime.." So what do we do in order to address that? We will do the DEX byte code [change], but I think there's sort of building block that we needed first - what we're calling multi-DEX. And the idea is.. here's something people were doing - they were breaking up their files into multiple DEX files (each of which exceeds the 64k limit), the main classes in DEX file could see the classes and use them in sort of references rather than loading those classes and having limitations in how you can use them. They will go and use reflection to find the boot class path and modify it to include their secondary DEX files. So this is kind of hack, but a necessary one for them.

>What we're doing in L is in runtime we will have a native support for multi-DEX. All your DEX files that you have in your app will get dexopt-ed or compiled by us and collapsed into a single .oat file which is a binary file we generate.. ..And we have a support library on top of that.. ..if you have multi-DEX it will work on older releases of Android back to Ice Cream Sandwich.. will work on Gingerbread too, but we're only validating back to ICS. So once you have that, than that free's you in the future to do the DEX byte code change, and then something that partitions it and runs on the existing [devices]..

One interesting note is that he mentions that they'll hopefully have the DEX revision by M. Well, we're on N so it looks like they missed that target date. Perhaps O will finally get the long awaited DEX file format revision.

Re:React Native at Instagram by pjmlp on Tuesday February 07, 2017 @02:50AM (#13587839)
Available on 0.x% of devices, thanks to them not forcing OEMs to update as part of Play Store contract.
Re:React Native at Instagram by bitmapbrother on Tuesday February 07, 2017 @03:25AM (#13587965)
Yeah, why they don't enforce this is baffling. I can give them a break for letting their OEM's take their time with OS updates, but not requiring their OEM's to issue security patches is negligent.
Re:React Native at Instagram by izacus on Tuesday February 07, 2017 @03:54AM (#13588087)
Microsoft enforced that and OEMs decided they'd rather build Android phones than Windows Phone ones. The rest is history.
Re:React Native at Instagram by pjmlp on Tuesday February 07, 2017 @04:03AM (#13588120)
I think having an OS available for free was more relevant.
Re:React Native at Instagram by MichaelGG on Tuesday February 07, 2017 @04:25AM (#13588202)
Yeah, it's not like Samsung can go make iOS phones. And I highly doubt any of these hardware OEMs could possibly pull off a software project that doesn't suck, let alone a whole OS and dev ecosystem.
Re:React Native at Instagram by izacus on Tuesday February 07, 2017 @05:23AM (#13588408)
Samsung already releases phones running Tizen to have a side bet against Android. The quality of software is not very important when your only competitor builds phones that are significantly more expensive.
Re:React Native at Instagram by pjmlp on Tuesday February 07, 2017 @05:32AM (#13588440)
Tizen is a joke for app developers, it was already rebooted so many times, no one serious would spend a second trying to target the platform.

1 - Meego reborn as Tizen

2 - Tizen gets the Bada C++ SDK, with its Symbian C++ like flavour

3 - Enlightment guys join Tizen

4 - C++ SDK gets kicked out and replaced by Enlightment C libraries

5 - Developers complain that Tizen 2.3 drops C++ and is a C only experience

6 - Samsung announces support for .NET Core as high level native Tizen tooling

Really, what SDK are they going to release next?!

Re:React Native at Instagram by realharo on Tuesday February 07, 2017 @06:16AM (#13588635)
There was also a period of only allowing "HTML5 apps" (during the jQuery era no less) if I remember it correctly.
Re:React Native at Instagram by pjmlp on Tuesday February 07, 2017 @06:30AM (#13588683)
It is still the case when targeting Tizen TV devices.
Re:React Native at Instagram by kartickv on Tuesday February 07, 2017 @12:09AM (#13587216)
So why can't Google create a new version of the dex format, update Android Studio to generate it, and update the store to accept and distribute it to new devices? Seems easy enough for Google to be able to do.
Re:React Native at Instagram by pjmlp on Tuesday February 07, 2017 @02:50AM (#13587832)
Because of their clever idea of not forcing OEMs to update devices, almost no one will get that.
Re:React Native at Instagram by lmm on Tuesday February 07, 2017 @05:07AM (#13588357)
The best time to ship support for an updated format was five years ago. The second-best time is today.
Re:React Native at Instagram by kartickv on Tuesday February 07, 2017 @03:29AM (#13587982)
New phones will get it. Surely you're not arguing that Google shouldn't make any improvements to core Android because of the update situation?
Re:React Native at Instagram by pjmlp on Tuesday February 07, 2017 @03:59AM (#13588107)
No, I am arguing that Google doesn't have the guts to improve the situation.

Most people around the world are on pre-paid, not contracts, and they only update their devices when the current one dies.

Even now, the majority of the devices being sold on the 300 € border are Android 5 or 6, with no planned upgrade to 7.

Re:React Native at Instagram by shortstuffsushi on Monday February 06, 2017 @11:21PM (#13586990)
You make it sound like it is as simple as that, only a method table instead of a header field :)

That said, you mention using a varint or uint32 which "use more CPU or memory" -- 16 bits vs 32 bits in applications that use megabytes seems trivial. Can you explain why that's a concern?

Re:React Native at Instagram by kuschku on Tuesday February 07, 2017 @07:15AM (#13588828)
It's not a concern anymore, but when Android was created, the average application was less than 20kiB. Back then, resource restrictions were a lot more problematic.
Re:React Native at Instagram by myroon5 on Monday February 06, 2017 @04:48PM (#13584477)
Usually most of the methods are from dependencies. Interestingly enough, some of Google's own libraries like Guava and Google Play Services are commonly what pushes apps over the limit.
Re:React Native at Instagram by otachack on Monday February 06, 2017 @09:30PM (#13586465)
True, but Google Play Services can now be cherry picked so you can depend only on what you want to use rather than a blanket dependency. See table one here: https://developers.google.com/android/guides/setup
Re:React Native at Instagram by aeturnum on Monday February 06, 2017 @04:46PM (#13584455)
Iirc, the reason you end up hitting the limit for 65k methods is that each library tends to keep independent copies of its dependencies. This simplifies dependency control, but ends up mushrooming quickly.
Re:React Native at Instagram by unsoundInput on Monday February 06, 2017 @04:56PM (#13584570)
That is actually not true. Afaik multiversioning of dependencies is not actually supported by build systems without manual effort (not counting major versions in non-conflicting namespaces). Problem is really more in fat libraries and "unnecessary" (getters, setters) and synthetic (e.g. access to private methods from inner classes) methods that are usually not optimized away, especially in debug builds.
Re:React Native at Instagram by ashark on Monday February 06, 2017 @05:51PM (#13585078)
I've definitely included a library and then seen (otherlibrary).(library you included) as an option in autocomplete, fairly often. Happens a lot with utility-level things like okhttp.
Re:React Native at Instagram by zcoyle on Monday February 06, 2017 @08:17PM (#13586076)
I've seen this with okhttp as well.
Re:React Native at Instagram by unsoundInput on Tuesday February 07, 2017 @06:10AM (#13588613)
I can't find the okhttp case you reference, but retrofit has a bunch of extra adapter artifacts that add an external library as a transient dependency to your project.

The code for the binding classes lives in retrofit.adapter.{guava|rxjava|…} [0] but the respective library still lives in its usual package. [1]

If that weren't the case you could A) not manually provide a minor version via your dependencies block in your build script and B) would have interop problems between libraries and between a library and your code as the same interface copied to a different packages is not the same interface for Java.

[0] https://github.com/square/retrofit/tree/master/retrofit-adap...

[1] https://github.com/square/retrofit/blob/master/retrofit-adap...

Re:React Native at Instagram by habosa on Monday February 06, 2017 @05:25PM (#13584861)
Almost every Android app that includes the most common dependencies (Android support lib, Google Play services, etc) will end up being multi-dex.

This used to be a massive pain but lately the Android Studio / gradle multidex tooling has gotten so good that I don't notice anymore.

Re:React Native at Instagram by krschultz on Monday February 06, 2017 @06:19PM (#13585278)
You still incur a startup penalty with multi-dex, it takes noticeably longer than a single dex, even if you are only marginally into that 2nd dex.
Re:React Native at Instagram by whatever_dude on Tuesday February 07, 2017 @10:16AM (#13589854)
> Do Android apps regularly encompass over 65.3k methods?

No. I've built huge applications that didn't come nowhere near that. I don't dispute some apps might run into that, but it's not a regular occurrence at all.

The real problem here is that it depends on what kind of libraries you're using. If you're liberal with adding a lot of all-purpose libraries when you only need a feature or two, you can run into that limit fast.

For example, look at the comparison table ("Replacing existing libraries") here:

http://jeroenmols.com/blog/2016/05/06/methodcount/

If you just need to load an image, do you want a library with 12k methods (!) or 800?

Re:React Native at Instagram by ronocod on Tuesday February 07, 2017 @12:16PM (#13590823)
It depends, Picasso is smallest but doesn't support GIFs. Glid is larger and does support GIFs. Fresco is larger again but has more advanced memory management and better transition handling.
Re:React Native at Instagram by hota_mazi on Tuesday February 07, 2017 @12:10AM (#13587220)
There is no practical difference between single and multidex. Recent versions of Android are even better at creating these dex files effectively so users won't even notice the difference.

Most of these methods are typically contributed by dependencies, not by the app itself.

Re:React Native at Instagram by therealasdf on Tuesday February 07, 2017 @03:13AM (#13587915)
Exactly, the only difference developers might notice is slower build times.