Skip to content

Commit

Permalink
Add support for setting JDK compile version in Android Test plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tresat committed Nov 4, 2024
1 parent df51376 commit b47ab1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
// common AbstractAndroidPlugin that AndroidSoftwarePlugin and this plugin
// both extend that folds these commonalities back into it
public interface AndroidTest {
/**
* JDK version to use for compilation.
*/
@Restricted
Property<Integer> getJdkVersion();

/**
* @see CommonExtension#getNamespace()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.android.build.api.dsl.ManagedVirtualDevice;
import com.android.build.api.dsl.TestExtension;
import com.android.build.api.dsl.UnitTestOptions;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurationContainer;
Expand Down Expand Up @@ -68,6 +69,14 @@ private void linkDslModelToPlugin(Project project, AndroidTest dslModel) {

ifPresent(dslModel.getTargetProjectPath(), android::setTargetProjectPath);

android.compileOptions(compileOptions -> {
// Up to Java 11 APIs are available through desugaring
// https://developer.android.com/studio/write/java11-minimal-support-table
compileOptions.setSourceCompatibility(JavaVersion.toVersion(dslModel.getJdkVersion().get()));
compileOptions.setTargetCompatibility(JavaVersion.toVersion(dslModel.getJdkVersion().get()));
return null;
});

TestOptions testOptions = dslModel.getTestOptions();
ifPresent(testOptions.getTestInstrumentationRunner(), android.getDefaultConfig()::setTestInstrumentationRunner);
testOptions.getManagedDevices().forEach(device -> {
Expand Down

0 comments on commit b47ab1f

Please sign in to comment.