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

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

解决: 在项目目录下安装:npm install @ionic/[email protected] --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/[email protected] --save-dev

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

JDK版本过高了

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

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

环境变量修改.png

 

问题4:Failed to execute aapt

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

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

v4+.png


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

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

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文件夹中的

 

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

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;
}