Skip to content

Commit 605e08c

Browse files
authored
Merge pull request #27 from nativeapptemplate/env-var-refactor
Read API endpoint from Gradle properties in Debug builds
2 parents cc0348a + bbfb450 commit 605e08c

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

CLAUDE.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,4 @@ cp scripts/pre-push .git/hooks/pre-push
7777

7878
## Connecting to Local API
7979

80-
In `app/build.gradle.kts`, swap the debug `buildConfigField` values:
81-
```kotlin
82-
buildConfigField("String", "DOMAIN","\"192.168.1.21\"")
83-
buildConfigField("String", "PORT","\"3000\"")
84-
buildConfigField("String", "SCHEME","\"http\"")
85-
```
80+
The debug `buildConfigField` entries in `app/build.gradle.kts` read `NATEMPLATE_API_DOMAIN`, `NATEMPLATE_API_PORT`, and `NATEMPLATE_API_SCHEME` via `project.findProperty(...)` (not `System.getenv` — Android Studio launched from Finder/Dock does not inherit shell env). Set them in `~/.gradle/gradle.properties` (user-global, per-developer); the same config then works from both the terminal and the IDE. Falls back to `https://api.nativeapptemplate.com` when unset. One-off override: `./gradlew -PNATEMPLATE_API_DOMAIN=... assembleDebug`.

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@ To run this app successfully, ensure you have:
121121

122122
## Running with the NativeAppTemplate-API on localhost
123123

124-
To connect to a local API server, update the following configuration in the build.gradle.kts (Module: app):
124+
By default the debug build hits the hosted API (`https://api.nativeapptemplate.com`). To point it at a Rails server running on your Wi-Fi, add the following to `~/.gradle/gradle.properties` (here `~` is your user home directory — `/Users/<you>` on macOS, `/home/<you>` on Linux, `C:\Users\<you>` on Windows; create the file if it doesn't exist):
125125

126-
```kotlin
127-
buildConfigField("String", "DOMAIN","\"192.168.1.21\"")
128-
buildConfigField("String", "PORT","\"3000\"")
129-
buildConfigField("String", "SCHEME","\"http\"")
130126
```
127+
# Use your current Wi-Fi IP (macOS: `ipconfig getifaddr en0`), or 10.0.2.2 for emulator → host.
128+
# Never use 127.0.0.1, localhost, or 0.0.0.0 — Rails and this app must agree on one reachable address.
129+
NATEMPLATE_API_DOMAIN=192.168.1.21
130+
NATEMPLATE_API_PORT=3000
131+
NATEMPLATE_API_SCHEME=http
132+
```
133+
134+
Then `./gradlew assembleDebug` — or Build → Rebuild Project from Android Studio. The debug `buildConfigField` entries in `app/build.gradle.kts` read these via `project.findProperty(...)`, so the same config works from both the terminal and the IDE. Remove the three properties to fall back to the hosted default. For a one-off override: `./gradlew -PNATEMPLATE_API_DOMAIN=192.168.1.21 -PNATEMPLATE_API_PORT=3000 -PNATEMPLATE_API_SCHEME=http assembleDebug`.
135+
136+
Cleartext HTTP to private IPs is already permitted in debug via `app/src/debug/res/xml/network_security_config.xml`; the release config (in `app/src/main/`) keeps `api.nativeapptemplate.com` HTTPS-only.
131137

132138
## Blog
133139

app/build.gradle.kts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ android {
2626
debug {
2727
extra["alwaysUpdateBuildId"] = false
2828
isDebuggable = true
29-
buildConfigField("String", "DOMAIN", "\"api.nativeapptemplate.com\"")
30-
buildConfigField("String", "PORT", "\"\"")
31-
buildConfigField("String", "SCHEME", "\"https\"")
32-
// buildConfigField("String", "DOMAIN","\"192.168.1.21\"")
33-
// buildConfigField("String", "PORT","\"3000\"")
34-
// buildConfigField("String", "SCHEME","\"http\"")
29+
buildConfigField("String", "DOMAIN", "\"${(project.findProperty("NATEMPLATE_API_DOMAIN") as String?)?.trim() ?: "api.nativeapptemplate.com"}\"")
30+
buildConfigField("String", "PORT", "\"${(project.findProperty("NATEMPLATE_API_PORT") as String?)?.trim() ?: ""}\"")
31+
buildConfigField("String", "SCHEME", "\"${(project.findProperty("NATEMPLATE_API_SCHEME") as String?)?.trim() ?: "https"}\"")
3532
}
3633

3734
release {

0 commit comments

Comments
 (0)