Android:将Kotlin与Dagger和Protobuf集成

时间:2018-07-03 13:18:27

标签: android kotlin dagger-2 grpc kapt

我有一个现有项目,该项目可以使用Java gRPC(Protobuf)和Dagger2成功构建。我想将Kotlin添加到项目中。

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

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
}

1 个答案:

答案 0 :(得分:0)

我必须将此添加到Android闭包中的应用程序级别gradle文件中。

    private JFrame frame;
    private JFrame newFrame;

    frame = new JFrame();
    frame.setBounds(100, 100, 452, 303);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    newFrame = new JFrame();
    newFrame.setBounds(100, 100, 452, 303);
    newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    newFrame.getContentPane().setLayout(null);