react-native fastlane自動化構建分發應用管理工具for iOS and Android


關於RN的相關資料,教你一步步開發react-native企業級應用 

 

ReactNative開發企業級電商APP應用,環境搭建->開發工具->重點知識庫->基礎框架構建->iOS/Android上線

 

應用完成開發后,講講自動化構建fastlane,react-native fastlane自動化構建分發應用管理工具for iOS and Android 

img/fastlane_text.png

簡單來說fastlane能為我們做些什么?

1、通過命令行 fastlane ios appstore / fastlane android google 即可將應用打包上傳至App Store或Google應用市場

2、如果還處於內測階段,fastlane ios debug / fastlane android debug 可以將應用打包上傳至蒲公英pgyer上測試


----- -------------------- -------------------- -------------------- ---------------

先來看看我們的RN應用目錄


一切開始的第一步:

brew cask install fastlane

進入RN項目根目錄,執行 fastlane init (fastlane for react-native官方文檔)

然后會生成fastlane目錄和Gemfile文件,cd fastlane 編輯對於Fastlane文件來自定義打包命令

 

文件說明:

Appfile:設置開發者賬號信息,如 Developer Portal Team ID 或 ITunes Connent Team ID等
app_identifier("com.wood.appname") # The bundle identifier of your app
apple_id("youremail@gmail.com") # Your Apple email address

#itc_team_id("133456152") # iTunes Connect Team ID
team_id("UD5YXYYV23") # Developer Portal Team ID
# Google Play Console APP管理權限生成的私鑰,這樣可以直接將 apk 傳送到PlayStore中
json_key_file "./android/keystores/key.json"

-------------------iOS前置工作--------------------------------------------------------------------------------------
iOS上架應用需要注冊成為蘋果開發者賬號(https://developer.apple.com/),個人:$99 內測adhoc會有100台設備限制  企業:$299 

先要設置一個APPID(Bundle identifier)com.company.rndemo -和一個git私有倉庫剩下的證書、簽名都由 match 搞定


在iTunes connect 中填寫APP相關信息,默認會創建一個1.0版本和本地的版本號對應


 

Git私有倉庫

fastlane match

這個倉庫包含你所有的 certificates(開發者證書) and provisioning profiles (PP證書),倉庫是通過OpenSSL來訪問的http協議不行

Important: 敲黑板划重點私有倉庫

Installation

Make sure you have the latest version of the Xcode command line tools installed:

xcode-select --install

Install fastlane using

[sudo] gem install fastlane -NV

or alternatively using brew cask install fastlane

Usage

Navigate to your project folder and run

fastlane match appstore
fastlane match adhoc
fastlane match development
fastlane match enterprise

For more information open fastlane match git repo


強行植入廣告(以前不知道打包上線怎么搞自己做APP當demo使用,fastlane和源碼都在GitHub)

 

-------------------Android前置工作----------------------------------------------------------------------------------

1、打開 Google Play Console

2、設置 - API權限(彈出服務條款-選擇全部接受)


3、點擊 創建新項目-創建服務賬號-join

 



4、點擊完成,記得保存json文件待會要用到,返回到上一頁面Google Play Console繼續

設置應用權限,添加用戶並關閉對話框,Google Play Console設置完畢。


 

Configure supply 

編輯 fastlane/Appfile 並設置 json_key_file 指定到你剛才下載的json私鑰文件(我把私鑰放到了項目中android/keysotres目錄中):

json_key_file "./android/keystores/key.json" 

Fetch your app metadata 

fastlane supply init //運行命令從Google play console中獲取App metadata

下載下來的目錄結構 fastlane/metadata/android.

由於受限 Google Play API, supply 不能下載APP詳情中的截圖和視頻(如果下載失敗記得科學上網、VPN)

---------------------------------------------------------------------------------------------------------------------- 

Fastlane:定義打包動作,例如 lane appstore ,就可以在命令行執行fastlane appstore來做某些事情

# fastlane配置文件,定義fastlane可執行的動作

default_platform(:ios)

def build_sign_app(mode="release")
# register_devices應用內測階段,注冊蘋果設備ID,可以掃碼下載
register_devices(
devices_file: "./fastlane/devices.txt",
team_id: "UD5YXYYV23",
username: "youremail@gmail.com",
platform: "ios"
)
configuration = "Release"
dirPrefix = "Release_"
if mode == "debug"
configuration = "Debug"
dirPrefix = "Beta_"
end
# match應用簽名,自動生成證書並上傳至私有git倉庫,保證安全
match(type: "adhoc", force_for_new_devices: true)
build_app(
export_method: "ad-hoc",
project: "./ios/yourapp.xcodeproj",
configuration: configuration,
scheme: "yourapp",
clean: true,
output_directory: "./build/output/#{dirPrefix}#{Time.now.strftime('%Y%m%d%H%M%S')}",
output_name: "rn-yourapp.ipa"
)
end

def upload_to_pgyer(desc="", mode="release")
description = "正式環境"
if mode == "debug"
description = "測試環境"
end
# pgyer上傳至pgyer進行內測
pgyer(
api_key: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
user_key: "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
update_description: "#{desc}\n#{description}"
)
end

def sign_appstore
increment_build_number(xcodeproj: './ios/yourapp.xcodeproj')
match(type: "appstore", readonly: true)
end

# 所有lane動作開始前都會執行這里的命令,例如指定打master上的包或執行project clean
before_all do |options|
#ensure_git_branch
#ensure_git_status_clean
#git_pull
end

############################################### iOS #############################################
platform :ios do
desc "構建一個測試環境版本上傳至pgyer"
lane :debug do|option|
build_sign_app(mode: "debug")
upload_to_pgyer(desc: option[:desc], mode: "debug")
end

desc "構建一個正式環境版本上傳至pgyer"
lane :release do|option|
build_sign_app
upload_to_pgyer(desc: option[:desc])
end

desc "構建一個正式環境版本上傳至AppStore"
lane :appstore do|option|
sign_appstore
build_app(
export_method: "app-store",
project: "./ios/yourapp.xcodeproj",
scheme: "yourapp",
clean: true,
output_directory: "./build/output/AppStore_#{Time.now.strftime('%Y%m%d%H%M%S')}",
output_name: "rn-yourapp.ipa"
)
upload_to_app_store(app_identifier: "com.ddt.yourapp")
commit_version_bump(message: 'Bump build', xcodeproj: './ios/name.xcodeproj')
push_to_git_remote
end

end




############################################### Android ##########################################
platform :android do

desc "構建一個測試環境版本上傳至pgyer"
lane :debug do|option|
gradle(task: 'clean', project_dir: 'android/')
gradle(task: 'assemble', build_type: 'Debug', project_dir: 'android/')
upload_to_pgyer(mode: "debug")
end

desc "構建一個正式環境版本上傳至pgyer"
lane :release do|option|
gradle(task: 'clean', project_dir: 'android/')
gradle(task: 'assemble', build_type: 'Release', project_dir: 'android/')
upload_to_pgyer(desc: option[:desc])
end

desc "構建一個正式環境版本上傳至AppStore"
lane :playstore do|option|
    # clean清理工程
gradle(task: 'clean', project_dir: 'android/')
# 從Google Play中獲取應用versioncode
google_play_track_version_codes(package_name: 'com.ddt.superbuy')
# 編譯打包
gradle(task: 'assemble', build_type: 'Release', project_dir: 'android/')
# 上傳Android APP Bundle到Google Play
upload_to_play_store(package_name: 'com.wood.rndemo')
# 因為修改了versioncode,自動提交代碼並push
git_commit(path: ['./android/gradle.properties'], message: 'Bump versionCode')
push_to_git_remote
end

end

 

Deliverfile:它屬於fastlane工具集中的一個模塊,用於配置上傳App Store功能(本教程暫未用到)

Matchfile:證書管理,配置應用信息和git倉庫地址,會自動生成簽名證書來給應用簽名,隨后將證書上傳至git私有倉庫中保證安全。私有倉庫這個要特別注意,保證你在命令行git clone your.git 可以download你的證書倉庫,需要配置你的git.email/username --global,因為這個命令不會提示你輸入密碼、要設置username什么的,這些前置工作不到位直接就掛了,如果你有多倉庫的話,參考:

 

 

 

git_url("git@github.com:xxx/react-native-fastlane.git")

type("development") # The default type, can be: appstore, adhoc, enterprise or development
type("adhoc")
type("appstore")

app_identifier(["com.ddt.yourapp"])
username("youremail@gmail.com") # Your Apple Developer Portal username

Pluginfile:fastlane插件,社區提供了數不清的插件供我們使用

依次添加安裝這些插件后會自動生成此文件

 

fastlane add_plugin versioning 
# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!

gem 'fastlane-plugin-versioning'
gem 'fastlane-plugin-appicon'
gem 'fastlane-plugin-changelog'
gem 'fastlane-plugin-pgyer' 

devices.txt:iOS應用內側時掃碼下載,在內的設備id才可以下載最多設置100台設備,蘋果企業級賬號沒有這個限制,如何獲取設備ID和相關信息

Device ID   Device Name
a5e2118eebdad4c85011ade6919c22bbcxxxxxxx 我的iPhone X 按照這個格式添加設備 

 

執行命令

fastlane ios debug/release/appstore

fastlane android debug/release/playstore

 

 

------------------------------------

注意事項:

1、iOS上傳APP失敗,多次重試后upload_to_appstore上傳失敗?

這是網絡問題,不要中斷腳本,保證沒有開啟代理

2、iOS開發者賬號開啟兩部驗證?


請打開控制台中指定鏈接,設置專用密碼


3、Android上傳playstore失敗,錯誤日志中有connection error?

網絡原因,國內需要科學上網,你懂得

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM