在库中使用 android Hilt (aar)
我使用 Hilt 创建了一个库。它在独立应用程序中工作正常,但是当我尝试集成到客户端应用程序中时,它开始给出错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: Hilt Activity must be attached to an @HiltAndroidApp Application. Did you forget to specify your Application's class name in your manifest's <application />'s android:name attribute?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3516)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3668)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
可能它要求客户端应用程序使用 Hilt 实现,但我的担忧如下:
- 我不希望客户端知道任何关于Hilt,
- 我不能要求客户集成 Hilt。Hilt
在 AAR 文件中尚不支持。 ?
I created a library using Hilt. it is working fine in standalone app, but when I tried to integrate in a client app, it start giving Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: Hilt Activity must be attached to an @HiltAndroidApp Application. Did you forget to specify your Application's class name in your manifest's <application />'s android:name attribute?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3516)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3668)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
Probably it asking client app to be implement using Hilt , but my concern is below:
- I don't want client to know anything about Hilt,
- I can not ask client to integrate Hilt
Is Hilt is yet to support in AAR files. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。正如堆栈跟踪指定的那样
Hilt 需要应用程序类上的
@HiltAndroidApp
注释才能开始生成注入代码。如果没有它并且没有将 Hilt 作为项目依赖项导入,则注入根本无法启动。在库中使用 hilt 并不是一个好的做法,因为您应该处理不支持 hilt 的情况,或者强制您的库用户更改其架构以适应您的库。
以下是有关此主题的一些反馈。
https://github.com/google/dagger/issues/2132#issuecomment- 704546736
You can't. As the stacktrace specifies
Hilt needs the
@HiltAndroidApp
annotation on the application class to start generating his injecting code. Without it and without importing Hilt too as a project dependency the injection simply doesn't start.Using hilt in a library is not a good practice as you should either handle the case where hilt is not supported or force your library users to change their architecture to fit your library.
Here's some feedback about this topic.
https://github.com/google/dagger/issues/2132#issuecomment-704546736