You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Studio示例项目无法编译运行,配置时无法选择模块的解决

Hey there, let's break down why you're hitting this issue and how to fix it step by step—this is a common snag I’ve helped devs work through plenty of times!

可能的原因及对应解决方案

1. Gradle同步未完成或失败

  • 原因:Android Studio often fails to fully sync Gradle when opening a sample project, especially if the project’s Gradle version doesn’t match what your local Studio supports. This silent failure keeps Studio from recognizing your app module.
  • Fixes:
    • First, click the Sync Project with Gradle Files button (the elephant icon in the top toolbar) and wait for the process to finish. Keep an eye on the Build tab for error messages.
    • If you see a Gradle version mismatch error, open your project’s root build.gradle (or settings.gradle.kts for Kotlin DSL) and update the Android Gradle Plugin version to one your Studio supports. You can find compatible versions in File > Project Structure > Project.
    • If sync still doesn’t work, try File > Invalidate Caches... > Invalidate and Restart. This clears Studio’s cached data and fixes most weird module-recognition glitches.

2. The module isn’t marked as an Android Application

  • 原因:Some sample projects might have misconfigured modules—if a folder isn’t tagged as an Android Application module, Studio won’t let you select it for run configurations.
  • Fixes:
    • Right-click your app module (usually the app folder) and select Open Module Settings (or press F4).
    • In the left pane, select your module. On the right, make sure:
      • Module SDK is set to an installed Android SDK.
      • Module Type is set to Android Application.
    • Click OK, then re-sync Gradle.

3. SDK or emulator version mismatch

  • 原因:Even if your emulator launches, a mismatch between your project’s compile/target SDK version and the emulator’s Android version can break compilation and module detection.
  • Fixes:
    • Go to File > Project Structure > Modules to check your module’s Compile SDK Version and Target SDK Version.
    • Open Tools > SDK Manager and confirm the corresponding SDK Platform is installed.
    • Create a new emulator with a system version that matches (or is compatible with) your project’s SDK versions—this avoids version-related compilation errors.

4. Incomplete or corrupted project files

  • 原因:Sometimes sample projects download with missing files, or importing them incorrectly can lead to broken module structures.
  • Fixes:
    • Re-download the sample project to ensure all files are intact.
    • Import it properly via File > New > Import Project, selecting the root folder that contains settings.gradle (or settings.gradle.kts). This ensures Studio picks up all modules correctly.

内容的提问来源于stack exchange,提问作者vico

火山引擎 最新活动