阅读视图

发现新文章,点击刷新页面。
🔲 ⭐

Android 模拟器实现 hosts 修改

有时候我们需要使用 Android 模拟器来 绑定一下 hosts 来实现功能的开发与验证,刚好最近遇到了这样的需求,处理完成,简单记录一下。

替换m1 实现(针对 苹果 M1 芯片才需要处理)

下载这个文件 https://github.com/google/android-emulator-m1-preview/releases/download/0.2/emulator-darwin-aarch64-0.2-engine-only.zip

解压,然后将 emulatoremulator-check 替换掉这里面的文件 ~/Library/Android/sdk/tools/ (原有的可以备份为 xxx_backup)

查看 avd_id

1
2
3
4
5
6
7
~/Library/Android/sdk/tools/emulator -list-avds
Pixel6ProAPI33
Pixel_3a_API_33_arm64-v8a
Pixel_6_API_22
Pixel_6_API_28
Pixel_6_Pro_API_23
Pixel_6_Pro_API_30_X86

启动 avd,可写入状态

1
~/Library/Android/sdk/tools/emulator -avd Pixel_3a_API_33_arm64-v8a  -writable-system

新起终端tab 执行

  1. adb root
  2. adb remount
  3. adb push your_hosts_on_mac /etc/hosts

验证ping

假设上面的 hosts 我们新增了 127.0.0.1 baidu.com

1
2
3
4
5
6
adb shell

ping baidu.com
PING baidu.com (127.0.0.1) 56(84) bytes of data.
64 bytes from baidu.com (127.0.0.1): icmp_seq=1 ttl=64 time=1.55 ms
64 bytes from baidu.com (127.0.0.1): icmp_seq=2 ttl=64 time=0.180 ms

注意: hosts 修改建议在 mac 上进行处理,然后使用adb push your_hosts_on_mac /etc/hosts 替换手机内的hosts。手机内置的 vi 很弱,可能无法编辑。

以上。



🔲 ⭐

Flutter 3 迁移后编译 warnings 一键修复

当我们的 app 支持 flutter 3 后,无论是编译速度,还是运行效率,方方面面会有很大的提升。但是在我们编译的时候,会有类似下面的这些警告。

1
2
3
../../../your_pub/lib/src/framework.dart:275:26: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
[        ]  - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../code/flutter_3/packages/flutter/lib/src/scheduler/binding.dart').
[        ]     if (SchedulerBinding.instance!.schedulerPhase ==

上面的警告虽然不会影响应用的编译,但是长久来看,还是需要解决的。

原因为何

原因是从 flutter 3 开始, SchedulerBinding.instance返回的是一个 非 null 实例,当我们使用SchedulerBinding.instance!.schedulerPhase 会得到这样的警告Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.

如何解决

解决起来很简单,按照下面的处理,将!去掉即可。

1
SchedulerBinding.instance.schedulerPhase

都有哪些场景

flutter3 开始,下面这些都会有编译警告问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SchedulerBinding.instance!.xxx
SchedulerBinding.instance?.xxx

WidgetsBinding.instance!.xxxx
WidgetsBinding.instance?.xxxx

PaintingBinding.instance?.xxx
PaintingBinding.instance!.xxx


RendererBinding.instance!.xxx
RendererBinding.instance?.xxxx

GestureBinding.instance!.xxx
GestureBinding.instance?.xxx

一键解决

那这么多内容需要解决,有没有一键处理的办法呢?

如果你接触过 终端脚本,答案是肯定的。我们可以使用下面的shell 脚本处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env bash

function sedReplaceFile() {
	echo $1
	sed -i "" -e "s/SchedulerBinding.instance!/SchedulerBinding.instance/g" $1
	sed -i "" -e "s/SchedulerBinding.instance?/SchedulerBinding.instance/g" $1
	sed -i "" -e "s/WidgetsBinding.instance!/WidgetsBinding.instance/g" $1
	sed -i "" -e "s/WidgetsBinding.instance?/WidgetsBinding.instance/g" $1
	sed -i "" -e "s/PaintingBinding.instance?/PaintingBinding.instance/g" $1
	sed -i "" -e "s/PaintingBinding.instance!/PaintingBinding.instance/g" $1
	sed -i "" -e "s/RendererBinding.instance!/RendererBinding.instance/g" $1
	sed -i "" -e "s/RendererBinding.instance?/RendererBinding.instance/g" $1
	sed -i "" -e "s/GestureBinding.instance!/GestureBinding.instance/g" $1
	sed -i "" -e "s/GestureBinding.instance?/GestureBinding.instance/g" $1
	
}

export -f sedReplaceFile
find . -name "*.dart"  | xargs -I {} bash -c 'sedReplaceFile {}'

执行

1
2
cd your_project
f3_fix.sh 

  • 上面的脚本仅在 mac 系统验证, Linux 可能需要自行做简易修改。
  • 如果是 三方pub 包含警告问题,可以选择对应适配 flutter 3 的版本升级即可。


🔲 ⭐

unpub 发布原子化处理

目前 unpub 作为我们重要的 pub 私有服务托管着 众多的 pubs。在日常的开发过程中,我们也会对pub 做出了一些约束。比如

  • 只允许在 master 或者 release/* 分支发布
  • 不符合上述条件的分支不允许发布。

今天我们讨论的问题重点,非上述的问题,而是发布 unpub 的原子性。

非原子化的两步

在讨论原子性之前,我们需要明确有两个步骤。

  • 我们需要执行 flutter packages pub publish --server=https://pub.aaa.com/ --verbose 进行发布
  • 在发布unpub 前后,我们需要将代码推送到远端 gitlab 服务器

非原子化的问题

那么如果我们忘记了,最后一步的推送工作,带来的问题可能会很严重

  • 某份代码 A 未被推送,后面的更改再发布 unpub 导致 代码 A 的功能 丢失
  • 后续发现丢失后,找回代码A 很可能无法 确定 当时的代码修改内容(可能包含其他修改,人的记忆里不总是可靠)
  • 找回后,往往需要投入一定的测试资源验证较为稳妥。

所以,将上面两部合成一步,来作为一个原子操作,显得尤为重要。

一个脚本原子化

这里有一个简单,却极为实用的方式,就是这个代码

1
2
3
#!/bin/bash
git push origin "$(git symbolic-ref -q HEAD 2>/dev/null | cut -d'/' -f 3)"
flutter packages pub publish --server=https://pub.aaa.com/ --verbose

下载到本地后,设置文件可执行(加入环境变量),然后当再次发布 unpub 时 这样处理即可。

1
unpubUpload.sh


☑️ ⭐

Linux/Mac 终端代理设置,取消与排除名单

在一个不友好的网络环境中,有些开发资源(依赖)无法被直接下载安装,这时候我们需要使用代理。

如果是经常使用终端的情况,终端关于代理的内容必不可少。

设置 http 代理

1
2
3
4
5
$ export http_proxy=http://server-ip:port/
$ export http_proxy=http://127.0.0.1:3128/
$ export http_proxy=http://proxy-server.mycorp.com:3128/
$ export http_proxy=socks5://PROXYHOST:PROXYPORT

设置 https 代理

1
2
3
4
$ export https_proxy=https://server-ip:port/
$ export https_proxy=https://127.0.0.1:3128/
$ export https_proxy=https://proxy-server.mycorp.com:3128/
$ export https_proxy=socks5://PROXYHOST:PROXYPORT

取消设置代理

1
2
3
unset http_proxy

unset https_proxy

设置代理排除名单

有时候,我们需要开启代理,但是有些域名不走代理,比如

  • 内部的网络,使用代理会出现错误
  • 国内网络,使用代理后会变慢
1
2

export NO_PROXY=droidyue.com,127.0.0.1

上述 设置代理,设置代理排除名单,也可以放到 .bashrc 或 .zshrc 自动持久化处理。



❌