Windows项目移至MacBook后Android应用无法运行,求解决方案
Hey there! As a fellow Android dev, I know how frustrating it is when a project works perfectly on one system but breaks after migration—especially when you're just starting out. Let's break down how to fix this KotlinNullPointerException that's causing your Gradle configuration error:
1. Clear Caches & Resync Gradle (First Step!)
Cross-system migrations often leave behind corrupted cache files that mess up Gradle. Let's wipe them clean:
- Click
Fileat the top of Android Studio → selectInvalidate Caches... - Check the box for
Invalidate and Restart, then confirm. Wait for Studio to restart fully. - Once restarted, click the elephant icon (
Sync Project with Gradle Files) in the top-right corner to resync your project.
2. Check Kotlin Version Configuration
A common culprit is mismatched or incorrectly referenced Kotlin versions between systems. Let's verify:
- Open your project-level
build.gradlefile (the one in the root folder, not theappsubfolder) - Make sure the
ext.kotlin_versionis set correctly and matches what you used on Windows. Example:buildscript { ext.kotlin_version = "1.8.22" // Use the same version as your Windows project repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.4.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } - Then open your app-level
build.gradleand confirm the Kotlin plugin is applied correctly:plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' // No typos here! }
3. Fix Case Sensitivity Issues (Critical for Mac!)
Windows ignores file name case, but Mac is case-sensitive. This can cause Gradle to fail finding files, leading to null pointers:
- Double-check all file names and their references in your code. For example:
- If you have a file named
UserProfile.kton Windows, make sure it's not renamed touserprofile.kton Mac. - Verify imports in Kotlin files, layout references, and resource names all match the actual file case exactly.
- If you have a file named
4. Verify Gradle Wrapper Version
Ensure your Mac is using the same Gradle version as your Windows project:
- Open
gradle/wrapper/gradle-wrapper.properties - Check the
distributionUrlline—make sure it's identical to what was in your Windows project. Example:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip - If it's different, update it and resync the project.
5. Fix Android SDK Path in local.properties
The local.properties file (auto-generated on Windows) might have an invalid SDK path for Mac:
- Open the
local.propertiesfile in your project root. If it doesn't exist, create it. - Set the correct SDK path for Mac. It usually looks like this:
Replacesdk.dir=/Users/YourMacUsername/Library/Android/sdkYourMacUsernamewith your actual Mac user name.
6. Last Resort: Re-Import the Project
If none of the above works, a fresh import can resolve hidden configuration issues:
- Close Android Studio completely.
- Delete the
.gradle,.ideafolders, and theapp/buildfolder from your project directory. - Reopen Android Studio, select
Import Project, and choose your project's rootbuild.gradlefile. Let Studio reconfigure everything from scratch.
内容的提问来源于stack exchange,提问作者wilrus

