为什么有些项目导入androidx.activity-ktx或不导入



我启动了一个新项目并导入了以前的依赖项设置,并尝试在活动中由viewModel使用。但我无法获得byviewModel引用,所以我尝试去外部库。上一个项目有androidx.activity:activity-ktx:1.0.0,尽管我没有添加implementation "androidx.activity:activity-ktx:1.0.0"。你可以在https://github.com/android/architecture-samples。

但是,当我尝试启动一个新项目并从以前的项目导入设置时,我无法获得androidx.activity:activity-ktx:1.0.0。我没有取消对以前的项目或谷歌的样本导入这个androidx.activity:activity-ktx:1.0.0的搜索,我的新项目也没有导入那个。尽管渐变设置和渐变版本是相同的。

Android KTX是一组Kotlin扩展,可以帮助您编写简洁、惯用的代码。

默认情况下,by viewModel扩展不适用于新的android项目,应该使用android KTX添加,尤其是androidx.fragment:fragment-ktx:'version'

您可能会发现您想添加到项目中的其他有用扩展,如CCD_ 9、CCD_;句法糖";所以不要犹豫,去学习它们的可能性。

请参阅参考Android KTX

我不知道如何将以前的依赖项设置从其他项目导入到新项目中。它是安卓工作室的一个功能吗?

但我认为打印整个依赖关系树将解决您的困惑,因为一些依赖关系可能是由直接声明的依赖关系隐含包含的

您可以打开终端并执行./gradlew dependencies来打印整个项目的所有依赖项。

请参阅https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html了解更多详细信息。

我试着找到了这个,

  • 回购:https://github.com/android/architecture-samples
  • 版本:d31721f
debugRuntimeClasspath - Runtime classpath of compilation 'debug' (target  (androidJvm)).
+--- androidx.compose.ui:ui-tooling:1.1.1
|    +--- androidx.annotation:annotation:1.1.0 -> 1.3.
|    +--- androidx.compose.runtime:runtime:1.1.1
|    |
|    |
...
|    --- androidx.activity:activity-compose:1.3.0 -> 1.4.0
|         +--- androidx.compose.runtime:runtime:1.0.1 -> 1.1.1 (*)
|         +--- androidx.compose.runtime:runtime-saveable:1.0.1 -> 1.1.1 (*)
|         +--- androidx.activity:activity-ktx:1.4.0
...

它告诉我们androidx.activity:activity-ktx:1.4.0androidx.activity:activity-compose:1.3.0导入,androidx.activity:activity-compose:1.3.0androidx.compose.ui:ui-tooling:1.1.1导入。

您可以发现androidx.compose.ui:ui-tooling:1.1.1是在应用程序的build.gradle中声明的。链接

然后,您可以使用此链接后面的api找到activity-compose导入activity-ktx

关于apibuild.gradle中的含义,请参阅此。

因此项目体系结构示例可以使用viewModels()而不使用implementationbuild.gradle中的依赖activity-ktx

最新更新