这是我第一次和Dagger在一起,我正在通过一个项目来学习。我确实创建了一个组件和一个模块,并确保我在成绩文件中有正确的依赖关系。但是,当我构建项目时,自动生成的文件无处可见。
下面是组件和模块类:
package com.mario.photoapp.dependency;
import com.mario.photoapp.photoalbum.ImageActivity;
import com.mario.photoapp.services.NetworkModule;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {NetworkModule.class,})
public interface Dependency {
void inject(ImageActivity imageActivity);
}
模块类:
package com.mario.photoapp.services;
import com.mario.photoapp.BuildConfig;
import java.io.File;
import java.io.IOException;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
@Module
public class NetworkModule {
File cacheFile;
public NetworkModule(File cacheFile) {
this.cacheFile = cacheFile;
}
@Provides
@Singleton
Retrofit provideCall() {
Cache cache = null;
try {
cache = new Cache(cacheFile, 10 * 1024 * 1024);
} catch (Exception e) {
e.printStackTrace();
}
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Customize the request
Request request = original.newBuilder()
.header("Content-Type", "application/json")
.removeHeader("Pragma")
.header("Cache-Control", String.format("max-age=%d", BuildConfig.CACHETIME))
.build();
okhttp3.Response response = chain.proceed(request);
response.cacheResponse();
// Customize or return the response
return response;
}
})
.cache(cache)
.build();
return new Retrofit.Builder()
.baseUrl(BuildConfig.BASEURL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
@Provides
@Singleton
@SuppressWarnings("unused")
public NetworkService providesNetworkService(
Retrofit retrofit) {
return retrofit.create(NetworkService.class);
}
@Provides
@Singleton
@SuppressWarnings("unused")
public RetrieverService providesService(
NetworkService networkService) {
return new RetrieverService(networkService);
}
}
成绩档案中的依赖关系:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
implementation 'com.android.support:cardview-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.1.6'
implementation 'com.github.bumptech.glide:glide:4.3.1'
//dagger 2
implementation 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
//to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes
implementation 'com.google.dagger:dagger-android:2.14.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'
//support libraries with dagger 2
implementation 'com.google.dagger:dagger-android-support:2.14.1'
compileOnly 'javax.annotation:jsr250-api:1.0'
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
为了解决这个问题,我还尝试通过进入文件 - >其他设置 - >默认设置 - >构建,执行,部署 - >注释处理器来启用注释处理。
不知道我还能做些什么来解决它。我在这里错过了什么吗?我认为在成绩文件中添加依赖项会修复它,但事实并非如此。
编辑:添加主活动类和图像活动类。
主要活动:
package com.mario.photoapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mario.photoapp.dependency.Dependency;
import com.mario.photoapp.services.NetworkModule;
import java.io.File;
public class MainActivity extends AppCompatActivity {
Dependency dependency;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File cacheFile = new File(getCacheDir(), "responses");
dependency = DaggerDependency.builder.networkModule(new NetworkModule(cacheFile)).build();
}
public Dependency getDependency() {
return dependency;
}
}
图像活动类:
package com.mario.photoapp.photoalbum;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.mario.photoapp.MainActivity;
import com.mario.photoapp.R;
import com.mario.photoapp.models.ImageListData;
import com.mario.photoapp.models.ImageListResponse;
import com.mario.photoapp.services.RetrieverService;
import javax.inject.Inject;
public class ImageActivity extends MainActivity implements ImageViewInterface {
private RecyclerView list;
@Inject
public RetrieverService service;
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getDependency().inject(this);
setContentView(R.layout.activity_main);
list = findViewById(R.id.list);
progressBar = findViewById(R.id.progress);
list.setLayoutManager(new LinearLayoutManager(this));
ImagePresenter presenter = new ImagePresenter(service, this);
presenter.getCityList();
}
@Override
public void showWait() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void removeWait() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onFailure(String appErrorMessage) {
}
@Override
public void getCityListSuccess(ImageListResponse imageListResponse) {
ImageAdapter adapter = new ImageAdapter(getApplicationContext(), imageListResponse.getData(),
new ImageAdapter.OnItemClickListener() {
@Override
public void onClick(ImageListData Item) {
Toast.makeText(getApplicationContext(), Item.getName(),
Toast.LENGTH_LONG).show();
}
});
list.setAdapter(adapter);
}
}
要查看生成的文件,请选择“项目”而不是“Android”并导航到
在Dependency.java文件中,您指定了不应该存在的逗号。
@Component(modules = {NetworkModule.class,})
应该
@Component(modules = {NetworkModule.class})