在 Cordova 上将 AngularJS webapp 构建到 android 时出现问题
Posted
技术标签:
【中文标题】在 Cordova 上将 AngularJS webapp 构建到 android 时出现问题【英文标题】:Problem building AngularJS webapp to android on Cordova 【发布时间】:2020-08-29 21:24:33 【问题描述】:我有一个用 html、CSS 和 AngularJS 制作的 webapp。
我将它托管在自己的服务器上,一切正常。
我安装了cordova,创建了一个项目,并在www文件夹中添加了与webapp相关的文件。
从命令运行应用程序:
cordova run browser
一切都很完美。
运行时:
cordova run android
apk 已构建。我将此 apk 上传到 android,但该应用程序无法正常工作。
这里是 index.html:
<!DOCTYPE html>
<html lang="pt" ng-app="myapp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exemple</title>
<script src="js/angular.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script src="js/angular-ui-router.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<!-- criação do app -->
<script src="app/app.js"></script>
<!-- definição das rotas -->
<script src="routes/routes.js"></script>
<!-- controllers do app -->
<script src="controller/DashController.js"></script>
<script src="controller/DashPainelController.js"></script>
<!-- services do app -->
<script src="services/isAuthenticated.js"></script>
</head>
<body ng-controller="DashController">
<div>
<a class="navbar-brand" ng-hide="habilitabotao" href="/">Home</a>
</div>
<div ui-view></div>
</body>
</html>
这是我的文件夹结构:
https://i.stack.imgur.com/beM8N.png
我尝试通过 Android Studio 调试应用。但是,我在 logcat 中找不到错误。 下面是我的日志:
2020-05-15 12:16:04.574 3814-3814/? I/art: Not late-enabling -Xcheck:jni (already on)
2020-05-15 12:16:04.576 3814-3814/? W/art: Unexpected CPU variant for X86 using defaults: x86
2020-05-15 12:16:06.246 3814-3814/? W/System: ClassLoader referenced unknown path: /data/app/com.parsoni-1/lib/x86
2020-05-15 12:16:06.447 3814-3814/? I/CordovaLog: Changing log level to DEBUG(3)
2020-05-15 12:16:06.447 3814-3814/? I/CordovaActivity: Apache Cordova native platform version 8.1.0 is starting
2020-05-15 12:16:06.447 3814-3814/? D/CordovaActivity: CordovaActivity.onCreate()
2020-05-15 12:16:07.155 3814-3814/? W/System: ClassLoader referenced unknown path: /system/app/Chrome/lib/x86
2020-05-15 12:16:07.165 3814-3814/? I/WebViewFactory: Loading com.android.chrome version 51.0.2704.90 (code 275509012)
2020-05-15 12:16:07.248 3814-3814/? I/cr_LibraryLoader: Time to load native libraries: 12 ms (timestamps 1724-1736)
2020-05-15 12:16:07.250 3814-3814/? I/cr_LibraryLoader: Expected native library version number "51.0.2704.90", actual native library version number "51.0.2704.90"
2020-05-15 12:16:07.759 3814-3814/? V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) eb5dc6
2020-05-15 12:16:07.760 3814-3814/? I/cr_LibraryLoader: Expected native library version number "51.0.2704.90", actual native library version number "51.0.2704.90"
2020-05-15 12:16:07.760 3814-3814/? I/chromium: [INFO:library_loader_hooks.cc(143)] Chromium logging enabled: level = 0, default verbosity = 0
2020-05-15 12:16:07.923 3814-3814/? I/cr_BrowserStartup: Initializing chromium process, singleProcess=true
2020-05-15 12:16:07.934 3814-3814/? E/ApkAssets: Error while loading asset assets/natives_blob_64.bin: java.io.FileNotFoundException: assets/natives_blob_64.bin
2020-05-15 12:16:07.935 3814-3814/? E/ApkAssets: Error while loading asset assets/snapshot_blob_64.bin: java.io.FileNotFoundException: assets/snapshot_blob_64.bin
2020-05-15 12:16:08.641 3814-3814/? D/EGL_emulation: eglCreateContext: 0x98f3c060: maj 2 min 0 rcv 2
2020-05-15 12:16:08.643 3814-3814/? D/EGL_emulation: eglMakeCurrent: 0x98f3c060: ver 2 0 (tinfo 0x98f26220)
2020-05-15 12:16:08.728 3814-3841/? W/cr_media: Requires BLUETOOTH permission
2020-05-15 12:16:08.948 3814-3814/? D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
2020-05-15 12:16:08.959 3814-3814/? W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
2020-05-15 12:16:08.961 3814-3814/? D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
2020-05-15 12:16:08.982 3814-3814/? D/SystemWebViewEngine: CordovaWebView is running on device made by: Google
2020-05-15 12:16:08.990 3814-3814/? D/PluginManager: init()
2020-05-15 12:16:09.050 3814-3814/? D/CordovaWebViewImpl: >>> loadUrl(file:///android_asset/www/index.html)
2020-05-15 12:16:09.088 3814-3814/? I/cr_Ime: ImeThread is not enabled.
2020-05-15 12:16:09.114 3814-3814/? D/CordovaActivity: Started the activity.
2020-05-15 12:16:09.119 3814-3814/? D/CordovaActivity: Resumed the activity.
2020-05-15 12:16:09.354 3814-3856/? E/libEGL: validate_display:99 error 3008 (EGL_BAD_DISPLAY)
2020-05-15 12:16:09.393 3814-3856/com.parsoni I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
2020-05-15 12:16:09.680 3814-3856/com.parsoni D/EGL_emulation: eglCreateContext: 0x96584240: maj 2 min 0 rcv 2
2020-05-15 12:16:09.681 3814-3856/com.parsoni D/EGL_emulation: eglMakeCurrent: 0x96584240: ver 2 0 (tinfo 0x96bf05a0)
2020-05-15 12:16:09.749 3814-3855/com.parsoni I/OpenGLRenderer: Initialized EGL, version 1.4
2020-05-15 12:16:09.749 3814-3855/com.parsoni D/OpenGLRenderer: Swap behavior 1
2020-05-15 12:16:10.146 3814-3855/com.parsoni W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-05-15 12:16:10.146 3814-3855/com.parsoni D/OpenGLRenderer: Swap behavior 0
2020-05-15 12:16:10.181 3814-3855/com.parsoni D/EGL_emulation: eglCreateContext: 0x98f3e5e0: maj 2 min 0 rcv 2
2020-05-15 12:16:10.287 3814-3855/com.parsoni D/EGL_emulation: eglMakeCurrent: 0x98f3e5e0: ver 2 0 (tinfo 0x96bf0050)
2020-05-15 12:16:10.606 3814-3814/com.parsoni I/Choreographer: Skipped 52 frames! The application may be doing too much work on its main thread.
2020-05-15 12:16:10.774 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:10.789 3814-3855/com.parsoni D/EGL_emulation: eglMakeCurrent: 0x98f3e5e0: ver 2 0 (tinfo 0x96bf0050)
2020-05-15 12:16:11.123 3814-3814/com.parsoni D/CordovaWebViewImpl: onPageDidNavigate(file:///android_asset/www/index.html)
2020-05-15 12:16:11.246 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:11.262 3814-3825/com.parsoni I/art: Background partial concurrent mark sweep GC freed 362(21KB) AllocSpace objects, 0(0B) LOS objects, 17% free, 18MB/22MB, paused 14.505ms total 163.363ms
2020-05-15 12:16:11.965 3814-3863/com.parsoni D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-15 12:16:16.025 3814-3814/com.parsoni W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 3814
2020-05-15 12:16:16.025 3814-3814/com.parsoni D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
2020-05-15 12:16:16.026 3814-3814/com.parsoni D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
2020-05-15 12:16:16.028 3814-3814/com.parsoni I/Choreographer: Skipped 286 frames! The application may be doing too much work on its main thread.
2020-05-15 12:16:16.038 3814-3814/com.parsoni W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 3814
2020-05-15 12:16:16.040 3814-3814/com.parsoni D/SystemWebChromeClient: file:///android_asset/www/js/angular.min.js: Line 125 : Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fhome.html&p1=-1&p2=
2020-05-15 12:16:16.040 3814-3814/com.parsoni I/chromium: [INFO:CONSOLE(125)] "Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fhome.html&p1=-1&p2=", source: file:///android_asset/www/js/angular.min.js (125)
2020-05-15 12:16:16.040 3814-3814/com.parsoni D/SystemWebChromeClient: file:///android_asset/www/js/angular-ui-router.min.js: Line 11 : Transition Rejection($id: 0 type: 6, message: The transition errored, detail: Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fhome.html&p1=-1&p2=)
2020-05-15 12:16:16.040 3814-3814/com.parsoni I/chromium: [INFO:CONSOLE(11)] "Transition Rejection($id: 0 type: 6, message: The transition errored, detail: Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fhome.html&p1=-1&p2=)", source: file:///android_asset/www/js/angular-ui-router.min.js (11)
2020-05-15 12:16:16.047 3814-3814/com.parsoni D/SystemWebChromeClient: file:///android_asset/www/js/angular-ui-router.min.js: Line 11 : Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fhome.html&p1=-1&p2=
at file:///android_asset/www/js/angular.min.js:7:168
at file:///android_asset/www/js/angular.min.js:164:28
at file:///android_asset/www/js/angular.min.js:138:44
at m.$digest (file:///android_asset/www/js/angular.min.js:149:147)
at m.$apply (file:///android_asset/www/js/angular.min.js:152:525)
at k (file:///android_asset/www/js/angular.min.js:103:129)
at V (file:///android_asset/www/js/angular.min.js:108:205)
at XMLHttpRequest.A.onerror (file:///android_asset/www/js/angular.min.js:109:195)
2020-05-15 12:16:16.048 3814-3814/com.parsoni I/chromium: [INFO:CONSOLE(11)] "Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fhome.html&p1=-1&p2=
at file:///android_asset/www/js/angular.min.js:7:168
at file:///android_asset/www/js/angular.min.js:164:28
at file:///android_asset/www/js/angular.min.js:138:44
at m.$digest (file:///android_asset/www/js/angular.min.js:149:147)
at m.$apply (file:///android_asset/www/js/angular.min.js:152:525)
at k (file:///android_asset/www/js/angular.min.js:103:129)
at V (file:///android_asset/www/js/angular.min.js:108:205)
at XMLHttpRequest.A.onerror (file:///android_asset/www/js/angular.min.js:109:195)", source: file:///android_asset/www/js/angular-ui-router.min.js (11)
2020-05-15 12:16:16.110 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:16.123 3814-3814/com.parsoni D/CordovaWebViewImpl: onPageFinished(file:///)
2020-05-15 12:16:16.183 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:17.559 3814-3814/com.parsoni I/Choreographer: Skipped 81 frames! The application may be doing too much work on its main thread.
2020-05-15 12:16:17.656 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:17.807 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:28.849 3814-3814/com.parsoni D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
2020-05-15 12:16:28.849 3814-3814/com.parsoni D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
2020-05-15 12:16:28.914 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:29.012 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:29.159 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:29.290 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:29.627 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:29.825 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:33.544 3814-3825/com.parsoni I/art: Background sticky concurrent mark sweep GC freed 6(128B) AllocSpace objects, 0(0B) LOS objects, 0% free, 25MB/25MB, paused 5.248ms total 21.323ms
2020-05-15 12:16:33.564 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:33.578 3814-3814/com.parsoni W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 3814
2020-05-15 12:16:33.663 3814-3814/com.parsoni D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
2020-05-15 12:16:33.663 3814-3814/com.parsoni D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
2020-05-15 12:16:33.675 3814-3814/com.parsoni D/SystemWebChromeClient: file:///android_asset/www/js/angular.min.js: Line 125 : Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fcontact.html&p1=-1&p2=
2020-05-15 12:16:33.675 3814-3814/com.parsoni I/chromium: [INFO:CONSOLE(125)] "Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fcontact.html&p1=-1&p2=", source: file:///android_asset/www/js/angular.min.js (125)
2020-05-15 12:16:33.720 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:33.740 3814-3814/com.parsoni D/SystemWebChromeClient: file:///android_asset/www/js/angular-ui-router.min.js: Line 11 : Transition Rejection($id: 1 type: 6, message: The transition errored, detail: Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fcontact.html&p1=-1&p2=)
2020-05-15 12:16:33.740 3814-3814/com.parsoni I/chromium: [INFO:CONSOLE(11)] "Transition Rejection($id: 1 type: 6, message: The transition errored, detail: Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fcontact.html&p1=-1&p2=)", source: file:///android_asset/www/js/angular-ui-router.min.js (11)
2020-05-15 12:16:33.851 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
2020-05-15 12:16:33.948 3814-3814/com.parsoni D/SystemWebChromeClient: file:///android_asset/www/js/angular-ui-router.min.js: Line 11 : Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fcontact.html&p1=-1&p2=
at file:///android_asset/www/js/angular.min.js:7:168
at file:///android_asset/www/js/angular.min.js:164:28
at file:///android_asset/www/js/angular.min.js:138:44
at m.$digest (file:///android_asset/www/js/angular.min.js:149:147)
at m.$apply (file:///android_asset/www/js/angular.min.js:152:525)
at k (file:///android_asset/www/js/angular.min.js:103:129)
at V (file:///android_asset/www/js/angular.min.js:108:205)
at XMLHttpRequest.A.onerror (file:///android_asset/www/js/angular.min.js:109:195)
2020-05-15 12:16:33.948 3814-3814/com.parsoni I/chromium: [INFO:CONSOLE(11)] "Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.2/$templateRequest/tpload?p0=pages%2Fcontact.html&p1=-1&p2=
at file:///android_asset/www/js/angular.min.js:7:168
at file:///android_asset/www/js/angular.min.js:164:28
at file:///android_asset/www/js/angular.min.js:138:44
at m.$digest (file:///android_asset/www/js/angular.min.js:149:147)
at m.$apply (file:///android_asset/www/js/angular.min.js:152:525)
at k (file:///android_asset/www/js/angular.min.js:103:129)
at V (file:///android_asset/www/js/angular.min.js:108:205)
at XMLHttpRequest.A.onerror (file:///android_asset/www/js/angular.min.js:109:195)", source: file:///android_asset/www/js/angular-ui-router.min.js (11)
2020-05-15 12:16:34.013 3814-3814/com.parsoni W/art: Attempt to remove non-JNI local reference, dumping thread
我认为应用程序没有找到 index.html 中引用的文件,因为 index.html Home 标签已加载,但路由文件中的 html 和控制器未加载。
有谁知道什么会导致这个问题?
欢迎任何帮助。
谢谢!
【问题讨论】:
【参考方案1】:我认为问题在于您需要将网址列入白名单。
安装插件cordova-plugin-whitelist
在 config.xml 中添加<access origin="*" />
<allow-navigation href="*" />
测试它。
如果可行,我建议您将 * 更改为更具体的地址。
我建议你阅读文档
https://github.com/apache/cordova-plugin-whitelist
【讨论】:
感谢 Maddoxswe 的回复。 access origin 行已经在 config.xml 文件中,所以它只包含 allow-navigation 行。但这对我不起作用。我创建了一个新的更简单的项目并尝试构建 apk。我注意到 index.html 已加载,但显然 index.html 中列出的文件没有。因为款式不收费。关于它可能是什么的任何建议? 您是否尝试安装白名单插件?我认为如果您使用 android studio 启动项目并阅读 logcat 输出,您会得到更好的输出。 我删除了所有外部加载的链接,将所有内容放在我的 js 文件夹中,但没有任何改变。我对问题应用了更正。 是的..我安装了白名单插件。 Maxddoxswe,当你说“用android studio启动项目”时,你会在Android Studio中打开cordova生成的apk吗?抱歉,我对移动开发了解不多。以上是关于在 Cordova 上将 AngularJS webapp 构建到 android 时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 AngularJS 视图无法在 Cordova 中加载?