Hilt 无法与 AOSP 中的 Jetpack Compose 配合使用

问题描述 投票:0回答:1

我正在 AOSP 中开发 Android 应用程序。 以下是我的应用程序的配置Android.bp

    android_library_import {
    name: "MyApp-hilt-navigation-compose",
    aars: ["libs/hilt-navigation-compose-1.2.0.aar"],
}
android_library_import {
    name: "MyApp-hilt-android",
    aars: ["libs/hilt-android-2.50.aar"],
}
android_library {
    name: "MyApp-feature-demohomecpp",
    platform_apis: true,
    srcs: [
        "src/main/**/*.kt",
    ],
    manifest: "src/main/AndroidManifest.xml",
    static_libs: [
        "car-ui-lib",
        "androidx.activity_activity-ktx",
        "androidx.annotation_annotation",
        "androidx.appcompat_appcompat",
        "androidx.activity_activity",
        "androidx.recyclerview_recyclerview",
        "androidx.cardview_cardview",
        "com.google.android.material_material",
        "androidx.navigation_navigation-compose",
        "androidx.compose.runtime_runtime",
        "androidx.activity_activity-compose",
        "androidx.navigation_navigation-compose",
        "androidx.compose.ui_ui",
        "androidx.compose.ui_ui-tooling",
        "androidx.compose.ui_ui-tooling-preview",
        "androidx.compose.material3_material3",
        "jetbrains-annotations",
        "hilt-android",
        "MyApp-hilt-navigation-compose",
        "MyApp-asis",
        "MyApp-core-cpp",
        "MyApp-core-common",
        "MyApp-core-ui",
        "MyApp-core-utils",
        "MyApp-data",
        "MyApp-runtime-android"
    ]
}

通过命令:m MyApp成功制作模块后,我在设备中安装了apk文件。然而,当应用程序启动时,发现以下错误。

10-30 07:20:57.720 E 30878    30878    AndroidRuntime: FATAL EXCEPTION: main 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime: Process: com.myapp.sample, PID: 30878 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/hilt/navigation/HiltViewModelFactory; 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.hilt.navigation.compose.HiltViewModelKt.createHiltViewModelFactory(HiltViewModel.kt:95) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at com.myapp.sample.feature.demo.demoScreenKt.demoScreen(demoScreen.kt:408) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at com.myapp.sample.feature.demo.navigation.demoNavigationKt$demoScreen$1.invoke(demoNavigation.kt:18) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at com.myapp.sample.feature.demo.navigation.demoNavigationKt$demoScreen$1.invoke(demoNavigation.kt:17) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:138) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt$NavHost$14$1.invoke(NavHost.kt:310) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt$NavHost$14$1.invoke(NavHost.kt:308) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:248) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider(NavBackStackEntryProvider.kt:65) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider(NavBackStackEntryProvider.kt:1) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:52) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:51) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavBackStackEntryProviderKt.LocalOwnersProvider(NavBackStackEntryProvider.kt:47) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt$NavHost$14.invoke(NavHost.kt:308) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt$NavHost$14.invoke(NavHost.kt:292) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:138) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.animation.AnimatedContentKt$AnimatedContent$6$1$5.invoke(AnimatedContent.kt:801) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.animation.AnimatedContentKt$AnimatedContent$6$1$5.invoke(AnimatedContent.kt:790) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:117) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.animation.AnimatedVisibilityKt.AnimatedEnterExitImpl(AnimatedVisibility.kt:773) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.animation.AnimatedContentKt$AnimatedContent$6$1.invoke(AnimatedContent.kt:772) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.animation.AnimatedContentKt$AnimatedContent$6$1.invoke(AnimatedContent.kt:755) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.animation.AnimatedContentKt.AnimatedContent(AnimatedContent.kt:815) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:277) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt$NavHost$16.invoke(Unknown Source:27) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.navigation.compose.NavHostKt$NavHost$16.invoke(Unknown Source:10) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:192) 
10-30 07:20:57.720 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2511) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2780) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3259) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3210) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:864) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1125) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:124) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:580) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:548) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:41) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1341) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1352) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.view.Choreographer.doCallbacks(Choreographer.java:952) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.view.Choreographer.doFrame(Choreographer.java:878) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1326) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:958) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.os.Looper.loopOnce(Looper.java:205) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.os.Looper.loop(Looper.java:294) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:8248) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime:  Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.runtime.PausableMonotonicFrameClock@ce858c2, androidx.compose.ui.platform.MotionDurationScaleImpl@20516d3, StandaloneCoroutine{Cancelling}@3513210, AndroidUiDispatcher@f336109] 
10-30 07:20:57.721 E 30878    30878    AndroidRuntime: Caused by: java.lang.ClassNotFoundException: androidx.hilt.navigation.HiltViewModelFactory 

以下是错误块的代码:

@Composable
internal fun DemoHomeCCPScreen(
    modifier: Modifier = Modifier,
    demoHomeCCPState: DemoHomeCCPState = rememberDemoHomeCCPState(),
    viewModel: DemoHomeCCPViewModel = hiltViewModel(),
    navigateToScreen: (String) -> Unit,
) {
}

在 AOSP 中构建应用程序时如何解决此问题?

android-jetpack-compose android-source dagger-hilt
1个回答
0
投票

包含 Hilt AAR 是行不通的,因为 Hilt 使用代码生成,因此需要更深入地集成到构建过程中。

AOSP 已经在

external/dagger2
中签入了 Hilt 和完整的 Soong 集成。请注意
Android.bp
中声明的许多插件。

因此,您不应该包含 Hilt AAR,而是通过引用预先存在的 Soong dagger2 静态库将 Hilt 包含在 AOSP 中,如下所示:

    static_libs: [
        "androidx.activity_activity-compose",
        "androidx.compose.foundation_foundation",
        "androidx.compose.runtime_runtime",
        "androidx.compose.ui_ui",
        "androidx.hilt_hilt-navigation-compose",
        "hilt_android",
    ],

请参阅作为 AOSP 一部分的这个项目的工作示例:

https://cs.android.com/android/platform/superproject/main/+/c97d572245aad0bd11bc2e89a0f50dcbdf53f6ae:packages/providers/MediaProvider/photopicker/Android.bp;bpv=1

© www.soinside.com 2019 - 2024. All rights reserved.