Android ionic 使用当中的几个问题记录

本文记录了在使用Ionic进行Android应用开发时遇到的六个问题及其解决方案:@ionic/app-scripts未安装、TypeError错误、JDK版本要求、aapt执行失败、自定义插件调用失败和线程问题。详细介绍了每个问题的错误信息及相应的解决步骤,帮助开发者顺利解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题1:@ionic/app-scripts not installed

解决: 在项目目录下安装:npm install @ionic/app-scripts@latest --save-dev

问题2:Cannot read property 'type' of undefined

完整的错误信息如下
Error: ./node_modules/tslib/tslib.es6.js
Module build failed: TypeError: Cannot read property 'type' of undefined

解决:这个问题和第一个问题的解决方式一样, 在项目目录下安装:npm install @ionic/app-scripts@latest --save-dev

问题3:CordovaError: Requirements check failed for JDK 1.8 or greater

JDK版本过高了

解决:如果你同时安装了1.8和更高的版本,不需要卸载较高的版本,只要在环境变量中的用户变量中设置你的JAVA_HOME的路径对应到你的1.8版本就行了。

环境变量修改.png

 

问题4:Failed to execute aapt

这个问题其实是FileOpener2插件引起的,因为这个插件使用的V4包没有指定具体的版本号。
我们可以再生成的Android项目的App的gradle中看到

v4+.png


修改为具体的版本后,build成功。

v4.png

 

解决:这个是FileOpener2的问题,在这个插件的plugin.xml 文件里修改
<framework src="com.android.support:support-v4:+" />

<framework src="com.android.support:support-v4:27.1.1" />

问题5:自定义插件调用失败

提示ng:///AppModule/StatisticsPage.ngfactory.js: Line 392 : ERROR

对比可以调用的插件中的www文件夹中的js文件,发现文件头有一行重复,多了一层,删除这层。 www是指生成Android目录中的asset文件夹中的

 

image.png

问题6:线程问题

默认不是在主线程当中的,如果你要操作activity,需要再主线程当中

Threading

The plugin's JavaScript does not run in the main thread of the WebView interface; instead, it runs on the WebCore thread, as does the execute method. If you need to interact with the user interface, you should use the Activity's runOnUiThread method like so:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        final long duration = args.getLong(0);
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                ...
                callbackContext.success(); // Thread-safe.
            }
        });
        return true;
    }
    return false;
}

If you do not need to run on the UI thread, but do not wish to block the WebCore thread either, you should execute your code using the Cordova ExecutorService obtained with cordova.getThreadPool() like so:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        final long duration = args.getLong(0);
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                ...
                callbackContext.success(); // Thread-safe.
            }
        });
        return true;
    }
    return false;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值