πŸš€ 8.9 Released! β†’ ⚑️ New Node-API Engine Preview, πŸ“² ns widget ios, πŸ’… Tailwind v4 and more...
Read Announcement

Adding native code to an application ​

There are different ways to add native code to an Android application. You can add .jar and .aar files, or Java/Kotlin source files in App_Resources/Android/libs and App_Resources/Android/src respectively.

bash
App_Resources/
β”œβ”€Android/
β”‚β”œβ”€app.gradle
β”‚β”œβ”€libs/
β”‚β”‚β”œβ”€HelloAndroidLib.aar# Android Archive
││└─HelloJavaLib.jar# Java Archive
│└─src/
│└─main/
β”‚β”œβ”€java/
β”‚β”‚β”œβ”€com/example/HelloKotlin.kt# Kotlin source code
││└─com/example/HelloJava.java# Java source code
│└─res/
└─...more

Adding Java code ​

Define the java file in App_Resources/Android/src/main/java.

java
// HelloJava.java
package com.example;

publicclassHelloJava {
public String getString() {
return"Hello from Java!";
 }
}

Given the example above, your JavaScript or TypeScript code can reference the Java code by using the full class name:

typescript
consthelloJava=new com.example.HelloJava()
console.log('Java says: '+ helloJava.getString())
// prints: Java says: Hello from Java!

Note

When using TypeScript, you may need to generate typings, or alternatively declare the top level package name as any.

typescript
declareconstcom:any

Adding Kotlin code ​

Configuring Kotlin ​

Enable kotlin ​

When using Kotlin, it must be enabled first.

Set useKotlin=true in App_Resources/Android/gradle.properties (create the file if it doesn't exist).

ini
useKotlin=true

Configure Kotlin version ​

Configure the version of Kotlin to use in the application in App_Resources/Android/before-plugins.gradle (create the file if it doesn't exist).

groovy
project.ext {
 kotlinVersion ="1.9.10"
}

Using kotlin ​

Define the kotlin file in App_Resources/Android/src/main/java.

kotlin
// HelloKotlin.kt
packagecom.example

classHelloKotlin {
val hello ="Hello from Kotlin!"
}

Given the example above, your JavaScript or TypeScript code can reference the Kotlin code by using the full class name:

typescript
consthelloKotlin=new com.example.HelloKotlin()
console.log('Kotlin says: '+ helloKotlin.hello)
// prints: Kotlin says: Hello from Kotlin!

Note

When using TypeScript, you may need to generate typings, or alternatively declare the top level package name as any.

typescript
declareconstcom:any

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /