-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New "fast" service loader makes the app crash when used with Apply Changes from Android Studio #1072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I will investigate this. |
@qwwdfsad How do I set a "system property" in an Android project that uses Gradle? |
You can do it programmatically with |
I've experimented with my sample project on AS 3.5 (Canary 9) and it appears to work. |
I haven't been able to reproduce the issue either. |
We (Apply Changes Team in Studio) have discovered that the issues is mostly related to ServiceLoader used in the library. What happen is that the ServiceLoader needs to read configuration files in the APK's META-INFO/. It does this by calling getResources on the classloader. If a swap were to happen and an new APK is installed in the process of this is happening, the classloader can still be referencing the old APK and trigger the FileNotFound exception. There is very little we can do on our end to make this work, especially for deploying to previous versions of Android. Is it possible to avoid the ServiceLoader solution here? At least for Android devices? |
@acleung Kotlin coroutines is not an Android-specific library that has to support Android conventions out-of-the box without extra configuration, so that's not much we can do, but to rely on ServiceLoader. The only upside here, is that the latest version of R8 should remove all usages of ServiceLoader from the resulting dex files. |
Potentially, yes, we can replace it with hierarchical But I am not sure whether it is a right approach at all. I realize there might be a lot of technical difficulties, but |
To be clear, I agree with @elizarov and @qwwdfsad. ServiceLoader is well supported in JVM and currently working as intended on Android. Yes, R8 has support for optimizing ServiceLoader but I don't think we can rely on it because most people who wants to Hot Swap code is probably doing development and unlikely to run full R8 on development build. I just spoke to the R8 team, who was incidentally visiting us last week, about supporting this in D8 instead but that would not make sense. Kotlin (+coroutine) is going to be important part of Android development going forward and I am doing every under my power to make sure Kotlin will work well for hotswapping. it would be a real shame if Kotlin developers get left out of Hot Swapping code as soon as they start using coroutine. I'll continue to push for better ServiceLoader reload support in Android devices in the future but it will only be available for future devices. I would really appreciate it if the developer team here at least bring up the ServiceLoader design / workaround decision once more if possible. Thank you! |
@acleung after thinking about it for a while, I think we can find a compromise that satisfies both parties. If I got the problem right, this is a race between swapping APKs and resolving The most straightforward solution here is to retry service-loading if we detect we are running on the Android (btw, what's the most reliable way to do it? It would be nice to check some system property instead of using |
As far as we understand, the runtime's classloader holds a path to the old APK and repeated calls to it will continue to get the stale path of a previous installed APK. I'll investigate a bit more and get back to you. In the mean time, if you can think of anything else, please let me know. Thanks! |
I don't know if the regular service loader would exhibit this behavior as I'm only getting started using the new Apply changes from Android Studio 3.5.
Here's the logs when the crash happened.
After restarting the app, no crash, but that kinda defeats the purpose of Apply Changes.
Related to #878
The text was updated successfully, but these errors were encountered: