使用Relay导入Figma设计至Android Studio遇compose引用未解析错误
解决Relay导入Figma设计后“Unresolved reference: compose in RelayBaseComposable”错误
这个错误的核心原因是Relay依赖Jetpack Compose,但你的项目未配置Compose开发环境,导致无法识别compose相关引用。按以下步骤修改项目配置即可解决:
1. 在Android配置块中启用Compose并添加编译选项
在android代码块内新增buildFeatures和composeOptions配置:
android { compileSdk 32 // 新增:启用Compose功能 buildFeatures { compose true } // 新增:配置Compose编译版本(需与Kotlin版本匹配,见下方说明) composeOptions { kotlinCompilerExtensionVersion = "1.3.2" } sourceSets { main { assets { srcDir('src/main/ui-packages') } } } // 其余原有配置保持不变... }
注意:
kotlinCompilerExtensionVersion必须与项目使用的Kotlin版本匹配,对应关系参考:
- Kotlin 1.7.20 → Compose Compiler 1.3.2
- Kotlin 1.8.0 → Compose Compiler 1.4.3
- Kotlin 1.9.0 → Compose Compiler 1.5.3
2. 添加Relay与Compose核心依赖
在dependencies块中补充Relay和Jetpack Compose的必要依赖:
dependencies { // 原有依赖保持不变... // 新增:Relay Compose依赖 implementation "com.google.relay:relay-compose:0.3.0" // 新增:Jetpack Compose核心依赖 implementation "androidx.compose.ui:ui:1.4.3" implementation "androidx.compose.material:material:1.4.3" implementation "androidx.compose.ui:ui-tooling-preview:1.4.3" debugImplementation "androidx.compose.ui:ui-tooling:1.4.3" }
修改后的完整build.gradle配置
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'com.google.relay' } android { compileSdk 32 buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion = "1.3.2" } sourceSets { main { assets { srcDir('src/main/ui-packages') } } } defaultConfig { applicationId "com.example.figma_layout" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } 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' } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' // Relay与Compose依赖 implementation "com.google.relay:relay-compose:0.3.0" implementation "androidx.compose.ui:ui:1.4.3" implementation "androidx.compose.material:material:1.4.3" implementation "androidx.compose.ui:ui-tooling-preview:1.4.3" debugImplementation "androidx.compose.ui:ui-tooling:1.4.3" }
配置完成后,点击Android Studio右上角的Sync Project with Gradle Files按钮同步项目,错误即可消失。
内容的提问来源于stack exchange,提问作者Husam Abu Zina
相关产品推荐
相关产品推荐

