Fehler beim Ausführen eines HiltWorkers beim App-Start: NoSuchMethodExceptionAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 Fehler beim Ausführen eines HiltWorkers beim App-Start: NoSuchMethodException

Post by Anonymous »

Ich versuche, zu Testzwecken einen Worker automatisch ausführen zu lassen, wenn die App gestartet wird. Die Idee ist, dass der Worker beim Öffnen der App die Liste der verfolgten Animes abruft und für jeden eine Benachrichtigung anzeigt.
Meine Anwendung ist wie folgt konfiguriert:

Code: Select all

@HiltAndroidApp
class NotakuApplication : Application(), Configuration.Provider {

@Inject
lateinit var workerFactory: HiltWorkerFactory

override fun onCreate() {
super.onCreate()
val workRequest = OneTimeWorkRequestBuilder().build()
WorkManager.getInstance(this).enqueue(workRequest)
}

override fun getWorkManagerConfiguration(): Configuration {
return Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
}
}

Code: Select all

@HiltWorker
class MyWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted workerParams: WorkerParameters,
private val getTrackedAnimesUseCase: GetTrackedAnimesUseCase
) : CoroutineWorker(context, workerParams) {

override suspend fun doWork(): Result {
return try {
val trackedAnimes = getTrackedAnimesUseCase.execute()
trackedAnimes.forEach { anime ->
val message = "Episode ${anime.episodeNumber} of ${anime.title} is available"
sendNotification(applicationContext, anime.title, message)
}
Result.success()
} catch (e: Exception) {
e.printStackTrace()
Result.retry()
}
}

private fun sendNotification(context: Context, title: String, message: String) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val channelId = "notaku_channel"
if (notificationManager.getNotificationChannel(channelId) == null) {
val channel = NotificationChannel(
channelId,
"Notaku Notifications",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
}

val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
}
}
Meine build.gradle (App-Modul)-Abhängigkeiten und -Version:

Code: Select all

[versions]
hiltAndroid = "2.57.2"
hiltNavigationCompose = "1.3.0"
hiltCommon = "1.3.0"
hiltWork = "1.3.0"

[libraries]
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
androidx-hilt-common = { group = "androidx.hilt", name = "hilt-common", version.ref = "hiltCommon" }
androidx-hilt-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hiltWork" }

[plugins]
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid"  }

Code: Select all

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
id("com.google.dagger.hilt.android")
kotlin("kapt")
}

android {
namespace = "com.sergiogv.notaku"
compileSdk = 36

defaultConfig {
applicationId = "com.sergiogv.notaku"
minSdk = 26
targetSdk = 36
versionCode = 1
versionName = "1.0"
}
}

dependencies {
// Hilt + WorkManager
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.androidx.hilt.common)
implementation(libs.androidx.hilt.work)
implementation("androidx.work:work-runtime-ktx:2.8.0")
}

Code: Select all

plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
id("com.google.dagger.hilt.android") version "2.57.1" apply false
id("com.google.gms.google-services") version "4.4.3" apply false
}
Wenn ich jedoch die App öffne, erhalte ich die folgende Fehlermeldung:
E/WM-WorkerFactory: com.sergiogv.notaku.MyWorker konnte nicht instanziiert werden
java.lang.NoSuchMethodException: com.sergiogv.notaku.MyWorker. [class android.content.Context, class androidx.work.WorkerParameters]
Ich würde mich freuen, wenn Sie mir bei der Behebung dieses Fehlers helfen könnten.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post