微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NoClassDefFound错误:将android sdk集成到flutter中时,运行时解析失败;尽管它适用于本机android项目

如何解决NoClassDefFound错误:将android sdk集成到flutter中时,运行时解析失败;尽管它适用于本机android项目

尝试设置polar-ble-sdk以便在我们的Flutter应用中使用,以便我们可以使用其蓝牙心率监视器进行离线记录。我们通过平台渠道来实现目标。

安装依赖项(来自Polar的2个.aar文件)后,我们得到一个运行时错误,尽管该依赖类在sdk中,也找不到其中一个依赖类。 如果您在没有抖动的本地android项目中加载sdk,则不会发生此问题。 既然我们采取了所有“预期”步骤,您就会认为这应该行之有效。

[ERROR:Flutter/shell/platform/android/platform_view_android_jni_impl.cc(43)] java.lang.NoClassDefFoundError: Failed resolution of: Lio/reactivex/rxjava3/android/schedulers/AndroidSchedulers;

我尝试过的事情

  1. 分析是否需要multidex->​​第一个项目超过65k个方法
  2. 使用最小的android sdk> 21并手动启用它,即可添加/验证multidex。
  3. 使用Java而不是kotlin支持创建MVP项目,该方法引用约为50K(不需要多dex)
  4. 升级Flutter v1.20
  5. 删除.aar文件以查看它们是否被使用过;这会导致构建时间错误。建议找到它们。另外,您希望它们已集成在一起,因为我们可以在运行时导入依赖项。只是不使用它们。.
  6. 已确保androixX与jetifier一起启用

如何复制

按照polar-ble-sdk:

中列出的步骤进行操作
  1. 在android / src / app / libs中,从其github添加polar-ble-sdk.aar和polar-protobuf-release.aar
  2. 在构建.gradle中添加依赖关系
dependencies {
    implementation files('libs/polar-ble-sdk.aar')
//    // Only needed if FEATURE_POLAR_FILE_TRANSFER used
    implementation files('libs/polar-protobuf-release.aar')
//    // Only needed if FEATURE_POLAR_FILE_TRANSFER used
    implementation group: 'commons-io',name: 'commons-io',version: '2.4'
//    // Only needed if FEATURE_POLAR_FILE_TRANSFER used
    implementation 'com.google.protobuf:protobuf-java:3.1.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
}
  1. 设置android清单以获取权限
    <uses-permission android:name="android.permission.BLUetoOTH" />
    <uses-permission android:name="android.permission.BLUetoOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  1. 添加本地导入和代码
package com.example.Flutter_app

import androidx.annotation.NonNull
import io.Flutter.embedding.android.FlutterActivity
import io.Flutter.embedding.engine.FlutterEngine
import io.Flutter.plugin.common.MethodChannel

import android.content.Context
import android.content.Contextwrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.Log

import polar.com.sdk.api.PolarBleApi;
import polar.com.sdk.api.PolarBleApiDefaultImpl;

// also tried with these imports later,according to sdk

import io.reactivex.CompletableObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import polar.com.sdk.api.PolarBleApiCallback;
import polar.com.sdk.api.model.PolaraccelerometerData;
import polar.com.sdk.api.model.PolarDeviceInfo;
import polar.com.sdk.api.model.PolarEcgData;
import polar.com.sdk.api.model.PolarExerciseData;
import polar.com.sdk.api.model.PolarExerciseEntry;
import polar.com.sdk.api.model.PolarHrbroadcastData;
import polar.com.sdk.api.model.PolarHrData;
import polar.com.sdk.api.model.PolarOhrPPGData;
import polar.com.sdk.api.model.PolarOhrPPIData;
import polar.com.sdk.api.model.PolarsensorSetting;

class MainActivity: FlutterActivity() {

    private val CHANNEL = "nl.company.company/polar"
    private val TAG = "Rick"

    override fun configureFlutterEngine(@NonNull FlutterEngine: FlutterEngine) {
        super.configureFlutterEngine(FlutterEngine)
        MethodChannel(FlutterEngine.dartExecutor.binaryMessenger,CHANNEL).setMethodCallHandler {
            call,result ->
            if (call.method == "initApi") {
               initApi()
            } else {
                result.notImplemented()
            }
        }
    }

    private fun initApi() {

        Log.d(TAG,"API to be created!")
        val api = PolarBleApiDefaultImpl.defaultImplementation(this,PolarBleApi.FEATURE_HR)  // Crash
        Log.d(TAG,"API created!")
    }
}
  1. 使用按钮创建UI来调用本机代码并初始化api以供使用
import 'dart:async';
import 'package:Flutter/material.dart';
import 'package:Flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',home: MyHomePage(title: 'Flutter Demo Home Page 1'),);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key,this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const platform = const MethodChannel('nl.company.company/polar');

  int _counter = 0;
  String _polardeviceid = "000";

  Future<void> _getPolardeviceid() async {
    String polardeviceid;
    String result = await platform.invokeMethod("initApi");
    polardeviceid = result;
    setState(() {
      _polardeviceid = polardeviceid;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called,for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast,so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),),body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
            RaisedButton(
              child: Text('$_polardeviceid'),onpressed: _getPolardeviceid,)
          ],);
  }
}

  1. 构建项目
  2. 按下按钮以调用代码;并发生错误,导致应用崩溃。

完整的堆栈跟踪:


E/Flutter (28735): [ERROR:Flutter/shell/platform/android/platform_view_android_jni_impl.cc(43)] java.lang.NoClassDefFoundError: Failed resolution of: Lio/reactivex/rxjava3/android/schedulers/AndroidSchedulers;
E/Flutter (28735):  at com.androidcommunications.polar.enpoints.ble.bluedroid.host.BDGattCallback.<init>(BDGattCallback.java:32)
E/Flutter (28735):  at com.androidcommunications.polar.enpoints.ble.bluedroid.host.BDDeviceListenerImpl.<init>(BDDeviceListenerImpl.java:73)
E/Flutter (28735):  at polar.com.sdk.impl.BDBleApiImpl.<init>(BDBleApiImpl.java:114)
E/Flutter (28735):  at polar.com.sdk.api.PolarBleApiDefaultImpl.defaultImplementation(PolarBleApiDefaultImpl.java:19)
E/Flutter (28735):  at com.example.Flutter_app.MainActivity.initApi(MainActivity.kt:62)
E/Flutter (28735):  at com.example.Flutter_app.MainActivity.access$initApi(MainActivity.kt:41)
E/Flutter (28735):  at com.example.Flutter_app.MainActivity$configureFlutterEngine$1.onMethodCall(MainActivity.kt:51)
E/Flutter (28735):  at io.Flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
E/Flutter (28735):  at io.Flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/Flutter (28735):  at io.Flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/Flutter (28735):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/Flutter (28735):  at android.os.MessageQueue.next(MessageQueue.java:336)
E/Flutter (28735):  at android.os.Looper.loop(Looper.java:174)
E/Flutter (28735):  at android.app.ActivityThread.main(ActivityThread.java:7682)
E/Flutter (28735):  at java.lang.reflect.Method.invoke(Native Method)
E/Flutter (28735):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
E/Flutter (28735):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/Flutter (28735): Caused by: java.lang.classNotFoundException: Didn't find class "io.reactivex.rxjava3.android.schedulers.AndroidSchedulers" on path: DexPathList[[zip file "/data/app/com.example.Flutter_app-BsARJP-hOr4YHOh1VaUPIw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.Flutter_app-BsARJP-hOr4YHOh1VaUPIw==/lib/arm64,/data/app/com.example.Flutter_app-BsARJP-hOr4YHOh1VaUPIw==/base.apk!/lib/arm64-v8a,/system/lib64,/system/product/lib64]]
E/Flutter (28735):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
E/Flutter (28735):  at java.lang.classLoader.loadClass(ClassLoader.java:379)
E/Flutter (28735):  at java.lang.classLoader.loadClass(ClassLoader.java:312)
E/Flutter (28735):  ... 17 more
E/Flutter (28735): 
F/Flutter (28735): [FATAL:Flutter/shell/platform/android/platform_view_android_jni_impl.cc(942)] Check Failed: CheckException(env). 
F/libc    (28735): Fatal signal 6 (SIGABRT),code -1 (SI_QUEUE) in tid 28735 (ple.Flutter_app),pid 28735 (ple.Flutter_app)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。