如何将Google-protobuf创建的Java代码构建,集成和编译到Android Studio项目中 [英] How do I build, integrate, and compile java code created by google-protobuf into an Android Studio Project

查看:745
本文介绍了如何将Google-protobuf创建的Java代码构建,集成和编译到Android Studio项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一种方法,将Google-protobuf的协议编译器生成的Java代码集成到Android Studio项目中.

I have been trying to find a way to integrate java code, generated by google-protobuf's protoc compiler, into an Android Studio project.

protoc --java_out=.  Navigation.proto

其中Navigation.proto包含:

where Navigation.proto contains:

syntax = "proto3";
option java_generic_services = true;
message Navigation {
    string name = 1;
    string url = 2;
} 

service NavRPC {
    rpc putNavigation(Navigation) returns (Navigation);                                                                                                   
};    

将生成一个Java类(Navigation.java),但该类引用了Android Studio中库列表(应用程序或外部)中不存在的包.

will generate a java class (Navigation.java), but that class references packages that are not present in the list of libraries (application or external) within Android Studio.

我尝试通过Android Studio中项目结构"的依赖项"选项卡从maven Central安装库-但我一直收到以下错误消息:

I tried installing the libraries from maven central through the dependencies tab of Project Structure, in Android Studio - but I keep getting error messages that include:

Error:(116, 79) error: incompatible types: IOException cannot be converted to String
Error:(261, 36) error: cannot find symbol method parseWithIOException(Parser<NavDrawerElement>,InputStream)
Error:(266, 36) error: cannot find symbol method parseWithIOException(Parser<NavDrawerElement>,InputStream,ExtensionRegistryLite)
Error:(269, 36) error: cannot find symbol method parseDelimitedWithIOException(Parser<NavDrawerElement>,InputStream)
Error:(274, 36) error: cannot find symbol method parseDelimitedWithIOException(Parser<NavDrawerElement>,InputStream,ExtensionRegistryLite)
Error:(278, 36) error: cannot find symbol method parseWithIOException(Parser<NavDrawerElement>,CodedInputStream)

对于Java,Android Studio和Maven来说是新手-我不确定为什么会发生错误.这些库显示在外部库列表中,但是我仍然遇到未解决的依赖关系和错误,这些错误似乎表明是相反的.

Being somewhat new to Java, Android Studio, and Maven -- I am not sure why the errors are happening. The libraries show up in the external-libraries list but I keep getting unresolved dependencies and errors that would seem to indicate otherwise.

推荐答案

我发现对我实际有效的一种解决方案是构建google-protobuf jar文件,然后将其添加到我的项目库中.

One solution I found that actually works for me is to build the google-protobuf jar files and then add to my project libraries.

要在MacOS中构建这些jar文件,必须正确安装,配置和设置多种技术/软件包.

To build these jar files in MacOS, there were several technologies/packages that had to be installed, configured, and set up correctly.

在MacOSX中,有一个实用程序(/usr/libexec/java_home)可以 用于生成正确的JAVA_HOME环境变量.

In MacOSX there is a utility (/usr/libexec/java_home) that can be used to generate the correct JAVA_HOME environment variable.

我将.bashrc文件修改为包括:

I modified my .bashrc file to include:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

如果系统上安装的MacOSX版本基于10.6, 那么默认的jdk是1.6版. Java和JDK的新版本 可以从系统preferences/java/java applet安装,但是 安装后,仍然需要将JAVA_HOME设置为point 到您要使用的版本.

If the version of MacOSX installed on your system is based on 10.6, then the default jdk is version 1.6. New versions of java and jdk can be installed from the system preferences/java/java applet, but once installed, it is still necessary to set JAVA_HOME to point to the version you want to use.

这是因为在Mac上可以存在多个Java版本,并且 更新实用程序不会尝试猜测您是否想要 最新版本.

This is because multiple versions of java can exist on a mac, and the update utility doesn't try to guess whether or not you want the latest versions.

我使用MacPorts安装maven3.在过程中 安装maven3之前,我必须更新端口和所有相关的 软件包以消除警告[关于过期 包].

I use MacPorts for the installation of maven3. In the process of installing maven3, I had to update ports and all of the related packages in order to get rid of warnings [regarding out-of-date packages].

我尝试使用'sudo ports'进行更新安装,但保持 遇到问题.所以我最终使用了'sudo bash'.

I tried to do the updates installations with 'sudo ports' but kept running in to problems. So I ended up using 'sudo bash'.

执行的命令:

sudo bash
ports selfupdate
ports install maven3
ports update

google-protobuf

我从Google下载了protobuf-master并构建了协议.

google-protobuf

I downloaded protobuf-master from google and built protoc.

我不知道为什么要构建我想要的jar文件 不是,这是提示安装和更新(以上)的原因

I couldn't figure out why the jar files that I expected to be built weren't, which is what prompted the installation and updates (above)

一旦我安装了正确的jdk并安装了maven,我就可以 进入java目录并安装maven3

Once I had the correct jdk and maven was installed, I was able to change into the java directory and install maven3

执行的命令:

mvn3 install
mvn3 package

我不知道安装和安装包之间的区别/ 但是同时运行它们似乎并没有伤害到任何东西.

I don't know if/what the differences between install and package - but it didn't seem to hurt anything by running both.

我使用了一个简单的.proto文件,其中包含几种消息类型, RPC服务的定义:

I used a simple .proto file with several message types and the definition of an RPC service:

syntax = "proto3";
option java_generic_services = true;
message Navigation {
    string name = 1;
    string url = 2;
};

service NavRPC {
    rpc putMessage(Navigation) returns (Navigation);
}

要编译

protoc --java_out=$PROJECT_DIR/path/to/package/Navigation.java \
       Navigation.proto

Android Studio

为了获得协议缓冲区生成的类进行编译:

Android Studio

In order to get the protocol-buffer generated class to compile:

我将这些文件复制到我的lib目录中:

I copied these files into my lib directory:

protobuf-master/java/core/target/protobuf-java-3.0.0-beta-2.jar
protobuf-master/java/util/target/protobuf-java-util-3.0.0-beta-2.jar

复制这些文件后,我必须输入要添加源文件的软件包的名称.

With those files copied, I had to put the name of the package that the source file was being added to.

我仍然有一些问题(例如如何制作Android) Studio自动全部编译一个.proto文件),但一切正常!

There are still some questions i have (like how to make Android Studio automaticall compile a .proto file), but everything works!

这篇关于如何将Google-protobuf创建的Java代码构建,集成和编译到Android Studio项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆