Fix android backend url

This commit is contained in:
2026-03-30 00:03:27 -06:00
parent 2d4e1be832
commit 410b68520a
3 changed files with 48 additions and 10 deletions

View File

@@ -1,7 +1,23 @@
import java.util.Properties
plugins {
alias(libs.plugins.android.application)
}
val localProperties = Properties().apply {
val file = rootProject.file("local.properties")
if (file.exists()) {
file.inputStream().use { load(it) }
}
}
fun quoted(value: String): String = "\"$value\""
val emulatorBackendUrl =
(localProperties.getProperty("petstore.backend.emulatorUrl") ?: "http://10.0.2.2:8080/").trim()
val deviceBackendUrl =
(localProperties.getProperty("petstore.backend.deviceUrl") ?: "http://10.0.0.200:8080/").trim()
android {
namespace = "com.example.petstoremobile"
compileSdk = 36
@@ -14,6 +30,13 @@ android {
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "EMULATOR_BACKEND_URL", quoted(emulatorBackendUrl))
buildConfigField("String", "DEVICE_BACKEND_URL", quoted(deviceBackendUrl))
}
buildFeatures {
buildConfig = true
}
buildTypes {

View File

@@ -2,7 +2,9 @@ package com.example.petstoremobile.api;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import com.example.petstoremobile.BuildConfig;
import com.example.petstoremobile.api.auth.AuthApi;
import com.example.petstoremobile.api.auth.AuthInterceptor;
@@ -15,22 +17,28 @@ import java.util.concurrent.TimeUnit;
//Retrofit client Used for API calls
public class RetrofitClient {
private static final String TAG = "RetrofitClient";
public static final String BASE_URL = getBaseUrl();
// Helper function to determine BASE_URL based on whether we are testing on an emulator or a real device
private static String getBaseUrl() {
if (Build.FINGERPRINT.contains("generic")
|| Build.FINGERPRINT.contains("unknown")
String url = isEmulator() ? BuildConfig.EMULATOR_BACKEND_URL : BuildConfig.DEVICE_BACKEND_URL;
Log.i(TAG, "Using backend URL: " + url + " (emulator=" + isEmulator() + ")");
return url;
}
private static boolean isEmulator() {
return Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| "google_sdk".equals(Build.PRODUCT)) {
return "http://10.0.2.2:8080/"; //emulator testing
} else {
return "http://10.0.0.200:8080/"; //Hardware testing
}
|| Build.HARDWARE.contains("goldfish")
|| Build.HARDWARE.contains("ranchu")
|| Build.PRODUCT.contains("sdk")
|| Build.PRODUCT.contains("sdk_gphone")
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"));
}
private static Retrofit retrofit = null;

View File

@@ -9,3 +9,10 @@
# Copy this file to local.properties and update the path below:
sdk.dir=/path/to/your/android/sdk
# Optional backend overrides for Android development:
# Emulator default should usually stay 10.0.2.2 for the local machine.
petstore.backend.emulatorUrl=http\://10.0.2.2\:8080/
# Physical device example. Replace with the machine IP running the backend.
petstore.backend.deviceUrl=http\://10.0.0.200\:8080/