Coroutineworker类未在Android中实例化
我一直在尝试在与之合作的Android应用程序中运行Coroutine Worker课程。我还试图向班级注入依赖。请参阅下面的代码,
@HiltWorker
class BackgroundWorker@AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
val myRepository: Repository
) : CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
val data = myRepository.getMessageList()
val outputData = workDataOf(Constants.WORKER_OUTPUT to "data")
return Result.success(outputData)
}
}
我的应用模块在
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Singleton
@Provides
fun provideHttpClient(): OkHttpClient {
return OkHttpClient
.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.build()
}
@Singleton
@Provides
fun provideConverterFactory(): GsonConverterFactory = GsonConverterFactory.create()
@Singleton
@Provides
fun provideRetrofit(
okHttpClient: OkHttpClient,
gsonConverterFactory: GsonConverterFactory
): Retrofit {
return Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(gsonConverterFactory)
.build()
}
@Provides
@Singleton
fun provideAPIService(retrofit: Retrofit): API = retrofit.create(API::class.java)
@Provides
@Singleton
fun provideRepository(api: API): Repository = RepositoryImpl(api)
}
我的应用程序类别下方看起来像
@HiltAndroidApp
class NyangaSMSApplication : Application(), Configuration.Provider{
var mNetworkMonitoringUtil: NetworkMonitoringUtil? = null
@Inject
lateinit var workerFactory: HiltWorkerFactory
override fun onCreate(){
super.onCreate()
}
override fun getWorkManagerConfiguration() =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build()
}
清单文件包含
<application
android:name=".NyangaSMSApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NyangaSMS">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
</provider>
</application>
依赖项
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
implementation "androidx.work:work-runtime-ktx:$work_version"
work_version = '2.7.1'
hilt_version = '2.38.1'
所有设置的 。我在尝试运行工人时会遇到错误
E/WM-WorkerFactory: Could not instantiate com.example.nyangasms.ui.main.BackgroundWorker
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getDeclaredConstructor(Class.java:2166)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:245)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:137)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
2022-06-04 16:31:19.816 433-631/com.example.nyangasms E/WM-WorkerWrapper: Could not create Worker com.example.nyangasms.ui.main.BackgroundWorker
,我会缺少什么?
I have been trying to get coroutine worker class run in an android app I am working with. I am also trying to inject dependences to the class. See codes below
@HiltWorker
class BackgroundWorker@AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
val myRepository: Repository
) : CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
val data = myRepository.getMessageList()
val outputData = workDataOf(Constants.WORKER_OUTPUT to "data")
return Result.success(outputData)
}
}
My app module looks like below
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Singleton
@Provides
fun provideHttpClient(): OkHttpClient {
return OkHttpClient
.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.build()
}
@Singleton
@Provides
fun provideConverterFactory(): GsonConverterFactory = GsonConverterFactory.create()
@Singleton
@Provides
fun provideRetrofit(
okHttpClient: OkHttpClient,
gsonConverterFactory: GsonConverterFactory
): Retrofit {
return Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(gsonConverterFactory)
.build()
}
@Provides
@Singleton
fun provideAPIService(retrofit: Retrofit): API = retrofit.create(API::class.java)
@Provides
@Singleton
fun provideRepository(api: API): Repository = RepositoryImpl(api)
}
My applicaion class looks like
@HiltAndroidApp
class NyangaSMSApplication : Application(), Configuration.Provider{
var mNetworkMonitoringUtil: NetworkMonitoringUtil? = null
@Inject
lateinit var workerFactory: HiltWorkerFactory
override fun onCreate(){
super.onCreate()
}
override fun getWorkManagerConfiguration() =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build()
}
Manifest file contains
<application
android:name=".NyangaSMSApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NyangaSMS">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
</provider>
</application>
dependencies
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
implementation "androidx.work:work-runtime-ktx:$work_version"
work_version = '2.7.1'
hilt_version = '2.38.1'
With all this setup. I get the error while trying to run worker
E/WM-WorkerFactory: Could not instantiate com.example.nyangasms.ui.main.BackgroundWorker
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getDeclaredConstructor(Class.java:2166)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:245)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:137)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
2022-06-04 16:31:19.816 433-631/com.example.nyangasms E/WM-WorkerWrapper: Could not create Worker com.example.nyangasms.ui.main.BackgroundWorker
Please, what could I be missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论