Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access app Module Classes from Library Classes in Android?

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'
like image 470
Harsh Bhavsar Avatar asked Nov 15 '16 12:11

Harsh Bhavsar


People also ask

Does Android library have application class?

You can extend the library's Application class in the application project and provide any additional implementation. If you do this, you will have to use this extended Application in the manifest.

What is Android library module?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

Where are modules Android Studio?

Click File > New > New Module. In the Create New Module window that appears, Android Studio offers the following device modules: Phone & Tablet Module.

Can we import module from source in Android Studio?

Select the source directory of the Module you want to import and click Finish. Open Project Structure Dialog (You can open the PSD by selecting File > Project Structure) and from the left panel click on Dependencies. Select the module from the Module(Middle) section In which you want to add module dependency.


3 Answers

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.

like image 132
premkamal Avatar answered Oct 14 '22 07:10

premkamal


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.

like image 24
Leonid Ustenko Avatar answered Oct 14 '22 07:10

Leonid Ustenko


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.

like image 23
R. Zagórski Avatar answered Oct 14 '22 07:10

R. Zagórski