【建置環境說明】
我的開發環境是win11,python是3.10.7版本,編碼的工具是Visual Studio Code(簡稱vscode)。
【本篇說明】
需先完成程式開發(可參考網址:APP開發),在範例專案資料夾裡有main.py、countinmind.kv、CountInMindIco.ico以及font資料夾(裡面有TW-Kai-98_1.ttf文字檔案)。先前幾篇已完成虛擬機安裝、linux環境及功能設定(可參考:02-Linux系統環境設定及操作過程),虛擬機也能正常開機,接下來就是使用buildozer來做打包。
以下分享打包過程詳細步驟及遭遇問題的解決方式:1.開啟虛擬機終端機,安裝pip,輸入以下: sudo apt install python3-pip
2.安裝buildozer,輸入以下: pip install --user --upgrade buildozer
3.更新apt,輸入:sudo apt update
4.安裝相關套件,輸入: sudo apt install -y git zip unzip openjdk-17-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev automake
5.輸入: pip3 install --user --upgrade Cython==0.29.33 virtualenv (一開始我漏掉這段,所以才會有下面的指令除錯,建議可在這個步驟先輸入指令)
6.若出現:# add the following line at the end of your ~/.bashrc file
輸入: export PATH=$PATH:~/.local/bin/
7.將終端機導航至 main.py的資料夾,輸入:cd (目的資料夾),我的是: cd /home/my account name/share_folder,我先在linux系統下建立一個share_folder資料夾,將本機共用資料夾裡面的專案資料複製到share_folder資料夾。
8.再輸入以下: buildozer init,在main.py的資料夾會出現 buildozer.spec,這個檔案是待會的打包設置清單,開啟它並編輯修改(通常可針對app名稱、圖像修改而已)
9.開始執行打包測試,輸入: buildozer android debug deploy run (這是要連接安卓手機測試佈署deploy用指令)。如果沒有連接手機,可輸入: buildozer android debug(這個是為了要產生.apk檔案),或是輸入:buildozer android release(這個是為了產生.aab檔案)
10.出現錯誤: # Cython (cython) not found, please install it.
安裝 Cython:輸入:sudo apt-get update # 更新軟體包列表
輸入:sudo apt-get install cython3 # 安裝 Cython
11.出現錯誤:Package cython is not available, but is referred to by another package.This may mean that the package is missing, has been obsoleted, or is only available from another source
E: Package 'cython' has no installation candidate。
這是因為buildozer搜尋cython,但是實際的指令是cython3。要解決這個問題,您必須建立一個cython命令,將所有參數傳遞給cython3。
所以輸入以下:cd /bin/ && sudo gedit cython ,在彈出的編輯器中寫: cython3 $@ ,這將把所有參數從cython傳遞到cython3。儲存檔案並在終端機輸入: sudo chmod 755 cython
12.接著再輸入一次: sudo apt-get install cython,然後導航至步驟7的資料夾,輸入: buildozer android debug deploy run (我是輸入: buildozer android debug run)
13.出現報錯: (我猜前面漏了安裝python3 或 cython 虛擬環境)
[WARNING]: ERROR: /home/farloveway/share_folder/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/native-build/python3 failed!
#Command failed: ['/usr/bin/python3', '-m', 'pythonforandroid.toolchain', 'create', '--dist_name=countinmind_android', '--bootstrap=sdl2', '--requirements=python3,kivy,requests,urllib3,chardet,idna,certifi', '--arch=arm64-v8a', '--arch=armeabi-v7a', '--copy-libs', '--color=always', '--storage-dir=/home/farloveway/share_folder/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a', '--ndk-api=21', '--ignore-setup-py', '--debug']
#Buildozer failed to execute the last command
#The error might be hidden in the log above this error
#Please read the full log, and search for it before
#raising an issue with buildozer itself.
#In case of a bug report, please add a full log with log_level = 2
14.再輸入: pip3 install --user --upgrade Cython==0.29.33 virtualenv,後面接著輸入: buildozer android debug deploy run (我是輸入: buildozer android debug)
15.終端機出現: # Waiting for application to start. 或是出現已打包完成apk文字 (Android packaging done!),會在步驟7的資料夾看到打包好的.apk檔案,我的在bin資料夾裡面。
【最後補充】如果遇到需重新執行打包或者是打包失敗、或是要修改.spec文件(像是遇到很多問題無法解決,就可考慮試試看),先在終端機輸入: buildozer appclean (目的是先清除 .buildozer: ANT、SDK、NDK等),然後關機虛擬機再重開起,並將.spec設置清單修改好,再在終端機輸入:buildozer android debug (或release),正常在打包過程中,會詢問2~3次是否同意 Google 授權協議,請輸入"y"鍵,不能輸入空白鍵。
【本篇小結】
以上是在虛擬機linux系統下,所使用 buildozer打包很多次所整理出來的過程及問題,希望能幫助到各位順利完成打包!
【本篇結束】
在自學路上遇到困難是很正常的事,只要堅持到底,相信就會有所成果,期勉大家一同努力。