أخطاء APK

CLEARTEXT communication not permitted

نص الخطأ

java.net.UnknownServiceException: CLEARTEXT communication to api.example.com not permitted by network security policy

السبب

من Android 9 فما فوق تُحظر طلبات http:// افتراضيًا. يحاول التطبيق الاتصال بعنوان غير مشفّر.

الحل خطوة بخطوة

  1. أفضل حل: شغّل الخادم عبر https:// وغيّر العنوان.
  2. للاختبار المحلي فقط أنشئ network_security_config.xml واسمح بنطاق محدد (10.0.2.2 أو localhost).
  3. أشر إلى الملف في <application android:networkSecurityConfig=...>.
  4. لا تكتب usesCleartextTraffic="true" لكل الحركة؛ أبقِه في بناء debug فقط.
  5. اختبر APK من نوع release على هاتف حقيقي: قد يتحول خطأ الشبكة إلى شاشة بيضاء.

أوامر للمثال

<!-- res/xml/network_security_config.xml -->
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">10.0.2.2</domain>
  </domain-config>
</network-security-config>
<application android:networkSecurityConfig="@xml/network_security_config" ...>

كيف لا يتكرر

استخدم HTTPS فقط في release وأبقِ إذن http في مجلد debug.

المؤلف والترخيص: Nibras Code