如何从Android Studio的运行/调试配置中将参数传递给Java代码 [英] How to pass a parameter to the Java code in run/debug configuration from Android Studio
问题描述
我的Android应用程序对我的服务器执行一些http请求。但是有时候我正在调试在我的开发机器上运行的新api代码。我希望能够在我的代码中传递一些东西(如环境变量),如果现在我可以使用它作为来自android模拟器的api请求的主机名。 / p>
所以我正在寻找一种方式传递如下:
API_SERVER = http://10.0.2.2/myapp/
在我的代码中,我会使用它不知何故,例如:
final static String API_SERVER_REAL =http://example.com/;
final String apiServerOverride = System.getenv(API_SERVER);
final String API_SERVER =(null!= apiServerOverride&&apiServerOverride.isEmpty()?apiServerOverride:API_SERVER_REAL);
我建议使用 productFlavors 。每种风味都可以包含环境特定设置。我只需要一个名为Environment的类,其中包含我需要的所有 public static final String
,每个产品风格包含此类的不同版本,其值为环境。
My android app does some http requests to my server. However sometimes I am debugging the new api code that runs on my development machine. I would like to be able to pass something (like an environment variable) so in my code, if it's present I would be able to use that as the hostname for the api requests from the android emulator.
So I'm looking for a way to pass something like:
API_SERVER=http://10.0.2.2/myapp/
and in my code I would use it somehow, for example:
final static String API_SERVER_REAL = "http://example.com/";
final String apiServerOverride = System.getenv("API_SERVER");
final String API_SERVER = (null != apiServerOverride && !apiServerOverride.isEmpty() ? apiServerOverride : API_SERVER_REAL);
I suggest using productFlavors. Each flavor can contain environment specific settings. I simply have a class called 'Environment' which contains all the public static final String
s that I need and each product flavor includes an different version of this class with the values set for the environment.
这篇关于如何从Android Studio的运行/调试配置中将参数传递给Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!