探索移動端可能性:Capacitor5.5.1和vue2在Android studio中精細融合

介紹:

移動應用開發是日益復雜的任務,本文將帶領您深入探索如何無縫集成Capacitor5.5.1、Vue2和Android Studio,以加速您的開發流程
  • Capacitor 是一個用于構建跨平臺移動應用程序的開源框架。
  • Vue 是一個流行的 JavaScript 框架,用于構建用戶界面。
  • Android Studio 是用于開發 Android 應用程序的官方集成開發環境(IDE)。

環境設置:

Capacitor 官方支持三個應用目標:Android、iOS 和 Web
(vue、recat…capacitor并不限制你使用特定的框架,根據偏好)

1、Android需要在Android studioAndroid SDK 環境下運行

Capacitor 5 至少需要 Android Studio 2022.2.1

2、IOS 需要在Xcode或者MacOS環境下運行
3、web是以下環境
3.1.首先你得有vue的開發環境

在這里插入圖片描述

3.2.確保您已經正確安裝并配置了Vue Cli、Capacitor和Android Studio,如果尚未安裝,請按照以下步驟進行設置。

項目創建:

使用 Vue CLI 創建一個新的 Vue 2 項目,并通過 Capacitor 初始化您的項目

使用 npm 或 yarn 在命令行中安裝 Vue CLI:npm install -g @vue/cli 或 yarn global add @vue/clivue create my-vue-app
cd my-vue-app
npm install @capacitor/core @capacitor/cli
npx cap init

平臺版本:

capacitor使用5.5.1的話,安卓和IOS 的版本都需要5.5.1、不然會報一堆奇怪的錯誤,導致耽誤開發進度
在這里插入圖片描述

Android平臺集成:

添加 Android 平臺,以便在 Android Studio 中進行原生開發。導入 Android Studio 并配置您的開發環境。

npx cap add android

打開 Android Studio,導入 Capacitor 項目的 Android 文件夾。

配置 Android Studio:
  • 在 Android Studio 中,打開 settings.gradle 文件,確保項目正確配置。
  • build.gradle 文件中,添加 Capacitor 插件和依賴項。
  • MainActivity.java 文件中,添加 Capacitor 相關的初始化代碼。
構建和運行應用程序:
  • 在命令行中運行 npm run build 命令,構建 Vue 項目。
  • 在命令行中運行 npx cap copy 命令,將構建后的文件復制到 Android 項目中,確保兩者保持同步
  • 在 Android Studio 中,點擊 “Sync Project with Gradle Files” 按鈕,確保項目正確同步。
  • 在 Android Studio 中,點擊 “Run” 按鈕,運行應用程序。

IOS平臺集成:

npx cap add 

可以自己安裝一個蘋果環境測試


原生開發:

在 Android Studio 中進行原生開發,編輯原生代碼,與 Vue 2 項目集成。

WebView 集成:

在 Vue 中使用 WebView 組件,可以按照以下步驟進行:
1.安裝 vue-webview 插件:
在 Vue 項目的根目錄下,使用 npm 或 yarn 安裝 vue-webview 插件:

npm install vue-webview

yarn add vue-webview

2.在 Vue 組件中引入 WebView 組件:
在需要使用 WebView 的 Vue 組件中,引入 vue-webview 插件,并注冊 WebView 組件:

import VueWebView from 'vue-webview';export default {components: {VueWebView,},// ...
}

3.在模板中使用 WebView 組件:
在 Vue 組件的模板中,使用 標簽來包裹 WebView 組件,并設置相應的屬性:

<template><div><vue-webview:src="webViewUrl":options="webViewOptions"@loadstart="handleLoadStart"@loadend="handleLoadEnd"></vue-webview></div>
</template>

在這個示例中,webViewUrl 是 WebView 加載的 URL,webViewOptions 是 WebView 的配置選項。你可以根據需要設置其他屬性,如 WebView 的寬度、高度、樣式等。
4.在 Vue 組件中處理 WebView 事件和方法:
在 Vue 組件的方法中,處理 WebView 的事件和方法。例如,你可以在 handleLoadStart 和 handleLoadEnd 方法中處理 WebView 的加載開始和加載結束事件。

export default {// ...methods: {handleLoadStart() {// WebView 加載開始時的處理邏輯},handleLoadEnd() {// WebView 加載結束時的處理邏輯},},
}

通過以上步驟,你就可以在 Vue 項目中使用 WebView 組件來呈現 Vue 2 項目的內容。可以根據需要設置 WebView 的屬性和處理 WebView 的事件,以實現更豐富的交互和功能。

插件和功能:

在這里插入圖片描述

插件:分為官方插件、社區插件

其實你使用了vue框架開發的也可以自己寫所需要的插件,以下給大家看看如何使用官方插件
**官方插件使用:**地理位置、相機、本地通知
地理位置:

注:需要在項目的根目錄下的AndroidManifest.xml文件中添加獲取位置權限(在獲取地理位置插件的xml中添加或者在主目錄下的xml)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
引入依賴
npm install @capacitor/geolocation
在需要的頁面注入
import { Geolocation } from '@capacitor/geolocation';
Ex:// 獲取地理位置(得到經緯度,需要墻,由于這里調用的是google map)async getCurrentPosition() {// 在這里編寫按鈕點擊后的處理邏輯console.log('Button clicked!');// 在需要獲取地理位置的地方調用以下代碼const coordinates = await Geolocation.getCurrentPosition();this.latitude = coordinates.coords.latitude;this.longitude = coordinates.coords.longitude;console.log('Current position:', coordinates);},

相機(支持拍照和相冊上傳):

引入依賴
npm install @capacitor/camera
在需要的頁面注入
import { Camera, CameraResultType } from '@capacitor/camera';
Ex:// 在需要拍照的地方調用該方法async takePicture() {const image = await Camera.getPhoto({quality: 90,allowEditing: false,resultType: CameraResultType.Uri});console.log("Current image", image.webPath);// 處理拍攝的照片,例如顯示在頁面上this.photo = image.webPath;},//在windows 中的web打開之后會報錯,在項目的main.js中添加// 某些 Capacitor 插件(例如Camera或Toast)在未本機運行時具有基于 Web 的 UI。
// 例如,Camera.getPhoto()在網絡上運行時,調用將加載響應式拍照體驗:import { defineCustomElements } from '@ionic/pwa-elements/loader';
// Call the element loader before the render calldefineCustomElements(window);

本地通知:

引入依賴
npm install @capacitor/local-notifications
在需要的頁面注入
import { LocalNotifications } from '@capacitor/local-notifications';
Ex:getMsg() {LocalNotifications.schedule({notifications: [{title: '新消息',body: '測試消息',id: 1,schedule: { at: new Date(Date.now() + 1000 * 5) }}]});},

調試和測試:

這是一個繁瑣的過程
需要安裝Android SDK 在官網下載完之后千萬不要放在C盤,安裝一個模擬器需要10多個G

在這里插入圖片描述

這里可以看到Gradle版本,可以去Gradle下載生成后需要的版本,會花費好長時間

在這里插入圖片描述

發布和部署:

參考

常見問題和解決方案:

常見問題:

i.Capacitor版本為5.5.1,Android、IOS 版本都應該為5.5.1否則不兼容 見 ”平臺版本“

ii:權限報錯:

 E  Error send stats: {"error":"response_error","message":"Error send stats: com.android.volley.NoConnectionError: java.net.UnknownHostException:Unable to resolve host \"api.capgo.app\": No address associated with hostname"}

解決方案:

The error message indicates that there is an issue with connecting to the host "api.capgo.app" due to a "java.net.UnknownHostException," suggesting that the hostname cannot be resolved to an IP address. This issue is commonly related to network connectivity problems. Here are some steps you can take to troubleshoot and resolve this issue:1. **Check Internet Connection:**Ensure that your device or emulator has a stable internet connection. If you are using a physical device, check if it is connected to a network, and if you are using an emulator, make sure your computer has an active internet connection.2. **DNS Resolution:**The error suggests a problem resolving the hostname to an IP address. Ensure that the DNS (Domain Name System) configuration on your device or network is functioning correctly. You can try accessing "api.capgo.app" from a web browser to see if the domain resolves correctly.3. **Hosts File:**Check if there are any entries related to "api.capgo.app" in your device's hosts file. If there are, ensure that they are correctly configured, and if necessary, remove or correct any incorrect entries.4. **Firewall and Proxy Settings:**If you are behind a firewall or using a proxy, ensure that it is configured correctly to allow connections to "api.capgo.app." Check if there are any network restrictions that might be preventing your application from reaching the server.5. **Server Status:**Check the status of the "api.capgo.app" server. It's possible that the server is temporarily down or experiencing issues. You can try accessing the API from a browser or using a tool like cURL to see if there are any issues with the server.6. **Network Permissions:**Ensure that your application has the necessary network permissions in its AndroidManifest.xml file. You should have the `<uses-permission android:name="android.permission.INTERNET" />` permission declared.7. **Retry Mechanism:**Implement a retry mechanism in your code to handle temporary network issues. If the error persists, your application can retry the network request after a short delay.8. **Logs and Debugging:**Utilize logging statements in your code to trace the flow and identify where the error occurs. Check if there are additional details in the logs that might provide more information about the issue.By going through these steps, you should be able to identify and address the underlying cause of the "UnknownHostException" and resolve the issue with connecting to the host "api.capgo.app."

iii.Capacitor5 需要Gradle8和java17 :是由我用的 Capacitor 5.5.1、Gradle 4.10.1、JDK 1.8

iv.Android Studio運行空白,沒有顯示任何控件 我的問題是由于使用vue Router 的index.js的默認的base沒有指定,導致在Android Studio中報錯從而顯示空白

v.以下報錯:大概意思就是:在Android studio 中初始化 報錯,這個錯誤是由于在你的項目中同時引入了 androidx.core:core:1.10.0 和 com.android.support:support-v4:23.2.1 這兩個庫,導致了重復的類沖突。解決這個問題的方法是統一使用 androidx 庫或者 com.android.support 庫,避免同時引入兩個版本不同的庫。你可以根據你的項目需要,選擇其中一個庫并將另一個庫的引用移除,然后重新構建你的項目。如果你需要更詳細的解決方法,可以查閱相關文檔或者搜索相關問題的解決方案。

Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable> Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-v4-23.2.1-runtime (com.android.support:support-v4:23.2.1)

該錯誤消息表明您的項目中存在重復的類,特別是與android.support.v4庫相關的類。當您的項目中混合使用舊支持庫和 AndroidX 庫時,通常會出現此問題。
要解決此問題,請按照下列步驟操作:
1、更新依賴項:確保您使用的是最新版本的依賴項,尤其是 AndroidX 庫。更新您的build.gradle文件以使用最新版本的庫。
將任何出現的 替換com.android.support為相應的 AndroidX 庫。例如,替換com.android.support:support-v4:23.2.1為androidx.core:core:1.10.0.
2、檢查已棄用的庫
刪除任何已棄用或過時的依賴項,并將其替換為更新版本。
3、使用 AndroidX:將您的項目遷移到 AndroidX(如果您尚未這樣做)。AndroidX是Google提供的新的包結構,它取代了舊的支持庫。確保您的所有依賴項和您自己的代碼都使用 AndroidX 工件。
您可以通過將以下行添加到gradle.properties文件中來遷移到 AndroidX:
android.useAndroidX=true
android.enableJetifier=true (我是以這種方式解決掉)
這會自動將您的依賴項遷移到 AndroidX。
4、檢查傳遞依賴關系:有時,問題可能是由傳遞依賴關系引起的。使用該./gradlew app:dependencies命令檢查依賴關系樹并識別任何沖突的依賴關系。
一旦確定了沖突的依賴項,您就可以排除build.gradle文件中的特定傳遞依賴項。例如:
implementation (‘com.example.library:library:1.0’) {
exclude group: ‘com.android.support’
}
5、清理并重建:
進行這些更改后,清理您的項目并重建它。
在 Android Studio 中,您可以通過選擇“Build”->“Clean Project”,然后選擇“Build”->“Rebuild Project”來完成此操作。
6、使用該–stacktrace選項
如果問題仍然存在并且您需要有關錯誤的更多詳細信息,請嘗試使用該
–stacktrace
選項運行構建。
這可能會提供有關項目的哪個部分導致沖突的更多見解。
./gradlew clean assembleDebug --stacktrace
7、檢查第三方庫
如果您在項目中使用第三方庫,請確保它們與 AndroidX 兼容。
檢查每個庫的文檔或發行說明以確認兼容性

解決方案

官方社區提交你的問題,響應速度還是可以的

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/166900.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/166900.shtml
英文地址,請注明出處:http://en.pswp.cn/news/166900.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

多線程Thread(初階三:線程的狀態及線程安全)

目錄 一、線程的狀態 二、線程安全 一、線程的狀態 1.NEW Thread&#xff1a;對象創建好了&#xff0c;但是還沒有調用 start 方法在系統中創建線程。 2.TERMINATED&#xff1a; Thread 對象仍然存在,但是系統內部的線程已經執行完畢了。 3.RUNNABLE&#xff1a; 就緒狀態&…

雪花算法原理(設計原理、優缺點、如何改造它、以及應用)

雪花算法原理&#xff08;設計原理、優缺點、如何改造它、以及應用&#xff09; 雪花算法源碼為什么雪花算法是 64 位&#xff1f;為什么時間戳是41位&#xff1f;占雪花算法的 43-47 bit 位為什么工作臺最大只支持設置 31 &#xff1f;工作臺設置成了 63 會導致什么后果&#…

JeecgBoot3.0 漏洞升級 — 快速文檔

近幾年來&#xff0c;黑客攻擊行為呈現出日益復雜和隱蔽的趨勢&#xff0c;對個人和組織的安全造成了嚴重威脅。黑客們不斷尋找新的漏洞和安全漏洞&#xff0c;利用各種手段進行網絡攻擊&#xff0c;包括惡意軟件、網絡釣魚、勒索軟件等。因此&#xff0c;我們每個人都需要關注…

Java 之 final 詳解

目錄 一. 前言 二. final 的基礎使用 2.1. 修飾類 2.2. 修飾方法 2.2.1. private 方法是隱式的 final 2.2.2. final 方法可以被重載 2.3. 修飾參數 2.4. 修飾變量 2.4.1. static final 2.4.2. blank final 2.4.3. 所有 final 修飾的字段都是編譯期常量嗎&#xff1f…

數據結構:二叉查找樹,平衡二叉樹AVLTree,紅黑樹RBTree,平衡多路查找數B-Tree,B+Tree

二叉查找樹 二叉樹具有以下性質&#xff1a;左子樹的鍵值小于根的鍵值&#xff0c;右子樹的鍵值大于根的鍵值。 對該二叉樹的節點進行查找發現深度為1的節點的查找次數為1&#xff0c;深度為2的查找次數為2&#xff0c;深度為n的節點的查找次數為n&#xff0c;因此其平均查找次…

2023年亞太數學建模C題數據分享+詳細思路

在報名截止的前一天&#xff0c;我嘗試進行了報名。到那時&#xff0c;已有11,000個隊伍注冊參賽。在我的了解中&#xff0c;在數模比賽中除了國賽美賽外&#xff0c;幾乎沒有其他競賽的參賽隊伍數量能與此相媲美。即便不考慮賽題的難度和認可度&#xff0c;亞太地區的這場競賽…

JavaScript實現動態背景顏色

JavaScript實現動態背景顏色 前言實現過程HTML實現過程CSS實現過程JS實現過程全部源碼 前言 本文主要講解JavaScript如何實現動態背景顏色&#xff0c;可以根據顏色選擇器選擇的顏色而實時更新到背景中&#xff0c;如下圖所示。 當我們在顏色選擇器中改變顏色時&#xff0c;會…

代碼掃描,漏洞檢測

1) SQL注入是一種數據庫攻擊手段。攻擊者通過向應用程序提交惡意代碼來改變原SQL語句的含義&#xff0c;進而執行任意SQL命令&#xff0c;達到入侵數據庫乃至操作系統的目的。在Mybatis Mapper Xml中&#xff0c;#變量名稱創建參數化查詢SQL語句,不會導致SQL注入。而$變量名稱…

SPSS信度分析

前言&#xff1a; 本專欄參考教材為《SPSS22.0從入門到精通》&#xff0c;由于軟件版本原因&#xff0c;部分內容有所改變&#xff0c;為適應軟件版本的變化&#xff0c;特此創作此專欄便于大家學習。本專欄使用軟件為&#xff1a;SPSS25.0 本專欄所有的數據文件請點擊此鏈接下…

內網滲透之Linux權限提升大法

文章目錄 內網滲透|Linux權限提升大法0x01 前言0x02 工具介紹1.traitor2.LinEnum3.linux-exploit-suggester.sh4.Linux Exploit Suggester 25.beroot 0X02提權手法1.環境變量提權2.利用suid提權3.定時任務提權3.1定時任務文件覆蓋提權3.2定時任務tar命令通配符注入提權 4.sudo提…

【matlab程序】matlab給風速添加圖例大小

【matlab程序】matlab給風速添加圖例大小 clear;clc;close all; % load 加載風速數據。 load(matlab.mat) % 加載顏色包信息 gray load(D:\matlab_work\函數名為colormore的顏色索引表制作\R_color_txt\R_color_single\gray89.txt); brown load(D:\matlab_work\函數名為color…

_STORAGE_WRITE_ERROR_ thinkphp報錯問題原因

整個報錯內容如下 Uncaught exception Think\Exception with message _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/1338db9dec777aab181d4e74d1bdf964.php in C:\inetpub\wwwroot\ThinkPHP\Common\functions.php:101 Stack trace: #0 C:\inetpub\wwwroot\ThinkPHP\Library\…

1. 應用編程概念

1. 應用編程概念 1 系統調用概念1 應用編程和裸機編程、驅動編程的區別 1 系統調用概念 系統調用其實是 Linux 內核提供給應用層的應用編程接口&#xff0c;是 Linux 應用層進入內核的入口。用戶通過系統調用來使用系統提供的各種服務&#xff0c;實現了與內核的交互。 1 應用…

JavaFx 設置窗口邊框圓角

UI界面要求窗口邊框有一定弧度&#xff0c;因為之前沒有做過&#xff0c;網上看了很多文章&#xff0c;都用到了css語句 "-fx-background-radius: ; 我在xml布局文件根節點使用無效&#xff0c;在Scene組件設置無效&#xff0c;gpt等ai問了一圈代碼也是無效&#xff0c;…

【JavaEE】認識多線程

作者主頁&#xff1a;paper jie_博客 本文作者&#xff1a;大家好&#xff0c;我是paper jie&#xff0c;感謝你閱讀本文&#xff0c;歡迎一建三連哦。 本文錄入于《vaEE》專欄&#xff0c;本專欄是針對于大學生&#xff0c;編程小白精心打造的。筆者用重金(時間和精力)打造&am…

React + BraftEditor 實現富文本編輯

Braft Editor 是一個基于 React 和 Draft-js 開發的富文本編輯器&#xff0c;提供了豐富的基礎功能&#xff0c;如基本文本格式化、列表、鏈接、圖片上傳、視頻插入等&#xff0c;并且還支持擴展。 首先&#xff0c;確保你已經在項目中安裝了 Braft Editor 和它的依賴項&#x…

NPU、CPU、GPU算力及算力計算方式

NVIDIA在9月20日發布的NVIDIA DRIVE Thor 新一代集中式車載計算平臺&#xff0c;可在單個安全、可靠的系統上運行高級駕駛員輔助應用和車載信息娛樂應用。提供 2000 萬億次浮點運算性能&#xff08;2000 萬億次8位浮點運算&#xff09;。NVIDIA當代產品是Orin&#xff0c;算力是…

Java基礎(問題+答案)——第4期

其他的幾期見這個專欄 Java中的多態性&#xff08;Polymorphism&#xff09;&#xff1a; 多態性是指一個對象可以用來引用多個類型的特性。在Java中&#xff0c;多態性通過方法的重寫和接口實現來實現。 Java中的final關鍵字的用途&#xff1a; final可以用于變量、方法和類。…

堪比數據恢復大師軟件推薦,恢復數據很簡單!

“作為一個經常丟失數據的電腦用戶來說&#xff0c;我覺得我非常需要一些簡單有效的數據恢復方法。大家有什么比較靠譜的軟件推薦嗎&#xff1f;非常感謝&#xff01;” 在數字化時代&#xff0c;數據的存儲是比較重要的。很多用戶都會選擇將重要的文件保存在電腦上。如果數據丟…

第二證券:北證50指數一枝獨秀 短劇游戲概念股持續活躍

周三&#xff0c;滬深兩市三大指數顫動調整&#xff0c;北證50指數“鶴立雞群”&#xff0c;大漲超8%。到收盤&#xff0c;上證綜指報3043.61點&#xff0c;跌0.79%&#xff1b;深證成指報9855.66點&#xff0c;跌1.41%&#xff1b;創業板指報1950.01點&#xff0c;跌1.73%。滬…