Skip to content
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

native-activity crashes on startup #929

Closed
henke96 opened this issue Jan 29, 2023 · 7 comments · Fixed by #1000
Closed

native-activity crashes on startup #929

henke96 opened this issue Jan 29, 2023 · 7 comments · Fixed by #1000

Comments

@henke96
Copy link

henke96 commented Jan 29, 2023

The native-activity sample crashes on startup for me:

01-29 13:21:53.692 19506 19506 D AndroidRuntime: Shutting down VM
01-29 13:21:53.693 19506 19506 E AndroidRuntime: FATAL EXCEPTION: main
01-29 13:21:53.693 19506 19506 E AndroidRuntime: Process: com.example.native_activity, PID: 19506
01-29 13:21:53.693 19506 19506 E AndroidRuntime: java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: java.lang.ClassNotFoundException: Didn't find class "androidx.startup.InitializationProvider" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/~~4YAeCpMLzSkqGKIKZ4d1ng==/com.example.native_activity-dtqDfrF_hud_ev66GpcWfw==/lib/arm64, /data/app/~~4YAeCpMLzSkqGKIKZ4d1ng==/com.example.native_activity-dtqDfrF_hud_ev66GpcWfw==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]]
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread.installProvider(ActivityThread.java:7478)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6990)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6739)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread.access$1500(ActivityThread.java:252)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2058)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:346)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:475)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7890)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
01-29 13:21:53.693 19506 19506 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.startup.InitializationProvider" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/~~4YAeCpMLzSkqGKIKZ4d1ng==/com.example.native_activity-dtqDfrF_hud_ev66GpcWfw==/lib/arm64, /data/app/~~4YAeCpMLzSkqGKIKZ4d1ng==/com.example.native_activity-dtqDfrF_hud_ev66GpcWfw==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]]
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:147)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        at android.app.ActivityThread.installProvider(ActivityThread.java:7462)
01-29 13:21:53.693 19506 19506 E AndroidRuntime:        ... 11 more

After some googling i managed to work around it by editing AndroidManifest.xml.
I changed the manifest element to be:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:versionCode="1"
          android:versionName="1.0">

Then i inserted the following under the application element:

<provider android:name="androidx.startup.InitializationProvider"
          android:authorities="${applicationId}.androidx-startup"
          tools:node="remove" />
@william-ferguson-au
Copy link

This fixes the issue, but I don't understand why configuring the InitializationProvider to remove all Initializers works, or is necessary. It seems like something is broken in the NativeActivity startup mechanism.

@hybcloud
Copy link

the same problem, the same solution, and the samely confused.

@hybcloud
Copy link

image
according to the source code's comment, the Android tends to find the initializer in the application before it starts the first activity. I guess since it is a purely native application, there is no implementation of the initializer in the application, which leads to the failure.

@Intro1997
Copy link

image according to the source code's comment, the Android tends to find the initializer in the application before it starts the first activity. I guess since it is a purely native application, there is no implementation of the initializer in the application, which leads to the failure.

@hybcloud I try to remove android:hasCode="false" in AndroidManifest.xml and it works. Does it mean this android:hasCode="false" config removes the initializer?

@hybcloud
Copy link

hybcloud commented Sep 27, 2023 via email

@DanAlbert
Copy link
Member

This fixes the issue, but I don't understand why configuring the InitializationProvider to remove all Initializers works, or is necessary. It seems like something is broken in the NativeActivity startup mechanism.

I think you're right in that androidx.startup is expecting NativeActivity to do work that it's not doing, but NativeActivity predates androidx.startup by a very long time, and it can't assume that old versions of NativeActivity have new behavior. I think androidx.appcompat probably needs to handle this (I'm guessing that's the right place, since it's supposed to be the thing that "backports" behaviors to old versions of Android).

FTR I think the proper fix here is to just use GameActivity instead of NativeActivity. The name is game-specific, but a more accurate name would be NativeActivityButTheBugsAreFixable.

But there's still something odd here that I want to see fixed (or, if it's expected that NativeActivity users need to work around this, documented). That's a question for the androidx folks though: https://issuetracker.google.com/209387755

DanAlbert added a commit to DanAlbert/ndk-samples that referenced this issue Apr 16, 2024
These dependencies were not actually used but were in contradiction with
the `android:hasCode=false` in the manifest. Remove the pointless
dependencies, but leave some comments explaining to anyone that copies
from here that they need to update `app:hasCode` if they add any DEX to
their app.

Fixes android#929.
DanAlbert added a commit to DanAlbert/ndk-samples that referenced this issue Apr 16, 2024
These dependencies were not actually used but were in contradiction with
the `android:hasCode=false` in the manifest. Remove the pointless
dependencies, but leave some comments explaining to anyone that copies
from here that they need to update `app:hasCode` if they add any DEX to
their app.

Fixes android#929.
@DanAlbert
Copy link
Member

With a bit more digging, I think we've managed to settle on the hasCode fix being the correct one. The docs are vague about what "code" means, but as best as I can tell it means dex, and whoever added the androidx dependency to the sample should have removed that property at the same time. I've sent a patch for the docs, and filed https://issuetracker.google.com/335293923 so this can be caught proactively during the build for anyone that makes the mistake in the future.

#1000 is the likely fix here (it's similar to the PR uploaded earlier today, but with comments for anyone that inevitably copy pastes from here).

DanAlbert added a commit that referenced this issue Apr 17, 2024
These dependencies were not actually used but were in contradiction with
the `android:hasCode=false` in the manifest. Remove the pointless
dependencies, but leave some comments explaining to anyone that copies
from here that they need to update `app:hasCode` if they add any DEX to
their app.

Fixes #929.
xcheng85 added a commit to xcheng85/simple-android-gl that referenced this issue Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants