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