Search code examples
javaandroideclipsesamsung-mobile

What could cause Android's DisplayMetrics.densityDpi to differ between Release and Debug builds?


I am debugging an issue on the Samsung Galaxy Note 5 Android device running 5.1.1

My issue is related to DisplayMetrics.densityDpi which is returning different values under Release and Debug builds (from Eclipse.)

Under Debug via Eclipse, DisplayMetrics.densityDpi returns 560 while a release build returns 420.

I am completely stumped by this and I've never seen a device do this. My suspicion in that I have an issue in my AndroidManifest.xml ...

<uses-sdk
    android:minSdkVersion="4"
    android:targetSdkVersion="22" />

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />

The code I am using to check is:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    if (metrics.densityDpi == 420) {
        Log.d(LOGTAG, "420 DPI");
    } else if (metrics.densityDpi == 560) {
        Log.d(LOGTAG, "560 DPI");
    }

The APK, both release and debug, are identical when compared using aapt dump badging but the output is curious.

        package: name='com.corytrese.games.startraders' versionCode='360' versionName='5.9.35'
      sdkVersion:'4'
      targetSdkVersion:'22'
      application-label:'Star Traders RPG'
      application-icon-120:'res/drawable-ldpi/icon.png'
      application-icon-160:'res/drawable-mdpi/icon.png'
      application-icon-213:'res/drawable-hdpi/icon.png'
      application-icon-240:'res/drawable-hdpi/icon.png'
      application-icon-320:'res/drawable-xhdpi/icon.png'
      application-icon-480:'res/drawable-xxhdpi/icon.png'
      application-icon-640:'res/drawable-xxhdpi/icon.png'
      application-icon-65535:'res/drawable-xxhdpi/icon.png'
      application: label='Star Traders RPG' icon='res/drawable-mdpi/icon.png'
      application-debuggable
      launchable-activity: name='com.corytrese.games.startraders.menu.MainMenu'  label='Star Traders RPG' icon=''
      uses-feature:'android.hardware.touchscreen'
      uses-implied-feature:'android.hardware.touchscreen','assumed you require a touch screen unless explicitly made optional'

      main
      other-activities
      supports-screens: 'small' 'normal' 'large' 'xlarge'
      supports-any-density: 'true'
      locales: '--_--' 'ca' 'da' 'fa' 'ja' 'nb' 'de' 'af' 'bg' 'th' 'fi' 'hi' 'vi' 'sk' 'uk' 'el' 'nl' 'pl' 'sl' 'tl' 'am' 'in
      ' 'ko' 'ro' 'ar' 'fr' 'hr' 'sr' 'tr' 'cs' 'es' 'it' 'lt' 'pt' 'hu' 'ru' 'zu' 'lv' 'sv' 'iw' 'sw' 'fr_CA' 'lo_LA' 'en_GB'
       'bn_BD' 'et_EE' 'ka_GE' 'ky_KG' 'km_KH' 'zh_HK' 'si_LK' 'mk_MK' 'ur_PK' 'hy_AM' 'my_MM' 'zh_CN' 'ta_IN' 'te_IN' 'ml_IN'
       'en_IN' 'kn_IN' 'mr_IN' 'mn_MN' 'ne_NP' 'gl_ES' 'eu_ES' 'is_IS' 'es_US' 'pt_PT' 'zh_TW' 'ms_MY' 'kk_KZ' 'uz_UZ'
      densities: '120' '160' '213' '240' '320' '480' '640' '65535' 'gl_ES' 'eu_ES' 'is_IS' 'es_US' 'pt_PT' 'zh_TW' 'ms_MY' 'kk_KZ' 'uz_UZ'

densities: '120' '160' '213' '240' '320' '480' '640' '65535'

I do not see 560 or 420 listed, nor do I see my xxxhdpi drawables being referenced.


Solution

  • This is caused by bugs in Android, whic seem specific to the stock OS image provided with the Note 5. I will report the issue to Google bug tracker.

    While the issue is unfortunate, the issue can be worked around by testing with a signed APK with android:debuggable="true". That way you can see the device's 'normal' DPI for applications instead of the buggy one one it assigns to debug builds.

    Unfortunately, this issue impacts all types of debug builds that I tested: Android Studio, Eclipse or Ant. None of them are capable of producing a workaround for the issue on the Note 5.