How to extend application class in xamarin android
Asked Answered
R

2

10

I want to extend the Android application and I want to override onCreate method. But for some reason it's been very terrible...Only if I remove [Application] am I able to run it but according to this link we have to add [Application]

[Application]
public class MainApp : Application
{
    public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
        //app init ...
    }
}

Can anybody clarify me what is the right way to extend application class in Xamarin android

Update

If I don't remove [application] I am exactly getting the below error

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets:
 Error: Error executing task GenerateJavaStubs: Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute. (myapp.Droid)

If I add Application then it compiles but it throws following runtime error

[AndroidRuntime] Shutting down VM [AndroidRuntime] FATAL EXCEPTION:
main [AndroidRuntime] Process: com.test.myapp, PID: 6524
[AndroidRuntime] java.lang.RuntimeException: Unable to instantiate
application com.test.myapp.MainApp: java.lang.ClassNotFoundException:
Didn't find class "com.test.myapp.MainApp" on path: DexPathList[[zip
file "/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm,
/data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a, /system/lib,
/vendor/lib]] [AndroidRuntime]  at
android.app.LoadedApk.makeApplication(LoadedApk.java:802)
[AndroidRuntime]    at
android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377)
[AndroidRuntime]    at
android.app.ActivityThread.-wrap2(ActivityThread.java)
[AndroidRuntime]    at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
[AndroidRuntime]    at
android.os.Handler.dispatchMessage(Handler.java:102) [AndroidRuntime]
    at android.os.Looper.loop(Looper.java:154) [AndroidRuntime]     at
android.app.ActivityThread.main(ActivityThread.java:6119)
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime]    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
[AndroidRuntime]    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
[AndroidRuntime] Caused by: java.lang.ClassNotFoundException: Didn't
find class "com.test.myapp.MainApp" on path: DexPathList[[zip file
"/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm,
/data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a, /system/lib,
/vendor/lib]] [AndroidRuntime]  at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
[AndroidRuntime]    at
java.lang.ClassLoader.loadClass(ClassLoader.java:380) [AndroidRuntime]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
[AndroidRuntime]    at
android.app.Instrumentation.newApplication(Instrumentation.java:992)
[AndroidRuntime]    at
android.app.LoadedApk.makeApplication(LoadedApk.java:796)
[AndroidRuntime]    ... 9 more

Here is my manifest file

Recitative answered 18/5, 2017 at 22:13 Comment(0)
O
17

In AssemblyInfo.cs

comment out

    #if DEBUG
    [Application(Debuggable=true)]
    #else
    [Application(Debuggable = false)]
    #endif

and move that on top of your application

#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
public class MainApp : Application
{
    public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
        //app init ...
    }
}

Basically you have defined it two places that's why this problem occurs. Commenting out in AssemblyInfo.cs and adding in the extended class will work out

FYI:

Manifest is ok

Octahedron answered 19/5, 2017 at 0:39 Comment(0)
M
12
  1. Apply the ApplicationAttribute, [Application], to your Application subclass to register this class in the manifest

  2. Add the Java/JNI constructor

    • .ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
    • You will receive a fault when the runtime tries to instance the Java wrapper if you do not add this....
  3. Override the OnCreate method

    • This avoids: [art] JNI RegisterNativeMethods: attempt to register 0 native methods and thus your application subclass is not instanced.

Example:

[Application]
public class MainApplication : Application
{
    public MainApplication(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
    }
}
Milestone answered 18/5, 2017 at 22:56 Comment(8)
I am getting compilation error .... it says the file couldn't be found .. I added android:name with applicationRecitative
@Recitative android:name?, Like: [Application(LargeHeap = true, Name = "com.sushihangover.application")]Milestone
I entered =".application" ... it says .. it couldn't find com.test.myapp.applcation ... here com.test.myapp is my packageRecitative
You have to fully qualify the "Name", xamarin does support the dot notation like andriod Java or gradleMilestone
you mean to say I have to enter com.test.myapp.application in android:nameRecitative
What is the full application attribute line that you are using?Milestone
Hey guys how can i add Xamarin.Forms.Init() in class extending ApplicationBin
@LutaayaHuzaifahIdris You are looking at the wrong type of Application, in Android you call Forms' Init in the first/main activity, on iOS you call it in the AppDelegate.Milestone

© 2022 - 2024 — McMap. All rights reserved.