# Flutter APK 筆記 以下都是以vscode內下完成操作 ### A. 不加任何金鑰直接Build 1. 在終端機上直接操作 ``` flutter build apk //在此狀態下他會直接把debug、release全部build出來 ``` * 通常路徑都會告訴你 ![](https://hackmd.io/_uploads/HkB3Jjyoh.png) ---- 2. 直接在虛擬機或是手機執行 ``` flutter install //這個就會自動幫你處理release版本 flutter run --debug //這個會指定run debug版本 ``` ### B. 架設金鑰並Build 1. 先完成金鑰的註冊 https://flutter.cn/docs/deployment/android#create-an-upload-keystore (參考資料) * 看你是要在Android Studio上執行還是要用Code方式執行。 Code方面 ``` keytool -genkey -v -keystore %userprofile%\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload ``` >%userprofile% 通常會放在專案內的keystore裡面 >![](https://hackmd.io/_uploads/rkMbzikjn.png) 2. 接著他會叫你輸入不少東西,照著填就好也可以都空白enter過去 > 假如這邊遇到==Keytool的相關指令問題==,請先確認你的jdk/bin裡面有沒有在環境變數裡面設定 3. 在Android底下創建名為key.properties的檔案 * 並輸入這些文字,記得照著需求填寫 ``` storePassword=<上一步骤中的密码> keyPassword=<上一步骤中的密码> keyAlias=upload storeFile=<密钥库的位置,例如: /Users/<user name>/upload-keystore.jks or ``` 4. 到/android/app/build.gradle 裡的android之前加上底下這串 ``` def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { ... } ``` 5. 同位置的buildTypes裡加上 ``` signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release } } ``` > 假如你有兩個或以上的key,記得名字要改 (keyAlias => d_keyAlias 之類的) #### 接下來就跟第一步的build apk一樣了 ### C. 不同名字的debug、release檔案 * 其餘步驟都跟第一、二步一樣這裡提到怎麼更改名字 首先先到android\app\build.gradle裡面的buildTypes 像是我想改成有deBug、release然後兩個名字都不相同 要在裡面補上==applicationIdSuffix "{你想要的前綴}"== ==resValue "string", "app_name", "de_APP"== 第一個參數是型態,第二個是label名稱,第三個是你想要他呈現的名字 ``` buildTypes { debug { signingConfig signingConfigs.debug applicationIdSuffix ".de" resValue "string", "app_name", "de_APP" } release { signingConfig signingConfigs.release applicationIdSuffix "" resValue "string", "app_name", "APP" } } ``` 接著到 android\app\src\main\AndroidManifest.xml裡面的 把label改成 ==@string/app_name== ``` <application android:label="@string/app_name" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> ``` 這樣去build apk兩個就會是不同名字且共存。