To those developing Flutter on Android:
When you build a Flutter environment, when you create your first Flutter project in a hurry, when you excitedly write "Flutter run", the long wait begins... ⣟Copy the code
Of course, you can optimize the Flutter by following the official documentation: Using Flutter in China.
However, you will still encounter network issues when running the Flutter project on an Android device.
This is because the Android build process still requires Internet access to some of the dependent libraries.
The optimization method is as follows:
Manually optimize android/build.gradle files:
Modify before:
repositories {
google()
jcenter()
}
Copy the code
Revised:
repositories {
maven { url 'https://maven.aliyun.com/repository/google'} // google()
maven { url 'https://maven.aliyun.com/repository/jcenter'} // jcenter()
}
Copy the code
The build.gradle file contains two repositories that need to be modified:
The first one is in BuildScript,
The second one is in AllProjects.
If you are maintaining a project with many components, build.gradle in each component needs this modification. This can be very cumbersome to change manually, but you can also use a script.
Automatic optimization with scripts:
You can place the script optimize_cn_network.py in the bin/ directory of your flutter project root directory.
For an example of script optimization, type bin/optimize_cn_network.py android/.
$ bin/optimize_cn_network.py android/
Optimizing cn network: /Volumes/user/cjf/w/ovo/flutterame/android/build.gradle
No line to optimize in file: /Volumes/user/cjf/w/ovo/flutterame/android/build.gradle
Copy the code
Suppose your component “gallery” directory looks like this
├ ─ ─ the README. Md ├ ─ ─ android ├ ─ ─ flutterame. On iml ├ ─ ─ ios ├ ─ ─ lib ├ ─ ─ modules │ └ ─ ─ gallery │ ├ ─ ─ the android │ └ ─ ─... ├── Heavy Exercises ── Heavy Exercises ── Heavy exercises.Copy the code
In this case, run the bin/optimize_cn_network.py modules/gallery/.android command.
$ bin/optimize_cn_network.py modules/gallery/.android
Optimizing cn network: /Volumes/user/cjf/w/ovo/flutterame/modules/gallery/.android/build.gradle
Copy the code
Bin /optimize_cn_network.py Works
The script reads the build.gradle file at the specified location.
Then search Google () and jCenter () between “repositories {” and “}” and replace them with
maven { url 'https://maven.aliyun.com/repository/google'} // google()
maven { url 'https://maven.aliyun.com/repository/jcenter'} // jcenter()
Copy the code
Bin /optimize_cn_network.py contains the following contents:
#! /usr/bin/env python3
import os
import sys
import re
from time import strftime, localtime
from ezutils.files import readlines, writelines
dir = os.path.abspath(os.path.dirname(__file__))
parent_dir = os.path.dirname(dir)
def replace_lines(target_reg, *, by_lines, begin_reg, end_reg, in_lines) :
re_begin = re.compile(begin_reg)
re_target = re.compile(target_reg)
re_end = re.compile(end_reg)
indexs = []
state = 'find_begin'
index = -1
for line in in_lines:
index += 1
if state == 'find_begin' and re_begin.match(line):
state = 'find_target'
continue
if state == 'find_target' and re_target.match(line):
indexs.insert(0, index)
state == 'find_end'
continue
if state == 'find_end' and re_end.match(line):
state == 'find_begin'
continue
if len(indexs) == 0:
return None
new_lines = in_lines
for index in indexs:
new_lines = new_lines[:index]+by_lines+new_lines[index+1:]
return new_lines
def optimize_cn_network(target_file) :
print(f'Optimizing cn network: {target_file}')
lines = readlines(target_file)
has_google = True
new_lines = replace_lines(
r'\s*google().*',
by_lines=[
" maven { url 'https://maven.aliyun.com/repository/google'} // google()"],
begin_reg=r'\s*repositories\s*{.*',
end_reg=r'\s*}.*',
in_lines=lines)
if(new_lines == None):
has_google = False
new_lines = lines
new_lines = replace_lines(
r'\s*jcenter().*',
by_lines=[
" maven { url 'https://maven.aliyun.com/repository/jcenter'} // jcenter()"],
begin_reg=r'\s*repositories\s*{.*',
end_reg=r'\s*}.*',
in_lines=new_lines)
if new_lines == None and not has_google:
print(f"No line to optimize in file: {target_file}")
return
writelines(new_lines, f'{target_file}.tmp')
current_time = strftime("%Y-%m-%d_%H:%M:%S", localtime())
os.rename(target_file, f'{target_file}.backup_{current_time}')
os.rename(f'{target_file}.tmp', target_file)
if __name__ == "__main__":
if len(sys.argv) == 1:
print(
"Input android project dir(example: 'android/' or 'modules/gallery/.android' ):")
target_dir = input(a)else:
target_dir = sys.argv[1]
target_file = os.path.join(parent_dir, target_dir, 'build.gradle')
optimize_cn_network(target_file)
Copy the code