Android開發使用Mac Apple M1 + protobuf時報錯

開篇廢話

Android開發使用Mac Apple M1 + protobuf時報Could not resolve all files for configuration ':app:protobufToolsLocator_protoc'. Could not find protoc-osx-aarch_64.exe

遇到的問題

今天換了MacBook Air筆記本進行開發,非常開心,因為電腦配置很高,但是隨之而來了一個問題,之前好好的代碼怎么跑不起來了,反而報了下面這樣的錯誤。

Execution failed for task ':app:generateDebugProto'.
> Could not resolve all files for configuration ':app: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

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

開始解決

簡單一看,不就是找不到某個庫,下載不下來了嘛,是不是代理的問題,是不是要換一下maven倉庫,倒騰了半天,發現事情并不簡單,其它同事從來沒遇到過這樣的問題,只有我換了新電腦遇到了,而新電腦的CPU是ARM架構的,看來只能去官方和github找找答案了。
github protobuf issues

protoc {
    artifact = if (project.hasProperty("protocPlatform")) {
        "com.google.protobuf:protoc:${protobuf_version}:osx-x86_64"
    } else {
        "com.google.protobuf:protoc:${protobuf_version}"
    }
}

從代碼來猜測,如果我們的CPU是Apple M1的話,我們就使用添加:osx-x86_64后綴,不同的架構下去使用不同的編譯器。

再次遇到問題

我在我的電腦上發現并沒有解決問題,但是當時提問的人應該是解決了問題了,是不是方法更新了,如果小伙伴們用上面的方法就解決了,就不用往下看了。

再次解決問題

最終我從protobuf的使用場景之一的grpc的github找到了解決辦法。
github grpc issues

protobuf {
  protoc {
    if (osdetector.os == "osx") {
      artifact = 'com.google.protobuf:protoc:${protobuf_version}:osx-x86_64'
    } else {
      artifact = 'com.google.protobuf:protoc:${protobuf_version}'
    }
  }
}

用上面的方法,最終解決了我的問題,其核心只不過是判斷是否是osx的方式變了。

寫在最后

如果解決了大家的問題,希望大家可以給我點個贊,您的支持是我創作的最大的動力。

更多內容戳這里(整理好的各種文集)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容