<compatible-screens> in Android

北城以北 提交于 2019-12-03 14:32:05

It looks like you are attempting to restrict screen sizes to only handsets, and not tablets. It is difficult to discern from your question, but either way I think I can clear up the confusion.

When you declare <compatible-screens> in your manifest you must declare every screen configuration that you would like your app to be compatible with:

You must declare each one of these; any combination of size and density that you do not specify is considered a screen configuration with which your application is not compatible.

I suspect the 5.5+ inch phones you mention have a higher density than xhdpi; such as xxhdpi or xxxhdpi. These densities are omitted from the documentation (either because the documentation is outdated or otherwise incomplete) but are still relevant; they are documented on the <compatible-screens> page.

Therefore if you want your app to be compatible with higher density devices, you must include those densities in your <compatible-screens> element. But an easier method would be to use the <supports-screens> element instead. As per the documentation, the <supports-screens> element does not take density into account:

Note: Although you can also use the <compatible-screens> element for the reverse scenario (when your application is not compatible with smaller screens), it's easier if you instead use the <supports-screens> as discussed in the next section, because it doesn't require you to specify each screen density your application supports.

With this you can just specify the following in your manifest:

<supports-screens android:smallScreens="true"
                  android:normalScreens="true"
                  android:largeScreens="false"
                  android:xlargeScreens="false"
                  android:largestWidthLimitDp="840"/>

The largestWidthLimitDp attribute should not be necessary, but 840dp seems like a good limit for handsets based on the Material Design documentation for density breakpoints.

Otherwise, you can still use the <compatible-screens> tag if you would like more fine-tuned control over which devices your app is compatible with:

<compatible-screens>
    <!-- all small size screens -->
    <screen android:screenSize="small" android:screenDensity="ldpi" />
    <screen android:screenSize="small" android:screenDensity="mdpi" />
    <screen android:screenSize="small" android:screenDensity="hdpi" />
    <screen android:screenSize="small" android:screenDensity="xhdpi" />
    <screen android:screenSize="small" android:screenDensity="xxhdpi" />
    <screen android:screenSize="small" android:screenDensity="xxxhdpi" />
    <!-- all normal size screens -->
    <screen android:screenSize="normal" android:screenDensity="ldpi" />
    <screen android:screenSize="normal" android:screenDensity="mdpi" />
    <screen android:screenSize="normal" android:screenDensity="hdpi" />
    <screen android:screenSize="normal" android:screenDensity="xhdpi" />
    <screen android:screenSize="normal" android:screenDensity="xxhdpi" />
    <screen android:screenSize="normal" android:screenDensity="xxxhdpi" />
</compatible-screens>

EDIT (2016-12-21)

After Bryan's suggest to use build tool 25.0.1, I have no more build error with his solution:

<compatible-screens>
<!-- all small size screens -->
<screen android:screenSize="small" android:screenDensity="ldpi" />
<screen android:screenSize="small" android:screenDensity="mdpi" />
<screen android:screenSize="small" android:screenDensity="hdpi" />
<screen android:screenSize="small" android:screenDensity="xhdpi" />
<screen android:screenSize="small" android:screenDensity="xxhdpi" />
<screen android:screenSize="small" android:screenDensity="xxxhdpi" />
<!-- all normal size screens -->
<screen android:screenSize="normal" android:screenDensity="ldpi" />
<screen android:screenSize="normal" android:screenDensity="mdpi" />
<screen android:screenSize="normal" android:screenDensity="hdpi" />
<screen android:screenSize="normal" android:screenDensity="xhdpi" />
<screen android:screenSize="normal" android:screenDensity="xxhdpi" />
<screen android:screenSize="normal" android:screenDensity="xxxhdpi" />

BUT... with the new build some devices did not appear in the supported device list (Google Play console): Google Pixel, Nexus 5x, Google Pixel XL, Nexus 6, Nexus 6P.

That's why my new solution looks like this:

<!-- just handsets allowed-->
<compatible-screens>
    <!-- all small size screens -->
    <screen android:screenSize="small" android:screenDensity="ldpi" /> <!-- 120 -->
    <screen android:screenSize="small" android:screenDensity="mdpi" /> <!-- 160 -->
    <screen android:screenSize="small" android:screenDensity="hdpi" /> <!-- 240 -->
    <screen android:screenSize="small" android:screenDensity="280" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="xhdpi" /> <!-- 320 -->
    <screen android:screenSize="small" android:screenDensity="360" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="420" /> <!-- Workaround Google Pixel, Nexus 5x -->
    <screen android:screenSize="small" android:screenDensity="xxhdpi" /> <!-- 480 -->
    <screen android:screenSize="small" android:screenDensity="560" /> <!-- Workaround Google Pixel XL, Nexus 6, Nexus 6P -->
    <screen android:screenSize="small" android:screenDensity="xxxhdpi" />  <!-- 640 -->
    <!-- all normal size screens -->
    <screen android:screenSize="normal" android:screenDensity="ldpi" /> <!-- 120 -->
    <screen android:screenSize="normal" android:screenDensity="mdpi" /> <!-- 160 -->
    <screen android:screenSize="normal" android:screenDensity="hdpi" /> <!-- 240 -->
    <screen android:screenSize="normal" android:screenDensity="280" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="xhdpi" />  <!-- 320 -->
    <screen android:screenSize="normal" android:screenDensity="360" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="420" /> <!-- Workaround Google Pixel, Nexus 5x -->
    <screen android:screenSize="normal" android:screenDensity="xxhdpi" /> <!-- 480 -->
    <screen android:screenSize="normal" android:screenDensity="560" /> <!-- Workaround Google Pixel XL, Nexus 6, Nexus 6P -->
    <screen android:screenSize="normal" android:screenDensity="xxxhdpi" /> <!-- 640 -->
</compatible-screens>

OLD:

According to Bryan's answer, my manifest snippet looks like this:

 <!-- just handsets allowed-->
<compatible-screens>
    <!-- all small size screens -->
    <screen android:screenSize="small" android:screenDensity="ldpi" /> <!-- approximately 120 dpi -->
    <screen android:screenSize="small" android:screenDensity="mdpi" /> <!-- approximately 160 dpi -->
    <screen android:screenSize="small" android:screenDensity="hdpi" /> <!-- approximately 240 dpi -->
    <screen android:screenSize="small" android:screenDensity="280" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="xhdpi"/> <!-- approximately 320 dpi -->
    <screen android:screenSize="small" android:screenDensity="360" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="420" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="480" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="560" /> <!-- Workaround -->
    <screen android:screenSize="small" android:screenDensity="640" /> <!-- Workaround -->
    <!-- all normal size screens -->
    <screen android:screenSize="normal" android:screenDensity="ldpi" /> <!-- approximately 120 dpi -->
    <screen android:screenSize="normal" android:screenDensity="mdpi" /> <!-- approximately 160 dpi -->
    <screen android:screenSize="normal" android:screenDensity="hdpi" /> <!-- approximately 240 dpi -->
    <screen android:screenSize="normal" android:screenDensity="280" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="xhdpi"/> <!-- approximately 320 dpi -->
    <screen android:screenSize="normal" android:screenDensity="360" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="420" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="480" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="560" /> <!-- Workaround -->
    <screen android:screenSize="normal" android:screenDensity="640" /> <!-- Workaround -->
</compatible-screens>

Could not use Bryan's solution because i got a compilation error: "AAPT: String types not allowed (at 'screenDensity' with value 'xxxhdpi')"

It works for (newer) devices like Google Pixel (2.6 * 160 dp = 416 dp -> 420dp -> explanation:) / Pixel XL (3.5 * 160 dp = 560 dp) or Samsung Galaxy S6 (4.0 * 160 dp = 640 dp). The dp-Values are described here: https://material.io/devices/

I think this works because the devices I mentioned above, appeared in "supported devices" list in Google Play console.

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