Android新项目启用DataBinding报错:找不到ActivityCompatApi23类
Hey David, sorry you're hitting this roadblock while setting up DataBinding—let's walk through some fixes that usually resolve this missing class error. It almost always boils down to mismatched support library versions or configuration oversights, so let's start checking:
Align all support library versions with your compileSdk
Since your project usescompileSdkVersion 27, every support library dependency should use the same 27.x.x version. Mixing versions is a common culprit for missing class errors. Update your module-levelbuild.gradledependencies to look something like this:dependencies { implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:design:27.1.1' // Add any other support libraries you need, all on 27.x.x }Double-check that there are no stray support libraries using older or newer versions hiding in your dependencies.
Confirm DataBinding is properly enabled
It's easy to misplace this config—make sure the DataBinding toggle is in the right spot in your module'sbuild.gradle:android { compileSdkVersion 27 buildToolsVersion "27.0.3" flavorDimensions "" defaultConfig { minSdkVersion 19 targetSdkVersion 22 versionCode 24 versionName "1.0" } // Make sure this block exists and is enabled dataBinding { enabled = true } }Sync your project after verifying this to ensure the changes take effect.
Clean out build caches and rebuild
Android Studio's cached build files often cause weird class-not-found issues. Try these steps:- Go to
Build > Clean Project - Then run
Build > Rebuild Project - If that doesn't work, delete the
.gradlefolder in your project root, restart Android Studio, and sync again.
- Go to
Consider aligning targetSdkVersion with compileSdkVersion
YourtargetSdkVersion 22is older than yourcompileSdkVersion 27. While this is technically allowed, support libraries for newer compileSdk versions can reference classes that expect a newer targetSdk. Updating targetSdk to 27 can eliminate this mismatch:defaultConfig { minSdkVersion 19 targetSdkVersion 27 // Match compileSdk here versionCode 24 versionName "1.0" }Verify buildToolsVersion is installed
Head to Android Studio's SDK Manager (Tools > SDK Manager), go to the SDK Tools tab, and confirm that "Android SDK Build-Tools 27.0.3" is installed. If it's missing, install it and sync your project once more.
Give these steps a shot in order—most folks fix this error with the first two or three. Let me know if you're still stuck after trying all of them!
内容的提问来源于stack exchange,提问作者David

