Cartographing Jetpack Compose: compiler and runtime
Have you noticed that I listed those areas in a specific order? While you can easily write wonderful Compose apps that do not use animation or material you will almost certainly need runtime and foundation. How about the first one, compiler?
androidx.compose.compiler
Well, while it is of utmost importance to get the machinery going, you do not need to include it in your build.gradle file. Let’s do it anyway, so that we can peek inside a little:
implementation "androidx.compose.compiler:compiler:$compose_version"
Enter fullscreen mode
Exit fullscreen mode
Doesn’t look like we would want to invoke any of these manually, right?
Please recall: while (besides being annotated with @Composable ) composables look like ordinary Kotlin functions, they are heavily processed during compile time.
fun test() = "" class MainActivity : ComponentActivity() override fun onCreate(savedInstanceState: Bundle?) super.onCreate(savedInstanceState) val a = ::test println(a)
Enter fullscreen mode
Exit fullscreen mode
I/System.out: function test (Kotlin reflection is not available)
Enter fullscreen mode
Exit fullscreen mode
On the other hand function references of @Composable functions are not currently supported.
Another important thing to recall is that @Composable invocations can only happen from the context of a @Composable function.
Talking about @Composable inevitably brings us to the second area, as the annotation is located in package androidx.compose.runtime .
androidx.compose.runtime
As of May 2021 its description is rather sparse. We have 14 interfaces with interesting names like Applier, Composer, Composition and State . And 12 classes, among them Updater. One enum ( Recomposer.State ) and 14 annotations.
For example, @Stable
is used to communicate some guarantees to the compose compiler
about how a certain type or function will behave.
can be applied to Composable functions in order to prevent code
from being generated which allow this function’s execution to be
skipped or restarted.
Now, which of these are parts of the Compose clockwork, and which will we be using in our apps?
Assume, at some point you are invoking androidx.compose.material.Text(«Hello Compose») . The resulting call chain has quite a few steps, among them are BasicText() and CoreText() . Both reside in androidx.compose.foundation.text . CoreText() , by the way, has an internal modifier. More importantly, it invokes yet another composable, Layout() in package androidx.compose.ui.layout .
Layout is the main core component for layout. It can be used
to measure and position zero or more layout children.The measurement, layout and intrinsic measurement behaviours of
this layout will be defined by the measurePolicy instance. See
MeasurePolicy for more details.For a composable able to define its content according to the
incoming constraints, see
androidx.compose.foundation.layout.BoxWithConstraints .
Let’s peek inside Layout.kt:
With the invocation of ComposeNode we make our way back to package androidx.compose.runtime . The docs say:
Emits a node into the composition of type T . Nodes emitted
inside of content will become children of the emitted node.
Without going into too much detail we see heavy use of currentComposer , which is an instance of the Composer interface. It is based upon the concept of nodes. Updater is
A helper receiver scope class used by ComposeNode to help
write code to initialized and update a node.
Take aways
At this point you might recall the title of this article, Cartographing Jetpack Compose, and ask yourself if we still are on track. We are, as we now have quite some understanding of the innards of Compose. We have seen that invoking composables leeds to building and updating node-based structures. How they become — or lead to — what we see on screen is another interesting story I may be telling later.
In this episode we have cartographed two areas, compose.compiler and compose.runtime. The first one is very important, yet uninteresting from an apps’ perspective. The latter one both drives the (re-)composition of the ui and offers key elements like @Composable and Layout . We have already seen foundation briefly. Please stay tuned for a more detailed look.
Did you like this article? Please share your thoughts in the comments.
androidx.compose.runtime:runtime-livedata
Release 1.7.0-alpha06
Source repo 2FA enabled TEXT! Package manager 2FA enabled TEXT! Is security responsive TEXT! Dependencies are managed TEXT! Issue-free release available TEXT! Succession plan available TEXT! Package manager 2FA enabled TEXT!
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.
Releases
Something wrong with this page? Make a suggestion
Last synced: 2024-04-03 17:31:47 UTC
Libraries.io helps you find new open source packages, modules and frameworks and keep track of ones you depend upon.
Copyright © 2024 Tidelift, Inc
Code is Open Source under AGPLv3 license
Data is available under CC-BY-SA 4.0 license
Integration of Jetpack Compose with LiveData.
Jetpack Compose and LiveData are two separate libraries provided by Android to help developers build modern UIs and handle data changes in a reactive way. Jetpack Compose is a declarative UI framework, while LiveData is a part of the Android Architecture Components and is used for observing and reacting to data changes.
However, Jetpack Compose works best with Kotlin’s StateFlow or other reactive programming libraries like RxJava, rather than LiveData. StateFlow is similar to LiveData in its reactivity, but it is specifically designed to be used with Kotlin coroutines, making it a more natural fit with Jetpack Compose.
To use Jetpack Compose with StateFlow, you can follow these general steps:
- Add the required dependencies to your project’s build.gradle :
dependencies implementation "androidx.compose.ui:ui:x.x.x"
implementation "androidx.compose.ui:ui-tooling:x.x.x"
implementation "androidx.compose.runtime:runtime-livedata:x.x.x"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:x.x.x"
>
Replace x.x.x with the latest version of Jetpack Compose and AndroidX libraries.
- Create a StateFlow object in your ViewModel:
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
class MyViewModel : ViewModel() private val _data = MutableStateFlow("Initial value")
val data: StateFlow get() = _data // Function to update data
fun updateData(newData: String) _data.value = newData
>
>
- Observe the StateFlow in your Composable function:
@Composable
fun MyComposable(viewModel: MyViewModel) val data: String by viewModel.data.collectAsState()
// UI code using the 'data' variable
>
- To update the data from the Composable, use the ViewModel’s function:
@Composable
fun MyComposable(viewModel: MyViewModel) val data: String by viewModel.data.collectAsState()
// UI code using the 'data' variable
Button(onClick = < viewModel.updateData("New value") >) Text("Update Data")
>
>
By using StateFlow with Jetpack Compose, you can observe changes to the data in your ViewModel and automatically recompose your UI whenever the data changes.
Keep in mind that the libraries and best practices might have evolved beyond my last update, so I recommend checking the official Android documentation and other relevant resources for the latest information on using Jetpack Compose with reactive data handling.
Xamarin. AndroidX. Compose. Runtime. LiveData 1.3.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module’s version of Install-Package.
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xamarin.AndroidX.Compose.Runtime.LiveData --version 1.3.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Xamarin.AndroidX.Compose.Runtime.LiveData, 1.3.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Xamarin.AndroidX.Compose.Runtime.LiveData as a Cake Addin #addin nuget:?package=Xamarin.AndroidX.Compose.Runtime.LiveData&version=1.3.3.1 // Install Xamarin.AndroidX.Compose.Runtime.LiveData as a Cake Tool #tool nuget:?package=Xamarin.AndroidX.Compose.Runtime.LiveData&version=1.3.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
.NET for Android (formerly Xamarin.Android) bindings for AndroidX — runtime-livedata (bindings without managed callable wrappers (C#) and are only used for including Java files) artifact=androidx.compose.runtime:runtime-livedata artifact_versioned=androidx.compose.runtime:runtime-livedata:1.3.3
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0-android31.0 net6.0-android31.0 is compatible. net7.0-android net7.0-android was computed. net8.0-android net8.0-android was computed. |
MonoAndroid | monoandroid12.0 monoandroid12.0 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
MonoAndroid 12.0
- Xamarin.AndroidX.Compose.Runtime (>= 1.3.3.1)
- Xamarin.AndroidX.Compose.UI (>= 1.3.3.1)
- Xamarin.AndroidX.Lifecycle.LiveData (>= 2.5.1.2)
- Xamarin.Kotlin.StdLib (>= 1.8.0.1)
net6.0-android31.0
- Xamarin.AndroidX.Compose.Runtime (>= 1.3.3.1)
- Xamarin.AndroidX.Compose.UI (>= 1.3.3.1)
- Xamarin.AndroidX.Lifecycle.LiveData (>= 2.5.1.2)
- Xamarin.Kotlin.StdLib (>= 1.8.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Xamarin.AndroidX.Compose.Runtime.LiveData:
The 365id Id Verification SDK enables you to integrate 365id services into your Xamarin app. The SDK supports identifying and validating ID documents such as passports, ID cards and drivers’ licenses, as well as reading the text on the document and automatically mapping these to relevant fields when used in conjunction with the 365id Integration Service.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.6.4 | 99 | 23.03.2024 |
1.6.3 | 125 | 12.03.2024 |
1.6.2 | 150 | 29.02.2024 |
1.6.1.1 | 118 | 22.02.2024 |
1.6.1 | 80 | 22.02.2024 |
1.6.0 | 141 | 11.02.2024 |
1.5.4.1 | 454 | 28.11.2023 |
1.5.4 | 329 | 24.10.2023 |
1.5.3.1 | 136 | 16.10.2023 |
1.5.3 | 119 | 15.10.2023 |
1.5.2 | 106 | 04.10.2023 |
1.5.1.1 | 156 | 15.09.2023 |
1.5.1 | 101 | 16.09.2023 |
1.5.0 | 152 | 11.09.2023 |
1.4.3.2 | 412 | 14.07.2023 |
1.4.3.1 | 310 | 24.05.2023 |
1.4.3 | 105 | 24.05.2023 |
1.4.2 | 155 | 19.05.2023 |
1.4.1 | 1 195 | 05.05.2023 |
1.4.0.1 | 128 | 01.05.2023 |
1.4.0 | 263 | 29.03.2023 |
1.3.3.1 | 413 | 21.01.2023 |
1.3.3 | 290 | 15.01.2023 |
1.3.2 | 308 | 15.12.2022 |
1.3.1 | 399 | 28.11.2022 |
1.3.0.1 | 332 | 09.11.2022 |
1.3.0 | 361 | 28.10.2022 |
1.2.1.1 | 339 | 25.10.2022 |
1.2.1 | 600 | 01.09.2022 |
1.2.0 | 571 | 06.08.2022 |
1.1.1.2 | 625 | 07.06.2022 |
1.1.1.1 | 578 | 05.04.2022 |
1.1.1 | 478 | 03.03.2022 |
1.1.0 | 393 | 01.03.2022 |
1.0.5.2 | 395 | 18.02.2022 |
1.0.5.1 | 424 | 01.02.2022 |
1.0.0 | 472 | 02.11.2021 |