Develop an Android Application Using Keycloak and Localhost Redirectionfleeting
develop an android application using keycloak and localhost redirection
I tend to like using a dev env similar to the prod env, so I suppose I’m using a local k8s cluster with https handled by cert-manager.
I suppose as well that the site is available in this local cluster using the standard url localtest.me.
By default, the localhost of the android device is its own localhost. The host localhost is available using 10.0.2.2.
Run the emulator and apply a port reversing using.
adb root adb reverse tcp:443 tcp:443
That’s all. The traffic going to https://myapp.localtest.me should be revered into your host’s localhost and dealt with correctly with keycloak.
If you unfortunately use a localhost subdomain, like https://myapp.localhost, you will also need to make this allow subdomains, using some network security stuff.
Also, you might be tempted to edit /etc/hosts to make myapp.localhost point towards 10.0.2.2. Yet, in my experience, this is an unstable solution because the browser make special handling for some regexps.
For instance, myapp.something -> 10.0.2.2 works, while myapp.localhost -> 10.0.2.2 does not1. Also, there are files in /etc/hosts, /etc/system/hosts and /system/etc/hosts, making it difficult to be sure you are editing the right one.
this is likely a special handling made by the browser itself. ping myapp.localhost shows the ip 10.0.2.2 as expected and still the browser won’t use it somehow. ↩︎