🦔

【Flutter】shared_preferencesのデータを消去するシェルスクリプト[Emulator, Simulator用]

2023/01/19に公開

はじめに

Flutter で開発中、shared_preferences のデータを消去したい場合に利用するシェルスクリプトを作成しました。下記 ①② 用です。

Android Emulator
iOS Simulator

シェルスクリプト

shared_preferences_data_clear.sh
# データ消去対象のアプリを指定
bundleId="ここにiOSアプリの「Bundle Identifier」を指定"

echo "
# --------------------------------------------------
#
# shared preferences data clear
# Android Emulator
#
# --------------------------------------------------
"

# データ消去対象アプリの存在を確認
packageName=$(echo 'pm list package' | adb shell | grep $bundleId)
appName=$(echo $packageName | sed -e 's/package://g')
echo "data clear target appName : $appName"

# 対象アプリのデータ消去
echo "pm clear $appName" | adb shell

echo "
# --------------------------------------------------
#
# shared preferences data clear
# iOS Simulator
#
# --------------------------------------------------
"

# データ消去対象デバイスのリスト作成
# xcrun simctl list | grep Booted | sed -e 's/.*generation) (//g' | sed -e 's/).*//g'
deviceList=$(flutter devices | grep ios | awk -F'•' '{print $2}' | sed -e 's/ //g')
echo "clear target device list"
echo "$deviceList"

# もしデバイスのリストが0件の場合、終了
if [ -z "$deviceList" ]; then echo "    no device.\n    done.\n"; exit; fi

# 各デバイス毎に、アプリをアンインストール
echo "$deviceList" | while read targetDevice; do
    echo "      --------------------------------------------------"
    echo "      targetDevice: $targetDevice"

    echo "      uninstall app before"
    xcrun simctl listapps $targetDevice | grep $bundleId

    echo "      uninstall app"
    xcrun simctl uninstall $targetDevice $bundleId

    echo "      uninstall app after"
    xcrun simctl listapps $targetDevice | grep $bundleId
done

Discussion