15

I created two modules in single android project.

  1. app module(which is default my app module)

  2. and another added library module.

Now app module have many java classes. i want to access .Java class of app module in library module.

Module app has a class DatabaseHelper in package xyz

Now I want to import class DatabaseHelper in Library module.

DatabaseHelper is not recognized by android.

Questions,

Is it possible to import Class from a app module to another module?

any other way.

MyFiles

build.gradle(app)

compile project(':materialList')

setting.gradle

include ':app', ':Library'
Harsh Bhavsar
  • 1,561
  • 4
  • 21
  • 39

3 Answers3

8

Is it possible to import Class from a app module to another module?

This won't be possible as this will be creating a circular dependency.

However there is a pattern that can be utilized in this scenario:

  • Define the data type : DatabaseHelperContent in the library module
  • Define an interface DatabaseHelperI in the library module the implementer of which is supposed to provide the data.
  • Create a DatabaseHelperImpl class implementing the DatabaseHelperI interface, providing the data (this class is in the app module)
  • Initialize the interface as the instance of class ABC while referring to it in the Application class, so that the data from the class can be dynamically passed to the sub-module.

This would become even simpler with some dependency-injection framework like Dagger where you can just specify provider of the interface in the @Module class and use the injected data from the common provider everywhere.

premkamal
  • 133
  • 1
  • 7
4

No, there is no way. Rethink your design. Maybe move DatabaseHelper into library project?

In your design, there would be a circular dependency between app module and library module.

The purpose on other modules is to separate completely independent pieces of code and move them to external place. And use them in another modules.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
3

It's quite an old question and I am sure the author has found a solution, but I think the question lacks this answer which many people would like to know.

So, actually, as suggested in other answers, this often might be caused by an issue with your architecture.
But sometimes it may be reasonable. For instance, if you need to access your application id inside a library or in many other cases.

So, if you need to access a resource from the app module in a library module,
it can easily be done with help of dependency injection.

For instance, with Dagger it can be done like this:

1.Create a module that will provide a shared resource. Let's call it the Integration module.

@Module()
class IntegrationModule {

    @Provides
    @Named("foo")
    fun provideFoo() = "Hey bro"
}

2.Include it in your App component

@Component(modules = [
    AndroidSupportInjectionModule::class,
    AppModule::class,
    IntegrationModule::class,
    YourLibraryModule::class
])
@Singleton
interface AppComponent : AndroidInjector<App> {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun app(app: Application): Builder

        @BindsInstance
        fun context(context: Context): Builder

        fun build(): AppComponent
    }
}

3.Inject it somewhere in your library

@Inject @Named("foo") lateinit var foo: String

That's it.
Base Dagger implementation code is omitted for simplicity.

Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54