离子,“科尔多瓦”不包含在平台中,本机插件不起作用
我继承了一个带有 Angular 和 Cordova 的 Ionic 5 项目,而不是 Capacitor。它使用几个本机插件,即 QR 扫描仪和应用程序内浏览器。在 Android 模拟器中启动时,该应用程序运行良好,使用:
ionic cordova run android
现在我想构建一个 APK,我可以将其分发给一些测试人员来尝试该应用程序。该 APK 必须使用特定配置来构建,以连接到预生产环境(使用 Firebase,但我认为这并不重要)。我正在使用的过程是:
ionic build --prod --platform=android --configuration=preprod
ionic cordova prepare android --no-build --prod
ionic cordova build android --no-build --release
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <PATH_TO_KS> <PATH_TO_APK> <KEY_NAME>
zipalign -v 4 <PATH_TO_APK> <PATH_TO_SIGNED_APK>
现在签名的 APK 已正确安装到模拟器,并且应用程序启动并连接到预生产环境。 但是本机插件(例如QR扫描仪)不起作用。我收到的错误是本机:尝试调用QRScanner.prepare,但Cordova不可用。确保包含 cordova.js 或在设备/模拟器中运行。 Cordova 似乎未激活,因为在打印平台时,它们不包含 "cordova"
字符串:
import { Platform } from '@ionic/angular';
constructor(private platform: Platform) {...}
ngOnInit() {
this.platforms = this.platform.platforms().join(',');
}
this.platforms
的结果是 android,phablet ,cordova,desktop,hybrid 当使用 ionic cordova run android
运行时,但在安装我使用上述过程构建的 APK 时只是 android,phablet,desktop 。
你有什么想法吗?我对移动开发还很陌生,所以你绝对可以认为我犯了新手错误。如果您认为任何其他信息有用,请告诉我,我很乐意添加它。
编辑:为了支持 preprod
配置,我在 angular.json 中添加了以下部分:
{
"projects": {
"app": {
"architect": {
"build": {
"configurations": {
"preprod": {
...
}}}}}}}
仅使用 ionic cordova build android
进行构建会产生以下错误:
> ng run app:ionic-cordova-build:preprod --platform=android
An unhandled exception occurred: Configuration 'preprod' is not set in the workspace.
这就是我分 3 个阶段进行构建的原因,ionic build ... --configuration=preprod
、ionic cordova prepare ...
、ionic cordova构建 android --no-build
。我的目的是第一个以正确的配置构建应用程序,第二个使用使用配置构建的 Web 文件更新 Android 项目,第三个构建最终的 APK。
I have inherited an Ionic 5 project with Angular and Cordova - not Capacitor. It is using a couple of native plugins, namely QR Scanner and In-app Browser. The application works fine when launching in the Android emulator using:
ionic cordova run android
Now I want to build an APK that I can distribute to a few testers to give the application a try. This APK has to be built using a specific configuration to connect to a pre-production environment (using Firebase, but that shouldn't matter I guess). The process I am using is:
ionic build --prod --platform=android --configuration=preprod
ionic cordova prepare android --no-build --prod
ionic cordova build android --no-build --release
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <PATH_TO_KS> <PATH_TO_APK> <KEY_NAME>
zipalign -v 4 <PATH_TO_APK> <PATH_TO_SIGNED_APK>
Now the signed APK installs correctly to the emulator and the application starts AND connects to the pre-production environment. But the native plugins (e.g. QR Scanner) do not work. The error I am getting is Native: tried calling QRScanner.prepare, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator. It seems that Cordova is not active because when printing the platforms, they do not include the "cordova"
string:
import { Platform } from '@ionic/angular';
constructor(private platform: Platform) {...}
ngOnInit() {
this.platforms = this.platform.platforms().join(',');
}
The result of this.platforms
is android,phablet,cordova,desktop,hybrid when running with ionic cordova run android
, but is just android,phablet,desktop when installing the APK I built with the process above.
Do you have any ideas why? I am quite new to mobile development, so you can absolutely assume I have made newbie mistakes. And if you feel that any other information would be useful, please let me know and I will happily add it.
EDIT: In order to support the preprod
configuration, I added the following sections in angular.json:
{
"projects": {
"app": {
"architect": {
"build": {
"configurations": {
"preprod": {
...
}}}}}}}
Building with just ionic cordova build android
yields the following error:
> ng run app:ionic-cordova-build:preprod --platform=android
An unhandled exception occurred: Configuration 'preprod' is not set in the workspace.
This is why I am building in 3 phases, ionic build ... --configuration=preprod
, ionic cordova prepare ...
, ionic cordova build android --no-build
. My intention is for the first to build the app in the correct configuration, the second to update the Android project with the web files built with the configuration and the third to build the final APK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
构建新的调试应用程序(apk)的简单正常方法只是调用
This 在内部调用prepare,因此不需要再次调用。它将生成一个 apk 文件,您可以使用该文件在设备(可能还包括模拟器,具体取决于功能)上进行测试。
当需要发布到商店时,考虑到这是一个新应用并且 Google 的要求已发生变化,您需要构建一个
.aab
文件。以下将做到这一点:至于签名,您需要向 Google 提供密钥,他们将通过提供的 .aab 为您签名。这里有更详细的解释:
https ://ionicframework.com/blog/google-play-android-app-bundle-requirement-for-new-apps/
现在,如果您需要创建自定义配置(您所指的
preprod
),因为您有一些非常具体的角度环境更改或要求,除了默认值必须提供的内容之外,正如您在评论中发现的那样,您将需要编辑angular.json
文件来添加它。Ionic 默认提供环境变量的结构,可以在 src/environments 中找到。有关其工作原理的更多详细信息,请访问:
https://medium .com/swlh/environment-variables-in-angular-ionic-8aa1698f2cc5
The simple normal way of building a new app for debug (apk) is just calling
This calls prepare internally, so no need for another call. It will produce an apk file that you can use for testing on devices (and possibly emulators depending on functionality).
When it's time to release to the store, considering this is a new app and Google's requirements have changed, you need to build a
.aab
file. The following will do that:As for signing, you need to provide the key to Google and they will sign it for you via the .aab provided. This is explained in more detail here:
https://ionicframework.com/blog/google-play-android-app-bundle-requirement-for-new-apps/
Now, if you need to create a custom configuration (what you're referring as
preprod
) because you have some very specific angular environment changes or requirements, outside of what the defaults have to offer, as you found out in the comments, you will need to edit theangular.json
file to add it.Ionic by default provides a structure for environment variables, which can be found in
src/environments
. More details about how this works can be found here:https://medium.com/swlh/environment-variables-in-angular-ionic-8aa1698f2cc5