Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why some projects import androidx.activity-ktx or not?

Tags:

android

gradle

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.

like image 248
Charles Avatar asked Oct 22 '25 18:10

Charles


1 Answers

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

like image 122
Ihar Paliashchuk Avatar answered Oct 24 '25 08:10

Ihar Paliashchuk