添加服务导致部署失败

Adding Service causes Deployment failed

我正在开发一个使用 Xamarin 和 MvvmCross 框架的应用程序。由于我需要每隔 x 小时进行一次远程 api 调用,因此我正在尝试实现 JobScheduler.

我开始添加这个 class:

using Android.App;
using Android.App.Job;

namespace CMKOS.Droid.Service
{
    [Service(Name = "CMKOS.FibonacciJob", Permission = "android.permission.BIND_JOB_SERVICE")]
    public class FibonacciJob : JobService
    {
        public override bool OnStartJob(JobParameters jobParams)
        {
            // Called by the operating system when starting the service.
            // Start up a thread, do work on the thread.
            return true;
        }

        public override bool OnStopJob(JobParameters jobParams)
        {
            // Called by Android when it has to terminate a running service.
            return false; // Don't reschedule the job.
        }
    }
}

这里我得到 部署失败 日志输出错误:

/Library/Frameworks/Mono.framework/Versions/5.8.0/lib/mono/msbuild/15.0/bin/Microsoft.Common.CurrentVersion.targets(2057,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "MvvmCross.Droid.Support.Design". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
/Library/Frameworks/Mono.framework/Versions/5.8.0/lib/mono/msbuild/15.0/bin/Microsoft.Common.CurrentVersion.targets(2057,5): warning MSB3277: Found conflicts between different versions of "Microsoft.CSharp" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error : Unexpected install output:  pkg: /data/local/tmp/com.xxx.cmkos-Signed.apk
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error : Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error : 
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error :   at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x0013f] in /Users/builder/data/lanes/5809/22d97e15/source/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/Internal/AdbOutputParsing.cs:337 
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error :   at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass94_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x00016] in /Users/builder/data/lanes/5809/22d97e15/source/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/AndroidDevice.cs:746 
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error :   at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <2722d81e5b26475cb5f475fea055f291>:0 
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets(479,2): error :   at System.Threading.Tasks.Task.Execute () [0x00010] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502 
    2 Warning(s)
    1 Error(s)

当我删除 JobService superclass 时,应用程序部署没有任何错误:

using Android.App;

namespace CMKOS.Droid.Service
{
    [Service(Name = "CMKOS.FibonacciJob", Permission = "android.permission.BIND_JOB_SERVICE")]
    public class FibonacciJob
    {
        //public override bool OnStartJob(JobParameters jobParams)
        //{
        //    // Called by the operating system when starting the service.
        //    // Start up a thread, do work on the thread.
        //    return true;
        //}

        //public override bool OnStopJob(JobParameters jobParams)
        //{
        //    // Called by Android when it has to terminate a running service.
        //    return false; // Don't reschedule the job.
        //}
    }
}

你知道为什么会这样吗?我也尝试向 AndroidManifest 授予许可,但没有成功。

ServiceAttribute.Name 值与 C# 命名空间不匹配时会发生这种情况。 class 名称之前的所有内容也必须为小写。将您的 ServiceAttribute 更改为如下所示:

[Service(Name = "cmkos.droid.service.FibonacciJob", Permission = "android.permission.BIND_JOB_SERVICE")]