Enable android:largeHeap in Android 4, and disable it in Android 2.3

后端 未结 5 739
粉色の甜心
粉色の甜心 2020-12-07 23:09

Currently, I have a piece of code, which is designed to run both in Android 2.3 and 4+

The code will perform much better (Where it will not have OutOfMemory exceptio

相关标签:
5条回答
  • 2020-12-07 23:55

    You can also disable large heap in Honeycomb and enable it in ICS or JB. Just a little hacky or something. Here's what I tried.

    Before we proceed, change your Build target to Honeycomb, Jelly Bean or ICS so we can put android:largeHeap attribute. Also, you can set android:minSdkVersion to API 10.

    Android API 10 doesn't support large heap.

    1. Create a folder values-v14 in res folder
    2. I created bools.xml in values-v14
    3. Put this value in bools.xml of values-v14

    <bool name="largeheap">true</bool>

    boolean value for values > bools.xml or values-[API VERSION] > bools.xml to disable large-heap in different API Version or by default.

    <bool name="largeheap">false</bool>
    

    Change the value of android:largeHeap to @bool/largeheap instead of hardcoded true or false

    <application
            android:largeHeap="@bool/largeheap"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher">
                    ....
    </application>
    

    I tested this code by making a memory leak application or just load a Huge bitmaps, and, its working!

    Good Luck!

    0 讨论(0)
  • 2020-12-07 23:56
    1. 1.Build project with target Android 3.0 (API 11) or above. (Project properties - Android - Project Build Target - select above API level 11)

    2. in Manifest file, Change the uses-sdk value as following

      < uses-sdk android:minSdkVersion="10" android:targetSdkVersion="11" />

    For prior versions of Android 3.0, you can use VMRuntime class for memory manipulations.

    0 讨论(0)
  • 2020-12-07 23:57

    Please change project properties:

    In project Properties -> Android -> Project Build Target -> Google API, API level 19 (or any other you need) :)

    So your can leave your minimum SDK option without change, for example 8 :)

    0 讨论(0)
  • 2020-12-08 00:00

    Keep the android:largeHeap="true" attribute in your AndroidManifest.xml. This should be ignored for versions that don't support it. Then, to support older versions, set the heap size using the VMRuntime class (via reflection, if necessary).

    More on this topic: How to increase heap size of an android application?

    0 讨论(0)
  • 2020-12-08 00:05

    Seems that you have incorrect target sdk in project.properties file, check it out and change target to your AndroidManifest`s targetSdk (15) and rebuild project.

    project.properties

    # Project target.
    target=15
    

    P.S. I tryed add android:largeHeap to my Project (minSdk = 7, targetSdk = 17) compiled and run normally on all Android versions.

    0 讨论(0)
提交回复
热议问题