0

There is a kotlin file in a project dependency jar (called, for example, KotlinClass) with an inline function

package io.pack

inline fun <T> every(){
    ///does stuff
}

If I import it into a Java class as a static:

import static io.pack.KotlinClass.every;

The import is recognised.

If I import it into a Kotlin class:

import io.pack.every

or (this ought not to work anyway, but tried for completeness) as

import io.pack.KotlinClass.every

It is not recognised.

(Note: If I create my own Kotlin file with an inline function, then that can be imported into a Kotlin class with no problem. The problem is when importing from a specific project dependency.)

What might be stopping the import of this function into a kotlin class?

jingx
  • 3,698
  • 3
  • 24
  • 40
Jason Harris
  • 53
  • 2
  • 9
  • One thing that could break using top-level functions from dependencies is the `*.kotlin_module` file not packaged correctly into the library artifact. You can check that in the JAR, these files should be placed as `META-INF/*.kotlin_module`. If they are missing, the compiler might not be able to see top-level declarations in the library. – hotkey Apr 22 '18 at 12:50
  • You can try rmb on function, and then copy reference. What is copied then? – Cililing Apr 22 '18 at 12:59
  • RMB/copy reference on the function in the decompiled .class file is `io.mockk.MockKKt#every` – Jason Harris Apr 22 '18 at 16:29
  • META-INF/*.kotlin_module in the jar does contain the name of the kotlin file (in the jar) where the functions are defined – Jason Harris Apr 22 '18 at 16:39

1 Answers1

2

It works for me. I create a Kotlin module with a file called TopLevelStandalone.kt:

package io.pack

inline fun every(){
    print("Inline")
}

fun normalFun() {
    print("Normal")
}

The jar this project builds into contains the class file, and a META-INF/top-level-standalone.kotlin_module containing:

io.packTopLevelStandaloneKt

I then create another Kotline module, manually adding the jar to its dependencies. I'm now able to call every or normalFun:

import io.pack.every
import io.pack.normalFun

fun main(args: Array<String>) {
    every()
    normalFun()
}
jingx
  • 3,698
  • 3
  • 24
  • 40
  • Thanks: the specific library is `io.mockk`. The dependent jar, (in gradle: `testCompile group: 'io.mockk', name: 'mockk', version: '1.7.15'`) is in my classpath and _does_ contain a `META-INF/mockk.kotlin_module` file but `import io.mockk.every` (the function I need) doesn't work when by all accounts it should (note, I _can_ import it as a static into a Java class) – Jason Harris Apr 22 '18 at 20:25
  • 1
    For me it recognizes the import, but gives this error "Cannot access class 'io.mockk.MockKStubScope'. Check your module classpath for missing or conflicting dependencies" at where `every` is called. Are you getting that? It's strange because I actually can't find class `io.mockk.MockKStubScope` anywhere in the jar. It is in the source tree though. – jingx Apr 22 '18 at 21:49
  • I also have mockk-dsl-jvm-1.7.15.jar, mockk-agent-1.7.15.jar and 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.2.40' on my classpath. – Jason Harris Apr 23 '18 at 08:13