Flutter 混合開發實戰問題記錄(五)1.9.1-hotfix 打包aar差異

孑孓發表於2020-02-24

目前flutter穩定版已經升級到了1.12.*,跟以前相比變化異較大,具體差異網上的帖子已經很多了,我也抽時間做些適配,計劃等原生專案遷移到androidx後再升級,畢竟專案大了一舉一動都要謹慎。

過年的時候做了個過渡,由用了很久的1.5.4版本小升到了1.9.1,簡單羅列下細節。

一、dart端API小變化

我使用的程式碼變化較小,只有一處:

舊的方式:

final ImageStream stream = provider.resolve(config);
stream.addListener(listener, onError: errorListener);
completer.future.then((List<ImageInfo> _) { stream.removeListener(listener); });
複製程式碼

新的方式

final ImageStream stream = provider.resolve(config);
ImageStreamListener imageStreamListener = ImageStreamListener(listener ,onError: errorListener);
stream.addListener(imageStreamListener);
completer.future.then((List<ImageInfo> _) { stream.removeListener(imageStreamListener); });
複製程式碼

其實就是引數個數的變化。


二、編譯中間產物的變化

這個變化是從1.7.8開始的,檢視我之前的帖子,四個中間產物不再獨立產生,現在統一被打包進入了so檔案,對於需要干預打包流程的朋友來說方便了些。


三、打包aar方式的變化

假如你需要構造打包aar同步到自己的maven私服上,針對之前的打包過程(例如基於1.5.4)有點小變化。

宿主專案三方依賴過多,你想精簡包大小,需要相容Cpu架構,只配置

ndk {
    abiFilters 'armeabi'
    }
複製程式碼

不過flutter只支援armeabi-v7a、arm64-v8a、x86和x86-64幾種模式,導致你的命令

flutter build aot --target lib/main.dart --target-platform android-arm
複製程式碼

的結果跟預期不符,你的jni內沒有armeabi資料夾,這樣。你需要兩步:

1 移動armeabiV7到armeabi
cd $FLUTTER_ROOT/bin/cache/artifacts/engine
for arch in android-arm android-arm-profile android-arm-release; do
  pushd $arch
  cp flutter.jar flutter-armeabi-v7a.jar # 備份
  unzip flutter.jar lib/armeabi-v7a/libflutter.so
  mv lib/armeabi-v7a lib/armeabi
  zip -d flutter.jar lib/armeabi-v7a/libflutter.so
  zip flutter.jar lib/armeabi/libflutter.so
  popd
done
複製程式碼

這樣之後在1.5.4時代就可以直接打包了,不過如果是1.9.1還需要

2 設定flutter_gradle指令碼

將 flutter/packages/flutter_tools/gradle/flutter.gradle的第52行的

private static final String ARCH_ARM32      = "armeabi-v7a";
複製程式碼

改為

private static final String ARCH_ARM32      = "armeabi";
複製程式碼

這樣就可以了。


四 其他

我升級了自己的電腦系統到mac 10.15,執行打包後出現了報錯

ProcessException: ProcessException: Bad CPU type in executable
  Command: /fluttersdk/flutter/bin/cache/artifacts/engine/android-arm-release/darwin-x64/gen_snapshot --causal_async_stacks --deterministic --print-snapshot-sizes --snapshot_kind=app-aot-blobs --vm_snapshot_data=build/app/intermediates/flutter/release/vm_snapshot_data --isolate_snapshot_data=build/app/intermediates/flutter/release/isolate_snapshot_data --vm_snapshot_instructions=build/app/intermediates/flutter/release/vm_snapshot_instr --isolate_snapshot_instructions=build/app/intermediates/flutter/release/isolate_snapshot_instr --no-sim-use-hardfp --no-use-integer-division build/app/intermediates/flutter/release/app.dill
複製程式碼

查詢issue(@alexayub using flutter on macOS Catalina is supported on v1.9.1 (the latest stable release) and later. More details here github.com/flutter/flu… 得知,flutter舊版還未支援新版本mac系統,官方直接要求你升級,我也未找到什麼相容解決方案。暫時不想升級的朋友們不要手欠。

[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15.2 19C57, locale zh-Hans-CN)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS (Xcode 11.3)
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods. For more info, see https://github.com/flutter/flutter/issues/14293.
      To re-install CocoaPods, run:
        sudo gem install cocoapods
[✓] Android Studio (version 3.5)
[!] IntelliJ IDEA Ultimate Edition (version 2019.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] Connected device
    ! No devices available

! Doctor found issues in 3 categories.

複製程式碼

相關文章