Injecting dependency inisde Worker from other module - android

I have been struggling with providing dependency on Android workers using the dagger2. I have followed numerous blogs about how I can achieve it but I somehow have missed something that I am not able to build my dependency graph with it.
The following is the git repo for the complete source code https://github.com/youranshul/Dagger_worker
The issue I am facing is that how can I provide the dependency which is a part of the activity module but the custom work factory is the part of AppModule.
EDIT 1:
The link github.com/google/iosched provided by #Yavor is a little difficult for me to grab the things keeping my basics in dagger in mind. I have actually looked into the [Dagger from google link][4] and built some understanding of how things work with subcomponents. I have implemented the learning into the mentioned git project and found that I could provide all the dependencies to my worker class. I would like to have a review of my changes and know if I need to improve further on it.

Related

MVVM repository in android

Am working on a huge android project that has more than 50 APIs request
and using the MVVM pattern, My question is:
can I add all the requests in the same app repository or I must create a repository for each service?
As others suggested in the comments you should first define the modules of your app and then create the corresponding Repositories. This way you can easily maintain and test your application.
I strongly suggest you to have a look on this https://github.com/nickbutcher/plaid and mostly this video https://youtu.be/Sy6ZdgqrQp0
A good solution to this problem is, You must not implement all your API calling code to the same repository as it will become a massive single repository class. It will also be violating design principles i.e. rule of 30 as you are saying you have at least 50 APIs to work with.
Also, it is not a good practice to modify a class, again and again, see Open Close Principle.
You can make multiple API calling classes under the same package name.

Need help learning to write tests for this Android app (Kotlin, MVVM, Room, Dagger2)

https://github.com/Spheniscine/dimsum-leadmanagement
I am currently a beginner to Android development.
This is an Android app I am currently working on, notes on its current working state is in the description. I am currently trying to learn how to write more robust code using MVVM (Model-View-View Model) for a testable architecture, and was supposed to learn TDD (test-driven development) patterns, but I seem to have hit a snag as to writing the actual tests.
There are a number of instrumented tests already here testing the simplest navigation stuff, but I am having trouble figuring out the best way to write tests for things like whether a salesperson shows up on the list or whether adding a salesperson works, without referencing or affecting the actual database on the phone. I'm also struggling to write unit tests for the backend (SalespersonMngViewModel and DimsumRepository)
From what I can glean, I need to use something called Dagger to inject a mock database or repository as needed... as evidenced by the "di" folder, I have managed to use it to make the repository injectable, however I am having trouble figuring out how to actually use it for testing. Additionally, I am having trouble figuring out how to extend that pattern to inject the ViewModel. From the tutorials and info I looked around for, it seems to require something called a ViewModelFactory, which again, I am having trouble understanding how to implement.
The problem is that the tutorials I found either talked about a whole bunch of other dependencies (e.g. Retrofit, RxJava, Glide) that I haven't had time to learn about yet, or were missing key features or use cases that I needed (e.g. there was an MVVM tutorial I followed that didn't have testing, and then there was a testing tutorial that used MVVM, but with neither a Room database nor dependency injection)

Android Dagger 2.11 and Retrofit dynamic URLs

I’m working on a project that is based on the GithubBrowserSample demo application that’s on the Android Architecture Repo on Github.
So far, I was able to inject dependencies to my App, activities and fragments and ViewModels. Also I was able to add more Modules; right now I use two, one for Shared preference and the other a NetModule that has all the mumbo jumbo related to Retrofit/OkHttp.
But here’s the catch: currently the demos that I found that shows how to implement the Retrofit/OkHttp module has a static base URL, which doesn’t work for me since that information is available until the User provide it on the Login section…
Digging around I found that one solution is to create a Submodule (StackOverflow post) with a given Scope Instantiate the component inside my Activity/Fragment. But because I’m using Dagger 2.11 for Android to perform the injection, I have no idea how to do so…
Is there an example that I can look around, or should I give up on this path and take the OkHttp interceptor to change the URL?
Thanks in advance.

Is it advisable to use dagger 2 in a library

I would be creating a library (.aar) at my current workplace. It has a lot of complicated business processes and would definitely need a lot of automated tests, due to which I was planning on using dagger in my library.
But as it is a library, it needs to be as small as possible and depend on as fewer dependencies as possible. Not to mention that dagger just bloats anything it is used with.
So, I am in crossroads and unable to decide what should be my approach.
Can someone please help me come to a conclusion.
there isnt any problem with using dagger in a library if you use the dagger just inside. i mean as long as you dont expect the user of that to provide some dependencies for you from out of the library.
dagger makes the code complicated but not for yourself. assume the person who uses the library knows nothing about DI or dagger.
I myself have some project including a library using a dagger and even needing some dependencies to be provided from out of the library but since the whole project is mine and im not gonna export the library everything is ok.
so this depends how you gonna use it and i suggest if you wanna give this library to others dont expect them to implement dagger and provide some dependencies for you.

Any examples of using Dagger (or another DI IoC container) in AAR?

Do you have a simple example of DI built on Gradle? I need to see setter injection used to express an optional dependecy, just as per the standard design pattern. Thank you
EDIT
To better define the context of the Q...
I already have my apk -OpenTweetSearch- on Google Play
https://play.google.com/store/apps/details?id=org.new_year
I would like to integrate my Twitter OAuth 2.0 with Google Drive SDK OAuth 2.0.
I've found here the Gradle dependencies of Google Drive SDK: https://gist.github.com/johnscheible/5994719
There is an exclusion of transitive dependencies. "However, exclude feels like the sort of thing that an app developer might need but that ideally is avoided" (see Gradle for Android, AARs, and Conditional Dependencies).
That's why am asking for code example of gradle and dependency injection in the context of Google Drive SDK: is would suffice also and example in c# .net WPF to help me.
In any case the code example is not the main point of my question and you could answer without any code examples: in fact a Design Pattern is something you should discuss before code implementation and it should be managed in a layer of abstraction separate from the layer of the building system. So I fail to understand why my Q is put on hold for a secondary point that is not the focus of my Q.
I am Frank Du mentioned above. Thank you for checking my older blog. In fact, I've already adopted a new method, which is simpler and better.
The link has motivated me to share my android + gradle + dagger setup. Of coz I learned the tips from many sources, as I said in my blog. The github project covers quite a few topics, such as aar dependencies, module reuse, dagger code gen, etc. Here are the links:
Check out the source codes: https://github.com/frankdu/android-gradle-dagger-tutorial
The blog post: http://www.frankdu.com/blog/2014/01/18/tutorial-create-android-gradle-project-with-dagger/
So, hope it helps!

Categories

Resources