skip to Main Content

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:

Project 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


  1. Depend on the project, which should expose the JNI bindings:

    dependencies {
        implementation project(":MyAppProj")
    }
    

    But one could as well publish to mavenLocal() or a flatDir repository.

    Login or Signup to reply.
  2. In order to import it you should add to build.gradle:

    dependencies {
        implementation project(":MyAppProj")
    }
    

    and this also to settings.gradle:

    include ':MyAppProj'
    

    And then on Kotlin you can import the classes you need like any other.

    Login or Signup to reply.
  3. You need to add below code in build.gradle:

    dependencies {
        implementation project(":MyAppProj")
    }
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search