Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Service started as a separate process crashes on start

liveolesliveoles HKMember ✭✭
edited November 2016 in Xamarin.Android

A service started as a separate process crashes on start (before anything is called, including OnStartCommand) with the error: java.lang.RuntimeException: Unable to get provider mono.MonoRuntimeProvider_1: java.lang.ClassNotFoundException: Didn't find class "mono.MonoRuntimeProvider_1" on path...


I tried to start a service as a separate process from the main activity by doing:
[Service(Enabled = true, Name ="com.mytestapp.mytestappservice", Process = ":testprocess")] public class MyTestAppService : Service

This crash doesn't happen if i don't set the 'Process' attribute (so the solutions i found online cannot fix this problem sadly)

This crash only happens with RELEASE builds (no shared mono runtime), DEBUG builds work fine with no crashes or error (with shared mono runtime)

Any help would be greatly appreciated! I am really frustrated with this. It seems like the process cannot access the mono runtime?


Full Logcat:
FATAL EXCEPTION: main at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358) java.lang.RuntimeException: Unable to get provider mono.MonoRuntimeProvider_1: java.lang.ClassNotFoundException: Didn't find class "mono.MonoRuntimeProvider_1" on path: DexPathList[[zip file "/data/app/com.mytestapp.mytestapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.mytestapp.mytestapp-2, /vendor/lib, /system/lib]] at android.os.Handler.dispatchMessage(Handler.java:99) at dalvik.system.NativeStart.main(Native Method) at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53) at java.lang.ClassLoader.loadClass(ClassLoader.java:501) at java.lang.ClassLoader.loadClass(ClassLoader.java:461) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4547) ... 12 more at android.app.ActivityThread.main(ActivityThread.java:5225) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.installProvider(ActivityThread.java:4989) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4607) at java.lang.reflect.Method.invokeNative(Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "mono.MonoRuntimeProvider_1" on path: DexPathList[[zip file "/data/app/com.mytestapp.mytestapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.mytestapp.mytestapp-2, /vendor/lib, /system/lib]]

P.S. Starting the service as a separate process is essential as it saves a lot of memory usage for my project.

Best Answer

Answers

  • AlbertKAlbertK MYMember ✭✭✭✭

    Hi,

    Make sure you disable the "Use Shared Runtime and "Use Fast Deployment" in your release version.

  • liveolesliveoles HKMember ✭✭
    edited November 2016

    @AlbertK said:
    Hi,

    Make sure you disable the "Use Shared Runtime and "Use Fast Deployment" in your release version.

    They are not enabled in release configuration. Another point is that only the service crashed, the activity itself did not crash. Everytime I tried to start the service, the service immediately crashed and threw that exception while the activity remain perfectly responsive.

    I have also tried cleaning the whole solutions and deleting bin and obj folders. Didn't help.

    Thanks nonetheless :smile:

  • AlbertKAlbertK MYMember ✭✭✭✭

    Hi,

    I have no problem here. I have my service run on a separate process and has tested on Android 4.4, 5.0 and 6.0. Can you post a bit more of your service code?.

  • liveolesliveoles HKMember ✭✭

    I have reduced my code to just this when trying to narrow the problem. The code doesn't seem to be the problem as no matter how much I deleted the problem persists. Console.WriteLine("called"); never got called.

    Code of the service:
    [Service(Enabled = true, Name ="com.mytestapp.mytestappservice", Process = ":testprocess")] public class MyTestAppService : Service { public override IBinder OnBind(Intent intent) { return null; } public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId) { Console.WriteLine("called"); return StartCommandResult.RedeliverIntent; } }

    How I start the service:

    Intent i = new Intent("com.mytestapp.mytestappservice"); i.SetClass(activity, typeof(MyTestAppService)); StartService(i);

    Also, this only happens in release builds, but not debug builds. In release configuration I have tried unchecking every boxes, including shared mono runtime, proguard, and I have also turned off linking (Linking is None). Problem persists.

    This also only happens when I add the Process = ... attribute.

    I am using Xamarin.Forms v2.3.2.127, with Xamarin.Android.Support libraries at v23.3.0.

    Thanks!

  • AlbertKAlbertK MYMember ✭✭✭✭

    Hi,

    From the code that you have I don't see any problems.

    How about anything in the OnCreate that might be causing the problem?
    How about "Clean Solution" and "Clean mytestappservice" and rebuild ?

  • liveolesliveoles HKMember ✭✭

    I have tried cleaning everything and even deleted the folders bin and obj entirely.

    My OnCreate (for the MainActivity) code is as follows:
    protected override void OnCreate(Bundle bundle) { Console.WriteLine("0"); ToolbarResource = Resource.Layout.toolbar; TabLayoutResource = Resource.Layout.tabs; base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); Intent i = new Intent("com.mytestapp.mytestappservice"); i.SetClass(this, typeof(MyTestAppService)); StartService(i); }

    I will try to reproduce the problem from a fresh new project and post the results here. Thanks for the help.

  • liveolesliveoles HKMember ✭✭

    @AlbertK said:
    Hi,

    From the code that you have I don't see any problems.

    How about anything in the OnCreate that might be causing the problem?
    How about "Clean Solution" and "Clean mytestappservice" and rebuild ?

    OK I think I have narrowed down the problem. It's ProGuard. I forgot to clean the project before rebuilding with ProGuard turned OFF before, so I thought ProGuard wasn't the problem. I can now reproduce the problem with the code above and ProGuard set to ON in release mode.

    Apparently when the service is started in another process, proguard messes up something and causes the service unable to access mono runtime.

    However, I still cannot make it work with ProGuard turned on with the code above. Trying to figure out what the configuration file should be. Please advise.

  • AlbertKAlbertK MYMember ✭✭✭✭

    Hi,

    I don't use Proguard with my project so I am not the best person for advice here. The remote service is special as it actually starts as separate Android process. I can't even debug the remote service in debug mode.

    From my little understanding of ProGuard, I think it strips out some essential method calls...

Sign In or Register to comment.