You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

createComposeRule无法导入,添加依赖后仍无法正常使用求助

问题描述

我在测试中尝试添加composeTestRule,需要导入createComposeRule()才能使用,但添加依赖后没有效果。改成仅使用testImplementation依赖后,又无法调用setContent方法。试过清除缓存、清理并重建项目,问题仍未解决。

测试代码

package com.junclabs.city

import org.junit.Test
import org.junit.Assert.*
import org.junit.Rule

class ExampleUnitTest {
    @get:Rule
    val composeTestRule = createComposeRule()
    @Test
    fun navigateToParkSuccessfully() {
        composeTestRule.setContent {
            MainActivity()
        }
    }
}

项目build.gradle配置

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.junclabs.city"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.junclabs.city"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = 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.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.6.5@aar")
    implementation("androidx.navigation:navigation-compose:2.7.7")
    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
    implementation("androidx.activity:activity-compose:1.8.2")
    implementation(platform("androidx.compose:compose-bom:2023.08.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.08.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}
解决方法
  1. 调整测试文件位置:createComposeRule()属于Android Instrumented测试范畴,你的测试类当前放在单元测试目录(src/test/java),需移至Android测试目录(src/androidTest/java/com/junclabs/city/)下。

  2. 清理重复依赖:你同时添加了androidx.compose.ui:ui-test-junit4-android和androidx.compose.ui:ui-test-junit4,且前者版本(1.6.5)与BOM版本(2023.08.00)冲突。删除androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.6.5@aar")这条依赖,保留BOM管理的ui-test-junit4即可,BOM会自动统一所有Compose相关依赖版本。

  3. 补充缺失导入:测试文件中缺少createComposeRule()的导入语句,需添加:

import androidx.compose.ui.test.junit4.createComposeRule
  1. 明确测试场景:如果要在测试中启动MainActivity,必须使用Instrumented测试(放在androidTest目录)。单元测试环境无法直接启动Activity,若坚持用单元测试,需额外配置且不适用于当前场景。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:07:44