0% found this document useful (0 votes)
625 views1,379 pages

Kotlin Reference

This document provides an overview of the Kotlin programming language and summaries of what's new in different versions of Kotlin, including Kotlin 1.9.0, 1.8.20, 1.9.0-RC, 1.8.0, 1.7.20, and 1.7.0. It covers topics like getting started with Kotlin, basic language features, collections, control flow, functions, classes, null safety, Kotlin Multiplatform, Kotlin for server-side, Android, WebAssembly, Native, JavaScript, data science, and competitive programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
625 views1,379 pages

Kotlin Reference

This document provides an overview of the Kotlin programming language and summaries of what's new in different versions of Kotlin, including Kotlin 1.9.0, 1.8.20, 1.9.0-RC, 1.8.0, 1.7.20, and 1.7.0. It covers topics like getting started with Kotlin, basic language features, collections, control flow, functions, classes, null safety, Kotlin Multiplatform, Kotlin for server-side, Android, WebAssembly, Native, JavaScript, data science, and competitive programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1379

Kotlin Language Documentation 1.9.

0
Table of Contents

Kotlin Docs 64

Get started with Kotlin 64

Install Kotlin 64

Create your powerful application with Kotlin 64

Is anything missing? 66

Welcome to our tour of Kotlin! 66

Hello world 66

Variables 67

String templates 67

Practice 68

Next step 68

Basic types 68

Practice 69

Next step 70

Collections 70

List 70

Set 71

Map 72

Practice 74

Next step 75

Control flow 75

Conditional expressions 75

Ranges 76

Loops 77

Practice 78

Next step 80

2
Functions
80
Named arguments 80

Default parameter values 81

Functions without return 81

Single-expression functions 81

Functions practice 82

Lambda expressions 83

Lambda expressions practice 86

Next step 86

Classes 86

Properties 86

Create instance 87

Access properties 87

Member functions 88

Data classes 88

Practice 90

Next step 90

Null safety 91

Nullable types 91

Check for null values 91

Use safe calls 92

Use Elvis operator 92

Practice 92

What's next? 93

Kotlin Multiplatform 93

Kotlin Multiplatform use cases 93

Code sharing between platforms 94

Get started 94

Kotlin for server side 94

3
Frameworks for server-side development with Kotlin 95

Deploying Kotlin server-side applications 95

Products that use Kotlin on the server side 95

Next steps 96

Kotlin for Android 96

Kotlin Wasm 96

Browser support 97

Interoperability 97

Compose Multiplatform for Web 97

How to get started 97

Libraries support 97

Feedback 97

Kotlin Native 97

Why Kotlin/Native? 97

Target platforms 98

Interoperability 98

Sharing code between platforms 98

How to get started 98

Kotlin for JavaScript 99

Kotlin/JS IR compiler 99

Kotlin/JS frameworks 99

Join the Kotlin/JS community 100

Kotlin for data science 100

Interactive editors 100

Libraries 102

Kotlin for competitive programming 103

Simple example: Reachable Numbers problem 104

4
Functional operators example: Long Number problem 105

More tips and tricks 106

Learning Kotlin 107

What's new in Kotlin 1.9.0 107

IDE support 108

New Kotlin K2 compiler updates 108

Language 110

Kotlin/JVM 111

Kotlin/Native 111

Kotlin Multiplatform 114

Kotlin/Wasm 115

Kotlin/JS 116

Gradle 117

Standard library 120

Documentation updates 125

Install Kotlin 1.9.0 125

Compatibility guide for Kotlin 1.9.0 125

What's new in Kotlin 1.8.20 125

IDE support 126

New Kotlin K2 compiler updates 126

Language 127

New Kotlin/Wasm target 131

Kotlin/JVM 132

Kotlin/Native 133

Kotlin Multiplatform 135

Kotlin/JavaScript 138

Gradle 139

Standard library 142

Serialization updates 144

5
Documentation updates 145

Install Kotlin 1.8.20 145

What's new in Kotlin 1.9.0-RC 145

IDE support 146

New Kotlin K2 compiler updates 146

Stable replacement of the enum class values function 146

Stable ..< operator for open-ended ranges 147

New common function to get regex capture group by name 147

New path utility to create parent directories 147

Preview of Gradle configuration cache in Kotlin Multiplatform 148

Changes for Android target support in Kotlin Multiplatform 148

No object initialization when accessing constant values in Kotlin/Native 148

Ability to configure standalone mode for iOS simulator tests in Kotlin/Native 148

How to update to Kotlin 1.9.0-RC 149

What's new in Kotlin 1.8.0 149

IDE support 149

Kotlin/JVM 149

Kotlin/Native 150

Kotlin Multiplatform: A new Android source set layout 151

Kotlin/JS 154

Gradle 155

Standard library 158

Documentation updates 160

Install Kotlin 1.8.0 161

Compatibility guide for Kotlin 1.8.0 161

What's new in Kotlin 1.7.20 161

Support for Kotlin K2 compiler plugins 162

Language 162

Kotlin/JVM 166

6
Kotlin/Native 168

Kotlin/JS 169

Gradle 169

Standard library 170

Documentation updates 172

Install Kotlin 1.7.20 172

What's new in Kotlin 1.7.0 173

New Kotlin K2 compiler for the JVM in Alpha 173

Language 174

Kotlin/JVM 176

Kotlin/Native 176

Kotlin/JS 178

Standard library 179

Gradle 183

Migrating to Kotlin 1.7.0 187

What's new in Kotlin 1.6.20 188

Language 188

Kotlin/JVM 190

Kotlin/Native 191

Kotlin Multiplatform 194

Kotlin/JS 196

Security 197

Gradle 199

What's new in Kotlin 1.6.0 201

Language 201

Supporting previous API versions for a longer period 203

Kotlin/JVM 203

Kotlin/Native 204

7
Kotlin/JS 206

Kotlin Gradle plugin 207

Standard library 207

Tools 210

Coroutines 1.6.0-RC 211

Migrating to Kotlin 1.6.0 211

What's new in Kotlin 1.5.30 211

Language features 212

Kotlin/JVM 215

Kotlin/Native 216

Kotlin Multiplatform 218

Kotlin/JS 220

Gradle 220

Standard library 223

Serialization 1.3.0-RC 226

What's new in Kotlin 1.5.20 226

Kotlin/JVM 226

Kotlin/Native 227

Kotlin/JS 228

Gradle 229

Standard library 229

What's new in Kotlin 1.5.0 230

Language features 230

Kotlin/JVM 232

Kotlin/Native 234

Kotlin/JS 235

Kotlin Multiplatform 235

Standard library 236

kotlin-test library 240

8
kotlinx libraries 242

Migrating to Kotlin 1.5.0 243

What's new in Kotlin 1.4.30 243

Language features 244

Kotlin/JVM 246

Kotlin/Native 247

Kotlin/JS 247

Gradle project improvements 247

Standard library 247

Serialization updates 249

What's new in Kotlin 1.4.20 250

Kotlin/JVM 250

Kotlin/JS 250

Kotlin/Native 252

Kotlin Multiplatform 254

Standard library 254

Kotlin Android Extensions 255

What's new in Kotlin 1.4.0 255

Language features and improvements 255

New tools in the IDE 259

New compiler 261

Kotlin/JVM 264

Kotlin/JS 265

Kotlin/Native 266

Kotlin Multiplatform 267

Gradle project improvements 270

Standard library 272

Stable JSON serialization 277

9
Scripting and REPL 277

Migrating to Kotlin 1.4.0 278

What's new in Kotlin 1.3 279

Coroutines release 279

Kotlin/Native 279

Multiplatform projects 279

Contracts 279

Capturing when subject in a variable 280

@JvmStatic and @JvmField in companions of interfaces 281

Nested declarations in annotation classes 281

Parameterless main 281

Functions with big arity 282

Progressive mode 282

Inline classes 282

Unsigned integers 282

@JvmDefault 283

Standard library 283

Tooling 285

What's new in Kotlin 1.2 286

Table of contents 286

Multiplatform projects (experimental) 286

Other language features 286

Standard library 289

JVM backend 291

JavaScript backend 291

Tools 292

What's new in Kotlin 1.1 292

Table of contents 292

JavaScript 292

10
Coroutines (experimental) 292

Other language features 293

Standard library 297

JVM Backend 300

JavaScript backend 301

Kotlin releases 301

Update to a new release 302

IDE support 302

Release details 302

Kotlin roadmap 308

Key priorities 308

Kotlin roadmap by subsystem 309

What's changed since May 2022 311

Basic syntax 312

Package definition and imports 312

Program entry point 312

Print to the standard output 313

Functions 313

Variables 314

Creating classes and instances 314

Comments 315

String templates 315

Conditional expressions 315

for loop 316

while loop 316

when expression 316

Ranges 317

Collections 318

11
Nullable values and null checks 318

Type checks and automatic casts 319

Idioms 320

Create DTOs (POJOs/POCOs) 320

Default values for function parameters 320

Filter a list 321

Check the presence of an element in a collection 321

String interpolation 321

Instance checks 321

Read-only list 321

Read-only map 321

Access a map entry 321

Traverse a map or a list of pairs 321

Iterate over a range 322

Lazy property 322

Extension functions 322

Create a singleton 322

Instantiate an abstract class 322

If-not-null shorthand 322

If-not-null-else shorthand 323

Execute a statement if null 323

Get first item of a possibly empty collection 323

Execute if not null 323

Map nullable value if not null 323

Return on when statement 323

try-catch expression 323

if expression 324

Builder-style usage of methods that return Unit 324

Single-expression functions 324

12
Call multiple methods on an object instance (with) 324

Configure properties of an object (apply) 325

Java 7's try-with-resources 325

Generic function that requires the generic type information 325

Nullable Boolean 325

Swap two variables 325

Mark code as incomplete (TODO) 325

What's next? 325

Coding conventions 326

Configure style in IDE 326

Source code organization 326

Naming rules 328

Formatting 329

Documentation comments 336

Avoid redundant constructs 337

Idiomatic use of language features 337

Coding conventions for libraries 341

Basic types 341

Numbers 341

Integer types 341

Floating-point types 342

Literal constants for numbers 342

Numbers representation on the JVM 343

Explicit number conversions 343

Operations on numbers 344

Unsigned integer types 346

Unsigned arrays and ranges 346

Unsigned integers literals 346

13
Use cases 347

Booleans 347

Characters 348

Strings 348

String literals 349

String templates 349

Arrays 350

Primitive type arrays 350

Type checks and casts 351

is and !is operators 351

Smart casts 351

"Unsafe" cast operator 352

"Safe" (nullable) cast operator 352

Generics type checks and casts 352

Conditions and loops 352

If expression 352

When expression 353

For loops 354

While loops 355

Break and continue in loops 356

Returns and jumps 356

Break and continue labels 356

Return to labels 356

Exceptions 358

Exception classes 358

Checked exceptions 358

The Nothing type 359

14
Java interoperability 359

Packages and imports 359

Default imports 360

Imports 360

Visibility of top-level declarations 360

Classes 360

Constructors 361

Creating instances of classes 363

Class members 363

Inheritance 363

Abstract classes 363

Companion objects 364

Inheritance 364

Overriding methods 364

Overriding properties 365

Derived class initialization order 365

Calling the superclass implementation 366

Overriding rules 366

Properties 367

Declaring properties 367

Getters and setters 367

Compile-time constants 368

Late-initialized properties and variables 369

Overriding properties 369

Delegated properties 369

Interfaces 370

Implementing interfaces 370

Properties in interfaces 370

15
Interfaces Inheritance 370

Resolving overriding conflicts 371

Functional (SAM) interfaces 371

SAM conversions 371

Migration from an interface with constructor function to a functional interface 372

Functional interfaces vs. type aliases 372

Visibility modifiers 373

Packages 373

Class members 374

Modules 374

Extensions 375

Extension functions 375

Extensions are resolved statically 375

Nullable receiver 376

Extension properties 376

Companion object extensions 377

Scope of extensions 377

Declaring extensions as members 377

Note on visibility 378

Data classes 378

Properties declared in the class body 379

Copying 379

Data classes and destructuring declarations 380

Standard data classes 380

Sealed classes and interfaces 380

Location of direct subclasses 380

Sealed classes and when expression 381

Generics: in, out, where 381

16
Variance 382

Type projections 383

Generic functions 385

Generic constraints 385

Type erasure 385

Underscore operator for type arguments 387

Nested and inner classes 387

Inner classes 388

Anonymous inner classes 388

Enum classes 388

Anonymous classes 388

Implementing interfaces in enum classes 389

Working with enum constants 389

Inline value classes 390

Members 390

Inheritance 391

Representation 391

Inline classes vs type aliases 392

Inline classes and delegation 393

Object expressions and declarations 393

Object expressions 393

Object declarations 395

Delegation 398

Overriding a member of an interface implemented by delegation 398

Delegated properties 399

Standard delegates 400

Delegating to another property 400

17
Storing properties in a map 401

Local delegated properties 402

Property delegate requirements 402

Translation rules for delegated properties 403

Providing a delegate 404

Type aliases 405

Functions 406

Function usage 406

Function scope 410

Generic functions 410

Tail recursive functions 411

High-order functions and lambdas 411

Higher-order functions 411

Function types 412

Lambda expressions and anonymous functions 414

Inline functions 416

noinline 417

Non-local returns 417

Reified type parameters 418

Inline properties 418

Restrictions for public API inline functions 419

Operator overloading 419

Unary operations 419

Binary operations 421

Infix calls for named functions 423

Type-safe builders 424

How it works 424

Scope control: @DslMarker 426

18
Full definition of the com.example.html package 426

Using builders with builder type inference 428

Writing your own builders 428

How builder inference works 430

Null safety 431

Nullable types and non-null types 431

Checking for null in conditions 432

Safe calls 433

Nullable receiver 433

Elvis operator 434

The !! operator 434

Safe casts 434

Collections of a nullable type 434

What's next? 434

Equality 435

Structural equality 435

Referential equality 435

Floating-point numbers equality 435

This expressions 435

Qualified this 435

Implicit this 436

Asynchronous programming techniques 436

Threading 436

Callbacks 437

Futures, promises, and others 437

Reactive extensions 438

Coroutines 438

19
Coroutines 439

How to start 439

Sample projects 439

Annotations 439

Usage 440

Constructors 440

Instantiation 441

Lambdas 441

Annotation use-site targets 441

Java annotations 442

Repeatable annotations 443

Destructuring declarations 444

Example: returning two values from a function 444

Example: destructuring declarations and maps 445

Underscore for unused variables 445

Destructuring in lambdas 445

Reflection 446

JVM dependency 446

Class references 446

Callable references 447

Get started with Kotlin Multiplatform for mobile 450

Next step 451

Join the community 451

Set up an environment 451

Install the necessary tools 451

Check your environment 452

Possible issues and solutions 452

Next step 454

20
Get help 454

Create your first cross-platform app 454

Create the project from a template 454

Examine the project structure 456

Run your application 458

Update your application 463

Next step 467

Get help 467

Add dependencies to your project 467

Dependency types 467

Add a multiplatform dependency 468

Next step 469

Get help 470

Upgrade your app 470

Add more dependencies 470

Create API requests 471

Update Android and iOS apps 472

Next step 475

Get help 476

Wrap up your project 476

Understand mobile project structure 476

Root project 477

Shared module 478

Android application 482

iOS application 483

Make your Android application work on iOS – tutorial 486

Prepare an environment for development 486

21
Make your code cross-platform 486

Make your cross-platform application work on iOS 494

Enjoy the results – update the logic only once 503

What else to share? 506

What's next? 506

Publish your application 507

Android app 507

iOS app 507

Create a multiplatform app using Ktor and SQLDelight – tutorial 507

Before you start 508

Create a Multiplatform project 509

Add dependencies to the multiplatform library 512

Create an application data model 513

Configure SQLDelight and implement cache logic 513

Implement an API service 516

Build an SDK 517

Create the Android application 518

Create the iOS application 523

What's next? 527

Get started with Kotlin Multiplatform 527

Start from scratch 527

Dive deep into Kotlin Multiplatform 527

Get help 527

Understand Multiplatform project structure 528

Multiplatform plugin 528

Targets 528

Source sets 529

Compilations 530

22
Set up targets for Kotlin Multiplatform 531

Distinguish several targets for one platform 531

Build a full-stack web app with Kotlin Multiplatform 532

Create the project 532

Build the backend 534

Set up the frontend 538

Build the frontend 541

Include a database to store data 545

Deploy to the cloud 548

What's next 549

Create and publish a multiplatform library – tutorial 549

Set up the environment 550

Create a project 550

Write cross-platform code 552

Provide platform-specific implementations 552

Test your library 554

Publish your library to the local Maven repository 555

Publish your library to the external Maven Central repository 556

Add a dependency on the published library 558

What's next? 558

Publishing multiplatform libraries 558

Structure of publications 559

Avoid duplicate publications 559

Publish an Android library 560

Share code on platforms 561

Share code on all platforms 561

Share code on similar platforms 561

Share code in libraries 562

23
What's next? 562

Connect to platform-specific APIs 563

Examples 563

Rules for expected and actual declarations 567

Hierarchical project structure 568

Default hierarchy 568

Target shortcuts 571

Manual configuration 573

Android source set layout 575

Check the compatibility 575

Rename Kotlin source sets 575

Move source files 575

Move the AndroidManifest.xml file 576

Check the relation between Android and common tests 576

Adjust the implementation of Android flavors 577

Adding dependencies on multiplatform libraries 577

Dependency on a Kotlin library 577

Dependency on Kotlin Multiplatform libraries 579

Dependency on another multiplatform project 580

What's next? 581

Adding Android dependencies 581

What's next? 582

Adding iOS dependencies 582

With CocoaPods 582

Without CocoaPods 583

What's next? 585

Run tests with Kotlin Multiplatform 585

24
Required dependencies 586

Run tests for one or more targets 586

Test shared code 586

Configure compilations 586

Configure all compilations 587

Configure compilations for one target 587

Configure one compilation 588

Create a custom compilation 588

Use Java sources in JVM compilations 589

Configure interop with native languages 590

Compilation for Android 591

Compilation of the source set hierarchy 592

Build final native binaries (Experimental DSL) 592

Declare binaries 593

Configure binaries 594

Build final native binaries 597

Declare binaries 598

Access binaries 599

Export dependencies to binaries 600

Build universal frameworks 602

Build XCFrameworks 602

Customize the Info.plist file 603

Multiplatform Gradle DSL reference 604

Id and version 604

Top-level blocks 604

Targets 605

Source sets 611

Compilations 613

25
Dependencies 616

Language settings 617

Kotlin Multiplatform for mobile samples 618

Kotlin Multiplatform for mobile FAQ 620

What is Kotlin Multiplatform for mobile? 620

Can I share UIs with Kotlin Multiplatform? 620

What is the Kotlin Multiplatform Mobile plugin? 620

What is Kotlin/Native and how does it relate to Kotlin Multiplatform? 621

What are the plans for the technology evolution? 621

Can I run an iOS application on Microsoft Windows or Linux? 622

Where can I get complete examples to play with? 622

In which IDE should I work on my cross-platform app? 622

How can I write concurrent code in Kotlin Multiplatform projects? 622

How can I speed up my Kotlin Multiplatform module compilation for iOS? 622

What platforms do you support? 622

Introduce cross-platform mobile development to your team 623

Start with empathy 623

Explain how it works 623

Show the value 623

Offer proof 624

Prepare for questions 624

Be supportive 625

Compatibility guide for Kotlin Multiplatform 625

New approach to auto-generated targets 625

Changes in Gradle input and output compile tasks 626

New configuration names for dependencies on the compilation 626

Deprecated Gradle properties for hierarchical structure support 627

Deprecated support of multiplatform libraries published in the legacy mode 628

26
Deprecated API for adding Kotlin source sets directly to the Kotlin compilation 628

Migration from kotlin-js Gradle plugin to kotlin-multiplatform Gradle plugin 629

Rename of android target to androidTarget 631

Kotlin Multiplatform Mobile plugin releases 631

Update to the new release 631

Release details 631

Get started with Kotlin/JVM 636

Create a project 636

Create an application 637

Run the application 638

What's next? 639

Comparison to Java 639

Some Java issues addressed in Kotlin 639

What Java has that Kotlin does not 640

What Kotlin has that Java does not 640

What's next? 640

Calling Java from Kotlin 640

Getters and setters 641

Java synthetic property references 641

Methods returning void 642

Escaping for Java identifiers that are keywords in Kotlin 642

Null-safety and platform types 642

Mapped types 647

Java generics in Kotlin 649

Java arrays 650

Java varargs 650

Operators 651

Checked exceptions 651

27
Object methods 651

Inheritance from Java classes 652

Accessing static members 652

Java reflection 652

SAM conversions 652

Using JNI with Kotlin 652

Using Lombok-generated declarations in Kotlin 653

Calling Kotlin from Java 653

Properties 653

Package-level functions 653

Instance fields 654

Static fields 655

Static methods 656

Default methods in interfaces 656

Visibility 658

KClass 658

Handling signature clashes with @JvmName 658

Overloads generation 659

Checked exceptions 659

Null-safety 660

Variant generics 660

Get started with Spring Boot and Kotlin 661

Next step 661

Join the community 661

Create a Spring Boot project with Kotlin 662

Before you start 662

Create a Spring Boot project 662

Explore the project Gradle build file 665

Explore the generated Spring Boot application 666

28
Create a controller 667

Run the application 668

Next step 669

Add a data class to Spring Boot project 669

Update your application 669

Run the application 671

Next step 671

Add database support for Spring Boot project 672

Add database support 672

Update the MessageController class 673

Update the MessageService class 673

Configure the database 674

Add messages to database via HTTP request 674

Retrieve messages by id 676

Run the application 677

Next step 678

Use Spring Data CrudRepository for database access 678

Update your application 679

Run the application 680

Next step 680

Test code using JUnit in JVM – tutorial 680

Add dependencies 680

Add the code to test it 681

Create a test 681

Run a test 682

What's next 683

Mixing Java and Kotlin in one project – tutorial 683

Adding Java source code to an existing Kotlin project 683

29
Adding Kotlin source code to an existing Java project 684

Converting an existing Java file to Kotlin with J2K 685

Using Java records in Kotlin 686

Using Java records from Kotlin code 687

Declare records in Kotlin 687

Further discussion 687

Strings in Java and Kotlin 687

Concatenate strings 688

Build a string 688

Create a string from collection items 688

Set default value if the string is blank 689

Replace characters at the beginning and end of a string 689

Replace occurrences 690

Split a string 690

Take a substring 690

Use multiline strings 691

What's next? 692

Collections in Java and Kotlin 692

Operations that are the same in Java and Kotlin 692

Operations that differ a bit 694

Operations that don't exist in Java's standard library 695

Mutability 696

Covariance 697

Ranges and progressions 697

Comparison by several criteria 698

Sequences 699

Removal of elements from a list 699

Traverse a map 700

Get the first and the last items of a possibly empty collection 700

30
Create a set from a list 700

Group elements 701

Filter elements 701

Collection transformation operations 702

What's next? 703

Nullability in Java and Kotlin 703

Support for nullable types 704

Platform types 705

Checking the result of a function call 705

Default values instead of null 706

Functions returning a value or null 706

Aggregate operations 707

Casting types safely 707

What's next? 708

Introduction 708

Cognitive complexity 708

What's next? 709

Readability 709

API consistency 709

Use a builder DSL 709

Use constructor-like functions where applicable 711

Use member and extension functions appropriately 711

Avoid using Boolean arguments in functions 712

What's next? 712

Predictability 712

Use sealed interfaces 713

Hide implementations with sealed classes 713

Validate your inputs and state 713

31
Avoid arrays in public signatures 714
Avoid varargs 715

What's next? 715

Debuggability 715

Always provide a toString() method 715

What's next? 717

Backward compatibility 717

Definition of backward compatibility 718

"Don't do" recommendations 718

The @PublishedApi annotation 721

The @RequiresOptIn annotation 721

Explicit API mode 722

Tools designed to enforce backward compatibility 722

Get started with Kotlin/Native in IntelliJ IDEA 723

Create a new Kotlin/Native project in IntelliJ IDEA 723

Build and run the application 725

Update the application 726

What's next? 728

Get started with Kotlin/Native using Gradle 728

Create project files 728

Build and run the application 729

Open the project in an IDE 729

What's next? 730

Get started with Kotlin/Native using the command-line compiler 730

Obtain the compiler 730

Write "Hello Kotlin/Native" program 730

Compile the code from the console 730

Interoperability with C 730

32
Platform libraries 731

Simple example 731

Create bindings for a new library 731

Bindings 733

Mapping primitive data types from C – tutorial 737

Types in C language 737

Example C library 738

Inspect generated Kotlin APIs for a C library 738

Primitive types in kotlin 740

Fix the code 740

Next steps 741

Mapping struct and union types from C – tutorial 741

Mapping struct and union C types 741

Inspect Generated Kotlin APIs for a C library 742

Struct and union types in Kotlin 743

Use struct and union types from Kotlin 743

Run the code 745

Next steps 746

Mapping function pointers from C – tutorial 746

Mapping function pointer types from C 746

Inspect generated Kotlin APIs for a C library 746

C function pointers in Kotlin 748

Pass Kotlin function as C function pointer 748

Use the C function pointer from Kotlin 748

Fix the code 748

Next Steps 749

Mapping Strings from C – tutorial 749

Working with C strings 749

33
Inspect generated Kotlin APIs for a C library 750

Strings in Kotlin 751

Pass Kotlin string to C 751

Read C Strings in Kotlin 752

Receive C string bytes from Kotlin 752

Fix the Code 752

Next steps 753

Create an app using C Interop and libcurl – tutorial 753

Create a Kotlin/Native project 753

Create a definition file 755

Add interoperability to the build process 756

Write the application code 757

Compile and run the application 757

Interoperability with Swift/Objective-C 758

Usage 758

Mappings 759

Casting between mapped types 764

Subclassing 764

C features 765

Export of KDoc comments to generated Objective-C headers 765

Unsupported 766

Kotlin/Native as an Apple framework – tutorial 766

Create a Kotlin library 766

Generated framework headers 768

Garbage collection and reference counting 771

Use the code from Objective-C 771

Use the code from Swift 772

Xcode and framework dependencies 772

Next steps 772

34
CocoaPods overview and setup 773

Set up an environment to work with CocoaPods 773

Add and configure Kotlin CocoaPods Gradle plugin 774

Update Podfile for Xcode 775

Possible issues and solutions 775

Add dependencies on a Pod library 776

From the CocoaPods repository 777

On a locally stored library 777

From a custom Git repository 778

From a custom Podspec repository 779

With custom cinterop options 779

Use a Kotlin Gradle project as a CocoaPods dependency 780

Xcode project with one target 780

Xcode project with several targets 781

CocoaPods Gradle plugin DSL reference 782

Enable the plugin 782

cocoapods block 782

pod() function 784

Kotlin/Native libraries 785

Kotlin compiler specifics 785

cinterop tool specifics 785

klib utility 786

Several examples 786

Advanced topics 787

Platform libraries 788

POSIX bindings 788

Popular native libraries 788

35
Availability by default 788

Kotlin/Native as a dynamic library – tutorial 788

Create a Kotlin library 789

Generated headers file 791

Use generated headers from C 794

Compile and run the example on Linux and macOS 794

Compile and run the example on Windows 794

Next steps 795

Kotlin/Native memory management 795

Garbage collector 795

Memory consumption 796

Unit tests in the background 796

Legacy memory manager 797

What's next 797

iOS integration 797

Threads 797

Garbage collection and lifecycle 798

Support for background state and App Extensions 800

Migrate to the new memory manager 801

Update Kotlin 801

Update dependencies 801

Update your code 801

Support both new and legacy memory managers 802

What's next 802

Immutability and concurrency in Kotlin/Native 803

Concurrency in Kotlin/Native 803

Concurrency overview 805

Rules for state sharing 806

36
Immutable and frozen state 806

Global state 807

Current and future models 808

Concurrent mutability 808

Atomics 809

Thread-isolated state 810

Low-level capabilities 811

Concurrency and coroutines 811

Coroutines 812

Multithreaded coroutines 813

Alternatives to kotlinx-coroutines 814

Debugging Kotlin/Native 815

Produce binaries with debug info with Kotlin/Native compiler 815

Breakpoints 816

Stepping 817

Variable inspection 817

Known issues 818

Symbolicating iOS crash reports 818

Producing .dSYM for release Kotlin binaries 818

Make frameworks static when using rebuild from bitcode 819

Decode inlined stack frames 819

Kotlin/Native target support 819

Tier 1 820

Tier 2 820

Tier 3 821

Deprecated targets 821

For library authors 822

37
Tips for improving Kotlin/Native compilation times 822

General recommendations 822

Gradle configuration 822

Windows OS configuration 823

License files for the Kotlin/Native binaries 823

Kotlin/Native FAQ 824

How do I run my program? 824

What is Kotlin/Native memory management model? 824

How do I create a shared library? 824

How do I create a static library or an object file? 825

How do I run Kotlin/Native behind a corporate proxy? 825

How do I specify a custom Objective-C prefix/name for my Kotlin framework? 825

How do I rename the iOS framework? 825

How do I enable bitcode for my Kotlin framework? 826

Why do I see InvalidMutabilityException? 826

How do I make a singleton object mutable? 826

How can I compile my project with unreleased versions of Kotlin/Native? 826

Get started with Kotlin/Wasm in IntelliJ IDEA 826

Enable an experimental Kotlin/Wasm Wizard in IntelliJ IDEA 827

Create a new Kotlin/Wasm project 827

Build and run the application 828

Update your application 831

What's next? 831

Add dependencies on Kotlin libraries to Kotlin/Wasm project 831

Supported Kotlin libraries for Kotlin/Wasm 832

Enable libraries in your project 833

What's next? 833

Interoperability with JavaScript 833

38
Use JavaScript code from Kotlin 833

Use Kotlin code from JavaScript 835

Kotlin types in JavaScript 836

Exception handling 836

Workarounds for Kotlin/JS features non-supported in Kotlin/Wasm 837

Set up a Kotlin/JS project 837

Execution environments 838

Dependencies 839

run task 840

test task 841

webpack bundling 842

CSS 843

Node.js 844

Yarn 845

Distribution target directory 847

Module name 848

package.json customization 848

Troubleshooting 848

Run Kotlin/JS 848

Run the Node.js target 849

Run the browser target 850

Development server and continuous compilation 851

Debug Kotlin/JS code 853

Debug in browser 853

Debug in the IDE 854

Debug in Node.js 857

What's next? 857

If you run into any problems 857

39
Run tests in Kotlin/JS