Running apps containing large amount of code

给你一囗甜甜゛ 提交于 2019-11-29 02:58:16

问题


Background

It seems some old Android OSs (and maybe even the newest ones) have a limitation on the amount of code each app can hold.

As I've found, the limitation is on a buffer called "LinearAlloc" .

On 2.2 or 2.3 it's about 5-8 MB , and I think it's 16 or more on others.

The problem

If you have a too large code (and apps can reach this state), you won't be able to install the app at all on older devices, getting the next error (also reported here) :

Installation error: INSTALL_FAILED_DEXOPT
Please check logcat output for more details.
Launch canceled!

What I've found

One solution is to just remove as much code and libraries as possible, but on some huge projects such a thing is very hard to do.

I've found the next links talking about how Facebook solved this, by somehow increasing the limit:

  • http://www.slashgear.com/how-facebook-fixed-its-gingerbread-dalvik-problem-04272478/
  • http://arstechnica.com/business/2013/03/how-facebook-dug-deep-within-android-to-fix-its-mobile-app/
  • https://www.facebook.com/notes/facebook-engineering/under-the-hood-dalvik-patch-for-facebook-for-android/10151345597798920

Also, Google has posted how to solve it by loading code dynamically :

http://android-developers.blogspot.co.il/2011/07/custom-class-loading-in-dalvik.html

The question

How did Facebook do it?

Is it possible to overcome this in other ways too?

Is there any free library that increases/removes the limitation of this buffer?

What is the limitation on newer Android versions, if there is any?

How do other huge apps (and games) handle this issue? Do they put their code into C/C++?

Would loading the dex files dynamically solve this?


回答1:


The limit is the total number of method references:

  • https://code.google.com/p/android/issues/detail?id=7147#c6
  • https://code.google.com/p/android/issues/detail?id=20814#c6

A middle ground between doing nothing and the multi-dex approach described in the FB/Google articles is to use a tool like ProGuard to remove references to unused code at the Java level. See:

  • http://proguard.sourceforge.net/
  • http://developer.android.com/tools/help/proguard.html



回答2:


There is a new solution, made by Google:

  • https://plus.google.com/+IanLake/posts/JW9x4pcB1rj?utm_source=Android%20Weekly&utm_campaign=59f1f4bf4d-Android_Weekly_125&utm_medium=email&utm_term=0_4eb677ad19-59f1f4bf4d-337848877
  • http://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

It seems all you have to do is any of the next things: - extend from "MultiDexApplication" instead of from "Application" - call MultiDex.install(context) in your application's attachBaseContext

But now I wonder:

  1. Is that really it?
  2. Does it have any issues ? Does it affect performance?
  3. How does it work?
  4. What should be done with ContentProvider, as it's getting called before Application gets initialized?
  5. The post says "gives you MultiDex support on all API 4+ devices (well, until v21, where you get this natively)" . Does it mean that from v21 it will be the default behavior, or just that the class will be built in and you won't need to use the support library's class ?
  6. Will this solution work on Eclipse too?


来源:https://stackoverflow.com/questions/21878324/running-apps-containing-large-amount-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!