4

I started a new project and import previous dependency settings, And tried using by viewModel in activity. But I can't get the by viewModel reference, So I tried going to External libraries. The previous project has androidx.activity:activity-ktx:1.0.0 even though I didn't add implementation "androidx.activity:activity-ktx:1.0.0". you can see this happening at https://github.com/android/architecture-samples .

But When I try to start a new project and import setting from previous project, I can't get the androidx.activity:activity-ktx:1.0.0. I don't unserstand previous project or the sample of google import this androidx.activity:activity-ktx:1.0.0 , and my new project do not import that. although the gradle setting and gradle version is same.

Zain
  • 37,492
  • 7
  • 60
  • 84
Charles
  • 111
  • 2
  • 7

2 Answers2

3

Android KTX is a set of Kotlin extensions to help your write concise and idiomatic code.

by viewModel extension is not available by default for a new android project and should be added using Android KTX, particularly androidx.fragment:fragment-ktx:'version'.

You may find other useful extensions you would like to add to project like androidx.activity:activity-ktx:, androidx.lifecycle:lifecycle-livedata-ktx: etc. Each one adds some special "syntactic sugar" so don't hesitate to learn their possibilities.

Please, see reference Android KTX

  • I understand why I have to add that two libraries. I'm saying I don't understand the example of google(what I linked) doesn't implement that two libraries, but have that two libraries – Charles Jul 14 '20 at 13:24
  • Why can't I use `by activityViewModels()` in my Activity? – IgorGanapolsky Apr 22 '21 at 01:20
  • 1
    @IgorGanapolsky because this extension belongs to Fragment... You can use `by activityViewModels()` only inside a Fragment. – Ihar Paliashchuk Apr 28 '21 at 16:25
0

I don't know how you can import previous dependency settings from other project into new project. Is it a function of Android Studio?

But I think print the whole dependency tree will solve your confusion, because some dependencies maybe be implicit included by directly declared dependencies.

You can open terminal and execute ./gradlew dependencies to print all dependencies of the whole project.

See https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html for more details.

I tried and found this,

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
...

It tells us that androidx.activity:activity-ktx:1.4.0 is imported by androidx.activity:activity-compose:1.3.0, and androidx.activity:activity-compose:1.3.0 is imported by androidx.compose.ui:ui-tooling:1.1.1.

You can find the androidx.compose.ui:ui-tooling:1.1.1 is declared in app's build.gradle. Link

Then, you can find the activity-compose import activity-ktx by using api following this link.

About the api's meaning in build.gradle, please refer to this.

So project architecture-samples can use viewModels() without implementation the dependency activity-ktx in build.gradle.

Jiahui
  • 1
  • 1