How to access app module from feature module? - android

I have an app module and a feature module named LoginModule.
I would like to call a method inside the app module from LoginModule.
Is there any way?
Of course, I know this is not standard at all. But I have to use it temporarily.
Because I just wrote part of the application as a module (LoginModule) and the rest of the application is still in the main/app module and I have to go back to the main/app module to run the rest of the application.
I plan to solve this problem with Navigation-Component in the future.

Related

Best practices for testing multi module android app

I have an application with 4 different modules. Network layer is separated in different module and I created a fake repository for unit testing in it.
The problem is that I cannot access that fake repository in my main module test classes (for creating test classes for viewmodels). I tried different solutions from google and none of them are working.
If I use something like this in gradle
testImplementation(project(":data"))
Test classes are still not visible.
My question is what are the best practices when writing tests for multi module android application? Is good practice to create one module for test and put everything related to testing from every module in that one? Or is there a way to make test classes from other modules visible in my main module?

Android How to Improve my app dependencies

I'm trying to improve my app dependencies and came across a problem. My code base is shared with another app, so I have modules that are shared between these two apps. The below image is a simplified version of what I currently have.
Currently, my app build gradle sets the AppNetwork dependency as implementation and the AppNetwork declares the SharedNetwork dependency as api. It's api because app module needs to know stuff about the SharedNetwork module (more on this next). App module also implements the SharedFuncionalities module.
SharedNetwork contains an INetwork interface. This interface is extended by IAppNetwork interface that it's implemented by AppNetworkImpl (the latter two files are placed in the AppNetwork module).
Here is an example of why this is being set like this.
My app module creates AppNetworkImpl and also creates an ItemRepository (location is inside the SharedFuncionalities module) that takes INetwork (implemented by AppNetworkImpl) as a param in its constructor. This allows the ItemRepository to "talk" with the SharedNetwork module.
If I set AppNetwork to declare the SharedNetwork dependency as implementation thus making my app module unaware of the SharedNetwork (that makes sense to me. I would then use AppNetworkImp as the bridge between the app module and the SharedNetwork module) I then lose the ability to create objects in the SharedFuncionalities that use AppNetworkImpl as the bridge for using the SharedNetwork.
What I would like to know if I have the possibility of the app module only knowing about AppNetwork but still create objects in SharedFuncionalities that can "talk" with the SharedNetwork.

Network module on a multi-module project android

I'm trying to implement this multi-module design on my app, the thing is that I'm thinking about create an "api" or "network" module where I create the retrofit instance (I'm using Dagger2, but it doesn't matter, if you have an example or pseudo with any other DI framework feel free).
Problem?
I've been working on an app that I had this similar architecture but it ended up with a big module because I had organised the instance of a retrofit and then inside of it a sub-packages with each feature where I have the services and the responses.
This helped me with a problem that I had for instance, use the same service on a different features.
Example
Home feature I need to call the Feature1Service to show X
Feature1 need to call Feature1Service to show more stuff
I've thought with a solution that instead of creating the api or network module create every service inside of that feature, but doing this is a problem.
So I'd like to listen options or if you have worked with that app with modules how did you do that?
Short question
How do you use on multi-module project where you share services between features? (Retrofit)

How can i access an activity from another feature module

I am creating an instant app, which include application module, base feature module, instant app module and an another feature module. Problem is i am not able to access the activities of application module from base-feature and feature module and same between base-feature module and feature module but i am able to access activity of base-feature module from application module.
Right now i am accessing the activities using :
Intent i = new Intent(this,
Class.forName("com.demo.test.appmodule.TextActivity"));
by this method studio don't show me any errors at compile time.
Is there any other way for communication between two different feature modules?
Why i am able to access base-feature module activity from application module but not vice versa?
can we access activities of application module from base or any other feature module?
Can i have a link which define the project structure for an instant app
Thanks in advance
The reason why you can't communicate directly between features is because they're independent from one another.
The correct way to handle this is calling it with its URL, example: android-instant-apps/hello-feature-module/HelloActivity.java
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://hello-feature.instantappsample.com/goodbye"));
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);
In an instant-app structure, the base acts as a library for the feature modules, and the features are built into APKs. In an installed-app structure, both the base and features act as libraries for the application module. Some explanation can be found here:
Why use the new Android feature plugin over the library plugin?
Android: Can you add activities to the Instant App module?
There used to be a page # https://g.co/instantapps that explained the structure of instant apps, but looks like it's missing. However, you can take a look at:
https://android-developers.googleblog.com/2017/08/android-instant-apps-best-practices-for.html
bottom of page # codelabs/android-multi-feature-instant-app/#3
And no, you won't be able to directly access activities of the application from a feature. As an installed-app, com.android.feature modules are compiled/behave as com.android.library modules, so apply the same rules here: the application depends on the library, not the other way around. To traverse that direction, you will need to use the same kind of Intent as shown above.
Anything in com.android.application will be isolated from the feature modules of the instant-app, and will only appear in the installed-app.

Android Instant Apps multiple activities

just like all of you, I've made MyFirst Instant App from AndroidInstantAppDemo, but what if I want to add more activities into my app?
I want to provide multiple links for them.
Should I simply add all of those activities under app module and provide dependencies{... implementation project (":base") } in build.gradle.
Along with that, adding the different path with same host address in .manifest file.
Or
Put all the activities under base module only.
If yes (2nd options), does that mean that we should transfer data from app module to base module, in order to add InstantApp functionality into our project.
I think I'm not very familiar with all three modules of them, and the PROJECT STRUCTURE just provided an overview of these modules. Can anyone help?
Basically both your Instant app module and application module depends on feature modules. As far as I know Instant app and application module does not contain any activities.
Out of all these feature modules there should be a baseFeature module. The feature modules should be less than 4mb in size. Now, when a linked is clicked for an Instant app, Google Play downloads base + feature1 apks and installs them in background. If you wants to travel between feature modules you can do that by using deep links.
A feature module can contain any number of activities but with the constraint that it's size should be less than 4 mb.
I will suggest not to put any activities in application module. Just make some feature modules and link them with a URL. The feature module works like a library generating an aar file for your installable app.
If you want to provide multiple links, maybe this can help - Here

Categories

Resources