Anki-Android icon indicating copy to clipboard operation
Anki-Android copied to clipboard

Implement dependency injection using hilt and inject CoroutineDisptacher

Open ShridharGoel opened this issue 2 years ago • 14 comments

ShridharGoel avatar Sep 16 '22 16:09 ShridharGoel

What does this do to build times?

david-allison avatar Sep 16 '22 17:09 david-allison

What does this do to build times?

I think there's no significant difference in build time, though it needs to be checked in detail.

ShridharGoel avatar Sep 16 '22 20:09 ShridharGoel

Any idea how we can exclude a directory from compilerArgs, specifically -Xlint:deprecation? Will need to exclude the build folder, since there's no point of checking for deprecation there and it leads to an error showing a deprecation in a hilt generated file. I tried using exclude '**/AnkiDroid/build/**' but it doesn't work, what can be the correct path to use here? Suggestions would be appreciated.

ShridharGoel avatar Sep 16 '22 20:09 ShridharGoel

lint at least offers this:

https://developer.android.com/reference/tools/gradle-api/7.1/com/android/build/api/dsl/LintOptions

     checkGeneratedSources true

Not sure if lint can check for deprecation or not, but if lint can detect deprecation then perhaps the compiler flag could go away and we could rely on lint with this flag.

Otherwise I'm not sure how this could be done. I did a brief search for a repo for hilt and could not find one ? But it strikes me that they should have a version that does not generate deprecated APIs, or if it does it does similar to us and generates a conditional where they use the old or the new depending on runtime SDK and suppresswarnings

mikehardy avatar Sep 16 '22 21:09 mikehardy

Should be fine after #12431 gets merged.

ShridharGoel avatar Sep 17 '22 20:09 ShridharGoel

I'm for dependency injection as this would be a step towards some sort of architecture where everything is not tied to everything. However I'm not sure this PR is the way to start with this.

Unless @ShridharGoel has plans for more work on top of this in the real near future, I wouldn't approve this:

  • the dependency injection in LoadPronunciationActivity is useless as the dispatchers are not used. There are no tests and writing some would require a refactoring(with the complexity of needing to abstract all http calls) of the entire activity. Even if this were to be done we still wouldn't use the dispatchers there as a ViewModel might be more appropriate(I've done such a refactoring on a local branch)
  • following issue one, this will increase build time(kapt is slow and Google is(slowly) replacing it with ksp anyway).
  • following issue two, the cost benefit of this is not great as we get one injection which isn't used versus: increased build times, more maintainer work to keep updated the plugins/libraries and more chances for potential issues with build and kapt errors to deal with.

Just my 2:moneybag:

lukstbit avatar Sep 18 '22 15:09 lukstbit

the dependency injection in LoadPronunciationActivity is useless as the dispatchers are not used

@lukstbit What do you mean by this? Aren't they being used inside the activity?

ShridharGoel avatar Sep 18 '22 15:09 ShridharGoel

The plan here is to use dependency injection at more places in the app and not only limiting it to coroutine dispatchers.

ShridharGoel avatar Sep 18 '22 15:09 ShridharGoel

this will increase build time

We're already using annotationProcessor, so isn't it already factored in?

ShridharGoel avatar Sep 18 '22 15:09 ShridharGoel

as we get one injection

This is only for the initial PR. Further changes would be there in different pull requests.

ShridharGoel avatar Sep 18 '22 15:09 ShridharGoel

@mikehardy auto-service was added in 1a81df55fbc929750e607e5aff823b16791b0670 (https://github.com/ankidroid/Anki-Android/pull/4969)

    compileOnly "com.google.auto.service:auto-service-annotations:1.0.1"
    annotationProcessor "com.google.auto.service:auto-service:1.0.1"

This was for ACRA. Is this still necessary as we've partially moved away from annotation-based ACRA instantiation

EDIT: Our one usage:

https://github.com/ankidroid/Anki-Android/blob/2f1f7891151924b19a2019486b44f59fdc30d132/AnkiDroid/src/main/java/com/ichi2/anki/analytics/AcraAnalyticsInteraction.kt#L27-L40

EDIT: https://github.com/ACRA/acra/wiki/Custom-Extensions#registering-an-extension - looks like this can be manual. We may want to extract this to another issue if it's a good target for reducing compile time

david-allison avatar Sep 19 '22 03:09 david-allison

Hmm annotations fir Acra are on the way out - the PR for Acra has the final structure more or less and the work that already went in killed annotations I think. Could be a remove it and see if it works test

mikehardy avatar Sep 19 '22 03:09 mikehardy

Edited the comment above (sorry), still used in one case

david-allison avatar Sep 19 '22 03:09 david-allison

What do you mean by this? Aren't they being used inside the activity?

They are just that ideally you'd use this injection in other places as well, most notable being tests for that class(which we don't have).

The plan here is to use dependency injection at more places in the app and not only limiting it to coroutine dispatchers.

This is what I'm concerned about, that we are introducing dependencies/plugins which are not going to really be used in the near future seeing that the bulk work is done in scoped storage, some cleanups and moving to the new backend(at least from the changes I see).

lukstbit avatar Sep 19 '22 16:09 lukstbit

Hello 👋, this PR has been opened for more than 2 months with no activity on it. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

github-actions[bot] avatar Nov 19 '22 04:11 github-actions[bot]