My app have two flavors. For sake of Espresso testing I created another flavor called mock
in order to mock server response and create something that I would like to test against with.
So build.gradle
file of my app project looks like this:
android {
useLibrary 'org.apache.http.legacy'
sourceSets {
main {
res.srcDirs = [
'src/main/res', 'src/main/res-flags', ...
]
}
test {
res.srcDirs = ['src/test/resources']
}
}
defaultConfig {
applicationId "com.my.package_1"
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
productFlavors {
mock {
applicationId "com.mock.package"
...
}
flavor_1 {
applicationId "com.my.package_1"
...
}
flavor_2 {
applicationId "com.my.package_2"
...
}
}
buildTypes {
debug {
// Setup default urls
buildConfigField "String", "API_URL_BASE", "\"https://www.example_1.com\""
// Enabling multidex support.
multiDexEnabled true
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
}
release {
// Enable when testing proguard
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.release
// Setup default urls
buildConfigField "String", "API_URL_BASE", "\"https://www.example_2.com\""
}
applicationVariants.all { variant ->
variant.outputs.each { v ->
def f = v.outputFile
def sha = 'git rev-parse --short HEAD'.execute().text.trim()
def fname = f.name.replace(".apk", "-${defaultConfig.versionName}-${defaultConfig.versionCode}-${sha}.apk")
v.outputFile = new File(f.parent, fname)
}
}
}
// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
if(variant.buildType.name.equals('release')
&& variant.getFlavors().get(0).name.equals('mock')) {
variant.setIgnore(true);
}
}
// Always show the result of every unit test, even if it passes.
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
}
I have created a Mock folder, it has its own manifest.xml
file and it looks like this:
My Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<permission
android:name="com.mock.passenger.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.mock.passenger.permission.MAPS_RECEIVE"/>
<!-- Settings for GCM -->
<permission
android:name="com.mock.passenger.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.mock.passenger.gcm.permission.C2D_MESSAGE"/>
<application
android:name="com.mock.passenger.MockApplication"
tools:replace="android:name">
...
</application>
</manifest>
Before running the app, I set my build variant on mockDebug
. I run the app and application gets installed on emulator. I confirm this build variant has installed because I have changed the name of it. However, it seems my MockApplication
never gets called although I have defined it in Manifest file. The reason is I don’t see those things that I have logged.
public class MockApplication extends MyApplication
{
public static final String TAG = MockApplication.class.getName();
@Override
public void onCreate()
{
Logger.debug(TAG, "*********************");
Logger.debug(TAG, "** MockApplication **");
Logger.debug(TAG, "*********************");
super.onCreate();
}
....
}
I’m not sure if something in my gradle file has missed so any idea would be appreciated, thanks.