没有Multi-Dex的Android4.4上的java.lang.ClassNotFoundException

时间:2017-09-08 16:11:00

标签: android xamarin xamarin.android

我有一个针对Android 6.0的Xamarin Android应用程序,其最低版本为4.4。 在6.0及以上它运行没有任何问题。但是在我的Android 4.4测试设备上它会在应用程序中的SetupWizard之后崩溃,因为它应该使用以下消息注册作业:java.lang.ClassNotFoundException

全部例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.postkca.friendsappdate/connectContacts.droid.activities.MainActivity}: java.lang.ClassNotFoundException: md5e63ccdb150b7ea0bbd5c3cb0d08710a0.FetchContactupdatesJob
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2263)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5212)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: md5e63ccdb150b7ea0bbd5c3cb0d08710a0.FetchContactupdatesJob
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:251)
    at connectContacts.droid.activities.MainActivity.n_onCreate(Native Method)
    at connectContacts.droid.activities.MainActivity.onCreate(MainActivity.java:30)
    at android.app.Activity.performCreate(Activity.java:5240)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
    ... 11 more
Caused by: java.lang.NoClassDefFoundError: md5e63ccdb150b7ea0bbd5c3cb0d08710a0/FetchContactupdatesJob
    ... 18 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "md5e63ccdb150b7ea0bbd5c3cb0d08710a0.FetchContactupdatesJob" on path: DexPathList[[zip file "/data/app/com.postkca.friendsappdate-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.postkca.friendsappdate-1, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
    ... 18 more

我看到很多帖子都有类似的问题,但是所有帖子都启用了MultiDex,我没有。 Xamarin链接也设置为None,有或没有ProGuard的例外是相同的。 这会导致什么?

EDIT1:添加构建配置的屏幕截图

method cascading

FetchContactUpdatesJob:

[Service(Exported = true, Permission = "android.permission.BIND_JOB_SERVICE")]
    public class FetchContactupdatesJob : JobService
    {
        private const int JOB_ID = 10;

        public override bool OnStartJob(JobParameters args)
        {
            Task.Run(async () => await FetchContactupdates(args));
            return true;
        }

        /// <inheritdoc />
        public override bool OnStopJob(JobParameters args)
        {
            return true;
        }

        /// <inheritdoc />
        public override StartCommandResult OnStartCommand(Intent intent, Android.App.StartCommandFlags flags, int startId)
        {
            var messenger = (Messenger)intent.GetParcelableExtra("messenger");

            SendMessage(MainActivity.MESSAGE_SERVICE_FETCH_CONTACTUPDATES, messenger);

            return StartCommandResult.NotSticky;
        }

        private void SendMessage(int what, Messenger messenger)
        {
            var message = Message.Obtain();

            message.What = what;
            message.Obj = this;

            try
            {
                messenger.Send(message);
            }
            catch (RemoteException e)
            {
                Debug.WriteLine(e);
            }
        }

        private async Task FetchContactupdates(JobParameters args)
        {
           // Service Code

            JobFinished(args, false);
        }

        /// <summary>
        ///     Schedules the task for execution.
        /// </summary>
        public void ScheduleTask()
        {
            var builder = new JobInfo.Builder(JOB_ID,
                                              new ComponentName(this, Java.Lang.Class.FromType(typeof(FetchContactupdatesJob))));
            // Execute all 15 Minutes
            builder.SetPeriodic(15 * 60 * 1000);
            builder.SetPersisted(true);
            builder.SetRequiredNetworkType(NetworkType.None);
            builder.SetRequiresDeviceIdle(false);
            builder.SetRequiresCharging(false);

            var tm = (JobSchedulerType)GetSystemService(JobSchedulerService);
            var status = tm.Schedule(builder.Build());
        }
    }

1 个答案:

答案 0 :(得分:4)

JobService

中添加了

API 21

回复:https://developer.android.com/reference/android/app/job/JobService.html

Android 4.4 / KitKat为API 19,因此错误有意义,因为该设备不支持JobService

有一些基于Java的库,比如Evernote的OSS android-job,可以绑定在Xamarin.Android库中,用于抽象用于调度作业的API级别......否则你可能想看看在代码中查看他们如何处理不同的API并在您的应用程序中执行相同操作。