Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend application class in xamarin android

I want to extend the Android application and I want to override oncreate method. But for some reason its been very terrible...Only if I remove [Application] I am 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 rightway 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 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

like image 783
Shiva Avatar asked May 18 '17 22:05

Shiva


2 Answers

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

like image 68
Durai Amuthan.H Avatar answered Oct 17 '22 08:10

Durai Amuthan.H


  1. Apply the ApplicationAttribute, [Application], to your Application subclass to register this class in the manifest

    • https://developer.xamarin.com/api/type/Android.App.ApplicationAttribute/
  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();
    }
}
like image 38
SushiHangover Avatar answered Oct 17 '22 10:10

SushiHangover