Протобуф с Android Студия - PullRequest
1 голос
/ 19 марта 2020

Я использую protocol buffer с плагином android gradle для генерации файлов java.

Это файл build.gradle: я использовал protobuf-javalite, потому что мои прото-файлы содержат Any

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

android {

    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    configurations {
//        implementation.exclude module:'proto-google-common-protos'
        implementation.exclude module: 'protolite-well-known-types'
        implementation.exclude module: 'protobuf-lite'
    }

    sourceSets {
        main {
            proto {

            }
            java {
                srcDir 'src/main/files/generated'
            }
        }
    }


    defaultConfig {
        applicationId "com.visualizer.proto"
        minSdkVersion 19
        targetSdkVersion 29
        multiDexEnabled true
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }


    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}


protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.8.0'
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option "lite"
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-inappmessaging-display:19.0.3'
    implementation 'com.google.firebase:firebase-analytics:17.2.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.protobuf:protobuf-javalite:3.8.0'
//    implementation "com.google.protobuf:protobuf-java:3.6.0"
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.firebase:firebase-messaging:20.1.2'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.android.gms:play-services-base:17.1.0'
    implementation 'com.google.android.gms:play-services-auth-api-phone:17.4.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.firebase:firebase-auth:19.3.0'
}

У меня есть этот код в MainActivity:

EventOuterClass.Event.newBuilder().setType(EventOuterClass.Event.EventType.forNumber(0)) // product view
        .setCustomerId(123456789)
        .setSourceValue(EventOuterClass.Event.EventSource.ANDROID_VALUE)
        .setEntityId(10003457689)
        .build()

Когда я запускаю приложение, оно строит, но оно падает

 java.lang.UnsupportedOperationException
    at com.hicart.mobile.EventOuterClass$Event.dynamicMethod(EventOuterClass.java:1816)
    at com.google.protobuf.GeneratedMessageLite.dynamicMethod(GeneratedMessageLite.java:336)
    at com.google.protobuf.GeneratedMessageLite.isInitialized(GeneratedMessageLite.java:1514)
    at com.google.protobuf.GeneratedMessageLite.isInitialized(GeneratedMessageLite.java:258)
    at com.google.protobuf.GeneratedMessageLite$Builder.build(GeneratedMessageLite.java:433)
    at com.hicart.mobile.fragments.ProductFragment$onProductReady$1.run(MainActivity.kt:472)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
...