Skip to content

Commit

Permalink
Make Robolectric works with Kotlin 1.7.x
Browse files Browse the repository at this point in the history
1. Add artifactType with jar for utils' runtimeElements

We should add artifactType with jar to ensure standard
runtimeElements variant has a max priority selection
sequence than other variants that brought by kotlin plugin.

2. Use annotations module to replace shadowapi for androidx_test

androidx_test only needs annotations module, instead of a larger one
called shadowapi.

3. Use java/main to replace kotlin/main for utils modules

Use java/main classes directory to replace default kotlin/main to
avoid d8 error when dexing & desugaring kotlin classes with non-exist
kotlin/main directory because utils module doesn't have kotlin code
in production. If utils module starts to add Kotlin code in main source
set, we can remove this destinationDirectory modification.

Signed-off-by: utzcoz <utzcoz@outlook.com>
  • Loading branch information
utzcoz committed Oct 17, 2022
1 parent 49ce3c7 commit 1389558
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 1 addition & 2 deletions integration_tests/androidx_test/build.gradle
Expand Up @@ -11,7 +11,6 @@ android {
defaultConfig {
minSdk 16
targetSdk 33
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -43,7 +42,6 @@ dependencies {
implementation "androidx.appcompat:appcompat:$appCompatVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintlayoutVersion"
implementation "androidx.multidex:multidex:$multidexVersion"
implementation project(path: ':shadowapi', configuration: 'default')

// Testing dependencies
testImplementation project(":robolectric")
Expand All @@ -59,6 +57,7 @@ dependencies {
testImplementation "androidx.test.ext:junit:$axtJunitVersion"
testImplementation "com.google.truth:truth:$truthVersion"

androidTestImplementation project(':annotations')
androidTestImplementation "androidx.test:runner:$axtVersion"
androidTestImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test:rules:$axtVersion"
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/ctesque/build.gradle
Expand Up @@ -45,8 +45,7 @@ android {
}

dependencies {
implementation project(path: ':testapp')
implementation project(path: ':shadowapi', configuration: 'default')
implementation project(':testapp')

testImplementation project(":robolectric")
testImplementation "junit:junit:${junitVersion}"
Expand All @@ -61,6 +60,7 @@ dependencies {
testImplementation("com.google.guava:guava:$guavaJREVersion")

// Testing dependencies
androidTestImplementation project(':shadowapi')
// temporarily use a newer monitor version for HardwareRendererCompat
androidTestImplementation("androidx.test:monitor:1.5.0-alpha03")
androidTestImplementation("androidx.test:runner:$axtVersion")
Expand Down
30 changes: 30 additions & 0 deletions utils/build.gradle
Expand Up @@ -14,9 +14,39 @@ spotless {
}

tasks.withType(GenerateModuleMetadata) {
// We don't want to release gradle module metadata now to avoid
// potential compatibility problems.
enabled = false
}

compileKotlin {
// Use java/main classes directory to replace default kotlin/main to
// avoid d8 error when dexing & desugaring kotlin classes with non-exist
// kotlin/main directory because utils module doesn't have kotlin code
// in production. If utils module starts to add Kotlin code in main source
// set, we can remove this destinationDirectory modification.
destinationDirectory = file("${projectDir}/build/classes/java/main")
kotlinOptions {
jvmTarget = "1.8"
}
}

afterEvaluate {
configurations {
runtimeElements {
attributes {
// We should add artifactType with jar to ensure standard runtimeElements variant
// has a max priority selection sequence than other variants that brought by
// kotlin plugin.
attribute(
Attribute.of("artifactType", String.class),
ArtifactTypeDefinition.JAR_TYPE
)
}
}
}
}

dependencies {
api project(":annotations")
api project(":pluginapi")
Expand Down

0 comments on commit 1389558

Please sign in to comment.