7

I don't understand the documentation:

Package-Level Functions

All the functions and properties declared in a file example.kt inside a package org.foo.bar, including extension functions, are compiled into static methods of a Java class named org.foo.bar.ExampleKt.

// example.kt
package demo

class Foo

fun bar() {
}

// Java
new demo.Foo();
demo.ExampleKt.bar();

my code below.

compile error; build failed:

thufir@dur:~/NetBeansProjects/kotlin$ 
thufir@dur:~/NetBeansProjects/kotlin$ gradle compileJava

> Task :compileJava
/home/thufir/NetBeansProjects/kotlin/src/main/java/net/bounceme/dur/kotlin/App.java:12: error: package demo does not exist
        new demo.Foo();
                ^
/home/thufir/NetBeansProjects/kotlin/src/main/java/net/bounceme/dur/kotlin/App.java:13: error: package demo does not exist
        demo.ExampleKt.bar();
            ^
2 errors


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
1 actionable task: 1 executed
thufir@dur:~/NetBeansProjects/kotlin$ 

java source:

package net.bounceme.dur.kotlin;

import java.util.logging.Logger;

public class App {

    private static final Logger LOG = Logger.getLogger(App.class.getName());

    private void run() {
        LOG.info("running");

        new demo.Foo();
        demo.ExampleKt.bar();

    }

    public static void main(String[] args) {
        new App().run();
    }
}

kotlin source:

package demo;

class Foo

fun bar() {
}

project:

thufir@dur:~/NetBeansProjects/kotlin$ 
thufir@dur:~/NetBeansProjects/kotlin$ tree
.
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   └── tmp
│       └── compileJava
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── net
    │   │       └── bounceme
    │   │           └── dur
    │   │               └── kotlin
    │   │                   └── App.java
    │   └── kotlin
    │       └── example.kt
    └── test
        └── java
            └── AppTest.java

18 directories, 9 files
thufir@dur:~/NetBeansProjects/kotlin$ 
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 2
    Apply the kotlin-android plugin to your library module build.gradle (see the docs): apply plugin: 'kotlin-android' – developer Sep 16 '19 at 07:57

4 Answers4

13

use this in your build.gradle (module):

apply plugin: 'kotlin-android'
m.sajjad.s
  • 711
  • 7
  • 19
8
  1. Make sure you have the Kotlin plugin installed in Android Studio
  2. add Kotlin to the class path in your projects gradle.build file:
         dependencies {
            classpath "com.android.tools.build:gradle:4.1.2"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
            classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.4.21"
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
  1. apply the kotlin-android plugin on your modlues (app) gradle.build like this:

    plugins {
        id 'com.android.application'
        id 'kotlin-android'
    }
    
0

The demo folder should be under the src/main/kotlin and the example.kt under it.

So it should be src/main/kotlin/demo/example.kt

[edit] It is not required to match directories and packages: source files can be placed arbitrarily in the file system.

[edit2] I made an example worked :

Java code

package test;

import demo.HelloWorldKt;

 public class Test {
    public static void main(String[] args) {
       ExampleKt.bar();
    }
}

Kotlin code :

package demo

fun bar() {
    println("yeah")
}

Where the kotlin file is helloWrold.kt directly under src/main/kotlin

The import of the kotlin file seems missing in your java file.

Xavier Bouclet
  • 922
  • 2
  • 10
  • 23
  • Absolutley, thx.yes, I made that change already; will update whole question to reflect that. – Thufir Nov 05 '17 at 00:41
  • no. wait. you say ../kotlin/demo/example.kt but that's NOT what the demo (link at bottom of q) seems to do. They just seem to do ../kotlin/example.kt I think... – Thufir Nov 05 '17 at 00:43
  • 1
    Yeah but the error tell you that it’s try to find demo.example.kt and you don’t have the demo package under your kotlin source files. – Xavier Bouclet Nov 05 '17 at 00:46
  • new demo.Foo(); is not useful but is just an example of how you can create an kotlin object from a java class. It's confusing though – Xavier Bouclet Nov 05 '17 at 01:05
  • 1
    It's confusing but I read in the documentation that It is not required to match directories and packages: source files can be placed arbitrarily in the file system. – Xavier Bouclet Nov 05 '17 at 01:09
  • apply plugin: 'kotlin-android' to your library module build.gradle. – developer Sep 16 '19 at 07:59
0

//app level build.gradle

    id("com.android.application")
    id("org.jetbrains.kotlin.android") //Added for kotlin
}


//project level build.gradle
plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 15 '23 at 13:47