display.getRealMetrics returns different results each time but works correctly if I change the manifest package name!!??

385 views
Skip to first unread message

Chanon Sajjamanochai

unread,
Jun 26, 2016, 10:46:33 PM6/26/16
to android-ndk
Hello, I am at a dead end here and don't know what else I can do.

I've created a simple NativeActivity application that does:


public class MainNativeActivity extends NativeActivity {
@Override
protected void onStart() {
super.onStart();

Point size = new Point();
//Display display = getWindowManager().getDefaultDisplay();
Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
display.getSize(size);
Log.d(TAG, "GET XXXXXXXXXXXXXXXXXX size is " + size.x + " " + size.y);


DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Log.d(TAG, "GET XXXXXXXXXXXX2 metrics size is " + metrics.widthPixels + " " + metrics.heightPixels + " density " + metrics.density + " scaled density " + metrics.scaledDensity);

display.getRealSize(size);
Log.d(TAG, "GET XXXXXXXXXXXXXXXXXX real size is " + size.x + " " + size.y);

DisplayMetrics metrics1 = new DisplayMetrics();
display.getRealMetrics(metrics1);
Log.d(TAG, "GET XXXXXXXXXXXX2 REAL  metrics size is " + metrics1.widthPixels + " " + metrics1.heightPixels + " density " + metrics1.density + " scaled density " + metrics1.scaledDensity);
}

There is no native code running just an empty ANativeActivity_onCreate (I commented everything out to determine what is causing the problem).

If I use package name com.mycompany.mypreviouspublishedapp in the manifest and gradle files, then the result is it returns the wrong resolution most of the time. Sometimes it gets the right resolution. Basically for a Galaxy S6 that has a resolution of 1440 x 2560 it returns 1080 x 1920 most of the time.

For a Galaxy Tab S2 that has a resolution of 1536 x 2048 it returns 1152 x 1536 most of the time and 1024 x 768 sometimes.

I tried to figure out what I was doing wrong, so I tried starting with a barebones project with a random package name and saw that it was working correctly. Then I tried changing my original project one part a time commenting out things and making it the same as the barebones project. Until finally the only thing different was the package name. And so I tried changing that and IT WORKED .. on the S6 it showed 1440 x 2560 every time.

Then in the barebones project I tried changing the package name to com.mycompany.mypreviouspublishedapp and it starts giving different results again.

Now, I can't fix it by publishing with a new package name (even if I'd go with that an absurd solution) because I'm trying to release an update to an existing published app.

The app/game was previously published with Google Play Games services, so I don't know if it is related to that.

Anyways... if anyone has any ideas it would be greatly appreciated.

J Decker

unread,
Jun 26, 2016, 10:52:49 PM6/26/16
to andro...@googlegroups.com
try including targeting higher android platform levels... I found that if I targeted too old I'd get a 'fake' surface... since the native window is really a OpenGL surface, it just stretches the one it tells you (and yields non pixel perfect results)

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/4302543a-bf16-406e-9336-207a10150fea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chanon Sajjamanochai

unread,
Jun 27, 2016, 11:43:20 PM6/27/16
to android-ndk
Hi J Decker,

Thank you for the suggestion. I saw your previous post about a similar issue, but I've done that, even increasing the minSdkVersion to 21 and it still has the same behavior.

This is driving me a bit crazy.

Chanon Sajjamanochai

unread,
Jun 27, 2016, 11:43:20 PM6/27/16
to android-ndk
One thing I notice is that, if I uninstall the app before running/reinstalling it through Android Studio it gives the correct resolution the first time. Then if I close the app and open it again it will return a smaller resolution and will keep doing that every time afterwards.

However if I change the manifest package name, it will return the correct resolution always.

Adventures on the Road

unread,
Jul 24, 2018, 4:47:21 PM7/24/18
to android-ndk
This exact problem plagued me as well.

My project has a number of flavors and due to that the build time had slowed as the complexities increased. In the pursuit of a speedier build I had added the line - org.gradle.configureondemand=true to gradle.properties
Removing that seemed to have solved my problem.

I did also recently File | Invalidate Caches/Restart and unchecked File | Settings | Build Execution Deployment |Instant Run

But I believe the removing configureondemand=true was the solution to my problem.

Adventures on the Road

unread,
Jul 25, 2018, 4:47:51 PM7/25/18
to android-ndk
Well, I thought that I had solved it as I stated in my previous reply but after a day of testing on multiple devices, I have come back to the same issue again.

On my Samsung S6, the first run, and sometimes subsequent runs, the display metrics show 1440 x 2560 but then on some future run reverts to always showing 1080 x 1920. I have not changed my display in anyway. The screenDensity also drops to show a lower number but again I have made no display changes.

If I uninstall the application and reinstall, again it runs for the first time giving the correct metrics.

I do have multiple flavors in this project but nothing about the flavors changes the display but other flavors which have different package names, as was stated in the initial question here, seem to work fine.

This is the most perplexing development issue that I have ever come across.

Is it potentially the Android Studio IDE making a mistake in the build due to multiple flavors? Perhaps it is actually the Android OS returning incorrect metrics.

I am eager to solve this issue as I have an update to publish which requires correct metrics values.
Reply all
Reply to author
Forward
0 new messages