79 lines
3.8 KiB
Python
79 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""안드로이드 설정 한 번에 맞추기.
|
|
|
|
사용: flutter create --platforms=android --org com.friendlymap --project-name friendly_map .
|
|
python3 tool/android_patch.py
|
|
권한(인터넷, 카메라, 같은 네트워크 발견), 평문 통신(같은 네트워크 직접 연결), minSdk 23 이상을 맞춘다.
|
|
여러 번 실행해도 같은 결과가 나온다.
|
|
"""
|
|
import pathlib, re, sys
|
|
|
|
root = pathlib.Path(__file__).resolve().parent.parent / "android" / "app"
|
|
manifest = root / "src" / "main" / "AndroidManifest.xml"
|
|
MIN_SDK = 23
|
|
PERMS = [
|
|
"android.permission.INTERNET",
|
|
"android.permission.ACCESS_NETWORK_STATE",
|
|
"android.permission.ACCESS_WIFI_STATE",
|
|
"android.permission.CHANGE_WIFI_MULTICAST_STATE",
|
|
"android.permission.CAMERA",
|
|
"android.permission.ACCESS_FINE_LOCATION",
|
|
"android.permission.ACCESS_COARSE_LOCATION",
|
|
"android.permission.READ_CALL_LOG",
|
|
"android.permission.POST_NOTIFICATIONS",
|
|
"android.permission.RECEIVE_BOOT_COMPLETED",
|
|
"android.permission.VIBRATE",
|
|
]
|
|
RECEIVERS = """ <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver"/>
|
|
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
|
|
<intent-filter>
|
|
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
|
</intent-filter>
|
|
</receiver>
|
|
"""
|
|
|
|
|
|
def patch_manifest(text: str) -> str:
|
|
add = "".join(f' <uses-permission android:name="{p}"/>\n' for p in PERMS if p not in text)
|
|
if add:
|
|
text = re.sub(r"(<manifest[^>]*>\s*\n)", lambda m: m.group(1) + add, text, count=1)
|
|
if "usesCleartextTraffic" not in text:
|
|
text = text.replace("<application", '<application\n android:usesCleartextTraffic="true"', 1)
|
|
if "ScheduledNotificationReceiver" not in text:
|
|
text = text.replace("</application>", RECEIVERS + " </application>", 1)
|
|
return text
|
|
|
|
|
|
def patch_gradle(text: str) -> str:
|
|
# minSdk 는 23 이상이면 된다. flutter.minSdkVersion(현재 Flutter 는 24)은 그대로 둔다.
|
|
# Flutter 는 빌드할 때 자기 최소값보다 낮은 숫자를 flutter.minSdkVersion 으로 되돌리므로 낮추지 않는다.
|
|
text = re.sub(r"(minSdk\s*=\s*|minSdkVersion\s+)(\d+)",
|
|
lambda m: m.group(1) + str(max(int(m.group(2)), MIN_SDK)), text)
|
|
# 예약 알림 플러그인이 요구하는 자바 새 기능 지원(desugaring)
|
|
if "isCoreLibraryDesugaringEnabled" not in text and "coreLibraryDesugaringEnabled" not in text:
|
|
if "compileOptions {" in text:
|
|
flag = "isCoreLibraryDesugaringEnabled = true" if "kotlin" in text or "=" in text.split("compileOptions {")[1][:80] else "coreLibraryDesugaringEnabled true"
|
|
text = text.replace("compileOptions {", "compileOptions {\n " + flag, 1)
|
|
if "coreLibraryDesugaring(" not in text and "coreLibraryDesugaring '" not in text:
|
|
kts = "isCoreLibraryDesugaringEnabled" in text
|
|
dep = 'coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4")' if kts else "coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4'"
|
|
text += "\ndependencies {\n " + dep + "\n}\n"
|
|
return text
|
|
|
|
|
|
def main() -> int:
|
|
if not manifest.exists():
|
|
print("android/ 가 비어 있어요. 먼저 flutter create --platforms=android ... . 를 실행해 주세요.")
|
|
return 1
|
|
manifest.write_text(patch_manifest(manifest.read_text(encoding="utf-8")), encoding="utf-8")
|
|
for name in ("build.gradle.kts", "build.gradle"):
|
|
g = root / name
|
|
if g.exists():
|
|
g.write_text(patch_gradle(g.read_text(encoding="utf-8")), encoding="utf-8")
|
|
print("안드로이드 설정을 맞췄어요.")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|