【问题标题】:How to write a Flutter (Android) plugin that uses a custom application?如何编写使用自定义应用程序的 Flutter (Android) 插件?
【发布时间】:2018-03-03 12:03:36
【问题描述】:

我正在尝试为 stetho 编写一个插件,遵循 these steps

它需要在自定义应用程序的 onCreate 方法中进行一些初始化。

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initializeWithDefaults(this);
  }
}

并在 AndroidManifest.xml 中为同一应用程序创建条目。

<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        ...>
        <application
                android:name="MyApplication"
                ...>
         </application>
</manifest>      

但我在尝试 flutter run 为依赖此插件的颤振应用程序时遇到错误 -

D:\Dev\Repo\flutter_test\myapp\android\app\src\main\AndroidManifest.xml:16:9-57 Error:
        Attribute application@name value=(io.flutter.app.FlutterApplication) from AndroidManifest.xml:16:9-57
        is also present at [:stetho] AndroidManifest.xml:7:18-76 value=(com.vilokanlabs.stetho.stetho.MyApplication).
        Suggestion: add 'tools:replace="android:name"' to <application> element at AndroidManifest.xml:15:5-38:19 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute application@name value=(io.flutter.app.FlutterApplication) from AndroidManifest.xml:16:9-57
        is also present at [:stetho] AndroidManifest.xml:7:18-76 value=(com.vilokanlabs.stetho.stetho.MyApplication).
        Suggestion: add 'tools:replace="android:name"' to <application> element at AndroidManifest.xml:15:5-38:19 to override.

【问题讨论】:

    标签: android flutter stetho


    【解决方案1】:

    将此行添加到您的清单中

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        **xmlns:tools="http://schemas.android.com/tools"**
        >
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme"
            **tools:replace="android:icon,android:theme,android:name"**
            >
    

    【讨论】:

    • 感谢您的建议,不知道工具:替换。但就我而言,它没有帮助。如果我在插件的清单中使用它,则会出现错误(似乎应用程序清单是在插件清单之后解析的)。如果我在我的应用程序清单中使用它,那就没用了;插件的应用程序永远不会初始化。现在我直接替换原始清单文件中的应用程序名称。再次感谢!
    【解决方案2】:

    Android 中似乎只允许一个应用程序节点。正如建议的here 和记录的here

    tool:replace 也可能是@sagar-chavada 建议的解决方案,但它不起作用。不知何故,应用程序的清单被认为/解析晚于插件的清单,因此 tool:replace 如果在应用程序的清单中使用(这是没有用的)有效,但如果在插件的清单中使用则无效(&抛出错误)。

    到目前为止,对我有用的唯一解决方案是插件中的extending the Flutter's application class -

    public class MyApplication extends FlutterApplication {
        public void onCreate() {
            super.onCreate();
            Stetho.initializeWithDefaults(this);
        }
    

    更新应用的清单文件以使用这个新的子类 -

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
             calls FlutterMain.startInitialization(this); in its onCreate method.
             In most cases you can leave this as-is, but you if you want to provide
             additional functionality it is fine to subclass or reimplement
             FlutterApplication and put your custom class here. -->
    
        <application
            android:name="com.vilokanlabs.stetho.stetho.MyApplication"
            android:label="myapp"
            ...
    

    事实证明,这里的评论表明相同。 对于一个插件来说可能不是一个很好的解决方案(一旦有两个竞争插件就会失败)但它可以工作!

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2018-01-12
      • 2020-08-05
      • 2020-10-07
      相关资源
      最近更新 更多