If I set minAPI to 21, do I need AppCompat?

2019-02-17 17:05发布

问题:

I decided to make my minimum API version 21 for Android, but does this mean it makes no sense to use the AppCompat anymore? Just use plain Material Design / no AppCompat / etc?

回答1:

No, you should always use AppCompat. This ensures that your app is already consistent across all API levels and you have access to all new APIs (such as using the Colored buttons, which was only introduced in API 23).

Many other libraries, such as the Design Support Library also require use of AppCompat.



回答2:

The one scenario where you need to use appcompat-v7 is if you want to have an action bar, and your minSdkVersion is below 11. At the moment, there is no commonly used and maintained alternative to appcompat-v7 for this.

Otherwise, appcompat-v7 is something to consider, but it is not a requirement, and there are definite costs for using it:

  • Increased fragility, with many widgets being replaced by subclasses. Your layout might call for an EditText, but AppCompatActivity will silently replace it with an AppCompatEditText, which extends EditText. In an ideal world, this would pose no problem. However, device manufacturers have had a history of messing with the standard widget implementations. Creating subclasses of standard widgets can trip over bugs introduced by the device manufacturers. I have run into this personally a lot with EditText specifically.

  • Increased app size. appcompat-v7 adds ~1MB to your APK size. As Google likes to point out, developers should aggressively try to reduce their APK size, as some users have to pay for bandwidth by the MB (so downloading an APK has cost), and some devices are pretty stingy with storage.

  • Mandated Material Design aesthetic. Google wants all apps to use Material Design, for their own political reasons. Material Design has its adherents and its detractors. Not all designers want to follow Material Design. appcompat-v7 may make it more difficult to implement non-Material Design designs, depending upon the desired deviations from Google's specification.

  • Visual dichotomy with pre-installed apps. Users of Android 4.x devices are used to the Holo theme, and many of the non-Google pre-installed apps will have Holo-based themes. For users who happen to make use of Google's apps, they will have been exposed to Material Design and may be used to it by now. And, of course, Android 5.0+ is ever-growing as a percentage of the Android device ecosystem. However, Material Design apps look out of place compared with the apps that Samsung, LG, SONY, HTC, etc. will have on their Android 4.x devices. It is unclear if Material Design is somehow so superior as to be worth the difference to the user on these devices.

Are any of these issues show-stoppers? No. So, if you want to use appcompat-v7, go right ahead. However, please understand that appcompat-v7 is a choice, one that should be made consciously and with intent.