3

我正在尝试按照这些步骤为 stetho 编写插件。

它需要在自定义应用程序的 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.
4

2 回答 2

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"**
        >
于 2018-03-03T12:13:39.250 回答
1

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

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

到目前为止,唯一对我有用的解决方案是在插件中扩展 Flutter 的应用程序类-

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"
        ...

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

于 2018-03-03T13:02:22.823 回答