如何使用 cordova 插件挂钩将文件复制到 /res/raw
How to copy file to /res/raw using cordova plugin hook
如标题所示,我正在尝试为 android 构建一个 cordova 插件(这是一个空插件),目的是将文件复制到 APK 的 /res/raw 文件夹中.这似乎是 Android 所需要的,因此 OneSignal 可以在收到通知时播放自定义铃声。为了实现这一点,我使用了 after_plugin_install 和 before_build 的钩子。内容是一样的,不同的是文件夹。这个钩子是 this one.
的修改版本
我为此使用插件而不是将资源添加到 cordova 项目文件夹的原因是因为我无权访问 cordova 项目文件夹。因此,我唯一的 "workaround" 是使用插件(带钩子)来完成它。
钩子文件摘录:
// configure all the files to copy from each of the resource paths.
// key of object is the source file, value is the destination location.
// the directory/file structure used closely mirrors how the resources
// are stored in each platform
var androidFilesToCopy = {
// android icons
"android/beep.wav": "beep.wav"
};
// required node modules
var fs = require('fs');
var path = require('path');
var rootdir = "plugins";
var buildDir = "build";
// android platform resource path
var platformAndroidPath = 'locales/android/raw/';
我已经测试了几个平台值AndroidPath 和几个"hook timeline types"(before_build、after_build、before_prepare 等)但是none 似乎有效。我还看到,如果我使用 "cordova plugins add location"(复数形式的插件),它会检测到该文件。如果我使用 "cordova plugin add location"(插件为单数),它不会检测到文件。
此时我有点迷茫,真的不知道现在该去哪里。如果有人能够提供一些指导,那将不胜感激。完整的插件是 here.
谢谢!
根据您定位的 cordova-android 版本,您要查找的路径会有所不同。
例如 cordova platform add android@6.4.0
这是整体文件夹结构:
├── hooks
├── platforms
│ └── android
│ ├── CordovaLib
│ ├── assets
│ ├── cordova
│ ├── libs
│ ├── platform_www
│ ├── res # <- this is the path you want!
│ └── src
├── plugins
├── res
└── www
请注意,res 就在 "android" 文件夹下,那是 android 资源所在的位置。
但是,对于 cordova platform add android@8.0.0
:
├── hooks
├── platforms
│ └── android
│ ├── CordovaLib
│ ├── app
│ │ └── src
│ │ └── main
│ │ ├── assets
│ │ ├── java
│ │ ├── libs
│ │ └── res # <- this is the path you want!
│ ├── cordova
│ └── platform_www
├── plugins
├── res
└── www
所以,在实践中,这取决于你是哪个 cordova 平台 运行 你的钩子,但一般来说它是这样的:
- cordova-android < 7:
platforms/android/res/raw
- cordova-android >= 7:
platforms/android/app/src/main/res/raw
你可以找到一个类似的例子here。
如标题所示,我正在尝试为 android 构建一个 cordova 插件(这是一个空插件),目的是将文件复制到 APK 的 /res/raw 文件夹中.这似乎是 Android 所需要的,因此 OneSignal 可以在收到通知时播放自定义铃声。为了实现这一点,我使用了 after_plugin_install 和 before_build 的钩子。内容是一样的,不同的是文件夹。这个钩子是 this one.
的修改版本我为此使用插件而不是将资源添加到 cordova 项目文件夹的原因是因为我无权访问 cordova 项目文件夹。因此,我唯一的 "workaround" 是使用插件(带钩子)来完成它。
钩子文件摘录:
// configure all the files to copy from each of the resource paths.
// key of object is the source file, value is the destination location.
// the directory/file structure used closely mirrors how the resources
// are stored in each platform
var androidFilesToCopy = {
// android icons
"android/beep.wav": "beep.wav"
};
// required node modules
var fs = require('fs');
var path = require('path');
var rootdir = "plugins";
var buildDir = "build";
// android platform resource path
var platformAndroidPath = 'locales/android/raw/';
我已经测试了几个平台值AndroidPath 和几个"hook timeline types"(before_build、after_build、before_prepare 等)但是none 似乎有效。我还看到,如果我使用 "cordova plugins add location"(复数形式的插件),它会检测到该文件。如果我使用 "cordova plugin add location"(插件为单数),它不会检测到文件。
此时我有点迷茫,真的不知道现在该去哪里。如果有人能够提供一些指导,那将不胜感激。完整的插件是 here.
谢谢!
根据您定位的 cordova-android 版本,您要查找的路径会有所不同。
例如 cordova platform add android@6.4.0
这是整体文件夹结构:
├── hooks
├── platforms
│ └── android
│ ├── CordovaLib
│ ├── assets
│ ├── cordova
│ ├── libs
│ ├── platform_www
│ ├── res # <- this is the path you want!
│ └── src
├── plugins
├── res
└── www
请注意,res 就在 "android" 文件夹下,那是 android 资源所在的位置。
但是,对于 cordova platform add android@8.0.0
:
├── hooks
├── platforms
│ └── android
│ ├── CordovaLib
│ ├── app
│ │ └── src
│ │ └── main
│ │ ├── assets
│ │ ├── java
│ │ ├── libs
│ │ └── res # <- this is the path you want!
│ ├── cordova
│ └── platform_www
├── plugins
├── res
└── www
所以,在实践中,这取决于你是哪个 cordova 平台 运行 你的钩子,但一般来说它是这样的:
- cordova-android < 7:
platforms/android/res/raw
- cordova-android >= 7:
platforms/android/app/src/main/res/raw
你可以找到一个类似的例子here。