https://kotlinlang.org logo
#compose
Title
# compose
d

Daniele B

02/14/2021, 3:52 PM
I am upgraded to: Kotlin 1.4.30 Compose 1.0.0-alpha12 NavigationCompose 1.0.0-alpha07 I get this error:
Copy code
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/activity/compose/LocalOnBackPressedDispatcherOwner;
    at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:83)
    at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:59)
    at myapp.android.NavigationKt.Navigation(Navigation.kt:22)
any idea?
d

Daniele B

02/14/2021, 3:59 PM
thanks! It fixed it However I still have another error:
which I don’t understand, as I am actually using 1.4.30
j

jim

02/14/2021, 4:01 PM
The problem is that your IDE is still using an old Kotlin plugin, since AndroidStudio with stable 1.4.30 hasn't yet been released, so you'll need to change your kotlin update channel. Assuming you are on a recent/latest canary, you should be able to open "Preferences" > "Languages & Frameworks" > "Kotlin", and then change the "Update Channel"
d

Daniele B

02/14/2021, 4:08 PM
thanks @jim I was able to download the preview Kotlin 1.4.30 plugin!
@Ian Lake I now have:
Copy code
implementation("androidx.activity:activity-ktx:1.2.0")
implementation("androidx.activity:activity-compose:1.3.0-alpha02")
do I need both?
in general, do I need all first 5 dependencies for a compose-only app?
• core-ktx • activity-ktx • activity-compose • material • appcompat
i

Ian Lake

02/14/2021, 4:14 PM
activity-compose
has
activity-ktx
as a transitive dependency, so you definitely don't need both of those (just
activity-compose
is enough)
d

Daniele B

02/14/2021, 4:19 PM
thanks, at least one goes away
i

Ian Lake

02/14/2021, 4:20 PM
The rest of them are perhaps more of a "do you need them" question.
activity-ktx
will already give you
core-ktx:1.1.0
(so the question is do you specifically want extensions from the newer version? Only then do you need to declare what specific version you want) and a pure compose app with no Views whatsoever doesn't need
material
or
appcompat
either (as those are primarily View focused dependencies)
If you are using
AppCompatActivity
for whatever reason, then you should use
appcompat:1.3.0-beta01
as it is the 1.3 releases that populate the ViewTree APIs compose depends on (if you're only using
ComponentActivity
, then you're fine - that potential issue only applies specifically to
AppCompatActivity
)
d

Daniele B

02/14/2021, 4:49 PM
@Ian Lake I am currently using:
Copy code
class MainActivity : AppCompatActivity() {

  private val appViewModel: AppViewModel by viewModels()

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      MyAppTheme(appViewModel.KMPModel) {
        Navigation(appViewModel.KMPModel)
      }
    }
  }

}
I didn’t know about
ComponentActivity
, is it new?
i

Ian Lake

02/14/2021, 5:02 PM
It has been the base class under
AppCompatActivity
and
FragmentActivity
for a year and a half or so now
Keep in mind that Compose also has APIs for getting a ViewModel, there's no advantage of using the Activity APIs in this case: https://developer.android.com/jetpack/compose/interop#viewmodel
👍 1
d

Daniele B

02/14/2021, 5:15 PM
I didn’t know about
ComponentActivity
! The code snippet on JetpackCompose tutorial page uses:
AppCompatActivity
https://developer.android.com/jetpack/compose/tutorial I have just replaced
AppCompatActivity
with
ComponentActivity
in my app and it seems to work correctly. So, can I safely use it?
i

Ian Lake

02/14/2021, 7:07 PM
If you have no Android Views and aren't using any AppCompat APIs or any Fragment APIs, then AppCompatActivity isn't really doing anything for you and, yep,
ComponentActivity
should be enough
👍🏽 1
Not every app is as lucky to be working in the pure Compose world right from the start. For those cases,
AppCompatActivity
makes more sense as a general recommendation that will give you a working solution in Compose without having to make changes later if you end up needing an
AndroidView
that assumes AppCompat, Material, etc. is available
d

Daniele B

02/14/2021, 8:34 PM
Thanks @Ian Lake! So, I guess if I want to use some WebViews, I would need to use AppCompatActivity, or do you expect to release WebViews for Compose?
i

Ian Lake

02/14/2021, 8:37 PM
WebView is a weird one, since it is a black box into web content and web content doesn't rely on AppCompat hooks either.
✔️ 1
d

Daniele B

02/14/2021, 8:38 PM
Interesting, to know!
2 Views