do not crash demo for GraphHopper rerouting#201
do not crash demo for GraphHopper rerouting#201karussell wants to merge 2 commits intomaplibre:mainfrom
Conversation
Yes, I think it would be good to mention that. The change itself looks good to me.
It's possible to work around this, but this would probably be too much for a small sample. We should allow to make the rerouting behavior easier to customize. |
gradle/developer-config.gradle
Outdated
| " <!-- Instead of valhalla you can use GraphHopper for the path finding. -->\n" + | ||
| " <!-- Don't use the following API key in production, it is for demonstration purposes only: -->\n" + | ||
| " <string name=\"graphhopper_url\" translatable=\"false\">https://graphhopper.com/api/1/navigate?key=7088b84f-4cee-4059-96de-fd0cbda2fdff</string>\n" + | ||
| " <!-- <string name=\"graphhopper_key\" translatable=\"false\">7088b84f-4cee-4059-96de-fd0cbda2fdff</string> -->\n" + |
There was a problem hiding this comment.
I think you can't have these commented out, or the build will fail (as it is right now)
boldtrn
left a comment
There was a problem hiding this comment.
The build is failing with error:
e: file:///Users/runner/work/maplibre-navigation-android/maplibre-navigation-android/app/src/main/java/org/maplibre/navigation/android/example/GraphHopperNavigationActivity.kt:223:78 Unresolved reference 'graphhopper_key'.
Please double check if ./gradlew test succeeds locally.
|
I've done the requested changes. Please note that the ValhallaNavigationActivity is still crashing for the same reason when rerouting. Also note that not only the graphhopper_url but also the base_url and valhalla_url has to end with '/'. This seems to be a requirement of the SDK for when doing the rerouting: |
boldtrn
left a comment
There was a problem hiding this comment.
Thanks for fixing this 👍
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent the GraphHopper demo app from crashing during rerouting (issue #168) by ensuring the demo’s GraphHopper configuration provides the fields required by the reroute pipeline and by splitting the GraphHopper URL/key configuration.
Changes:
- Update the generated
developer-config.xmltemplate to separategraphhopper_urland a newgraphhopper_key. - Update
GraphHopperNavigationActivityto append the GraphHopper key to the request URL and to populateRouteOptionsso rerouting doesn’t immediately crash.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| gradle/developer-config.gradle | Adjusts generated developer config strings for GraphHopper (separate URL + key, trailing slash note). |
| app/src/main/java/org/maplibre/navigation/android/example/GraphHopperNavigationActivity.kt | Uses new graphhopper_key and updates RouteOptions intended to prevent reroute crashes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| baseUrl = getString(R.string.graphhopper_url), | ||
| profile = "car", | ||
| user = "gh", | ||
| accessToken = "pk." + getString(R.string.graphhopper_key), |
There was a problem hiding this comment.
These RouteOptions are used by the reroute pipeline, which is implemented via NavigationRoute/MapboxDirections (see MapLibreRouteFetcher). Setting baseUrl to the GraphHopper /navigate/ endpoint and using user="gh" / profile="car" will cause reroute requests to be formed as /directions/v5/gh/car/..., which is not a valid endpoint for either Mapbox or GraphHopper and effectively guarantees rerouting will fail. If the intent is only to avoid crashes, consider using Mapbox-compatible user/profile values and a base URL intended for MapboxDirections (ending with /), while keeping the dummy accessToken to satisfy token-format validation.
| baseUrl = getString(R.string.graphhopper_url), | |
| profile = "car", | |
| user = "gh", | |
| accessToken = "pk." + getString(R.string.graphhopper_key), | |
| // Use Mapbox-compatible settings so NavigationRoute/MapboxDirections | |
| // constructs syntactically valid reroute URLs, with a dummy token. | |
| baseUrl = "https://api.mapbox.com/", | |
| profile = "driving", | |
| user = "mapbox", | |
| accessToken = "pk.00000000000000000000000000000000", |
| " <!-- Instead of valhalla you can use GraphHopper for the path finding. Note that the graphhopper_url has to end with '/'. (#201) -->\n" + | ||
| " <!-- Don't use the following API key in production, it is for demonstration purposes only: -->\n" + | ||
| " <string name=\"graphhopper_url\" translatable=\"false\">https://graphhopper.com/api/1/navigate?key=7088b84f-4cee-4059-96de-fd0cbda2fdff</string>\n" + | ||
| " <string name=\"graphhopper_key\" translatable=\"false\">7088b84f-4cee-4059-96de-fd0cbda2fdff</string>\n" + | ||
| " <string name=\"graphhopper_url\" translatable=\"false\">https://graphhopper.com/api/1/navigate/</string>\n" + |
There was a problem hiding this comment.
Introducing a new required string resource (graphhopper_key) will break builds for anyone who already has an existing untracked developer-config.xml generated by older versions, because this Gradle task only writes the file when it does not exist. Consider updating the task to detect/migrate an existing file (e.g., add graphhopper_key and normalize graphhopper_url), or fail early with a clear, actionable message telling the developer to delete/regenerate the file.
| .header("User-Agent", "MapLibre Android Navigation SDK Demo App") | ||
| .url(getString(R.string.graphhopper_url)) | ||
| .url(getString(R.string.graphhopper_url) + "?key=" + getString(R.string.graphhopper_key)) | ||
| .post(requestBodyJson.toRequestBody("application/json; charset=utf-8".toMediaType())) |
There was a problem hiding this comment.
Building the GraphHopper request URL via string concatenation is brittle: it will produce an invalid URL if graphhopper_url already contains a query string, and it doesn't URL-encode the key. Prefer constructing the URL with OkHttp's HttpUrl (parse + newBuilder + addQueryParameter) so key is appended correctly regardless of trailing slashes or existing parameters.
This change avoids the GraphHopper demo app crash when rerouting, i.e. fixes #168.
However, a potential different request is used for the GET request compared to the initial POST request, which is a general problem also for ValhallaNavigationActivity.
Note that the baseUrl (and so also graphhopper_url) has to end with '/'. Maybe this should be added in the comment of the developer xml config?