Skip to content

Commit bb0b70d

Browse files
committed
Kapt: Fix extending configurations in Gradle, create "main" configuration if it does not exist yet. Also fix checking kapt configuration dependencies, use allDependencies instead (KT-15814)
1 parent 3b4a8e0 commit bb0b70d

File tree

12 files changed

+193
-3
lines changed

12 files changed

+193
-3
lines changed

libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/AbstractKotlinAndroidGradleTests.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ fun getSomething() = 10
157157
}
158158
}
159159

160+
@Test
161+
fun testKaptKt15814() {
162+
val project = Project("kaptKt15814", gradleVersion)
163+
val options = defaultBuildOptions().copy(incremental = false)
164+
165+
project.build("assembleDebug", "test", options = options) {
166+
assertSuccessful()
167+
}
168+
}
169+
160170
@Test
161171
fun testAndroidIcepickProject() {
162172
val project = Project("AndroidIcepickProject", gradleVersion)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
4+
android {
5+
compileSdkVersion 23
6+
buildToolsVersion "23.0.1"
7+
defaultConfig {
8+
applicationId "com.example.genclassesnotfound"
9+
minSdkVersion 19
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
sourceSets {
22+
main.java.srcDirs += 'src/main/kotlin'
23+
test.java.srcDirs += 'src/test/kotlin'
24+
}
25+
}
26+
27+
dependencies {
28+
compile fileTree(dir: 'libs', include: ['*.jar'])
29+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
30+
exclude group: 'com.android.support', module: 'support-annotations'
31+
})
32+
testCompile 'junit:junit:4.12'
33+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
34+
kaptTest "com.squareup.dagger:dagger-compiler:$dagger_version"
35+
compile "com.squareup.dagger:dagger:$dagger_version"
36+
}
37+
38+
repositories {
39+
mavenLocal()
40+
mavenCentral()
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/knjohn/.android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.genclassesnotfound">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:label="@string/app_name">
8+
<activity android:name=".MainActivity">
9+
<intent-filter>
10+
<action android:name="android.intent.action.MAIN"/>
11+
<category android:name="android.intent.category.LAUNCHER"/>
12+
</intent-filter>
13+
</activity>
14+
</application>
15+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.genclassesnotfound;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
6+
public class MainActivity extends Activity {
7+
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
setContentView(R.layout.activity_main);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:id="@+id/activity_main"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<TextView
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"/>
11+
12+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">GenClassesNotFound</string>
3+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.genclassesnotfound
2+
3+
import dagger.Module
4+
import dagger.ObjectGraph
5+
import dagger.Provides
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Before
8+
import org.junit.Test
9+
import javax.inject.Inject
10+
11+
class ExampleUnitTest {
12+
13+
lateinit var graph: ObjectGraph
14+
lateinit @Inject var config: Config
15+
16+
@Before
17+
fun setUp() {
18+
graph = ObjectGraph.create(TestModule())
19+
graph.inject(this)
20+
}
21+
22+
@Test
23+
fun addition_isCorrect() {
24+
assertEquals(4 + config.magicNumber, 9)
25+
}
26+
27+
@Module(
28+
injects = arrayOf(ExampleUnitTest::class),
29+
complete = true,
30+
library = true
31+
32+
)
33+
class TestModule {
34+
@Provides fun providesDependency(): Config {
35+
return Config(5)
36+
}
37+
}
38+
39+
data class Config(val magicNumber : Int)
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
ext.dagger_version = '1.2.5'
5+
repositories {
6+
mavenLocal()
7+
jcenter()
8+
}
9+
dependencies {
10+
classpath "com.android.tools.build:gradle:$android_tools_version"
11+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12+
classpath "com.jakewharton.sdkmanager:gradle-plugin:0.12.+"
13+
}
14+
}
15+
16+
allprojects {
17+
repositories {
18+
mavenLocal()
19+
jcenter()
20+
}
21+
}
22+
23+
task clean(type: Delete) {
24+
delete rootProject.buildDir
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.jvmargs=-ea -XX:MaxPermSize=512m
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ':app'

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ internal class Kotlin2JvmSourceSetProcessor(
124124

125125
var kotlinAfterJavaTask: KotlinCompile? = null
126126

127-
if (aptConfiguration.dependencies.size > 1 && !Kapt3GradleSubplugin.isEnabled(project)) {
127+
if (!Kapt3GradleSubplugin.isEnabled(project) && aptConfiguration.allDependencies.size > 1) {
128128
javaTask.dependsOn(aptConfiguration.buildDependencies)
129129

130130
val (aptOutputDir, aptWorkingDir) = project.getAptDirsForSourceSet(sourceSetName)
@@ -388,7 +388,7 @@ internal open class KotlinAndroidPlugin(
388388
for (provider in variantData.sourceProviders) {
389389
val aptConfiguration = aptConfigurations[(provider as AndroidSourceSet).name]
390390
// Ignore if there's only an annotation processor wrapper in dependencies (added by default)
391-
if (aptConfiguration != null && aptConfiguration.dependencies.size > 1) {
391+
if (aptConfiguration != null && aptConfiguration.allDependencies.size > 1) {
392392
javaTask.dependsOn(aptConfiguration.buildDependencies)
393393
aptFiles.addAll(aptConfiguration.resolve())
394394
}
@@ -609,6 +609,7 @@ private fun Project.getAptDirsForSourceSet(sourceSetName: String): Pair<File, Fi
609609
private fun Project.createAptConfiguration(sourceSetName: String, kotlinPluginVersion: String): Configuration {
610610
val aptConfigurationName = Kapt3KotlinGradleSubplugin.getKaptConfigurationName(sourceSetName)
611611

612+
configurations.findByName(aptConfigurationName)?.let { return it }
612613
val aptConfiguration = configurations.create(aptConfigurationName)
613614

614615
// Add base kotlin-annotation-processing artifact for the main kapt configuration,
@@ -617,7 +618,10 @@ private fun Project.createAptConfiguration(sourceSetName: String, kotlinPluginVe
617618
val kotlinAnnotationProcessingDep = "org.jetbrains.kotlin:kotlin-annotation-processing:$kotlinPluginVersion"
618619
aptConfiguration.dependencies.add(dependencies.create(kotlinAnnotationProcessingDep))
619620
} else {
620-
Kapt3KotlinGradleSubplugin.findMainKaptConfiguration(this)?.let { aptConfiguration.extendsFrom(it) }
621+
// "main" configuration can be created after some other. We should handle this case
622+
val mainConfiguration = Kapt3KotlinGradleSubplugin.findMainKaptConfiguration(this)
623+
?: createAptConfiguration("main", kotlinPluginVersion)
624+
aptConfiguration.extendsFrom(mainConfiguration)
621625
}
622626

623627
return aptConfiguration

0 commit comments

Comments
 (0)