Fix android backend url
This commit is contained in:
@@ -1,7 +1,23 @@
|
|||||||
|
import java.util.Properties
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.android.application)
|
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 {
|
android {
|
||||||
namespace = "com.example.petstoremobile"
|
namespace = "com.example.petstoremobile"
|
||||||
compileSdk = 36
|
compileSdk = 36
|
||||||
@@ -14,6 +30,13 @@ android {
|
|||||||
versionName = "1.0"
|
versionName = "1.0"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|
||||||
|
buildConfigField("String", "EMULATOR_BACKEND_URL", quoted(emulatorBackendUrl))
|
||||||
|
buildConfigField("String", "DEVICE_BACKEND_URL", quoted(deviceBackendUrl))
|
||||||
|
}
|
||||||
|
|
||||||
|
buildFeatures {
|
||||||
|
buildConfig = true
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package com.example.petstoremobile.api;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
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.AuthApi;
|
||||||
import com.example.petstoremobile.api.auth.AuthInterceptor;
|
import com.example.petstoremobile.api.auth.AuthInterceptor;
|
||||||
|
|
||||||
@@ -15,22 +17,28 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
//Retrofit client Used for API calls
|
//Retrofit client Used for API calls
|
||||||
public class RetrofitClient {
|
public class RetrofitClient {
|
||||||
|
private static final String TAG = "RetrofitClient";
|
||||||
public static final String BASE_URL = getBaseUrl();
|
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
|
// Helper function to determine BASE_URL based on whether we are testing on an emulator or a real device
|
||||||
private static String getBaseUrl() {
|
private static String getBaseUrl() {
|
||||||
if (Build.FINGERPRINT.contains("generic")
|
String url = isEmulator() ? BuildConfig.EMULATOR_BACKEND_URL : BuildConfig.DEVICE_BACKEND_URL;
|
||||||
|| Build.FINGERPRINT.contains("unknown")
|
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("google_sdk")
|
||||||
|| Build.MODEL.contains("Emulator")
|
|| Build.MODEL.contains("Emulator")
|
||||||
|| Build.MODEL.contains("Android SDK built for x86")
|
|| Build.MODEL.contains("Android SDK built for x86")
|
||||||
|| Build.MANUFACTURER.contains("Genymotion")
|
|| Build.MANUFACTURER.contains("Genymotion")
|
||||||
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|
|| Build.HARDWARE.contains("goldfish")
|
||||||
|| "google_sdk".equals(Build.PRODUCT)) {
|
|| Build.HARDWARE.contains("ranchu")
|
||||||
return "http://10.0.2.2:8080/"; //emulator testing
|
|| Build.PRODUCT.contains("sdk")
|
||||||
} else {
|
|| Build.PRODUCT.contains("sdk_gphone")
|
||||||
return "http://10.0.0.200:8080/"; //Hardware testing
|
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Retrofit retrofit = null;
|
private static Retrofit retrofit = null;
|
||||||
|
|||||||
@@ -9,3 +9,10 @@
|
|||||||
# Copy this file to local.properties and update the path below:
|
# Copy this file to local.properties and update the path below:
|
||||||
|
|
||||||
sdk.dir=/path/to/your/android/sdk
|
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/
|
||||||
|
|||||||
Reference in New Issue
Block a user