这是我第一次与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'
}
要解决此问题,我还尝试通过进入文件->其他设置->默认设置->构建,执行,部署->注释处理器来启用注释处理。
不确定该如何解决。我在这里想念其他东西吗?我以为在档次文件中添加依赖项可以解决此问题,但是没有。
编辑:添加主要活动类和图像活动类。
main_activity:
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);
}
}
答案 0 :(得分:0)
要查看生成的文件,请选择“项目”而不是“ Android”,然后导航至
答案 1 :(得分:-1)
在Dependency.java文件中,您指定了逗号,该逗号不应该存在。
@Component(modules = {NetworkModule.class,})
应该是
@Component(modules = {NetworkModule.class})