我正在开发一个 Flutter 应用程序,它集成了使用 Protobuff 的本机包。我在 Android Studio 和 Mac(Apple Ship)上工作。
在 Windows 上,可以构建应用程序,但由于我在 MacO 上工作,所以无法再构建应用程序。我收到以下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ‘:mypackage:generateDebugProto’.
> Could not resolve all files for configuration ‘:mypackage:protobufToolsLocator_protoc’.
> Could not find protoc-3.9.2-osx-aarch_64.exe (com.google.protobuf:protoc:3.9.2).
Searched in the following locations:
https://jcenter.bintray.com/com/google/protobuf/protoc/3.9.2/protoc-3.9.2-osx-aarch_64.exe
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 888ms
Exception: Gradle task assembleDebug failed with exit code 1
iOS 上的构建也不起作用。
如何解决此错误?您对问题出在哪里有什么想法吗?
我找到了基于以下的解决方案:https://github.com/grpc/grpc-java/issues/7690
我创建了文件:
$HOME/.gradle/gradle.properties
,其中:
protoc_platform=osx-x86_64
我的build.gradle:
protobuf {
// Configure the protoc executable
protoc {
// for apple m1, add protoc_platform=osx-x86_64 in $HOME/.gradle/gradle.properties
if (project.hasProperty('protoc_platform')) {
artifact = "com.google.protobuf:protoc:3.9.2:${protoc_platform}"
} else {
artifact = "com.google.protobuf:protoc:3.9.2"
}
}
plugins {
javalite {
// The codegen for lite comes as a separate artifact
if (project.hasProperty('protoc_platform')) {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0:${protoc_platform}"
} else {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
}
}
}
x86_64
适用于 Intel 和 Apple 芯片
import org.apache.tools.ant.taskdefs.condition.Os
// Compatible with macOS on Apple Silicon
def archSuffix = Os.isFamily(Os.FAMILY_MAC) ? ':osx-x86_64' : ''
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.20.1$archSuffix"
}
plugins {
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0$archSuffix"
}
}
}
它适用于我的 MacOs-m1 pro 芯片:
我创建了文件:$HOME/.gradle/gradle.properties,其中:
protoc_platform=osx-x86_64
并将其添加到您的
build.gradle
(应用程序)
android {
......
sourceSets {
main {
proto {
}
}
}
.......
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
resolutionStrategy.force "com.android.support:support-annotations:$supportLibVersion"
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.0.2:osx-x86_64"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.1.2:osx-x86_64'
}
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0:osx-x86_64"
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
generatedFilesBaseDir = "$projectDir/build/generated"
}
如果您使用 Apple Silicon 芯片并遇到此类错误:
* What went wrong:
Execution failed for task ':generateProto'.
> Could not resolve all files for configuration ':protobufToolsLocator_protoc'.
> Could not find protoc-3.6.1-osx-aarch_64.exe (com.google.protobuf:protoc:3.6.1).
Searched in the following locations:
https://repo.maven.apache.org/maven2/com/google/protobuf/protoc/3.6.1/protoc-3.6.1-osx-aarch_64.exe
只需在工件末尾添加“:osx-x86_64”即可
之前:
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.6.1'
}
}
之后:
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.6.1:osx-x86_64'
}
}