[Hilt]java.lang.reflect.InvocationTargetException
See original GitHub issueI encountered an error using hilt.
When I use @AndroidEntryPoint to annotate Fragment, it will cause a reflection error in my BaseFragment. When I don’t use @AndroidEntryPoint, the app can work normally. I tried many methods, but failed to fix this error.
Class
LoginActivity.kt
@AndroidEntryPoint
class LoginRegisterActivity : BaseActivity<ActivityLoginBinding, LoginRegisterViewModel>() {
override var hasAutoAddBaseLayout: Boolean = false
}
LoginFragment.kt
@AndroidEntryPoint
class LoginFragment : BaseFragment<FragmentLoginBinding, LoginRegisterViewModel>() {
......
}
BaseFragment.class and BaseActivity.kt
abstract class BaseFragment<out VB : ViewBinding, out VM : BaseViewModel> : Fragment(), IWindow,ILauncher by LauncherImpl(),
ViewBindModelImpl<VB, VM> {
......
private var container: ViewGroup? = null
protected val viewBinding: VB by lazy { declaredViewBind(layoutInflater, container) }
......
}
ViewBindModelImpl.kt
interface ViewBindModelImpl<out VB : ViewBinding, out VM : BaseViewModel> {
/**
* view binding
*/
@Suppress("UNCHECKED_CAST")
fun declaredViewBind(
layoutInflater: LayoutInflater,
container: ViewGroup? = null
): VB {
val type = javaClass.genericSuperclass as ParameterizedType
val aClass = type.actualTypeArguments[0] as Class<VB>
if (aClass.name == EmptyViewBinding.javaClass.name) {
return EmptyViewBinding as VB
}
val method = aClass.getDeclaredMethod(
"inflate",
LayoutInflater::class.java,
ViewGroup::class.java,
Boolean::class.java
)
return method.invoke(null, layoutInflater, container, false) as VB
}
......
}
Error logs
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxx.xxx.debug, PID: 30602
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx.debug/com.xxx.xxx.ui.activities.LoginRegisterActivity}: java.lang.reflect.InvocationTargetException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.xxx.xxx.ui.base.impl.ViewBindModelImpl$DefaultImpls.declaredViewBind(ViewBindModelImpl.kt:49)
at com.xxx.xxx.ui.base.BaseActivity.declaredViewBind(BaseActivity.kt:30)
at com.xxx.xxx.ui.base.impl.ViewBindModelImpl$DefaultImpls.declaredViewBind$default(ViewBindModelImpl.kt:34)
at com.xxx.xxx.ui.base.BaseActivity$viewBinding$2.invoke(BaseActivity.kt:46)
at com.xxx.xxx.ui.base.BaseActivity$viewBinding$2.invoke(BaseActivity.kt:30)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.xxx.xxx.ui.base.BaseActivity.getViewBinding(Unknown Source:2)
at com.xxx.xxx.ui.base.BaseActivity.onCreate(BaseActivity.kt:74)
at com.xxx.xxx.ui.activities.Hilt_LoginRegisterActivity.onCreate(Hilt_LoginRegisterActivity.java:29)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.LinkageError: Method androidx.fragment.app.FragmentActivity com.xxx.xxx.ui.fragments.login.Hilt_LoginFragment.getActivity() overrides final method in class Landroidx/fragment/app/Fragment; (declaration of 'com.xxx.xxx.ui.fragments.login.Hilt_LoginFragment' appears in /data/app/~~Wk0--XoONsDW7ic00Sns-Q==/com.xxx.xxx.debug--TiGFUQanOEnNBUy2b4GTQ==/base.apk!classes2.dex)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at androidx.fragment.app.FragmentFactory.loadClass(FragmentFactory.java:47)
at androidx.fragment.app.FragmentFactory.loadFragmentClass(FragmentFactory.java:88)
at androidx.fragment.app.Fragment.instantiate(Fragment.java:595)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483)
at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132)
at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162)
at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58)
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:71)
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:28)
at androidx.navigation.NavController.navigate(NavController.java:1057)
at androidx.navigation.NavController.onGraphCreated(NavController.java:637)
at androidx.navigation.NavController.setGraph(NavController.java:590)
at androidx.navigation.NavController.setGraph(NavController.java:555)
at androidx.navigation.NavController.setGraph(NavController.java:537)
2021-01-08 00:26:55.090 30602-30602/com.xxx.xxx.debug E/AndroidRuntime: at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:248)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2921)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:496)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2173)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2089)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1954)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:306)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:166)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:313)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.inflate(LayoutInflater.java:657)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.xxx.xxx.databinding.ActivityLoginBinding.inflate(ActivityLoginBinding.java:42)
... 25 more
2021-01-08 00:26:55.156 30602-30602/com.xxx.xxx.debug I/Process: Sending signal. PID: 30602 SIG: 9
I also saw other issue try to use, but to no effect, such as
kapt 'com.squareup:javapoet:1.13.0'
My current module reference:
Android Studio 4.1.1 Build #AI-201.8743.12.41.6953283, built on November 5, 2020 Runtime version: 1.8.0_242-release-1644-b01 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0
ext { hilt_version = '2.30.1-alpha' hilt_view_model_version = '1.0.0-alpha02' } hiltAndroidKapt = "com.google.dagger:hilt-android-compiler:$hilt_version" hiltCompilerKapt = "androidx.hilt:hilt-compiler:$hilt_view_model_version" hiltAndroid = "com.google.dagger:hilt-android:$hilt_version" hiltViewModel = "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_view_model_version"
Extremely grateful!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5
Top Results From Across the Web
android - Got this error when tried a code using dagger hilt ...
Got this error when tried a code using dagger hilt, Error:[Hilt] java.lang.reflect.InvocationTargetException (no error message) · Ask Question.
Read more >[Solved]-Kotlin + hilt error. [Hilt] and java.lang.reflect ...
Coding example for the question Kotlin + hilt error. [Hilt] and java.lang.reflect.InvocationTargetException (no error message)-kotlin.
Read more >Execution failed for task ':app:kaptDebugKotlin'. > A failure ...
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution > java.lang.reflect.InvocationTargetException (no error message).
Read more >java.lang.reflect.InvocationTargetException(no error message)
[Android] Hilt 에러-java.lang.reflect.InvocationTargetException(no error message). jsoh·2022년 9월 5일. 0. androidhilt.
Read more >Androidx Startup starts up before testing instrumentation is ...
InvocationTargetException at java.lang.reflect.Constructor. ... checkState(Preconditions.java:83) at dagger.hilt.android.internal.testing.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found the problem when writing the example!
In fact, the problem occurred in another
ILauncher
class of mine, this is an interface that usesregisterForActivityResult
. In order to bind the fragment and the lifecycle of the Activity, the activity or fragment is passed in. And there are two fields named activity and fragment in this interface. When BaseActivity and BaseFragment implement ILauncher interfaces, it will cause getActivity() to be rewritten (Kotlin will automatically rewrite get/set).After I renamed
var activity
tovar activityInside
andvar fragment
tovar fragmentInside
, this problem was solved. But I still haven’t figured out why the problem was exposed after using@AndroidEntryPoint
, when the@AndroidEntryPoint
annotation is not use, the App can work normally.Thanks again @bcorso
Hmm, yeah something sounds very off here. Is there any way for you to put together a repro example for us to look into?