Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Android 9 can not access bundler via cleartext request without additional config #22375

Closed
TikiTDO opened this issue Nov 21, 2018 · 16 comments
Labels
Component: Text Platform: Android Android applications. Platform: Linux Building on Linux. Resolution: Locked This issue was locked by the bot. Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used.

Comments

@TikiTDO
Copy link
Contributor

TikiTDO commented Nov 21, 2018

Environment

  React Native Environment Info:
    System:
      OS: Linux 4.18 Arch Linux undefined
      CPU: x64 AMD Ryzen 7 1700X Eight-Core Processor
      Memory: 682.77 MB / 7.79 GB
      Shell: 5.6.2 - /bin/zsh
    Binaries:
      Node: 10.7.0 - /usr/local/bin/node
      Yarn: 1.12.3 - /usr/local/bin/yarn
      npm: 6.4.1 - /usr/local/bin/npm
      Watchman: 4.7.0 - /usr/local/bin/watchman
    SDKs:
      Android SDK:
        Build Tools: 23.0.1, 23.0.3, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.2, 27.0.3, 28.0.1, 28.0.3
        API Levels: 23, 24, 26, 27, 28
    npmPackages:
      react-native: 0.57.x => 0.57.4 
    npmGlobalPackages:
      create-react-native-app: 1.0.0
      react-native-git-upgrade: 0.2.7

Description

As of Android 9 (API Level 28), the default behavior is to now block all clear text requests, unless a domain is specifically configured to allow this behavior. As a result the bundler URL is not reachable in development mode, with a generic error that offers no context to what the problem could be, or what solutions are available.

The issue is fairly trivial to resolve once the root cause is apparent, but getting to that root cause can take a bit of searching.

Reproducible Demo

Start a react-native app on an Android 9 devices (I only tested physical device), in development mode. The app will not start, and will complain that the bundle can not be fetched.

@react-native-bot react-native-bot added Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used. 🔶Components Component: Text Platform: Android Android applications. Platform: Linux Building on Linux. labels Nov 21, 2018
@meatwallace
Copy link

sorry @TikiTDO but can you provide an example of a working network_security_config.xml?

@TikiTDO
Copy link
Contributor Author

TikiTDO commented Nov 26, 2018

@meatwallace I'm using the block almost verbatim from the example:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

Obviously this will only work if you're connecting using USB, and using adb reverse to connect to the bundler, and any servers that the app may need to talk to. If you want connectivity over the network then you will need elements to set it to the values that you require.

@hey99xx
Copy link

hey99xx commented Nov 26, 2018

For localhost development these are the domains that we put in our debug app in src/debug/res/xml/network_security_config.xml

<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>

It's based on

public static final String EMULATOR_LOCALHOST = "10.0.2.2";
public static final String GENYMOTION_LOCALHOST = "10.0.3.2";
public static final String DEVICE_LOCALHOST = "localhost";

@iamdurui
Copy link

where is the network_security_config.xml? my apk don't have this !!! @TikiTDO
11_19_04__11_29_2018

@TikiTDO
Copy link
Contributor Author

TikiTDO commented Nov 29, 2018

@iamdurui You create it.

@Salakar
Copy link
Contributor

Salakar commented Jan 22, 2019

Came across this issue today, I used a one-liner to work around this for now;

Added android:usesCleartextTraffic="true" inside <application ...here...></application> in my AndroidManifest.xml. Diff: invertase/react-native-firebase@b887849

Though adding a network-security-config file should be the recommended workaround for now.

Have pushed up a PR that adds the network security config as part of RN devsupport (not part of release builds) 👇

@cpojer
Copy link
Contributor

cpojer commented Jan 23, 2019

Thanks to @Salakar's PR this will be fixed in RN soon :)

grabbou pushed a commit that referenced this issue Jan 23, 2019
Summary:
This fixes #22375. Android API level 28 and above now blocks all [clear text requests](https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted) unless a network configuration rule is added to exclude it specifically.

The packager falls into this category and therefore gets blocked; resulting in the app being unable to connect to it.

Domains/IPs for the config have been taken from [here](https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java#L20-L22).

This change only adds to DevSupport android resources - so won't affect builds without DevSupport, e.g. release builds.

Changelog:
----------

[ANDROID] [DevSupport] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes #22375)
Pull Request resolved: #23105

Differential Revision: D13781516

Pulled By: cpojer

fbshipit-source-id: f7574f3e1b0fe6984efe1d0598ef631385c245bc
@hey99xx
Copy link

hey99xx commented Jan 23, 2019

@Salakar How is devsupport files filtered out of release builds, I cannot find such information. RN only releases one AAR file, not for debug and release separately.

Also as far as I know having this XML file alone does not do anything, the application needs to point to it in AndroidManifest.xml through:

<application android:networkSecurityConfig="@xml/network_security_config" ... >

Did you confirm your commit fixes the issue in debug build and does not make localhost usable in relase builds? If it doesn't work, maybe we can move the file into template/android/app/src/debug folder path.

Salakar added a commit to react-native-firebase/react-native that referenced this issue Feb 1, 2019
facebook-github-bot pushed a commit that referenced this issue Feb 1, 2019
…23135)

Summary:
This is a follow-up PR for #23105 - as mentioned on discord.

 ---

This PR applies the network security config for the RN template project only. New RN projects started with the updated template will be able to connect to the packager on builds built with Android API 28 & above.

See https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted for more information about this newly required config, specifically:

![image](https://user-images.githubusercontent.com/5347038/52124287-b3de2580-2620-11e9-958d-bc2da15c6f01.png)

Changelog:
----------
[ANDROID] [Template] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes #22375)
Pull Request resolved: #23135

Differential Revision: D13917058

Pulled By: cpojer

fbshipit-source-id: 0e66f2cde712c1285d217e3625b73028c3770b65
matt-oakes pushed a commit to matt-oakes/react-native that referenced this issue Feb 7, 2019
Summary:
This fixes facebook#22375. Android API level 28 and above now blocks all [clear text requests](https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted) unless a network configuration rule is added to exclude it specifically.

The packager falls into this category and therefore gets blocked; resulting in the app being unable to connect to it.

Domains/IPs for the config have been taken from [here](https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java#L20-L22).

This change only adds to DevSupport android resources - so won't affect builds without DevSupport, e.g. release builds.

Changelog:
----------

[ANDROID] [DevSupport] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes facebook#22375)
Pull Request resolved: facebook#23105

Differential Revision: D13781516

Pulled By: cpojer

fbshipit-source-id: f7574f3e1b0fe6984efe1d0598ef631385c245bc
matt-oakes pushed a commit to matt-oakes/react-native that referenced this issue Feb 7, 2019
…acebook#23105) (facebook#23135)

Summary:
This is a follow-up PR for facebook#23105 - as mentioned on discord.

 ---

This PR applies the network security config for the RN template project only. New RN projects started with the updated template will be able to connect to the packager on builds built with Android API 28 & above.

See https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted for more information about this newly required config, specifically:

![image](https://user-images.githubusercontent.com/5347038/52124287-b3de2580-2620-11e9-958d-bc2da15c6f01.png)

Changelog:
----------
[ANDROID] [Template] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes facebook#22375)
Pull Request resolved: facebook#23135

Differential Revision: D13917058

Pulled By: cpojer

fbshipit-source-id: 0e66f2cde712c1285d217e3625b73028c3770b65
grabbou pushed a commit that referenced this issue Feb 18, 2019
…23135)

Summary:
This is a follow-up PR for #23105 - as mentioned on discord.

 ---

This PR applies the network security config for the RN template project only. New RN projects started with the updated template will be able to connect to the packager on builds built with Android API 28 & above.

See https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted for more information about this newly required config, specifically:

![image](https://user-images.githubusercontent.com/5347038/52124287-b3de2580-2620-11e9-958d-bc2da15c6f01.png)

Changelog:
----------
[ANDROID] [Template] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes #22375)
Pull Request resolved: #23135

Differential Revision: D13917058

Pulled By: cpojer

fbshipit-source-id: 0e66f2cde712c1285d217e3625b73028c3770b65
@Exilz
Copy link

Exilz commented Feb 21, 2019

@Salakar 's workaround worked right away on my MI8 with android 9. Thanks 👍

@hey99xx
Copy link

hey99xx commented Feb 22, 2019

Well, we iterated on Salakar's work, accidentally broke it, and fixed it again. But by at this point in 0.59.0-rc.2 this should be completely fixed.

@RodrigoAngeloValentini
Copy link

RodrigoAngeloValentini commented Apr 9, 2019

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>

<application
...
android:usesCleartextTraffic="true"
...
...

`

t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
Summary:
This fixes facebook#22375. Android API level 28 and above now blocks all [clear text requests](https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted) unless a network configuration rule is added to exclude it specifically.

The packager falls into this category and therefore gets blocked; resulting in the app being unable to connect to it.

Domains/IPs for the config have been taken from [here](https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java#L20-L22).

This change only adds to DevSupport android resources - so won't affect builds without DevSupport, e.g. release builds.

Changelog:
----------

[ANDROID] [DevSupport] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes facebook#22375)
Pull Request resolved: facebook#23105

Differential Revision: D13781516

Pulled By: cpojer

fbshipit-source-id: f7574f3e1b0fe6984efe1d0598ef631385c245bc
t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
…acebook#23105) (facebook#23135)

Summary:
This is a follow-up PR for facebook#23105 - as mentioned on discord.

 ---

This PR applies the network security config for the RN template project only. New RN projects started with the updated template will be able to connect to the packager on builds built with Android API 28 & above.

See https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted for more information about this newly required config, specifically:

![image](https://user-images.githubusercontent.com/5347038/52124287-b3de2580-2620-11e9-958d-bc2da15c6f01.png)

Changelog:
----------
[ANDROID] [Template] add Network Security Config file to allow access to packager via cleartext requests in Android API 28 and above. (fixes facebook#22375)
Pull Request resolved: facebook#23135

Differential Revision: D13917058

Pulled By: cpojer

fbshipit-source-id: 0e66f2cde712c1285d217e3625b73028c3770b65
@GihanMaheshWijesuriya
Copy link

Came across this issue today, I used a one-liner to work around this for now;

Added android:usesCleartextTraffic="true" inside <application ...here...></application> in my AndroidManifest.xml. Diff: invertase/react-native-firebase@b887849

Though adding a network-security-config file should be the recommended workaround for now.

Have pushed up a PR that adds the network security config as part of RN devsupport (not part of release builds) 👇

thankz buddy you save my day

@aryo
Copy link

aryo commented Jun 25, 2019

Just a heads up for people that have existing RN projects and just started targeting API level 28, to apply the whitelist for debug builds only:

  1. Add the network_security_config.xml to the android/src/debug/res/xml directory (make the dir if it doesn't exist)
  2. put <application android:networkSecurityConfig="@xml/network_security_config" ... /> in android/src/debug/AndroidManifest.xml

edit: updated wrong application attribute

@npedrini
Copy link

npedrini commented Jul 1, 2019

@aryo, thanks. Was wondering how to do this myself and found little documentation.

@usrbowe
Copy link
Contributor

usrbowe commented Jul 25, 2019

ℹThere were couple workarounds, but the final solution is documented here:

https://facebook.github.io/react-native/docs/integration-with-existing-apps#cleartext-traffic-api-level-28
This will fix issue, when packager server is not reachable on Android API28+.

@nkraev
Copy link
Contributor

nkraev commented Dec 25, 2019

Please note that presence of android:networkSecurityConfig="@xml/network_security_config" in your AndroidManifest.xml will override android:usesCleartextTraffic="true" anyway.
So for bringing RN into existing app with existing network security config the only way to set up packager is to update network_security_config.xml.

emiljoha added a commit to emiljoha/lightning-app that referenced this issue Jan 12, 2020
Android api level 28+ seems to not allow http traffic by default. This breaks
react-native debugging that relies on fetching artifacts over http for quick
iteration. See react native github isse
facebook/react-native#22375 for more details.
@facebook facebook locked as resolved and limited conversation to collaborators Jan 23, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jan 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Component: Text Platform: Android Android applications. Platform: Linux Building on Linux. Resolution: Locked This issue was locked by the bot. Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used.
Projects
None yet