diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 552480f0..e3955e2f 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -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 { @@ -62,4 +85,4 @@ dependencies { testImplementation(libs.junit) androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.espresso.core) -} \ No newline at end of file +} diff --git a/android/app/src/main/java/com/example/petstoremobile/api/RetrofitClient.java b/android/app/src/main/java/com/example/petstoremobile/api/RetrofitClient.java index 3a93deb2..b08af532 100644 --- a/android/app/src/main/java/com/example/petstoremobile/api/RetrofitClient.java +++ b/android/app/src/main/java/com/example/petstoremobile/api/RetrofitClient.java @@ -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; @@ -123,4 +131,4 @@ public class RetrofitClient { return getClient(context).create(CategoryApi.class); } -} \ No newline at end of file +} diff --git a/android/local.properties.template b/android/local.properties.template index 788baf64..e41a73bc 100644 --- a/android/local.properties.template +++ b/android/local.properties.template @@ -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/