简体   繁体   English

带有 Android Studio 的 Protobuf

[英]Protobuf with Android Studio

I am using protocol buffer with the android gradle plugin to generate the java files.我正在使用带有 android gradle插件的protocol buffer来生成 java 文件。

This is the build.gradle file: I used the protobuf-javalite because my proto files contain Any这是 build.gradle 文件:我使用了protobuf-javalite因为我的 proto 文件包含 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'
}

I have this code in the MainActivity:我在 MainActivity 中有这个代码:

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

When I run the application, it is building but it is crashing当我运行应用程序时,它正在构建但它正在崩溃

 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)

For anyone wondering how it was solved, I added the Any file from Google's protobuf manually in the project.对于想知道它是如何解决的任何人,我在项目中手动添加了来自 Google protobuf 的 Any 文件。

I guess this is a temporary solution until a new update comes out.我想这是一个临时解决方案,直到出现新的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM