Giter Site home page Giter Site logo

vestrel00 / android-dagger-butterknife-mvp Goto Github PK

View Code? Open in Web Editor NEW
216.0 22.0 46.0 308 KB

Simple application using Dagger Android (2.11-2.17), Butterknife (8.7-8.8), and Model-View-Presenter (MVP) with support for Singleton, Activity, Fragment, and child Fragment scopes written in Java and Kotlin

Home Page: https://proandroiddev.com/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd

License: Apache License 2.0

Java 100.00%
dagger-2 butterknife mvp dagger-android kotlin

android-dagger-butterknife-mvp's People

Contributors

chronvas avatar rainer-lang avatar vandolf-viacom avatar vestrel00 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-dagger-butterknife-mvp's Issues

Version upgrades (September 4, 2018)

It's September 4, 2018 and its about time to upgrade stuff here to the latest versions;

  1. Android Studio: 3.2.0-beta02 -> 3.2.0-rc02
  2. Gradle: 4.6 -> 4.10
  3. Kotlin: 1.2.50 -> 1.2.61
  4. Support Lib: 27.1.1 -> 28.0.0-rc02
  5. buildToolsVersion: 27.0.3 -> 28.0.2
  6. compileSdkVersion: 27 -> 28
  7. targetSdkVersion: 27 -> 28

NOTE: As of API 28, fragments and all related fragment code have been deprecated in favor of the support fragments in the support lib. Therefore, it is recommended that the master-support and master-support-kotlin be used as reference instead of the master and master-kotlin branches. See PR #91.

The above updates will be applied to all branches: master, master-support, master-kotlin, master-support-kotlin.

Update

Here are the PRs that apply the above upgrades to the main branches;

  • master: #87
  • master-kotlin: #89
  • master-support: #88
  • master-support-kotlin: #90

Question - Injecting activityFragmentManager in a different way

Thanks for crunching all these important libraries into one solid sample. Really appreciate it :)

My question might be slight off from the library but i'm using the same approach.
I have a small scenario where i have this following structure but for some reason I'm not in luck.

@Singleton
@Component(modules = { AppModule.class,
        ActivityModule.class,
        AndroidSupportInjectionModule.class })
public interface AppComponent extends AndroidInjector<App> {

    @Override
    void inject(App application);

    @Component.Builder interface Builder {

        @BindsInstance
        AppComponent.Builder application(App application);

        AppComponent build();
    }
}

ActivityModule.class

    @PerActivity
    @ContributesAndroidInjector(modules = BaseActivityModule.class)
    abstract BaseActivity baseActivity();

BaseActivityModule.class

    static final String ACTIVITY_FRAGMENT_MANAGER = "ACTIVITY_FRAGMENT_MANAGER";

    @PerActivity
    @Named(ACTIVITY_FRAGMENT_MANAGER)
    @Provides
    static FragmentManager activityFragmentManager(BaseActivity activity) {
        return activity.getSupportFragmentManager();
    }

BaseAcitivity.class

public abstract class BaseActivity extends DaggerAppCompatActivity {

    @Named(ACTIVITY_FRAGMENT_MANAGER)
    @Inject
    FragmentManager fragmentManager;
}

So even though I'm providing my fragment manager in BaseActivityModule.class dagger is throwing this following compilation error. I even tried with just Activity instead of BaseActivity as my input parameter in BaseActivityModule. Even then I land up in this same issue. Not sure what exactly I'm screwing up. So any help is appreciated. Thanks in advance :)

2nd Attempt
I also tried using includes in my child activity modules (ABCActivityModule.class)

@Module(includes = BaseActivityModule.class)

without using
@ContributesAndroidInjector(modules = BaseActivityModule.class) inside AcitivityModule.class

Result - results me to same error

Error:(17, 8) error: [dagger.android.AndroidInjector.inject(T)] @javax.inject.Named("ACTIVITY_FRAGMENT_MANAGER") android.support.v4.app.FragmentManager cannot be provided without an @Provides- or @Produces-annotated method.
@javax.inject.Named("ACTIVITY_FRAGMENT_MANAGER") android.support.v4.app.FragmentManager is injected at
com.abc.views.base.BaseActivity.fragmentManager
com.abc.views.def.ABCActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
A binding with matching key exists in component: om.abc.views.base.BaseActivity_BaseActivity.BaseActivitySubcomponent

Example for Fragment Stack

Using this "framework", how do I add fragments in the stack? Example: onClick in Fragment1 open Fragment2; onClick in Fragment2 open Fragment3..

All datas each fragments must be supplied to an object in the Activity if possible.

How to use the dagger.android.support lib for compatibility with AppCompatActivity and support Fragment?

Demonstrate how to use the dagger.android.support framework for AppCompatActivity and support Fragment users. The current dagger.android, Butterknife, and MVP setup only works for minSdkVersion as low as 17. This will enable support for applications whose minSdkVersion is as low as API level 14.

Although it is my philosophy to use the latest and greatest, sometimes it may be necessary to support older versions of Android. Usually, such cases arise in legacy applications. Still, I urge upgrading to at least API level 17 to get rid of the need of the support libs. Looking at the Platform Version Distribution Dashboard, anything below API level 19 (KitKat) seems obsolete... Just something to think about!

This question provides an answer to #39.

Fragment injection does not occur. NullPointerException for Android versions 22 (Lollipop 5.1) and below.

Crash

The following crash occurs on devices running API 22 (Lollipop 5.1) and below.

   java.lang.NullPointerException: Attempt to invoke interface method 'void com.vestrel00.daggerbutterknifemvp.ui.main.view.MainFragmentListener.onExample1Clicked()' on a null object reference
       at com.vestrel00.daggerbutterknifemvp.ui.main.view.MainFragment.onExample1Clicked(MainFragment.java:63)
      ...

The crash does not occur for API 23 (Marshmallow 6.0) and above.

Steps to Reproduce

  1. Open the app
  2. Click on Example 1 button

Observe the crash. Note that clicking on any of the example buttons will cause the crash.

Analysis

The issue is that the MainFragmentListener listener in MainFragment is null for some reason. This suggests a bug with Dagger 2.11.

The issue is that the injection is currently happening in onAttach(Context), which is added in API level 23. This means that API level 22 and below will not invoke onAttach(Context), thereby bypassing the injection.

Solutions

  1. Perform fragment injection in onAttach(Activity).
  2. Use AppCompat Activity and support Fragment with dagger.android.support injection frameworks.

Upgrade Android Studio to 3.2.0 (Beta 2), Kotlin to 1.2.50, and support lib to 27.1.1

It's July 2, 2018 and its about time to upgrade stuff here to the latest versions;

  1. Android Studio: 3.2.0 (Canary 5) -> 3.2.0 (Beta 2)
  2. Kotlin: 1.2.21 -> 1.2.50
  3. Support lib: 27.1.0 -> 27.1.1

The above updates will be applied to all branches: master, master-support, master-kotlin, master-support-kotlin.

Update

Here are the PRs that apply the above upgrades to the main branches;

  • master: #77
  • master-kotlin: #79
  • master-support: #78
  • master-support-kotlin: #80

A: 8 - Create example 3

Create an activity with 1 fragment that contains a child fragment that utilizes the scoped utils.

A: 4 - Create scoped utils

Create scoped utilities that demonstrate each of the scopes; Singleton, PerActivity, PerFragment, and PerChildFragment.

Providing activity context

I'm trying to follow-up what you did just did inside your sample. I am having a hard-time building the app because of the Context and abstract Presenter injected inside BaseActivity and BasePresenter respectively.

For the Context, here's the error I have:

Error:(35, 8) error: [dagger.android.AndroidInjector.inject(T)] android.support.v7.app.AppCompatActivity cannot be provided without an @Inject constructor or from an @Provides-annotated method. android.support.v7.app.AppCompatActivity is injected at mypackage.base.BaseActivityModule.activityContext(activity) android.content.Context is injected at mypackage.base.BaseFragment.activityContext

The BaseActivity and BaseFraggement classes have been left unchanged. I simply extended them to my activities and fragments.

Here's my CoreComponent interface:
@Singleton @Component(modules = {ContextModule.class, NetworkModule.class, AppHelperModule.class, StorageModule.class, SchedulerFacadeImplModule.class, DatabaseModule.class}) public interface CoreComponent extends AndroidInjector<MyApp> { @Component.Builder abstract class Builder extends AndroidInjector.Builder<MyApp> { } }

and here's my ContextModule class:
`@Module(includes = AndroidInjectionModule.class)
public abstract class ContextModule
{
@BINDS
abstract Application application(CoreApp coreApp);

@PerActivity
@ContributesAndroidInjector(modules = MainViewModule.class)
abstract MainView provideMainView();

}`

MainView is the name of my activity

Upgrade Android Studio to 3.2.0 (and other version upgrades)

It's March 7, 2018 and its about time to upgrade stuff here to the latest versions;

Special thanks to @1and1get2 for starting this process in #69

  1. Android Studio: 2.3.3 -> 3.2.0 (canary 5)
  2. Gradle: 3.4.1 -> 4.6 (Android Studio 3.x+ requires Gradle version 4.1 or higher).
  3. Kotlin: 1.2.0 -> 1.2.21 (The newest version is 1.2.30. However, Android Studio 3.2.0 includes Kotlin plugin v1.2.21.)
  4. Support Lib: 26.0.1 -> 27.1.0
  5. Dagger 2: 2.14 -> 2.14.1
  6. Butterknife: 8.8.0 -> 8.8.1
  7. buildToolsVersion: 26.0.1 -> 27.0.3
  8. compileSdkVersion: 26 -> 27
  9. targetSdkVersion: 26 -> 27

The above updates will be applied to all branches: master, master-support, master-kotlin, master-support-kotlin.

Update

Here are the PRs that apply the above upgrades to the main branches;

  • master: #71
  • master-kotlin: #72
  • master-support: #73
  • master-support-kotlin: #74

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.