Reputation: 5938
Is there a way to share code between those two without creating another module/external library ?
Upvotes: 1
Views: 172
Reputation: 7188
As of around June 2022, Android Studio Bumblebee stopped supporting the existing answer. (The reason can be found in this developer thread) Now, the appropriate solution is to create a new library module, move your shared code into that module, and update your app/build.gradle file to depend on the library module for its tests and androidTests.
So follow these steps :
Open your app/build.gradle file, and scroll down to the dependencies
section. Add the newly created module as a dependency for your tests and instrumented tests.
dependencies {
... all of your regular dependencies...
testImplementation project(":testShared")
androidTestImplementation project(":testShared")
}
Next, if you want to be able to reference your app code in the shared test helper code, you'll need to add a dependency onto the app. In the testHelpers/build.gradle file, add the following into the dependencies section.
dependencies {
implementation project(path: ':app')
}
Don't forget to update the package references in your shared test files and your old tests to reflect the new paths.
And that should be it!
plugins {
id 'com.android.library'
id 'kotlin-android'
}
android {
compileSdk 31
}
dependencies {
// ADD ANY NECESSARY DEPENDENCIES HERE
implementation 'org.jetbrains.kotlin:kotlin-ksp:1.4.0-rc-dev-experimental-20200828'
implementation project(path: ':app')
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.YOURWEBSITE.testshared">
</manifest>
Update AndroidStudio to be at least version Chipmunk | 2021.2.1 Patch 2
Update the Gradle plugin to be at least version 7.2.2.
Upvotes: 0
Reputation: 960
It's been 5 years so hopefully you figured it out, but in case someone stumbles on this.
// This allows us to share classes between androidTest & test directories.
android.sourceSets {
test {
java.srcDirs += "$projectDir/src/testShared"
}
androidTest {
java.srcDirs += "$projectDir/src/testShared"
}
}
Upvotes: 1