How to import class from another module in android studio?

JavaAndroidAndroid Studio

Java Problem Overview


I created two modules in single android project, named it x and y.

  1. Module x has a class Egg (Package: com.example.x)
  2. Module y has a class Foo (Package: com.example.y)

Now I want to import class Foo in the class Egg, for which I wrote the statement mentioned below in class Egg

Import com.example.y.Foo;

Now, Foo is not recognized by android.

Questions,

> Is it possible to import Class from a different module using just > import statement? > > Do I need to create library of Module y and then import created > library into module x?

Or may the solution is something else.

Java Solutions


Solution 1 - Java

Make sure of the following:

In settings.gradle, you should have: include ':x', ':y'.

In x/build.gradle, you should add y as a dependency:

dependencies {
        compile project(':y')
        // other dependencies
}

Solution 2 - Java

now when create new module,settings.gradle add automatically this module.after that u should add this line:

    dependencies {
    implementation(
    ...,
    ..,
            project(":y")
)
}

Solution 3 - Java

Combining and correcting two previous answers the best solution is to add this single line to x/build.gradle -> dependencies

implementation project(':y')

compile project() - is deprecated and won't work anymore

If you want to implement just a single module there is no need to use implementation(..., .., project(":y") structure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPalakView Question on Stackoverflow
Solution 1 - Javapdegand59View Answer on Stackoverflow
Solution 2 - JavaFelhi AbdelhafidhView Answer on Stackoverflow
Solution 3 - JavaDanielView Answer on Stackoverflow