google/dagger

Having an issue with Dagger Hilt Proguard Rules

Dzikry123 opened this issue · 13 comments

I'm a beginner Android developer facing a challenge with Proguard while using Dagger Hilt for dependency injection in my project. Despite trying various solutions, I'm still encountering errors related to missing classes generated by Dagger Hilt.

In essence, when attempting to build my app, I receive errors indicating missing classes from Dagger Hilt, even after implementing Proguard rules suggested in the Dagger repository. This issue has proven to be quite stubborn, and I'm seeking guidance from the community to resolve it.

Could someone experienced with Dagger Hilt and Proguard offer insights into properly configuring Proguard to address these errors? here is my code in my project:

// proguard-rules.pro :

Keep file R and Manifest

-keep class **.R
-keep class **.R$* {*;}
-keep class **.BuildConfig {*;}
-keep class **.Manifest {*;}

-keep class androidx.constraintlayout.motion.widget.KeyAttributes { *; }


# Keep Dependency Injection Framework related classes and methods
-keep class dagger.hilt.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.annotation.** { *; }

# Keep Model and Repository Classes
-keep class com.example.core.data.model.** { *; }
-keep class com.example.core.data.repo.** { *; }

# Keep ViewModels
-keep class * extends androidx.lifecycle.ViewModel

# Keep Parcelable Classes
-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

# Keep UI-related classes
-keep class com.example.ui.activity.** { *; }
-keep class com.example.ui.fragment.** { *; }
-keep class com.example.service.** { *; }

# Menjaga semua kelas dalam paket tertentu
-keep class com.example.package.** { *; }

# Menjaga semua kelas yang disebutkan dalam file tertentu
#-keep @com.example.rules.txt


# Menjaga kelas model
-keep class com.example.model.** { *; }

# Menjaga kelas service
-keep class com.example.service.** { *; }

# Menjaga kelas-kelas yang diperlukan untuk penanganan SSL/TLS
-keep class okhttp3.internal.platform.ConscryptPlatform {*;}
-keep class okhttp3.internal.platform.OpenJSSEPlatform {*;}
-keep class org.bouncycastle.** {*;}
-keep class org.conscrypt.** {*;}
-keep class org.openjsse.** {*;}


# Keep classes required by OpenJSSE
-keep class sun.security.x509.** { *; }
-keep class sun.util.logging.** { *; }

-keep class com.example.projectone.GithubUserApplication { *; }
-keep class com.example.projectone.ui.main.MainActivity { *; }
-keep class com.example.projectone.ui.userdetail.DetailUserActivity { *; }

Keep classes generated by Hilt

-keep class com.example.projectone.*Hilt* {
    *;
}

Keep Hilt generated classes

-keep class com.example.projectone.Hilt_* {*;}
-keep,allowobfuscation,allowshrinking @dagger.hilt.EntryPoint class *
-keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class *

#missing_rules.txt

-dontwarn com.example.projectone.Hilt_GithubUserApplication
-dontwarn com.example.projectone.ui.main.Hilt_MainActivity
-dontwarn com.example.projectone.ui.userdetail.Hilt_DetailUserActivity

here is the error :

Missing class com.example.projectone.Hilt_GithubUserApplication (referenced from: void com.example.projectone.GithubUserApplication.() and 1 other context)

Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in missing_rules.txt (see the file above)

Missing class com.example.projectone.ui.main.Hilt_MainActivity (referenced from: void com.example.projectone.ui.main.MainActivity.() and 5 other contexts)

Missing class com.example.projectone.ui.userdetail.Hilt_DetailUserActivity (referenced from: void com.example.projectone.ui.userdetail.DetailUserActivity.() and 5 other contexts)

Caused by: [CIRCULAR REFERENCE: com.android.tools.r8.utils.b: Missing class com.example.projectone.Hilt_GithubUserApplication (referenced from: void com.example.projectone.GithubUserApplication.() and 1 other context)

Any assistance or advice would be greatly appreciated. Thank you for your time and your support.

Hi, did you follow the proguard configuration provided in official guidance?. We already has some rules in dagger keeping entrypoints, example. Your error seems coming from a over aggressive proguard shrinking, I vaguely remember I had similar issue before when I didn't include getDefaultProguardFile("proguard-android-optimize.txt"). If you did included the android proguard file, can you create a repro for your problem, so that we can look into this further.

i have try to follow the official guidance and put all of the rules into my proguard-rules.pro, and i have include the getDefaultProguardFile("proguard-android-optimize.txt"). too in my build.gradle, but somehow the error is still exist, i put the link for my project repository below so you can see the detail of my code

My Repository

Hi, I patched your repro and ran ./gradlew assembleDebug, but I'm not seeing the missing dagger errors, I only see missing StringConcatFactory, which looks like Kotlin/kotlinx.serialization#2145. But I noticed that your repo is using pretty old version of Dagger, Kotlin, Ksp and Compose. Can you try to upgrade your versions first to see if the errors goes away?

Hello, i have tried to update all of the version of my Dagger, Kotlin, Ksp and Compose but it seems the error is remains the same,

Screenshot 2024-03-15 141931
Screenshot 2024-03-15 142012

i have updated my repository and here is the link to the updated repository :
Repository-Update

Hi, I noticed that the hilt gradle plugin is missing from your class path. https://github.com/Dzikry123/Android-Proguard/blob/version2/build.gradle#L9. https://dagger.dev/hilt/gradle-setup.html, please refer to the hilt gradle section for how to include it in the class path.