Skip to content

Commit

Permalink
Add Kotlin DSL extension function for configuring proto source direct…
Browse files Browse the repository at this point in the history
…ory set in Android builds (#433)

This change adds the missing Kotlin DSL extension for configuring the `proto` SourceDirectorySet in Android build. The exiting extension has the receiver type of SourceSet while it should be AndroidSourceSet in Android builds. This caused method not found error when using the `proto` function in Android builds. This change add a compileOnly dependency on android build tools. An integration test covering Android build with Kotlin DSL is included.
  • Loading branch information
voidzcy authored Dec 14, 2020
1 parent eb77a21 commit 371b114
Show file tree
Hide file tree
Showing 26 changed files with 837 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ configurations {

dependencies {
compileOnly "org.gradle:gradle-kotlin-dsl:1.0.4"
compileOnly "com.android.tools.build:gradle:3.5.0"

compile 'com.google.guava:guava:27.0.1-jre'
compile 'com.google.gradle:osdetector-gradle-plugin:1.6.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.google.protobuf.gradle

import com.android.build.gradle.api.AndroidSourceSet
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.file.SourceDirectorySet
Expand Down Expand Up @@ -31,8 +32,8 @@ fun Project.protobuf(action: ProtobufConfigurator.()->Unit) {
}

/**
* Applies the supplied action to the [ProtobufSourceDirectorySet] extension on
* a receiver of type [SourceSet]
* Applies the supplied action to the "proto" [SourceDirectorySet] extension on
* a receiver of type [SourceSet].
*
* @since 0.8.7
* @usage
Expand Down Expand Up @@ -60,6 +61,38 @@ fun SourceSet.proto(action: SourceDirectorySet.() -> Unit) {
?.apply(action)
}

/**
* Applies the supplied action to the "proto" [SourceDirectorySet] extension on
* a receiver of type [AndroidSourceSet] for Android builds.
*
* @since 0.8.15
* @usage
* ```
* android {
* sourceSets {
* create("sample") {
* proto {
* srcDir("src/sample/protobuf")
* }
* }
* }
* }
* ```
*
* @receiver [AndroidSourceSet] The Android source set for which the "proto"
* [SourceDirectorySet] extension will be configured
*
* @param action A configuration lambda to apply on a receiver of type [SourceDirectorySet]
* @return [Unit]
*/
fun AndroidSourceSet.proto(action: SourceDirectorySet.() -> Unit) {
(this as? ExtensionAware)
?.extensions
?.getByName("proto")
?.let { it as? SourceDirectorySet }
?.apply(action)
}

/**
* Uses the supplied action to configure the [ExecutableLocator] for protoc.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.protobuf.gradle

import static com.google.protobuf.gradle.ProtobufPluginTestHelper.buildAndroidProject

import groovy.transform.CompileDynamic
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
Expand All @@ -12,8 +14,8 @@ import spock.lang.Unroll
*/
@CompileDynamic
class ProtobufKotlinDslPluginTest extends Specification {
// Current supported version is Gradle 5+.
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.0", "6.1"]
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.1.1", "6.5"]
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0", "4.0.0", "4.1.0"]

@Unroll
void "testProjectKotlinDsl should be successfully executed (java-only project) [gradle #gradleVersion]"() {
Expand Down Expand Up @@ -42,4 +44,33 @@ class ProtobufKotlinDslPluginTest extends Specification {
where:
gradleVersion << GRADLE_VERSIONS
}
@Unroll
void "testProjectAndroidKotlinDsl should be successfully executed [android #agpVersion, gradle #gradleVersion]"() {
given: "project from testProjectKotlinDsl"
File testProjectAndroidKotlinDslStaging = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidKotlinDsl')
.copyDirs('testProjectAndroidKotlinDsl')
.build()
File testProjectLiteStaging = ProtobufPluginTestHelper.projectBuilder('testProjectLite')
.copyDirs('testProjectLite')
.build()
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidDslMain')
.copySubProjects(testProjectAndroidKotlinDslStaging, testProjectLiteStaging)
.withAndroidPlugin(agpVersion)
.build()
when: "build is invoked"
BuildResult result = buildAndroidProject(
mainProjectDir,
gradleVersion,
"testProjectAndroidKotlinDsl:build"
)
then: "it succeed"
result.task(":testProjectAndroidKotlinDsl:build").outcome == TaskOutcome.SUCCESS
where:
agpVersion << ANDROID_PLUGIN_VERSION
gradleVersion << GRADLE_VERSIONS
}
}
Loading

0 comments on commit 371b114

Please sign in to comment.