I was trying to understand how to create multiple independent modules inside a project and use them in the main app
module. I am using android studio Arctic Fox 2020.3.1. I have difficulty in importing the module into Kotlin code. Here is the step I followed.
I have created a ‘basic activity’ project named MyAppProj
. It created a project with module app
inside it. (build+run works fine)
Next I added a new module to the project MyAppProj
. Since I needed to call C++, I created a ‘Android native library’ module. As per the suggestion from Android Studio, I used MyAppProj:nativelib
as the module name. I didn’t know what that ":" was for. (build+run works fine)
Now I have the following structure:
█ Question: What should I do to use the NativeLib
class (defined inside MyAppProj:nativelib
module) inside the app
module’s MainActivity.kt
file?
All the paths that I tried so far have failed, such as:
import com.myhomeorg.myappproj.nativelib
import nativelib
Note: I am a beginner in Android development. Your help will be greatly appreciated!
3
Answers
Depend on the project, which should expose the JNI bindings:
But one could as well publish to
mavenLocal()
or aflatDir
repository.In order to import it you should add to
build.gradle
:and this also to
settings.gradle
:And then on Kotlin you can import the classes you need like any other.
You need to add below code in build.gradle:
and then add below code in settings.gradle:
include ‘:MyAppProj’
in Android studio click on file -> new -> import module -> add library location path (MyAppProj file) -> finish
it will automatically add