小编dmi*_*sev的帖子

匕首2混合范围

以前我只有一个AppComponent有四个模块(AppModule,NetworkModule,StorageModule,PresentersModule),并且到处都注入了单例.最近,我决定在我的应用程序中进行小型重构并将其划分为范围.我认为,演示者只能在活动中生活,所以我创建了@ActivityScopeActivityModule,但由于我误解了如何混合这些范围,所以无法编译项目.我已经在stackoverflow上阅读了很多文章和问题,但是到处都有模块独立的简单示例.在我的情况下这样的事情

@Singleton
@Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class })
public interface AppComponent {
  ActivityComponent plus(PresentersModule module); // <-- error
}
Run Code Online (Sandbox Code Playgroud)

不管用.我收到此错误:

Error:(19, 1) error: com.my.package.di.component.ActivityComponent scoped with @com.my.package.di.scope.ActivityScope may not reference bindings with different scopes:
@Provides @Singleton android.app.Application com.my.package.di.module.AppModule.provideApplication()
@Provides @Singleton com.my.package.network.FeedBurnerApi com.my.package.di.module.NetworkModule.provideFeedBurnerApi(android.app.Application)
@Provides @Singleton android.database.sqlite.SQLiteOpenHelper com.my.package.di.module.StorageModule.provideSQLiteOpenHelper(android.app.Application)
@Provides @Singleton com.my.package.storage.Repository com.my.package.di.module.StorageModule.provideRepository(android.database.sqlite.SQLiteOpenHelper)
@Provides @Singleton com.my.package.SharedPreferencesHelper com.my.package.di.module.StorageModule.provideSharedPreferencesHelper(android.app.Application)
Run Code Online (Sandbox Code Playgroud)

那么,问题是如何获取ActivityComponent的实例?

您可以在下面看到模块之间的依赖关系:

申请模块:

@Module
public final class AppModule {
  private final …
Run Code Online (Sandbox Code Playgroud)

android components scope dependency-injection dagger-2

7
推荐指数
1
解决办法
2496
查看次数

Lint使用安全性错误"WrongConstant:Incorrect constant"使构建失败.IntDef注释

在我的Result类中,我使用@IntDef注释newInstance()方法中的第一个整数参数,如下所示:

public class Result {
    public static final int SUCCESS = 0;
    public static final int FAIL = 1;
    public static final int UNKNOWN = 2;

    // ...

    private Result(@Status int status, Uri uri) {
        mStatus = status;
        mUri = uri;
    }

    public static Result newInstance(@Status int status, Uri uri) {
        return new Result(status, uri);
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({ SUCCESS, FAIL, UNKNOWN })
    @interface Status {}
}
Run Code Online (Sandbox Code Playgroud)

接下来,在我的Utils类中,我调用该方法并将正确的常量作为参数传递.我确保我使用这样的特定常量:

public static Result foo() { …
Run Code Online (Sandbox Code Playgroud)

android annotations lint gradle

6
推荐指数
2
解决办法
3349
查看次数