abc
abc

Reputation: 93

Import func from file in diffrent directory kotlin

I have a file structure that looks like this -

-- Parent Dir -- -- Dir a -- - main.kt -- Dir b -- - app.kt Let's say app.kt has a function fun meaningOfLife():Int{return 42}
How do I import meaningOfLife in main.kt

Upvotes: 5

Views: 14445

Answers (1)

Yoni Gibbs
Yoni Gibbs

Reputation: 7018

The folder structure doesn't strictly speaking matter too much. What is important is the packages (though these should generally match the folder structure in some way), and whether you're separating the project into different modules (which I'm assuming in this case you're not). I take it your meaningOfLife function isn't in a class, it's just a top-level function in a ".kt" file? If so, just add an import statement at the top of your "main.kt" file with the package name and the method pointing at the definition of the meaningOfLife function. e.g. if "app.kt" has this:

package com.something.b

fun meaningOfLife()...

Then in "main.kt" you should have this:

package com.something.a

import com.something.b.meaningOfLife

fun test() {
    meaningOfLife()
}

Upvotes: 10

Related Questions