Android kotlin中配置protobuf

奔跑的吉他手發表於2018-11-18

用kotlin寫了個小玩意,資料打算用protobuf儲存,結果各種Unresolved reference: proto,折騰半天終於搞定。

Android kotlin中配置protobuf

根目錄下build.gradle:

buildscript {
    ext.kotlin_version = '1.2.71'
    ext.grpc_version = '1.15.1'
    repositories {
        mavenCentral()
        google()
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
}

allprojects {
    repositories {
        mavenCentral()
        google()
        jcenter()
        mavenLocal()
    }
}
複製程式碼

app下build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "Your applicationId"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
        textReport true
        textOutput "stdout"
    }
    // Android Studio 3.1 does not automatically pick up '<src_set>/kotlin' as source input
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
    }

    lintOptions {
        // Do not complain about outdated deps, so that this can javax.annotation-api can be same
        // as other projects in this repo. Your project is not required to do this, and can
        // upgrade the dep.
        disable 'GradleDependency'
        // The Android linter does not correctly detect resources used in Kotlin.
        // See:
        //   - https://youtrack.jetbrains.com/issue/KT-7729
        //   - https://youtrack.jetbrains.com/issue/KT-12499
        disable 'UnusedResources'
        textReport true
        textOutput "stdout"
    }

}

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


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'



    /**
     * -------------------------------------------------------------------------------------------------------------------------------------
     * Multi Dex
     * -------------------------------------------------------------------------------------------------------------------------------------
     */
    //noinspection AndroidLintGradleCompatible
    implementation 'com.android.support:multidex:1.0.1'

//    implementation project(':protobuf')


    implementation 'javax.annotation:javax.annotation-api:1.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation "io.grpc:grpc-okhttp:$grpc_version" // CURRENT_GRPC_VERSION
    implementation "io.grpc:grpc-protobuf-lite:$grpc_version" // CURRENT_GRPC_VERSION
    implementation "io.grpc:grpc-stub:$grpc_version" // CURRENT_GRPC_VERSION
}
複製程式碼

相關文章