Now create a Database class, which will wrap the AppDatabase class and contain the caching logic. So far, you have added platform database drivers and an AppDatabase class to perform database operations. You can navigate through the expect declarations and actual implementations by clicking the handy gutter icon: Implement cache Instances of these factories will be created later in the code of your Android and iOS projects. Return NativeSqliteDriver(AppDatabase.Schema, "test.db") The block will contain a list of databases and their parameters: To configure it, go to the shared directory and add the sqldelight block to the end of the file. During compilation, the generator validates the SQL queries and turns them into Kotlin code that can be used in the shared module. The SQLDelight library allows you to generate a type-safe Kotlin database API from SQL queries. Configure SQLDelight and implement cache logic Configure SQLDelight The annotation allows you to redefine field names, which helps to declare properties in data classes with more easily readable names. However, you don't need to do that in this case. The rialization plugin automatically generates a default serializer for classes unless you explicitly pass a link to a serializer through the annotation argument. Var launchYear = class patch: article: class small: large: String?Įach serializable class must be marked with the annotation. Import class flightNumber: missionName: launchDateUTC: details: launchSuccess: links: Links In addition, Ktor needs the serialization feature to use rialization for processing network requests and responses.Īt the very beginning of the file in the same shared directory, add the following lines to the plugins block: Implementation(":native-driver:$sqlDelightVersion")Įach library requires a core artifact in the common source set.īoth the SQLDelight and Ktor libraries need platform drivers in the iOS and Android source sets, as well. Implementation("io.ktor:ktor-client-darwin:$ktorVersion") Implementation(":android-driver:$sqlDelightVersion") Implementation("io.ktor:ktor-client-android:$ktorVersion") Implementation(":kotlinx-datetime:$dateTimeVersion") Implementation(":runtime:$sqlDelightVersion") Implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") Implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion") Implementation("io.ktor:ktor-client-core:$ktorVersion") Implementation(":kotlinx-coroutines-core:$coroutinesVersion") In the shared directory, specify the dependencies on all the required libraries in the file: To see the full file structure of the project, which is more convenient for multiplatform development, switch the view from Android to Project: The default view in Android Studio is optimized for Android development. Navigate to the unpacked project folder and then click Open.Īndroid Studio detects that the folder contains a Gradle build file and opens the folder as a new project. On the Welcome screen, click Open, or select File | Open in the editor. To add a multiplatform library to the shared module, you need to add dependency instructions ( implementation) for all libraries to the dependencies block of the relevant source sets in the file.īoth the rialization and SQLDelight libraries also require additional configurations. It's not necessary for this tutorial.Ĭlick the Download button and unpack the resulting archive.Īdd dependencies to the multiplatform library On the New project tab, ensure that the Android and iOS options are selected.įor iOS, choose the Do not share UI option. Check the list of necessary tools and update them to the latest versions if necessary. Prepare your environment for multiplatform development. You can find the template project as well as the source code of the final application in the corresponding GitHub repository. SQLDelight to generate Kotlin code from SQL queries and create a type-safe database API. rialization to deserialize JSON responses into objects of entity classes. Ktor as an HTTP client for retrieving data over the internet. You will use the following multiplatform libraries in the project: The output will be an app that retrieves data over the internet from the public SpaceX API, saves it in a local database, and displays a list of SpaceX rocket launches together with the launch date, results, and a detailed description of the launch: The business logic and data access layers will be implemented only once in the shared module, while the UI of both applications will be native. The application will include a module with shared code for both the iOS and Android platforms. This tutorial demonstrates how to use Android Studio to create a mobile application for iOS and Android using Kotlin Multiplatform with Ktor and SQLDelight.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |