0

我在为 android SDK 创建插件时遇到以下问题

package com.me.plugin

import android.app.Activity
import android.content.ContentValues.TAG
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.util.Log
import androidx.annotation.NonNull
import com.me.plugins.meplugin.Api
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel


class MainActivity: FlutterPlugin, MethodChannel.MethodCallHandler{


  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel
  private lateinit var context: Context
  private lateinit var activity: Activity

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    Api.getInstance()
    channel= MethodChannel(flutterPluginBinding.binaryMessenger,"net.plzft.poc_od")
    context = flutterPluginBinding.applicationContext
    channel.setMethodCallHandler(this)

  }

  override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    }
    if (call.method.equals("isRegisteredToBackend_p")) {
      result.success(Api.getInstance().isRegisteredToBackend)
    }else {
            result.notImplemented()
          }
  }
}
import 'dart:async';

import 'package:flutter/services.dart';

class Meplugin {
  static const MethodChannel _channel =
      const MethodChannel('net.plzft.poc_od');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<bool> get isRegisteredToBackend__p async {
    final bool version = await _channel.invokeMethod('isRegisteredToBackend_p');
    print(version);
    return version;
  }
}
import 'dart:developer';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:meplugin/meplugin.dart';
import 'package:shared_preferences/shared_preferences.dart';

class InitPage extends StatefulWidget {
  @override
  InitPageState createState() => InitPageState();
}

class InitPageState extends State<InitPage> {
  @override
  void initState() {
    super.initState();

    setState(() {
      _isRegisteredToBackend = null;
    });

    initIsRegisteredToBackend();
    initVibrateState();
  }

  void initVibrateState() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setInt('notifications', 1);
  }

  //static const platform = const MethodChannel('net.plzft.poc_od');

  bool _isRegisteredToBackend = false;

  bool isRegisteredToBackend() {
    return _isRegisteredToBackend;
  }

  Future<void> initIsRegisteredToBackend() async {
    bool temp;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      temp = await meplugin.isRegisteredToBackend__p;
    } on PlatformException {
      temp = false;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    if (!temp && _isRegisteredToBackend != temp) {
      log('_isRegisteredToBackend: $temp');
      setState(() {
        _isRegisteredToBackend = temp;
      });
      Navigator.popAndPushNamed(context, '/welcome_page');
    }

    if (temp) {
      Navigator.popAndPushNamed(context, '/home_page');
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            new CircularProgressIndicator(),
          ],
        ),
      ),
    );
  }
}
Launching lib/main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
E/flutter (16050): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method isRegisteredToBackend on channel net.plzft.poc_od)
E/flutter (16050): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)
E/flutter (16050): <asynchronous suspension>
E/flutter (16050): #1      Logitmeplugin.isRegisteredToBackend (package:logitmeplugin/logitmeplugin.dart:16:26)
E/flutter (16050): <asynchronous suspension>
E/flutter (16050): #2      InitPageState.initIsRegisteredToBackend (package:logitmeplugin_example/init_page.dart:44:14)
E/flutter (16050): <asynchronous suspension>
E/flutter (16050): 
E/flutter (16050): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

如您所见,我试图创建一个应该发送一个布尔值作为对来自颤动的请求(方法调用)的回答的通道。1.我在 api isregisteredtobackend中有一个方法 2.我在调用它的 MainActivity 中使用它 3.我在lib源的插件中实现了这个方法 4.在初始化页面上调用它 5.找不到该方法

我已经花了很多时间来找到我的问题所在......我不知道为什么颤振看不到我的方法实现

4

1 回答 1

0

看起来类在缩小时被剥离了。

尝试将以下内容添加到您的app/build.gradlein 中buildTypes,看看它是否有效:

minifyEnabled false
shrinkResources false

例子:

buildTypes {
    debug {
        minifyEnabled false
        shrinkResources false
        signingConfig signingConfigs.debug
    }
    release {
        minifyEnabled false
        shrinkResources false
        signingConfig signingConfigs.debug
    }
}
于 2021-09-03T07:33:16.373 回答