Bug 11182 - Unable to override Application.AttachBaseContext(Context)
Summary: Unable to override Application.AttachBaseContext(Context)
Status: ASSIGNED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler (show other bugs)
Version: 4.6.x
Hardware: PC Windows
: High normal
Target Milestone: master
Assignee: Atsushi Eno
URL:
: 22877 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-03-15 10:33 UTC by Joakim Larsson
Modified: 2017-03-21 20:28 UTC (History)
8 users (show)

See Also:
Tags: XATriaged
Is this bug a regression?: ---
Last known good build:


Attachments
Solution with the repro (7.69 KB, application/x-tar)
2014-07-30 18:53 UTC, Marek Habersack
Details

Description Joakim Larsson 2013-03-15 10:33:52 UTC
When overriding Application.AttachBaseContext(Context) deployment to emulator fails.

Sample code to rep:
    [Application(Label = "XXX", Icon = "@drawable/icon", Persistent = false)]
    public class App : Application{
        protected override void AttachBaseContext(Context bc)
        {
            base.AttachBaseContext(bc);
            // do some stuff...
        }
    }

Filtered (and anonymized) output from Android Device log:

03-15 15:29:02.084 I/ActivityManager( 1016): Start proc org.mycompany.myapp for activity org.mycompany.myapp/XXX.activities.main.MainActivity: pid=2545 uid=10045 gids={1015, 1006, 3003, 1028}
03-15 15:29:02.084 D/dalvikvm( 2545): Not late-enabling CheckJNI (already on)
03-15 15:29:02.094 I/dalvikvm( 2545): Turning on JNI app bug workarounds for target SDK version 7...
03-15 15:29:02.103 E/Trace   ( 2545): error opening trace file: No such file or directory (2)
03-15 15:29:02.127 W/dalvikvm( 2545): No implementation found for native LXXX/App;.n_attachBaseContext:(Landroid/content/Context;)V
03-15 15:29:02.127 D/AndroidRuntime( 2545): Shutting down VM
03-15 15:29:02.127 W/dalvikvm( 2545): threadid=1: thread exiting with uncaught exception (group=0xb4e02288)
03-15 15:29:02.127 E/AndroidRuntime( 2545): FATAL EXCEPTION: main
03-15 15:29:02.127 E/AndroidRuntime( 2545): java.lang.UnsatisfiedLinkError: Native method not found: XXX.App.n_attachBaseContext:(Landroid/content/Context;)V
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at XXX.App.n_attachBaseContext(Native Method)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at XXX.App.attachBaseContext(App.java:35)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.Application.attach(Application.java:146)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.Instrumentation.newApplication(Instrumentation.java:983)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.Instrumentation.newApplication(Instrumentation.java:967)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4124)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.ActivityThread.access$1300(ActivityThread.java:130)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.os.Looper.loop(Looper.java:137)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-15 15:29:02.127 E/AndroidRuntime( 2545): 	at dalvik.system.NativeStart.main(Native Method)
Comment 1 Marek Habersack 2014-07-30 18:53:24 UTC
Created attachment 7541 [details]
Solution with the repro

This is still reproducible using the current master. Full repro attached.
Comment 2 Jonathan Pryor 2014-09-11 14:00:33 UTC
*** Bug 22877 has been marked as a duplicate of this bug. ***
Comment 3 Atsushi Eno 2014-11-28 07:22:08 UTC
This is unfortunately by design so far.
Mono runtime provider is initialized as a content provider, which is initialized after Application#attachBaseContext().
When you write an override of the managed Application.AttachBaseContext(), it is generated as override in the Android Callable Wrapper. But since the native method should be registered by the Mono runtime provider and it's not initialized yet, the native method is not resolved.
That is what this line in the log means:

> No implementation found for native LXXX/App;.n_attachBaseContext:(Landroid/content/Context;)V

(I've hit this issue lately, and noticed it is already a known issue.)
Comment 4 Dimitar Dobrev 2015-09-23 11:21:34 UTC
I need this to work in order to work around https://bugzilla.xamarin.com/show_bug.cgi?id=34203 by using the third approach at https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html . If AttachBaseContext is not appropriate, is there a later point I can install the multi-dexing at?
Comment 5 Atsushi Eno 2015-09-23 22:58:27 UTC
Our multidex support is applied in the project properties. You don't need this. Actually my last comment was for the same reason.
Comment 6 Alexandre Chohfi 2016-05-03 16:37:28 UTC
Where in the source is the multidex (inherit from MultiDexApplication or MultiDex.install call) when subclassing Application class?

I've found this:
https://github.com/xamarin/xamarin-android/blob/master/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs#L444
But it only relates to when you are not using a custom Android.App.Application inherited class.

Note You need to log in before you can comment on or make changes to this bug.