Issue
I configured a constant in the build.gradle of the application I developed. It is an "End-Point" constant for API calls. When I'm developing in the development environment, the application calls an "End-Point", when I'm developing in the production environment the application calls another "End-Point". Now comes the problem, when I'm developing the tests for the application, the configuration is not working to pick up the End-Point Correctly. Can someone help me?
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField STRING, API_URL, '"https://end-point-production/"'
}
debug {
buildConfigField STRING, API_URL, '"https://end-point-dev/"'
}
}
productFlavors {
ui_tests {
buildConfigField STRING, API_URL, '"http://end-point-tests/"'
}
}
public class BaseController {
// Service url.
protected static final String API_URL = BuildConfig.API_URL;
/**
* Crates a basic retrofit request.
*
* @return Retrofit request.
*/
public static Retrofit getRetrofit() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(5, TimeUnit.SECONDS);
client.setReadTimeout(15, TimeUnit.SECONDS);
client.setWriteTimeout(5, TimeUnit.SECONDS);
return new Retrofit.Builder()
.baseUrl(API_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create()))
.build();
}
}
@Before
public void setUp() throws Exception {
server = new MockWebServer();
server.start(8681);
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(5, TimeUnit.SECONDS);
client.setReadTimeout(15, TimeUnit.SECONDS);
client.setWriteTimeout(5, TimeUnit.SECONDS);
new Retrofit.Builder()
.baseUrl(API_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create()))
.build();
}
Solution
You could try to do like this:
There is an additional flavor
productFlavors {
non_test {
}
ui_tests {
buildConfigField STRING, API_URL, '"http://end-point-tests/"'
}
}
applicationVariants.all { variant ->
println("Iterating variant: " + variant.getName())
if (variant.getName() == "ui_testsRelease") {
buildConfigField STRING, API_URL, '"https://end-point-production/"'
} else if (variant.getName() == "ui_testsDebug") {
buildConfigField STRING, API_URL, '"http://end-point-tests/"'
} else if (variant.getName() == "non_testRelease"){
buildConfigField STRING, API_URL, '"https://end-point-production/"'
} else if (variant.getName() == "non_testDebug") {
buildConfigField STRING, API_URL, '"https://end-point-dev/"'
}
}
or for example(it looks a little better):
just move ui_tests
from productFlavors
to buildTypes
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField STRING, API_URL, '"https://end-point-production/"'
}
debug {
buildConfigField STRING, API_URL, '"https://end-point-dev/"'
}
ui_tests {
buildConfigField STRING, API_URL, '"http://end-point-tests/"'
}
}
Answered By - Dima Kozhevin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.