Ionic 的原生“Push”插件导致错误

Native "Push" plugin for Ionic causes errors

提问人:Niek Jonkman 提问时间:12/22/2017 最后编辑:Niek Jonkman 更新时间:1/8/2018 访问量:710

问:

我正在尝试在我的离子应用程序中实现推送通知。为此,我想使用本机插件推送:

https://ionicframework.com/docs/native/push/

在安装此插件之前,我可以使用以下命令在我的 Android 设备上启动我的应用程序:

ionic cordova run android

应用程序启动,并且不会发生任何错误。但是在像这样安装 Cordova/PhoneGap 插件之后:

ionic cordova plugin add phonegap-plugin-push

还有这个:

npm install --save @ionic-native/push

我无法再在我的设备上运行该应用程序,因为构建因一堆错误而失败。

BUILD FAILED

Total time: 22.02 secs
Error: cmd: Command failed with exit code 1 Error output:
D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\AndroidManifest.xml:28:13-35 Error:
        Attribute meta-data#android.support.VERSION@value value=(25.4.0) from [com.android.support:appcompat-v7:25.4.0] AndroidManifest.xml:28:13-35
        is also present at [com.android.support:support-v13:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(25.4.0) from [com.android.support:appcompat-v7:25.4.0] AndroidManifest.xml:28
:13-35
        is also present at [com.android.support:support-v13:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

此错误有一个建议:将“tools:replace=”android:value“'添加到AndroidManifest中的eleemet。我这样做了,再次构建后出现此错误:

BUILD FAILED

Total time: 2.171 secs
> Exception while parsing the supplied manifest file D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\AndroidManifest.xml

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Error: cmd: Command failed with exit code 1 Error output:
[Fatal Error] :13:146: The prefix "tools" for attribute "tools:replace" associated with an element type "meta-data" is not bound.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android'.
> Exception while parsing the supplied manifest file D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\AndroidManifest.xml

我发现的另一个“解决方案”是添加以下代码:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.1'
            }
        }
    }
}

build.gradle文件,它修复了第一个提到的错误,但给了我另一个错误(Push Plugin):

BUILD FAILED
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Total time: 12.765 secs
Error: cmd: Command failed with exit code 1 Error output:
D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\src\com\adobe\phonegap\push\FCMService.java:393: error: constructor Builder in class Bu
ilder cannot be applied to given types;
        mBuilder = new NotificationCompat.Builder(context, channelID);
                   ^
  required: Context
  found: Context,String
  reason: actual and formal argument lists differ in length
D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\src\com\adobe\phonegap\push\FCMService.java:403: error: constructor Builder in class Bu
ilder cannot be applied to given types;
        mBuilder = new NotificationCompat.Builder(context, channelID);
                   ^
  required: Context
  found: Context,String
  reason: actual and formal argument lists differ in length
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我还试图添加这个:

cordova.system.library.1=com.google.android.gms:play-services-auth:11.0.4
cordova.system.library.2=com.google.android.gms:play-services-identity:11.0.4

添加到 project.properties 文件中,但又出现了另一个错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':processDebugGoogleServices'.
> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bint
ray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 11.0.1.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.547 secs
Error: cmd: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':processDebugGoogleServices'.
> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bint
ray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 11.0.1.

所以我也尝试使用 11.0.1(而不是 11.0.4,它们都已安装):

enter image description here

但它给了我这个错误:

BUILD FAILED

Total time: 14.349 secs
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Error: cmd: Command failed with exit code 1 Error output:
D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\src\com\adobe\phonegap\push\FCMService.java:393: error: constructor Builder in class Bu
ilder cannot be applied to given types;
        mBuilder = new NotificationCompat.Builder(context, channelID);
                   ^
  required: Context
  found: Context,String
  reason: actual and formal argument lists differ in length
D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\src\com\adobe\phonegap\push\FCMService.java:403: error: constructor Builder in class Bu
ilder cannot be applied to given types;
        mBuilder = new NotificationCompat.Builder(context, channelID);
                   ^
  required: Context
  found: Context,String
  reason: actual and formal argument lists differ in length
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

在这一点上,当我像这样删除插件时,我真的不知道要解决这个问题:

ionic cordova plugin remove phonegap-plugin-push

我可以再次启动应用程序,没有问题或错误。

我怎样才能让这个插件工作?

提前致谢。

编辑

因此,我创建了一个新的 Ionic 应用程序,其中包含如下所示的样板:

ionic start myApp tabs

我像这样安装了 phonegap 插件推送:

ionic cordova plugin add phonegap-plugin-push

使用此功能时:

ionic cordova run android

该应用程序刚刚启动,没有错误,因此是我的另一个项目导致了这些问题。我在空白应用程序上使用了离子信息,它向我展示了以下内容:

cli packages: (C:\Users\njonkman\AppData\Roaming\npm\node_modules)

    @ionic/cli-utils  : 1.19.0
    ionic (Ionic CLI) : 3.19.0

global packages:

    cordova (Cordova CLI) : 8.0.0

local packages:

    @ionic/app-scripts : 3.1.6
    Cordova Platforms  : android 6.3.0
    Ionic Framework    : ionic-angular 3.9.2

System:

    Node : v6.11.3
    npm  : 4.6.1
    OS   : Windows 10

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro

所以我对我自己的应用程序做了同样的事情,它显示一些字段已经过时(或者至少比样板安装的字段更旧,也是cordova),我开始像这样更新字段:

npm install -g cordova 

npm install -g ionic

npm install @ionic/app-scripts@latest

但是再次运行它后,我收到以前的错误之一:

BUILD FAILED in 2s
(node:19208) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: cmd: Command failed with exit code 1 Error output:
D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy (2)\platforms\android\app\src\main\AndroidManifest.xml:28:13-35 Error:
        Attribute meta-data#android.support.VERSION@value value=(26.1.0) from [com.android.support:support-v13:26.1.0] AndroidManifest.xml:28:13-35
        is also present at [com.android.support:appcompat-v7:25.4.0] AndroidManifest.xml:28:13-35 value=(25.4.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.1.0) from [com.android.support:support-v13:26.1.0] AndroidManifest.xml:28:
13-35
        is also present at [com.android.support:appcompat-v7:25.4.0] AndroidManifest.xml:28:13-35 value=(25.4.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

在这一点上,重新创建一个新项目,安装我需要的所有插件,测试它们是否有效,然后将我的逻辑/类添加到其中不是更好吗?

谢谢

编辑

所以我已经“弄清楚”了是什么导致了问题,我不知道为什么会发生这种情况,也不知道如何解决它。我正在使用 2 个单独运行良好的插件,但都安装了,导致错误。这些插件是:

Android iOS git cordova ionic-framework

评论

1赞 Suraj Rao 12/22/2017
您可以添加输出以了解cordova-android等的版本。ionic info
0赞 Sébastien 12/22/2017
如果您更新 Ionic CLI 并创建一个仅使用本机推送插件的空白项目,它是否有效?
0赞 Niek Jonkman 12/22/2017
我已经更新了我的 OP。

答:

0赞 Niek Jonkman 1/8/2018 #1

好的,所以我已经设法解决了我的问题。这是我所做的:

我创建了一个空白的 Ionic 项目(ionic start blank )来测试该插件是否可以在新创建的项目中工作。它做到了。之后,我一个接一个地安装了所有其他插件,看看它是否仍然有效。在某个时候,我遇到了文件路径插件(https://ionicframework.com/docs/native/file-path/)。安装此插件后,我遇到了与OP中提到的相同的错误。 在谷歌搜索(?)之后,我发现了这个主题:

android 在更新到新版本的 gradle 后出现清单合并失败错误

其中一个解决方案是将这段代码添加到build.gradle文件中:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

请注意,我已经尝试过类似的解决方案,但版本号不同( 25.3.1 )。该错误提到了 2 个版本,我在解决方案中选择了最新的一个( 26.1.0 ),但我最初没有。

在空白项目上试用后,我在现有项目上进行了尝试。只是为了确保我删除了平台(ionic cordova rm platform android)并再次添加它(ionic cordova add platform android)。另请注意,我将上述部分添加到 platforms>android>app 中的 build.gradle 中,而不是在 platforms>android 中找到的 build.gradle 中。

我现在可以再次运行我的项目,而不会出现任何错误。