Issue
It has been a day now that I am searching for a resolution to my problem, this is why I submit it.
I use Espresso to run Android tests. Everything works fine for Lollipop and newer, but not for older Android versions (from API 16 that is the project's target to API 21 that is the first working version).
My issue is that the files used for the tests are those in /androidTest folder for Android Lollipop and newer but for older versions it uses the default flavor that is the 'mock' flavor.
For example I have a class UserManager in both /mock and /androidTest folders, and I need the tests to use the one in /androidTest directory.
Do you know how to correct it? This breaks the tests only for Android 4.x, for newer versions the expected behavior happens so I am really disappointed.
I tried to set the sourceSets but it did not solve my issue.
I saw that multiDex could interfere with androidTest so I tried some solutions that were working for others but it did not fix it too.
I take any advise if someone can help.
Thanks
Android Studio 3.2.1
Gradle 4.6
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "my.app"
minSdkVersion 16
targetSdkVersion 28
versionCode Integer.parseInt(app_version_code)
versionName app_version_name
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
}
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.json:json:20140107'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
}
Solution
Ok I finally found a solution for my issue.
Thanks to this post I figured out that it was a multidex issue.
So I added a proguard rules file in my project and I added these lines in my build.gradle:
buildTypes {
debug {
// Other debug settings
multiDexKeepProguard file('proguard-multidex-rules.pro')
}
}
The content of the proguard-multidex-rules.pro:
-keep class android.support.test.internal** { *; }
-keep class org.junit.** { *; }
-keep @com.google.common.annotations.VisibleForTesting class *
-keepclasseswithmembers class * {
@com.google.common.annotations.VisibleForTesting *;
}
Now the last thing to do is to add the @VisibleForTesting annotation before every class declaration that you need in you instrumented tests. It worked for me and now the files that are used for tests are those annotated (found here).
Issue solved
Answered By - LAcrym0
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.