如何将 'com.google.protobuf' 包含在 android 项目 (gradle) 中作为完整而不是精简?
How do you include 'com.google.protobuf' on android project (gradle) as full but not as lite?
我正在尝试使用 protobuf 生成的代码来解码来自网络的消息。我有一个 .proto 文件,看起来像这样:
syntax = "proto3";
message CanData {
string id = 1; // name
Data data1 = 2;
Data data2 = 3;
}
message Data {
oneof data1 {
int32 int_type = 2; // Magic and other int types
float float_type = 3; // Timestamp and other float types
uint32 unsigned_type = 4; // unsigned types
}
}
当我使用 protoc 命令生成代码时。它成功地工作。但是后来我打开 android 项目,它没有按预期工作。我收到如下所示的错误:
Error:(11, 26) error: cannot find symbol class ExtensionRegistry
Error:(1719, 43) error: package com.google.protobuf.GeneratedMessageV3 does not exist
Error:(1716, 55) error: package com.google.protobuf.Descriptors does not exist
Error:(1724, 43) error: package com.google.protobuf.GeneratedMessageV3 does not exist
Error:(1721, 55) error: package com.google.protobuf.Descriptors does not exist
还有很多错误。但为了简单明了,我将它们放在一边。
然后在互联网上查找,我们遇到了一堆对我们不起作用的解决方案,例如我们在 build.gradle 中尝试做的事情:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
和build.gradle(模块应用)
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 25
defaultConfig {
applicationId "oronos.oronosmobileapp"
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.protobuf:protobuf-lite:3.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
我认为精简版:
com.google.protobuf:protobuf-lite:3.0.0
不是我要找的,但我找不到解决方案。
在这里试试这个
// https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.5.1'
或
compile 'com.google.protobuf:protobuf-java:3.5.1'
你可以简单地 google 它 https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
我正在尝试使用 protobuf 生成的代码来解码来自网络的消息。我有一个 .proto 文件,看起来像这样:
syntax = "proto3";
message CanData {
string id = 1; // name
Data data1 = 2;
Data data2 = 3;
}
message Data {
oneof data1 {
int32 int_type = 2; // Magic and other int types
float float_type = 3; // Timestamp and other float types
uint32 unsigned_type = 4; // unsigned types
}
}
当我使用 protoc 命令生成代码时。它成功地工作。但是后来我打开 android 项目,它没有按预期工作。我收到如下所示的错误:
Error:(11, 26) error: cannot find symbol class ExtensionRegistry
Error:(1719, 43) error: package com.google.protobuf.GeneratedMessageV3 does not exist
Error:(1716, 55) error: package com.google.protobuf.Descriptors does not exist
Error:(1724, 43) error: package com.google.protobuf.GeneratedMessageV3 does not exist
Error:(1721, 55) error: package com.google.protobuf.Descriptors does not exist
还有很多错误。但为了简单明了,我将它们放在一边。
然后在互联网上查找,我们遇到了一堆对我们不起作用的解决方案,例如我们在 build.gradle 中尝试做的事情:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
和build.gradle(模块应用)
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 25
defaultConfig {
applicationId "oronos.oronosmobileapp"
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.protobuf:protobuf-lite:3.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
我认为精简版:
com.google.protobuf:protobuf-lite:3.0.0
不是我要找的,但我找不到解决方案。
在这里试试这个
// https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.5.1'
或
compile 'com.google.protobuf:protobuf-java:3.5.1'
你可以简单地 google 它 https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java