Skip to content

Commit ecebfc5

Browse files
committed
Add open source build logic for Firestore Android
Adds the gradle logic, adjusts the CMake dependencies, and adds resource file generation, for Android builds of Firestore. Remove Nullable annotation, since that is not present in all the versions of the Android SDK that the open source repo can support. PiperOrigin-RevId: 300221250
1 parent 8fc85b8 commit ecebfc5

File tree

7 files changed

+209
-35
lines changed

7 files changed

+209
-35
lines changed

Android/firebase_dependencies.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def firebaseDependenciesMap = [
2323
'auth' : ['com.google.firebase:firebase-auth:19.2.0'],
2424
'database' : ['com.google.firebase:firebase-database:19.2.1'],
2525
'dynamic_links' : ['com.google.firebase:firebase-dynamic-links:19.1.0'],
26+
'firestore' : ['com.google.firebase:firebase-firestore:21.4.0'],
2627
'functions' : ['com.google.firebase:firebase-functions:19.0.2'],
2728
'instance_id' : ['com.google.firebase:firebase-iid:20.0.2'],
2829
'invites' : ['com.google.firebase:firebase-invites:17.0.0'],
@@ -42,6 +43,7 @@ def firebaseResourceDependenciesMap = [
4243
'admob' : [':admob:admob_resources'],
4344
'auth' : [':auth:auth_resources'],
4445
'database' : [':database:database_resources'],
46+
'firestore' : [':firestore:firestore_resources'],
4547
'storage' : [':storage:storage_resources']
4648
]
4749

@@ -74,6 +76,9 @@ class Dependencies {
7476
def getDynamicLinks() {
7577
libSet.add('dynamic_links')
7678
}
79+
def getFirestore() {
80+
libSet.add('firestore')
81+
}
7782
def getFunctions() {
7883
libSet.add('functions')
7984
}

CMakeLists.txt

+35-32
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,29 @@ if(FIREBASE_CPP_USE_PRIOR_GRADLE_BUILD)
120120
"${CMAKE_CURRENT_LIST_DIR}/[^/]+/(.*)"
121121
"${CMAKE_CURRENT_LIST_DIR}/app/\\1"
122122
APP_BINARY_DIR "${FIREBASE_BINARY_DIR}")
123+
124+
set(FIRESTORE_SOURCE_DIR ${APP_BINARY_DIR}/external/src/firestore)
123125
else()
124126
# Run the CMake build logic that will download all the external dependencies.
125127
message(STATUS "Downloading external project dependencies...")
126128
download_external_sources()
127129
message(STATUS "Download complete.")
130+
endif()
128131

129-
# Include Firestore's external build early to resolve conflicts on packages.
130-
if(FIREBASE_INCLUDE_FIRESTORE)
131-
set(FIRESTORE_SOURCE_DIR ${FIREBASE_BINARY_DIR}/external/src/firestore)
132-
set(FIRESTORE_BINARY_DIR ${FIRESTORE_SOURCE_DIR}-build)
132+
# Include Firestore's external build early to resolve conflicts on packages.
133+
if(NOT ANDROID AND FIREBASE_INCLUDE_FIRESTORE)
134+
set(FIRESTORE_SOURCE_DIR ${FIREBASE_BINARY_DIR}/external/src/firestore)
135+
set(FIRESTORE_BINARY_DIR ${FIRESTORE_SOURCE_DIR}-build)
133136

134-
set(
135-
FIREBASE_IOS_BUILD_TESTS
136-
${FIREBASE_CPP_BUILD_TESTS}
137-
CACHE BOOL "Force Firestore build tests to match"
138-
)
137+
set(
138+
FIREBASE_IOS_BUILD_TESTS
139+
${FIREBASE_CPP_BUILD_TESTS}
140+
CACHE BOOL "Force Firestore build tests to match"
141+
)
139142

140-
add_subdirectory(${FIRESTORE_SOURCE_DIR} ${FIRESTORE_BINARY_DIR})
143+
add_subdirectory(${FIRESTORE_SOURCE_DIR} ${FIRESTORE_BINARY_DIR})
141144

142-
copy_subdirectory_definition(${FIRESTORE_SOURCE_DIR} NANOPB_SOURCE_DIR)
143-
endif()
145+
copy_subdirectory_definition(${FIRESTORE_SOURCE_DIR} NANOPB_SOURCE_DIR)
144146
endif()
145147

146148
# Disable the Flatbuffer build tests, install and flathash
@@ -252,28 +254,29 @@ if (NOT ANDROID AND NOT IOS)
252254
endif()
253255
endif()
254256

255-
if(FIREBASE_INCLUDE_FIRESTORE)
256-
# The Firestore build includes protobuf and nanopb already
257-
list(APPEND CMAKE_MODULE_PATH ${NANOPB_SOURCE_DIR}/extra)
258-
find_package(Nanopb REQUIRED)
259-
260-
set(PROTOBUF_FOUND ON)
261-
262-
else()
263-
find_package(Protobuf)
264-
if (PROTOBUF_FOUND)
265-
# NanoPB requires Protobuf to be present, so only add it if it was found.
266-
add_external_library(nanopb)
267-
268-
# NanoPB has a FindNanopb which defines the function to generate files, so
269-
# add it to the module path, and use that.
257+
if(NOT ANDROID)
258+
if(FIREBASE_INCLUDE_FIRESTORE)
259+
# The Firestore build includes protobuf and nanopb already
270260
list(APPEND CMAKE_MODULE_PATH ${NANOPB_SOURCE_DIR}/extra)
271-
find_package(Nanopb)
261+
find_package(Nanopb REQUIRED)
272262

273-
target_compile_definitions(
274-
protobuf-nanopb-static
275-
PUBLIC -DPB_FIELD_32BIT -DPB_ENABLE_MALLOC
276-
)
263+
set(PROTOBUF_FOUND ON)
264+
else()
265+
find_package(Protobuf)
266+
if (PROTOBUF_FOUND)
267+
# NanoPB requires Protobuf to be present, so only add it if it was found.
268+
add_external_library(nanopb)
269+
270+
# NanoPB has a FindNanopb which defines the function to generate files,
271+
# so add it to the module path, and use that.
272+
list(APPEND CMAKE_MODULE_PATH ${NANOPB_SOURCE_DIR}/extra)
273+
find_package(Nanopb)
274+
275+
target_compile_definitions(
276+
protobuf-nanopb-static
277+
PUBLIC -DPB_FIELD_32BIT -DPB_ENABLE_MALLOC
278+
)
279+
endif()
277280
endif()
278281
endif()
279282

cmake/external_rules.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function(download_external_sources)
5151
WORKING_DIRECTORY ${FIRESTORE_BINARY_DIR}
5252
)
5353

54-
set(NANOPB_SRC_DIR ${FIRESTORE_BINARY_DIR}/external/src/nanopb)
54+
set(NANOPB_SOURCE_DIR ${FIRESTORE_BINARY_DIR}/external/src/nanopb)
5555
endif()
5656

5757
# Set variables to indicate if local versions of third party libraries should

firestore/CMakeLists.txt

+20-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ set(common_SRCS
4444
src/common/write_batch.cc
4545
${settings_apple_SRCS})
4646

47+
# Define the resource build needed for Android
48+
firebase_cpp_gradle(":firestore:firestore_resources:generateDexJarRelease"
49+
"${CMAKE_CURRENT_LIST_DIR}/firestore_resources/build/dexed.jar")
50+
binary_to_array("firestore_resources"
51+
"${CMAKE_CURRENT_LIST_DIR}/firestore_resources/build/dexed.jar"
52+
"firebase_firestore"
53+
"${FIREBASE_GEN_FILE_DIR}/firestore")
54+
4755
set(android_SRCS
56+
${firestore_resources_source}
4857
src/android/blob_android.cc
4958
src/android/blob_android.h
5059
src/android/collection_reference_android.cc
@@ -164,16 +173,21 @@ add_library(firebase_firestore STATIC
164173

165174
set_property(TARGET firebase_firestore PROPERTY FOLDER "Firebase Cpp")
166175

176+
if(NOT ANDROID)
177+
set(firestore_api_dep firebase_firestore_api)
178+
else()
179+
set(firestore_api_dep)
180+
endif()
181+
167182
# Set up the dependency on Firebase App.
168183
target_link_libraries(firebase_firestore
169184
PUBLIC
170185
firebase_app
171186
firebase_auth
172187
PRIVATE
173-
firebase_firestore_api
188+
${firestore_api_dep}
174189
)
175190

176-
177191
# Public headers all refer to each other relative to the src/include directory,
178192
# while private headers are relative to the entire C++ SDK directory.
179193
target_include_directories(firebase_firestore
@@ -183,6 +197,10 @@ target_include_directories(firebase_firestore
183197
PRIVATE
184198
${FIREBASE_CPP_SDK_ROOT_DIR}
185199
)
200+
target_compile_definitions(firebase_firestore
201+
PRIVATE
202+
-DINTERNAL_EXPERIMENTAL=1
203+
)
186204

187205
if(ANDROID)
188206
firebase_cpp_proguard_file(firestore)

firestore/build.gradle

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
buildscript {
16+
repositories {
17+
google()
18+
jcenter()
19+
}
20+
dependencies {
21+
classpath 'com.android.tools.build:gradle:3.2.1'
22+
}
23+
}
24+
allprojects {
25+
repositories {
26+
google()
27+
jcenter()
28+
}
29+
}
30+
31+
apply plugin: 'com.android.library'
32+
33+
android {
34+
compileSdkVersion 28
35+
buildToolsVersion '28.0.3'
36+
37+
sourceSets {
38+
main {
39+
manifest.srcFile '../android_build_files/AndroidManifest.xml'
40+
}
41+
}
42+
43+
externalNativeBuild {
44+
cmake {
45+
path '../CMakeLists.txt'
46+
}
47+
}
48+
49+
defaultConfig {
50+
// Jelly Bean is the minimum supported version needed by Firebase.
51+
minSdkVersion 16
52+
targetSdkVersion 28
53+
versionCode 1
54+
versionName "1.0"
55+
56+
buildTypes {
57+
release {
58+
minifyEnabled false
59+
}
60+
}
61+
62+
externalNativeBuild {
63+
cmake {
64+
targets 'firebase_firestore'
65+
// Args are: Re-use app library prebuilt by app gradle project.
66+
// Don't configure all the cmake subprojects.
67+
// Only include needed project.
68+
arguments '-DFIREBASE_CPP_USE_PRIOR_GRADLE_BUILD=ON',
69+
'-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF',
70+
'-DFIREBASE_INCLUDE_FIRESTORE=ON'
71+
}
72+
}
73+
}
74+
75+
lintOptions {
76+
abortOnError false
77+
}
78+
}
79+
80+
dependencies {
81+
implementation project(':app')
82+
}
83+
apply from: "$rootDir/android_build_files/extract_and_dex.gradle"
84+
apply from: "$rootDir/android_build_files/generate_proguard.gradle"
85+
project.afterEvaluate {
86+
generateProguardFile('firestore')
87+
setupDexDependencies(':firestore:firestore_resources')
88+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
buildscript {
16+
repositories {
17+
google()
18+
jcenter()
19+
}
20+
dependencies {
21+
classpath 'com.android.tools.build:gradle:3.2.1'
22+
classpath 'com.google.gms:google-services:4.2.0'
23+
}
24+
}
25+
allprojects {
26+
repositories {
27+
google()
28+
jcenter()
29+
}
30+
}
31+
32+
apply plugin: 'com.android.library'
33+
34+
android {
35+
compileSdkVersion 28
36+
37+
sourceSets {
38+
main {
39+
manifest.srcFile '../../android_build_files/AndroidManifest.xml'
40+
java {
41+
srcDirs = ['../src_java/com/google/firebase/firestore/internal/cpp']
42+
exclude 'Dummy.java'
43+
}
44+
}
45+
}
46+
}
47+
48+
dependencies {
49+
implementation 'com.google.firebase:firebase-analytics:17.2.2'
50+
implementation 'com.google.firebase:firebase-firestore:21.4.1'
51+
}
52+
53+
afterEvaluate {
54+
generateReleaseBuildConfig.enabled = false
55+
}
56+
57+
apply from: "$rootDir/android_build_files/extract_and_dex.gradle"
58+
extractAndDexAarFile('firestore_resources')

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ include ':app',
1111
':database',
1212
':database:database_resources',
1313
':dynamic_links',
14+
':firestore',
15+
':firestore:firestore_resources',
1416
':functions',
1517
':instance_id',
1618
':messaging',

0 commit comments

Comments
 (0)