GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • NativeScript
  • Blog
  • Community
  • Support
  • Open NativeScript GitHub
  • Adding Native Code
  • Adding Java/Kotlin Code

This view

  • Original page
  • New address

GeezerVison is showing docs.nativescript.org — Adding Java/Kotlin Code to an application | NativeScript

NativeScript 9.1 Released → V8 14.9, Vite 8 HMR, built for rapid agentic visual iteration
Dig in→
  • Docs
  • Tutorials
  • Preview
  • Snacks
  • Plugins
  • UI Components
  • API Reference
  • Getting Started

    • Introduction
    • Environment Setup
      • Windows
      • macOS
      • Linux
    • Creating a Project
    • Choosing an editor
    • Troubleshooting
  • Development Workflow

    • CLI Basics
    • Running
    • Debugging
    • Publishing
    • Plugins
    • Recommended Plugins
    • Testing
    • Updating
      • To Latest
  • Project Structure

    • App_Resources/
    • src/
      • fonts/
      • app.css•scss
      • main.js•ts
    • nativescript.config.ts
    • package.json
    • references.d.ts
    • tsconfig.json
    • vite.config.mts
    • webpack.config.js
  • Configuration

    • Config Reference
    • Vite Reference
    • Vite Framework Flavors
    • Webpack Reference
  • Fundamental Concepts

    • Accessibility
    • Animations
    • Best Practices
    • Commonly Used APIs
      • @nativescript/core
        • Application
        • ApplicationSettings
        • Color
        • Connectivity
        • Device
        • FileSystem
        • FPS Meter
        • Http
        • ImageCache
        • Observable
        • Screen
        • Trace
        • Utils
        • XmlParser
    • Data Binding
    • Error Handling
    • Gestures
    • Navigation
      • Using Frames and Pages
      • Using Modals
    • Property System
    • Styling
  • Crash Reporting

    • Using Sentry
  • Developing with Vision Pro

    • Developing with visionOS
    • Previewing visionOS from StackBlitz
  • Agentic Coding

    • Leverage AI assistants
    • Using the Docs with AI
  • Advanced Concepts

    • Adding Native Code
      • Adding Java/Kotlin Code
      • Adding ObjectiveC/Swift Code
      • Generating TypeScript types
    • Extending Native Classes
      • Android
      • iOS
    • Create Custom Native Elements
    • Customizing View Elements
    • Shared Element Transitions
    • Multithreading
    • Multi-Window Development
    • Code Sharing
    • Metadata
    • Marshalling
      • iOS Marshalling
      • iOS Runtime Types
      • Android Marshalling
    • Hooks
    • Platform Version Handling
    • Widgets for iOS
Are you an LLM? You can read better optimized documentation at /guide/native-code/android.md for this page in Markdown format

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;

public class HelloJava {
  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
const helloJava = 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
declare const com: 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
package com.example

class HelloKotlin {
  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
const helloKotlin = 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
declare const com: any
Previous
←Adding Native Code
Next
Adding ObjectiveC/Swift Code→
On this page
  1. Adding native code to an application
  2. Adding Java code
  3. Adding Kotlin code
    1. Configuring Kotlin
    2. Using kotlin

Contributors

OmbuwebNathanWalkerrigor789vallemar
Edit this page

Last updated:

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.