菜鸟教程小白 发表于 2022-12-11 19:29:49

android - Ionic 的 native "Push"插件导致错误


                                            <p><p>我正在尝试在我的 ionic 应用中实现推送通知。为此,我想使用原生插件推送:</p>

<p> <a href="https://ionicframework.com/docs/native/push/" rel="noreferrer noopener nofollow">https://ionicframework.com/docs/native/push/</a> </p>

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

<pre><code>ionic cordova run android
</code></pre>

<p>应用程序启动,没有出现错误。但是像这样安装 Cordova/PhoneGap 插件后:</p>

<pre><code>ionic cordova plugin add phonegap-plugin-push
</code></pre>

<p>还有这个:</p>

<pre><code>npm install --save @ionic-native/push
</code></pre>

<p>我无法再在我的设备上运行该应用程序,因为由于一堆错误导致构建失败。</p>

<pre><code>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 AndroidManifest.xml:28:13-35
      is also present at AndroidManifest.xml:28:13-35 value=(26.1.0).
      Suggestion: add &#39;tools:replace=&#34;android:value&#34;&#39; to &lt;meta-data&gt; element at AndroidManifest.xml:26:9-28:38 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task &#39;:processDebugManifest&#39;.
&gt; Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(25.4.0) from AndroidManifest.xml:28
:13-35
      is also present at AndroidManifest.xml:28:13-35 value=(26.1.0).
      Suggestion: add &#39;tools:replace=&#34;android:value&#34;&#39; to &lt;meta-data&gt; element at AndroidManifest.xml:26:9-28:38 to override.
</code></pre>

<p>这个错误有一个建议:在AndroidManifest的eleemet中加入'tools:replace="android:value"'。我这样做了,再次构建后出现此错误:</p>

<pre><code>BUILD FAILED

Total time: 2.171 secs
&gt; 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:
:13:146: The prefix &#34;tools&#34; for attribute &#34;tools:replace&#34; associated with an element type &#34;meta-data&#34; is not bound.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project &#39;android&#39;.
&gt; Exception while parsing the supplied manifest file D:\Data\njonkman\Desktop\kostendeclaratie_mobile - Copy\platforms\android\AndroidManifest.xml
</code></pre>

<p>我发现的另一个“解决方案”是添加这段代码:</p>

<pre><code>configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details -&gt;
      def requested = details.requested
      if (requested.group == &#39;com.android.support&#39;) {
            if (!requested.name.startsWith(&#34;multidex&#34;)) {
                details.useVersion &#39;25.3.1&#39;
            }
      }
    }
}
</code></pre>

<p>到 <strong>build.gradle</strong> 文件修复了第一个提到的错误但给了我另一个错误(Push Plugin):</p>

<pre><code>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 &#39;:compileDebugJavaWithJavac&#39;.
&gt; 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 &#39;:compileDebugJavaWithJavac&#39;.
&gt; Compilation failed; see the compiler error output for details.
</code></pre>

<p>我也尝试添加这个:</p>

<pre><code>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
</code></pre>

<p>到 <strong>project.properties</strong> 文件,但又一个错误:</p>

<pre><code>FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task &#39;:processDebugGoogleServices&#39;.
&gt; 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 &#39;:processDebugGoogleServices&#39;.
&gt; 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.
</code></pre>

<p>所以我也尝试使用 11.0.1(而不是都安装的 11.0.4):</p>

<p> <a href="/image/5JHmb.png" rel="noreferrer noopener nofollow"><img src="/image/5JHmb.png" alt="enter image description here"/></a> </p>

<p>但它给了我这个错误:</p>

<pre><code>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 &#39;:compileDebugJavaWithJavac&#39;.
&gt; Compilation failed; see the compiler error output for details.
</code></pre>

<p>此时,当我像这样删除插件时,我真的不知道如何解决这个问题:</p>

<pre><code>ionic cordova plugin remove phonegap-plugin-push
</code></pre>

<p>我可以再次启动应用程序,没有任何问题或错误。</p>

<p>我怎样才能使这个插件工作?</p>

<p>提前致谢。</p>

<p><strong>编辑</strong></p>

<p>所以我用这样的样板创建了一个新的 Ionic 应用程序:</p>

<pre><code>ionic start myApp tabs
</code></pre>

<p>我安装了phonegap插件push这样的:</p>

<pre><code>ionic cordova plugin add phonegap-plugin-push
</code></pre>

<p>当使用这个时:</p>

<pre><code>ionic cordova run android
</code></pre>

<p>应用程序刚启动时没有错误,因此是我的另一个项目导致了这些问题。我在空白应用程序上使用了 ionic 信息,它向我展示了这一点:</p>

<pre><code>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
</code></pre>

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

<pre><code>npm install -g cordova
</code></pre>

<p>和</p>

<pre><code>npm install -g ionic
</code></pre>

<p>和</p>

<pre><code>npm install @ionic/app-scripts@latest
</code></pre>

<p>但再次运行后,我得到了之前的错误之一:</p>

<pre><code>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 AndroidManifest.xml:28:13-35
      is also present at AndroidManifest.xml:28:13-35 value=(25.4.0).
      Suggestion: add &#39;tools:replace=&#34;android:value&#34;&#39; to &lt;meta-data&gt; element at AndroidManifest.xml:26:9-28:38 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task &#39;:app:processDebugManifest&#39;.
&gt; Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.1.0) from AndroidManifest.xml:28:
13-35
      is also present at AndroidManifest.xml:28:13-35 value=(25.4.0).
      Suggestion: add &#39;tools:replace=&#34;android:value&#34;&#39; to &lt;meta-data&gt; element at AndroidManifest.xml:26:9-28:38 to override.
</code></pre>

<p>此时,重新创建一个新项目,安装我需要的所有插件,测试它们是否工作,然后将我的逻辑/类添加到其中不是更好吗?</p>

<p>谢谢</p>

<p><strong>编辑</strong></p>

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

<ul>
<li> <a href="https://ionicframework.com/docs/native/file-path/" rel="noreferrer noopener nofollow">https://ionicframework.com/docs/native/file-path/</a> </li>
<li> <a href="https://ionicframework.com/docs/native/push/" rel="noreferrer noopener nofollow">https://ionicframework.com/docs/native/push/</a> </li>
</ul></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>好的,所以我已经设法解决了我的问题。这就是我所做的:</p>

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

<p> <a href="https://stackoverflow.com/questions/43280871/android-getting-manifest-merger-failed-error-after-update-to-new-version-of-grad" rel="noreferrer noopener nofollow">android getting Manifest merger failed error after update to new version of gradle</a> </p>

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

<pre><code>configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details -&gt;
      def requested = details.requested
      if (requested.group == &#39;com.android.support&#39;) {
            if (!requested.name.startsWith(&#34;multidex&#34;)) {
                details.useVersion &#39;26.1.0&#39;
            }
      }
    }
}
</code></pre>

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

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

<p>我现在可以再次运行我的项目而没有错误。</p></p>
                                   
                                                <p style="font-size: 20px;">关于android - Ionic 的 native   &#34;Push&#34;插件导致错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47928626/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47928626/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: android - Ionic 的 native &#34;Push&#34;插件导致错误