0

I built a project with compileSdkVersion 29 and buildToolsVersion "29.0.0" . When I set minSdkVersion 21 and tried to run the project with Nexus S(API 21) and Nexus 5X (API 26). It did not work at all. Only when I run the project with the latest API(29), it works. Would you teach me how I should use minSdkVersion properly?

built.gradle(Module: app)

    android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"

    defaultConfig {
        applicationId ""
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 9
        versionName "1.0.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility java_version
        targetCompatibility java_version
    }
    }


    dependencies {
    implementation 'com.google.firebase:firebase-core:17.0.1'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.core:core-ktx:1.2.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'}

    apply plugin: 'com.google.gms.google-services'

built.gradle(Project)

    buildscript {
    ext.kotlin_version = '1.3.41'
    ext.java_version = JavaVersion.VERSION_1_8
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.google.gms:google-services:4.2.0'
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    }

    allprojects {
    repositories {
        google()
        jcenter()

    }
    }

task clean(type: Delete) {
    delete rootProject.buildDir
}

Error I got

 java.lang.OutOfMemoryError: Failed to allocate a 216000012 byte allocation with 1940394 free bytes and 125MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
    at android.content.res.Resources.loadDrawable(Resources.java:2330)
    at android.content.res.Resources.getDrawable(Resources.java:758)
    at android.content.Context.getDrawable(Context.java:402)
    at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:454)
    at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144)
    at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:132)
    at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
    at androidx.appcompat.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
    at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
    at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
    at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:187)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
    at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1397)
    at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1447)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:545)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.example.a36questions.MainActivity.onCreate(MainActivity.kt:15)
    at android.app.Activity.performCreate(Activity.java:5937)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

After I applied Hardik Bambhania's advise to the manifest file, the project started working with Nexus 5(API 26). But still the same error occurs with the device(API 21). The detail of the error is below.

    java.lang.OutOfMemoryError: Failed to allocate a 216000012 byte allocation with 1940586 free bytes and 125MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
        at android.content.res.Resources.loadDrawable(Resources.java:2330)
        at android.content.res.Resources.getDrawable(Resources.java:758)
        at android.content.Context.getDrawable(Context.java:402)
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:454)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:132)
        at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
        at androidx.appcompat.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
        at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:187)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1397)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1447)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:545)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.a36questions.MainActivity.onCreate(MainActivity.kt:15)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Kuya
  • 7,280
  • 4
  • 19
  • 31
Kotaro
  • 147
  • 2
  • 12
  • Read this topic it will help you. https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio][1] – Ahmet Yılmaz Jul 26 '19 at 09:04
  • 1
    Possible duplicate of [Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM](https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio) – Lingeshwaran Jul 26 '19 at 09:05
  • > Ahmet Yılmaz Thank you for the link. I am going to check it out. – Kotaro Jul 26 '19 at 10:26
  • >Lingeshwaran, thank you for pointing it out. When I successfully address this issue, I am going to delete this page. – Kotaro Jul 26 '19 at 10:27

2 Answers2

1

OutOfMemoryError is the most common problem occured in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

You can add below entities in your manifest file android:hardwareAccelerated="false" , android:largeHeap="true" it will work for some environment's.

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

Let me know if not working

Hardik Bambhania
  • 1,732
  • 15
  • 25
  • Thank you for the advise! It worked with the Nexus 5 emulator(API26). But the project still does not work with the Nexus S(API 21). The error was the same "OutOfMemoryError" But the numbers in the error was slightly different. Do you think there is any other approach I should try? – Kotaro Jul 26 '19 at 11:20
0

I found another cause. In my case, I accidentally used an oversized image(an image of over 2048 x 2048). When I resized it to a smaller image. The error disappeared.

Kotaro
  • 147
  • 2
  • 12