相關鏈接,如有需要請移步:
Flutter-已有iOS工程中加入Flutter之Cocoapods+Flutter環(huán)境方式集成
Flutter-已有iOS工程中加入Flutter之遠程Pod產物方式集成
Flutter-遠程Pod集成Flutter腳本優(yōu)化
最近块茁,我研究了下不依賴flutter開發(fā)環(huán)境的方式集成剥险,推送到遠程pod倉庫,以Framework形式在cocoapods中集成狰闪。
開始
1. 創(chuàng)建flutter_module:flutter create -t module flutter_module
KKdeMacBook-Pro:remotepod kaye$ flutter create -t module flutter_module
Creating project flutter_module... androidx: true
flutter_module/test/widget_test.dart (created)
flutter_module/flutter_module.iml (created)
flutter_module/.gitignore (created)
flutter_module/.metadata (created)
flutter_module/pubspec.yaml (created)
flutter_module/README.md (created)
flutter_module/lib/main.dart (created)
flutter_module/flutter_module_android.iml (created)
flutter_module/.idea/libraries/Flutter_for_Android.xml (created)
flutter_module/.idea/libraries/Dart_SDK.xml (created)
flutter_module/.idea/modules.xml (created)
flutter_module/.idea/workspace.xml (created)
Running "flutter pub get" in flutter_module... 1.7s
Wrote 12 files.
All done!
Your module code is in flutter_module/lib/main.dart.
添加依賴庫梢薪,比如:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
shared_preferences: ^0.5.6
運行flutter pub get
安裝依賴庫
KKdeMacBook-Pro:flutter_module kaye$ flutter pub get
Running "flutter pub get" in flutter_module... 0.4s
KKdeMacBook-Pro:flutter_module kaye$
build獲取產物
KKdeMacBook-Pro:flutter_module kaye$ flutter build ios --debug --no-codesign
Warning: Building for device with codesigning disabled. You will have to manually codesign
before deploying to device.
Building com.example.flutterModule for device (ios)...
Running Xcode build...
├─Assembling Flutter resources... 6.8s
└─Compiling, linking and signing... 4.5s
Xcode build done. 17.6s
Built /Users/kaye/Desktop/remotepod/flutter_module/build/ios/iphoneos/Runner.app.
產物路徑:
我們先將各個產品,手動copy出來尝哆,放到一個文件夾ios_frameworks
中備用秉撇。
note:這里我們先不用腳本,手動獲取秋泄,驗證一下pod能不能成功
2. 創(chuàng)建Pod:pod lib create KKFlutterPod
KKdeMacBook-Pro:remotepod kaye$ pod lib create KKFlutterPod
Cloning `https://github.com/CocoaPods/pod-template.git` into `KKFlutterPod`.
Configuring KKFlutterPod template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and double click links to open in a browser. )
What platform do you want to use?? [ iOS / macOS ]
> ios
What language do you want to use?? [ Swift / ObjC ]
> objc
Would you like to include a demo application with your library? [ Yes / No ]
> no
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> none
Would you like to do view based testing? [ Yes / No ]
> no
What is your class prefix?
> KK
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `KKFlutterPod` from `../`
Downloading dependencies
Installing KKFlutterPod (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `KKFlutterPod.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Automatically assigning platform `ios` with version `9.3` on target `KKFlutterPod_Tests` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'KKFlutterPod/Example/KKFlutterPod.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
KKdeMacBook-Pro:remotepod kaye$
將上一步的ios_frameworks
文件夾移動到Pod倉庫中琐馆,目錄結構如下:
3. 創(chuàng)建遠程Pod對應的Git倉庫
3.1 創(chuàng)建倉庫
這里自己測試使用的是GitHub,創(chuàng)建過程就不描述了恒序。
3.2 關聯倉庫
tip:或者你也可以先把遠程的Pod倉庫創(chuàng)建好瘦麸,clone下來,然后把創(chuàng)建的KKFlutterPod里面除了.git之外的所有文件拷貝到你clone的git倉庫中歧胁,從而省去關聯的關系
KKdeMacBook-Pro:flutter_module kaye$ cd ../KKFlutterPod/
KKdeMacBook-Pro:KKFlutterPod kaye$ git remote add origin https://github.com/yuyangkk/flutterpod.git
修改KKFlutterPod.podsepc
文件,在其末尾添加如下:
s.static_framework = true
p = Dir::open("ios_frameworks")
arr = Array.new
arr.push('ios_frameworks/*.framework')
s.ios.vendored_frameworks = arr
3.3 提交Pod到遠程
git add .
git commit -m "初始化"
git pull origin master
git push -u origin master
git tag -m "v0.1.0" -a 0.1.0
git push --tags
3.4 使用
pod 'KKFlutterPod', :git => 'https://github.com/yuyangkk/KKFlutterPod.git'
4. 腳本化
每次我們修改了flutter代碼滋饲,都要重新打包出來Framework厉碟,然后打上tag推送到遠程,這個過程雖然簡單屠缭,但是重復性很強箍鼓,所以考慮寫成腳本實現,以免有漏掉某些步驟呵曹!
ps:腳本有點不太規(guī)范款咖,是一邊看語法,一遍寫的奄喂,所以铐殃,有問題還請指出來
4.1. 編譯Framework并導出到指定位置
#cd ~/Desktop/remotepod/flutter_module
#獲取當前腳本目錄
projectURL=$(cd $(dirname $0); pwd)
#echo $projectURL
cd ${projectURL}
#out是指將編譯號的文件copy到指定的位置的路徑
if [ -z $out ]; then
out='../KKFlutterPod/KKFlutterPod/ios_frameworks'
fi
echo "準備輸出所有文件到目錄: $out"
echo "清除所有已編譯文件"
#find . -d -name build | xargs rm -rf
flutter clean
rm -rf $out
rm -rf build
flutter packages get
echo "編譯flutter"
flutter build ios --debug --no-codesign
#如果是release包,將debug改為release
#flutter build ios --release --no-codesign
echo "編譯flutter完成"
mkdir $out
cp -r build/ios/Debug-iphoneos/*/*.framework $out
cp -r .ios/Flutter/App.framework $out
cp -r .ios/Flutter/engine/Flutter.framework $out
#本地Pod庫中提交代碼跨新,并修改version富腊,打上標簽,標簽和version保持一致域帐,自增
#上傳到pod蟹肘,請執(zhí)行腳本push_pod_spec.sh
4.2. 將導出的Framework上傳到遠程pod倉庫
projectURL=$(cd $(dirname $0); pwd)
#echo $projectURL
cd ${projectURL}
cd ../KKFlutterPod
echo "準備更新本地倉庫"
git pull
if [ $? -eq 0 ]; then
echo "拉取遠程代碼成功"
else
echo "拉取遠程代碼失敗"
exit 1
fi
cd ${projectURL}
#修改KKFlutterPod.podspec中s.version = '0.1.0'
# 讀取KKFlutterPod.podspec的第11行
# a=`cat KKFlutterPod.podspec |head -ne12`
# echo $a
a=`sed -n 11p KKFlutterPod.podspec `
# echo $a #s.version = '0.1.4'
# echo "刪除最右邊.開始向及其左邊的數據"
vs=${a##*.}
# echo $vs
oldNum=${vs%%\'*}
# echo '當前版本:'$oldNum
newNum=$((oldNum+1))
# echo '新版本:'$newNum
# echo "替換版本號"
v=0.1.$newNum
version=" s.version = '$v'"
echo $version
sed -i '' "s/$a/$version/" KKFlutterPod.podspec
# echo "add 并且commit"
# git status
git rm --cached
git add .
git commit -m "修改版本$v"
# echo "添加tag"
git tag -a $v -m "$v"
# echo "連同tag推送到遠程"
git push
git push --tags
自此,差不多應該可以跑起來俯树,run一下試試帘腹,如果遇見build錯誤,請查看一下Xcode的bitcode是否打開了许饿,如果打開請關閉阳欲。
5. 問題
5.1 undefined symbol:OBJC_CLASS$_GeneratedPluginRegistrant
這個問題,是因為導出的包是不支持模擬器的陋率,因為通過flutter開發(fā)環(huán)境導出的包球化,是可以支持模擬器的,所以猜測是我寫的腳本有問題瓦糟,等待后續(xù)優(yōu)化下筒愚,目前是只支持真機。
5.2 有關Framework包的大小
通過flutter build ios-framework --output=導出包路徑
菩浙,獲取到debug巢掺、profile、release三種情況下的包劲蜻,其中包含的Flutter.framework的包大小分別是95MB陆淀、66.8MB、369.8MB先嬉,如果用release的包轧苫,推送到遠程pod庫的時候,會超過git的100MB限制疫蔓,可能會失敗,所以我們暫時只用了debug模式下的包,推送到遠程。
當然也有可能是我使用的命令有缺陷,如果有什么可以解決release環(huán)境下叫编,包過大的問題,請務必留言給我,感謝~~~
錯誤待續(xù)。樱哼。哀九。