0

我有使用 Java、gRPC (Protobuf) 和 Dagger2 成功构建的现有项目。我想将 Kotlin 添加到项目中。

我已经很容易地将数据文件转换为 Kotlin。当我转换第一个活动(包含 Dagger2)时,构建失败。添加apply plugin: 'kotlin-kapt'并按下 Sync Now 后,它说

Folder C:\AndroidStudioProjects\Bingo\app\build\generated\source\kaptKotlin\debug


Folder C:\AndroidStudioProjects\Bingo\app\build\generated\source\kaptKotlin\release


3rd-party Gradle plug-ins may be the cause

构建仍然失败,现在它也无法识别 protobuf 生成的文件。

有没有人一起尝试过 Kotlin、Dagger、Protobuf?我不想用 Kotlin 完全取代 Java。

这是我的模块级 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.0'
    defaultConfig {
        applicationId "com.productions.bingo"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }

    androidExtensions {
        experimental = true
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.4.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.8.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
    main.java.srcDirs += "${protobuf.generatedFilesBaseDir}/main/javalite"
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // Butterknife
    implementation 'com.jakewharton:butterknife:8.8.1'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'

    // Support Libraries
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
    implementation 'com.android.support:support-v4:28.0.0-alpha1'
    implementation 'com.android.support:preference-v7:28.0.0-alpha1'
    implementation "com.android.support:support-core-utils:28.0.0-alpha1"
    implementation 'com.android.support:cardview-v7:28.0.0-alpha1'
    implementation 'com.android.support:customtabs:28.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'

    // gRPC
    implementation 'io.grpc:grpc-okhttp:1.11.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.11.0'
    implementation 'io.grpc:grpc-stub:1.11.0'
    implementation 'javax.annotation:javax.annotation-api:1.2'

    //Dagger
    implementation 'com.google.dagger:dagger:2.13'
    kapt "com.google.dagger:dagger-compiler:2.13"

    implementation 'com.google.code.gson:gson:2.8.5'

    // Celebration
    implementation 'nl.dionsegijn:konfetti:1.1.2'
    implementation 'com.getkeepsafe.taptargetview:taptargetview:1.11.0'
    implementation 'com.facebook.shimmer:shimmer:0.2.0'

    // Testing-only dependencies
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support:support-annotations:27.1.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    testImplementation 'junit:junit:4.12'

    // Kotlin libraries
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.anko:anko:0.10.5"
}

这是我的项目 gradle 文件:

buildscript {
    ext.kotlin_version = '1.2.50'
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath 'com.google.gms:google-services:3.1.1'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
4

1 回答 1

0

我必须在 android 闭包中将它添加到我的应用程序级 gradle 文件中。

sourceSets {
        debug.java.srcDirs += 'build/generated/source/proto/debug/java'
        debug.java.srcDirs += 'build/generated/source/proto/debug/grpc'
        debug.java.srcDirs += 'build/generated/source/proto/debug/javalite'
        release.java.srcDirs += 'build/generated/source/proto/release/java'
        release.java.srcDirs += 'build/generated/source/proto/release/grpc'
        release.java.srcDirs += 'build/generated/source/proto/release/javalite'
        main.proto.srcDirs += 'src/main/proto'
        main.kotlin.srcDirs 'build/generated/source/proto'
    }
于 2018-07-09T02:18:45.100 回答