Charles Thomas
Charles Thomas

Reputation: 1005

How do I build, integrate, and compile java code created by google-protobuf into an Android Studio Project

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

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);                                                                                                   
};    

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.

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)

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.

Upvotes: 3

Views: 10430

Answers (1)

Charles Thomas
Charles Thomas

Reputation: 1005

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

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

JDK 1.8

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

I modified my .bashrc file to include:

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

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.

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.

Maven 3.3.8

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].

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

The commands executed:

sudo bash
ports selfupdate
ports install maven3
ports update

google-protobuf

I downloaded protobuf-master from google and built protoc.

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)

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

The commands executed:

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.

Compiling the .proto files

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);
}

To compile

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

Android Studio

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

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.

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

Upvotes: 4

Related Questions