Android API19升级构建工具至27后出现Invalid Transition types异常求助
java.lang.IllegalArgumentException: Invalid Transition types on KitKat (API 19) Hey there, I’ve run into a similar snag after upgrading support libraries, so let’s break down what’s going on here.
Why this exception pops up
After upgrading your build tools and support libraries to version 27, you’re hitting a validation check that was either loose or unenforced in older library versions. The core issue is:
AutoTransitionis a composite transition (it bundlesFade,ChangeBounds, andChangeTransformby default) built for general fragment enter/exit animations.- On KitKat (API 19), the Shared Element Transition system is extremely limited—it doesn’t support composite transitions like
AutoTransition. Only single, shared-element-compatible transition types (likeChangeBounds,ChangeTransform, orChangeImageTransform) are allowed here.
Key KitKat Transition Changes to Know
Android 4.4 was the first release to introduce Shared Element Transitions, but it was a very basic implementation:
- Only a tiny subset of transition types work for shared elements (no composite transitions, no complex animations like
ExplodeorSlidefor shared elements). - The transition framework was still in its early stages, so many smooth behaviors from API 21+ don’t translate to KitKat.
- Later support library versions (like 27+) tightened up validation for these older APIs, which is why you’re seeing this exception now when you didn’t before.
The Solution
Replace the AutoTransition with a single, shared-element-compatible transition. For most use cases, ChangeBounds is the safest pick—it handles resizing and moving shared elements, which is the most common shared element scenario:
Fragment fragment = new Fragment(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Swap AutoTransition for a shared-element-friendly single transition Transition transition = new ChangeBounds(); fragment.setSharedElementEnterTransition(transition); } String backStateName = ...
If you need an effect similar to AutoTransition, you can try combining supported transitions manually (just note KitKat’s transition system can be finicky with combinations):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { TransitionSet transitionSet = new TransitionSet(); transitionSet.addTransition(new ChangeBounds()); transitionSet.addTransition(new Fade()); // Fade works for non-shared elements, test carefully fragment.setSharedElementEnterTransition(transitionSet); }
Alternatively, if shared element transitions aren’t critical for KitKat users, you can restrict complex transitions to API 21+ only:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Use AutoTransition or other complex transitions only on newer devices Transition transition = new AutoTransition(); fragment.setSharedElementEnterTransition(transition); }
内容的提问来源于stack exchange,提问作者Martynas

