Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Crash on released apk #8

Closed
Goooler opened this issue Jan 24, 2022 · 7 comments
Closed

Crash on released apk #8

Goooler opened this issue Jan 24, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@Goooler
Copy link
Owner

Goooler commented Jan 24, 2022

2022-01-25 00:14:25.637 8875-8875/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.goooler.demoapp, PID: 8875
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.goooler.demoapp/io.goooler.demoapp.main.ui.MainActivity}: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3622)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3786)
        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:2257)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:8030)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
     Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
        at x4.c.a(SourceFile:1)
        at r4.a.onCreate(SourceFile:2)
        at io.goooler.demoapp.main.ui.MainActivity.onCreate(SourceFile:1)
        at android.app.Activity.performCreate(Activity.java:8006)
        at android.app.Activity.performCreate(Activity.java:7990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3595)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3786) 
        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:2257) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:233) 
        at android.app.ActivityThread.main(ActivityThread.java:8030) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978) 

MainActivity extends BaseBindingActivity with generic argument MainActivityBinding:

@AndroidEntryPoint
@Route(path = RouterPath.MAIN)
class MainActivity : BaseBindingActivity<MainActivityBinding>() {

binding instantiated by inflateBinding extension in BaseBindingActivity:

abstract class BaseBindingActivity<VB : ViewDataBinding> : BaseThemeActivity(), IBinding<VB> {
override lateinit var binding: VB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = (inflateBinding(layoutInflater) as VB).also {
it.lifecycleOwner = this
setContentView(it.root)
}
}
}

inflateBinding generate binding by reflection:

fun <T : ViewBinding> LifecycleOwner.inflateBinding(inflater: LayoutInflater): T {
return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
.filterIsInstance<Class<T>>()
.first()
.getDeclaredMethod("inflate", LayoutInflater::class.java)
.invoke(null, inflater) as T
}

@Goooler Goooler added the bug Something isn't working label Jan 24, 2022
@Goooler
Copy link
Owner Author

Goooler commented Jan 24, 2022

Since 856d4c3.

@Goooler
Copy link
Owner Author

Goooler commented Jan 24, 2022

Caused by updating AGP 7.0.0-beta01 with android.enableR8.fullMode=true enabled.
If I disable android.enableR8.fullMode=true, no crash happend.

@Goooler Goooler linked a pull request Jan 24, 2022 that will close this issue
@Goooler
Copy link
Owner Author

Goooler commented Jan 24, 2022

Have not been fixed in AGP 7.0.4 or 7.1.0-rc01 or AGP 7.2.0-alpha07.

@Goooler Goooler changed the title Crash on release apk Crash on released apk Jan 25, 2022
@Goooler
Copy link
Owner Author

Goooler commented Jan 25, 2022

Decompile MainActivity with android.enableR8.fullMode=true enabled:

@Route(path = "/main/")
public final class MainActivity extends a {
  ...
}

Decompile MainActivity with android.enableR8.fullMode=true disabled:

@Route(path = "/main/")
public final class MainActivity extends a<MainActivityBinding> {
  ...
}

So r8 fullMode erase the generic type on MainActivity's super class, make javaClass.genericSuperclass as ParameterizedType throw ClassCastException, seems this behavior starts from AGP 7.0.0-beta01 and I already add -keepattributes Signature rule in proguard files.

@Goooler Goooler removed a link to a pull request Jan 25, 2022
@Goooler
Copy link
Owner Author

Goooler commented Jan 26, 2022

Here is the MainActivity decompiled from dex:

public final class MainActivity extends a {
  ...
}

No generic type on a class.

@Goooler Goooler reopened this Jan 30, 2022
@Goooler
Copy link
Owner Author

Goooler commented Jan 30, 2022

Still showing after AGP 7.1, to solve this problem absolutely, should wait Hilt to release a new version that supports new R8 rules.

@Goooler
Copy link
Owner Author

Goooler commented Feb 17, 2022

After #13 & #31, solved.

@Goooler Goooler closed this as completed Feb 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant