我正在尝试学习MVP模式,并按照一个获得此源代码的教程进行学习。
ApplicationModule类
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "no endpoints available for service \"kubernetes-dashboard\"",
"reason": "ServiceUnavailable",
"code": 503
}
ApplicationComponent接口
@Module
public class ApplicationModule {
private Application application;
public ApplicationModule(Application application) {
this.application = application;
}
@Provides
@Singleton
public Context provideContext() { return application; }
}
应用程序类
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent
{ void inject (MainActivity target); }
MainActivity
public class App extends Application {
private ApplicationComponent component;
@Override
public void onCreate() {
super.onCreate();
//the below line is showing error
component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
public ApplicationComponent getComponent() {
return component;
}
}
模块级别的build.gradle
public class MainActivity extends AppCompatActivity
{
@Inject
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((App) getApplication()).getComponent().inject(this);
}
}
build.gradle用于项目级别
android {
compileSdkVersion 28
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "com.renegens.daggerexample"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation 'com.google.dagger:dagger:2.12'
annotationProcessor 'com.google.dagger:dagger-compiler:2.12'
compileOnly 'javax.annotation:jsr250-api:1.0'
}
但是在App类的DaggerApplicationComponent中显示错误。我必须更改一些设置以导入项目-将buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
更改为classpath 'com.android.tools.build:gradle:3.0.0'
,还更改了'com.android.tools.build:gradle:3.2.0'
,minSDK
,buildTool
的版本和重新进行的更改。
为什么显示错误,我该如何解决?请帮帮我,否则我会卡住。
更新
我在工具栏的“构建”选项卡中收到错误消息compileSDK
。下面是该消息的屏幕截图。
答案 0 :(得分:0)
使用最新的匕首依赖项
implementation 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
除此之外,还向ApplicationModule接口添加了一行,即
Context provideContext();