Android Studio Flamingo 2022.2.1中找不到Empty-Compose-Activity模板怎么办?
我在学习Jetpack Compose时遇到了无法找到Empty-Compose-Activity模板的问题,我的Android Studio版本信息如下:
Android Studio Flamingo | 2022.2.1
Build #AI-222.4459.24.2221.9862592, built on March 31, 2023
Runtime version: 17.0.6+0-b2043.56-9586694 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 16
Registry:
external.system.auto.import.disabled=true
ide.text.editor.with.preview.show.floating.toolbar=false
gradle.version.catalogs.dynamic.support=trueNon-Bundled Plugins:
com.levinzonr.arch.jetpackcompose.plugin (0.5.0)
新项目模板页面截图:
以下是几种可行的解决办法:
检查Compose相关插件完整性
打开Android Studio的File > Settings > Plugins,搜索Jetpack Compose相关插件,确认所有Compose组件已启用并更新至最新版本。若有未安装项,点击Install后重启Studio。手动改造Empty Activity为Compose项目
若模板确实缺失,可先创建Empty Activity项目,再手动修改为Compose结构:- 打开Module级别的
build.gradle,添加Compose相关配置与依赖:plugins { id 'com.android.application' id 'kotlin-android' } android { compileSdk 34 defaultConfig { applicationId "com.example.yourapp" minSdk 21 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.4.3' // 需匹配你的Kotlin版本,建议使用Studio对应兼容版本 } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.10.1' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2' implementation 'androidx.activity:activity-compose:1.7.2' implementation platform('androidx.compose:compose-bom:2023.03.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' } - 删除
res/layout/activity_main.xml文件,修改MainActivity.kt为Compose入口:import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import com.example.yourapp.ui.theme.YourAppTheme class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { YourAppTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Greeting("Android") } } } } } @Composable fun Greeting(name: String, modifier: Modifier = Modifier) { Text( text = "Hello $name!", modifier = modifier ) } @Preview(showBackground = true) @Composable fun GreetingPreview() { YourAppTheme { Greeting("Android") } } - 点击Sync Now同步Gradle,等待依赖下载完成后即可运行。
- 打开Module级别的
重置模板缓存
关闭Android Studio,找到缓存目录(Windows默认路径:C:\Users\[你的用户名]\.AndroidStudioFlamingo\system\caches),删除caches文件夹后重启Studio,模板会重新加载。排查插件冲突
你安装的com.levinzonr.arch.jetpackcompose.plugin可能与系统模板冲突,尝试暂时禁用该插件,重启Studio后查看模板是否恢复。
内容的提问来源于stack exchange,提问作者VictorChee

