如何在我的 Cordova 应用程序中创建 android.json?

Posted

技术标签:

【中文标题】如何在我的 Cordova 应用程序中创建 android.json?【英文标题】:How is android.json created in my Cordova app? 【发布时间】:2018-06-22 07:13:16 【问题描述】:

所以我有一个使用 ionic-plugin-deeplinks 插件的 ios/android Ionic Cordova 应用程序。当 AndroidManifest.xml 中包含以下内容时,该插件无法正常工作:

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="synapticforce.com" android:pathPrefix="/" android:scheme="https" />
    <data android:host=" " android:pathPrefix="/" android:scheme=" " />
    <data android:host=" " android:pathPrefix="/" android:scheme=" " />
    <data android:host=" " android:pathPrefix="/" android:scheme=" " />
    <data android:host=" " android:pathPrefix="/" android:scheme=" " />
</intent-filter>

但是当我把它放在它的位置时它工作正常:

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="yourekamobile" />
</intent-filter>

但是,每当我 ionic prepare 将我的修复覆盖回原来的非正常方式时。我试图在父级config.xml 中更改cordova 以及plugin.xml parent/plugins/ionic-plugin-deeplinks 文件夹中的某些内容,但它仍会被覆盖为原来的损坏方式。然后我进入parent/platforms/android/android.json 并将原始版本的编写位置更改为我的版本,并且它始终构建良好。

所以我很高兴它有效,但我的问题是它为什么有效? android.json 是如何构建的?编辑该文件是正确的做法,还是从中提取了一些我应该编辑的东西?

【问题讨论】:

【参考方案1】:

很久没发这个了,但我也遇到了同样的问题,这是我的解决方法。

如果您进入 plugins plugin.xml 文件,您会看到它执行此操作...

        <config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="$URL_SCHEME" />
            </intent-filter>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="$DEEPLINK_SCHEME" android:host="$DEEPLINK_HOST" android:pathPrefix="$ANDROID_PATH_PREFIX" />
                <data android:scheme="$DEEPLINK_2_SCHEME" android:host="$DEEPLINK_2_HOST" android:pathPrefix="$ANDROID_2_PATH_PREFIX" />
                <data android:scheme="$DEEPLINK_3_SCHEME" android:host="$DEEPLINK_3_HOST" android:pathPrefix="$ANDROID_3_PATH_PREFIX" />
                <data android:scheme="$DEEPLINK_4_SCHEME" android:host="$DEEPLINK_4_HOST" android:pathPrefix="$ANDROID_4_PATH_PREFIX" />
                <data android:scheme="$DEEPLINK_5_SCHEME" android:host="$DEEPLINK_5_HOST" android:pathPrefix="$ANDROID_5_PATH_PREFIX" />
            </intent-filter>
        </config-file>

如果您在该文件中看起来更高一点,它会将所有这些变量设置为空白或“/”。

这是设置 android.json,然后是 AndroidManifest.xml。

为了解决这与深度链接有关的问题,我在 config.xml 中添加了以下内容以覆盖这些“默认”设置。可能值得一提的是,以下内容与标签位于同一级别,因此它实际上不在其中任何一个内。

    <plugin name="ionic-plugin-deeplinks" spec="1.0.22">
      <variable name="URL_SCHEME" value="myapp" />
      <variable name="DEEPLINK_SCHEME" value="https" />
      <variable name="DEEPLINK_HOST" value="temp-site.ngrok.io" />
      <variable name="ANDROID_PATH_PREFIX" value="/app-auth" />
      <variable name="DEEPLINK_2_SCHEME" value="https" />
      <variable name="DEEPLINK_2_HOST" value="temp-site.ngrok.io" />
      <variable name="ANDROID_2_PATH_PREFIX" value="/app-auth" />
      <variable name="DEEPLINK_3_SCHEME" value="https" />
      <variable name="DEEPLINK_3_HOST" value="temp-site.ngrok.io" />
      <variable name="ANDROID_3_PATH_PREFIX" value="/app-auth" />
      <variable name="DEEPLINK_4_SCHEME" value="https" />
      <variable name="DEEPLINK_4_HOST" value="temp-site.ngrok.io" />
      <variable name="ANDROID_4_PATH_PREFIX" value="/app-auth" />
      <variable name="DEEPLINK_5_SCHEME" value="https" />
      <variable name="DEEPLINK_5_HOST" value="temp-site.ngrok.io" />
      <variable name="ANDROID_5_PATH_PREFIX" value="/app-auth" />
    </plugin>

我是 Cordova 的新手,所以我确信有更好的方法来做到这一点(也许使用 'after' 挂钩?)但这是我快速而肮脏的解决方案。

【讨论】:

以上是关于如何在我的 Cordova 应用程序中创建 android.json?的主要内容,如果未能解决你的问题,请参考以下文章

无法在 cordova 2.4 中创建 phonegap ios 插件

我可以通过 IOS 中的 phonegap/cordova 在 www 目录中创建一个文件夹吗?

如何在 Cordova 中创建基本的重复回调?

如何在 Laravel 中创建 RESTful API 以在我的 BackboneJS 应用程序中使用

如何在我的 React Native 应用程序中创建脚本

如何在我的程序的开始菜单中创建一个菜单?