xref: /aosp_15_r20/external/grpc-grpc-java/COMPILING.md (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1Building gRPC-Java
2==================
3
4Building is only necessary if you are making changes to gRPC-Java or testing/using a non-released
5 version (e.g. master HEAD) of gRPC-Java library.
6
7Building requires JDK 8, as our tests use TLS.
8
9grpc-java has a C++ code generation plugin for protoc. Since many Java
10developers don't have C compilers installed and don't need to run or modify the
11codegen, the build can skip it. To skip, create the file
12`<project-root>/gradle.properties` and add `skipCodegen=true`.
13
14Some parts of grpc-java depend on Android. Since many Java developers don't have
15the Android SDK installed and don't need to run or modify the Android
16components, the build can skip it. To skip, create the file
17`<project-root>/gradle.properties` and add `skipAndroid=true`.
18Otherwise, create the file `<project-root>/gradle.properties` and add `android.useAndroidX=true`.
19
20Then, to build, run:
21```
22$ ./gradlew build
23```
24
25To install the artifacts to your Maven local repository for use in your own
26project, run:
27```
28$ ./gradlew publishToMavenLocal
29```
30
31### Notes for IntelliJ
32Building in IntelliJ works best when you import the project as a Gradle project and delegate IDE
33build/run actions to Gradle.
34
35You can find this setting at:
36```Settings -> Build, Execution, Deployment
37      -> Build Tools -> Gradle -> Runner
38      -> Delegate IDE build/run actions to gradle.
39```
40
41How to Build Code Generation Plugin
42-----------------------------------
43This section is only necessary if you are making changes to the code
44generation. Most users only need to use `skipCodegen=true` as discussed above.
45
46### Build Protobuf
47The codegen plugin is C++ code and requires protobuf 21.7 or later.
48
49For Linux, Mac and MinGW:
50```
51$ PROTOBUF_VERSION=21.7
52$ curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz
53$ tar xzf protobuf-all-$PROTOBUF_VERSION.tar.gz
54$ cd protobuf-$PROTOBUF_VERSION
55$ ./configure --disable-shared
56$ make   # You may want to pass -j to make this run faster; see make --help
57$ sudo make install
58```
59
60If you are comfortable with C++ compilation and autotools, you can specify a
61``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in
62``LDFLAGS`` to reference it. The
63environment variables will be used when building grpc-java.
64
65Protobuf installs to ``/usr/local`` by default.
66
67For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/cmake/README.md)
68for how to compile Protobuf. gRPC-java assumes a Release build.
69
70#### Mac
71Some versions of Mac OS X (e.g., 10.10) doesn't have ``/usr/local`` in the
72default search paths for header files and libraries. It will fail the build of
73the codegen. To work around this, you will need to set environment variables:
74```
75$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
76```
77
78### Notes for Visual C++
79
80When building on Windows and VC++, you need to specify project properties for
81Gradle to find protobuf:
82```
83.\gradlew publishToMavenLocal ^
84    -PvcProtobufInclude=C:\path\to\protobuf\src ^
85    -PvcProtobufLibs=C:\path\to\protobuf\vsprojects\Release ^
86    -PtargetArch=x86_32
87```
88
89Since specifying those properties every build is bothersome, you can instead
90create ``<project-root>\gradle.properties`` with contents like:
91```
92vcProtobufInclude=C:\\path\\to\\protobuf\\src
93vcProtobufLibs=C:\\path\\to\\protobuf\\vsprojects\\Release
94targetArch=x86_32
95```
96
97By default, the build script will build the codegen for the same architecture as
98the Java runtime installed on your system. If you are using 64-bit JVM, the
99codegen will be compiled for 64-bit. Since Protobuf is only built for 32-bit by
100default, the `targetArch=x86_32` is necessary.
101
102### Notes for MinGW on Windows
103If you have both MinGW and VC++ installed on Windows, VC++ will be used by
104default. To override this default and use MinGW, add ``-PvcDisable=true``
105to your Gradle command line or add ``vcDisable=true`` to your
106``<project-root>\gradle.properties``.
107
108### Notes for Unsupported Operating Systems
109The build script pulls pre-compiled ``protoc`` from Maven Central by default.
110We have built ``protoc`` binaries for popular systems, but they may not work
111for your system. If ``protoc`` cannot be downloaded or would not run, you can
112use the one that has been built by your own, by adding this property to
113``<project-root>/gradle.properties``:
114```
115protoc=/path/to/protoc
116```
117
118How to install Android SDK
119---------------------------
120This section is only necessary if you are building modules depending on Android
121(e.g., `cronet`). Non-Android users only need to use `skipAndroid=true` as
122discussed above.
123
124### Install via Android Studio (GUI)
125Download and install Android Studio from [Android Developer site](https://developer.android.com/studio).
126You can find the configuration for Android SDK at:
127```
128Preferences -> System Settings -> Android SDK
129```
130Select the version of Android SDK to be installed and click `apply`. The location
131of Android SDK being installed is shown at `Android SDK Location` at the same panel.
132The default is `$HOME/Library/Android/sdk` for Mac OS and `$HOME/Android/Sdk` for Linux.
133You can change this to a custom location.
134
135### Install via Command line tools only
136Go to [Android SDK](https://developer.android.com/studio#command-tools) and
137download the commandlinetools package for your build machine OS. Decide where
138you want the Android SDK to be stored. `$HOME/Library/Android/sdk` is typical on
139Mac OS and `$HOME/Android/Sdk` for Linux.
140
141```sh
142export ANDROID_HOME=$HOME/Android/Sdk # Adjust to your liking
143mkdir $HOME/Android
144mkdir $ANDROID_HOME
145mkdir $ANDROID_HOME/cmdline-tools
146unzip -d $ANDROID_HOME/cmdline-tools DOWNLOADS/commandlinetools-*.zip
147mv $ANDROID_HOME/cmdline-tools/cmdline-tools $ANDROID_HOME/cmdline-tools/latest
148# Android SDK is now ready. Now accept licenses so the build can auto-download packages
149$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses
150
151# Add 'export ANDROID_HOME=$HOME/Android/Sdk' to your .bashrc or equivalent
152```
153