I created two modules in single android project.
app module(which is default my app module)
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'
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.
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.
Click File > New > New Module. In the Create New Module window that appears, Android Studio offers the following device modules: Phone & Tablet Module.
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.
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:
library
moduleapp
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With