Adr3nl
Adr3nl

Reputation: 453

Dynamicaly change SDK build options

I have been developing automated tests to my app, however, we reached a point where we have to change options outside the app environment. To do so we decided to use the latest UIAutomator v2. By doing so we are able to use espresso testing inside our app and UI Automator for system popups or change settings in the same test. The issue at this point is that we have to maintain min SDK 16 for contract purposes. This makes it impossible to use UIAutomator v2. The compiler itself suggests a solution :

Suggestion: use tools:overrideLibrary="android.support.test.uiautomator.v18" to force usage

My current gradle relevant gradle config:

 compileSdkVersion rootProject.ext.compileSdkVersion
 buildToolsVersion rootProject.ext.buildToolsVersion

 // default apk name (to be overriden)
 project.archivesBaseName = "AwessomeApp";

 android.enforceUniquePackageName=false

 defaultConfig {
        String charPrefix = "AWS"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode appVersion.getCode()
        versionName appVersion.getName()
        applicationId "pt.company.awesome"
        testApplicationId "pt.company.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

However, it doesn't work, probably because we have the minSdk set by the root project using gradle. If i bump the project version to 18 it works flawlessly. At this point i have no idea on how to solve this issue. Is it possible to do something like set a different minSdk por testing or flavor?

Upvotes: 2

Views: 1088

Answers (1)

Adr3nl
Adr3nl

Reputation: 453

Ok, i've eventually managed to fix this. I don't know if it's the best available solution, but it was the only one i could find.

 /**
 * Task to add UIAutomator for Test builds and tasks
 */
task addUIAutomatorDependency(dependsOn: ['taskThatNeedsLowerSdk1', 'taskThatNeedsLowerSdk2']) {

    rootProject.minSdkVersion = 18

    dependencies{
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    }
}

subprojects { subproject ->
    afterEvaluate{
        if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

If there is any better solution out there please let me know.

EDIT: Way better solution is to add an AndroidManifest.xml in src/AndroidTest/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      package="pt.bla.blablabla">

<uses-sdk android:targetSdkVersion="22" android:minSdkVersion="16"
          tools:overrideLibrary="android.support.test.uiautomator.v18"/>

</manifest>

Note that the package should be the same as your original application.

Upvotes: 4

Related Questions