Skip to content

Commit 2664033

Browse files
austinh0pull[bot]
authored andcommitted
[Android] Add Gradle repository that translates to Java GN targets (#9996)
* Add Gradle repository in third_party/android_deps. * Use os.path.join and inline if/else statement
1 parent 73c8190 commit 2664033

File tree

19 files changed

+582
-6
lines changed

19 files changed

+582
-6
lines changed

.restyled.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ exclude:
6666
- "third_party/cirque/repo/**/*"
6767
- "third_party/nanopb/repo/**/*"
6868
- "src/android/CHIPTool/gradlew" # gradle wrapper generated file
69+
- "third_party/android_deps/gradlew" # gradle wrapper generated file
6970
- "src/controller/python/chip/clusters/CHIPClusters.py" # generated file
7071

7172

scripts/build/builders/android.py

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def generate(self):
9191
'python3', 'build/chip/java/tests/generate_jars_for_test.py'
9292
], title='Generating JARs for Java build rules test')
9393

94+
self._Execute([
95+
'python3', 'third_party/android_deps/set_up_android_deps.py'
96+
], title='Setting up Android deps through Gradle')
97+
9498
if not os.path.exists(self.output_dir):
9599
# NRF does a in-place update of SDK tools
96100
if not self._runner.dry_run:

scripts/build/expected_all_platform_commands.txt

+12
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ west build --cmake-only -d {out}/nrf-nrf5340-pump_controller -b nrf5340dk_nrf534
122122
# Generating JARs for Java build rules test
123123
python3 build/chip/java/tests/generate_jars_for_test.py
124124

125+
# Setting up Android deps through Gradle
126+
python3 third_party/android_deps/set_up_android_deps.py
127+
125128
# Generating android-arm-chip_tool
126129
gn gen --check --fail-on-unused-args {out}/android-arm-chip_tool '--args=target_os="android" target_cpu="arm" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning="true"'
127130

@@ -131,6 +134,9 @@ bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null'
131134
# Generating JARs for Java build rules test
132135
python3 build/chip/java/tests/generate_jars_for_test.py
133136

137+
# Setting up Android deps through Gradle
138+
python3 third_party/android_deps/set_up_android_deps.py
139+
134140
# Generating android-arm64-chip_tool
135141
gn gen --check --fail-on-unused-args {out}/android-arm64-chip_tool '--args=target_os="android" target_cpu="arm64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning="true"'
136142

@@ -140,6 +146,9 @@ bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null'
140146
# Generating JARs for Java build rules test
141147
python3 build/chip/java/tests/generate_jars_for_test.py
142148

149+
# Setting up Android deps through Gradle
150+
python3 third_party/android_deps/set_up_android_deps.py
151+
143152
# Generating android-x64-chip_tool
144153
gn gen --check --fail-on-unused-args {out}/android-x64-chip_tool '--args=target_os="android" target_cpu="x64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning="true"'
145154

@@ -149,6 +158,9 @@ bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null'
149158
# Generating JARs for Java build rules test
150159
python3 build/chip/java/tests/generate_jars_for_test.py
151160

161+
# Setting up Android deps through Gradle
162+
python3 third_party/android_deps/set_up_android_deps.py
163+
152164
# Generating android-x86-chip_tool
153165
gn gen --check --fail-on-unused-args {out}/android-x86-chip_tool '--args=target_os="android" target_cpu="x86" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning="true"'
154166

scripts/examples/android_app_ide.sh

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ if [ -z "$TARGET_CPU" ]; then
3636
fi
3737

3838
source scripts/activate.sh
39+
40+
# Set up JARs
41+
python3 build/chip/java/tests/generate_jars_for_test.py
42+
python3 third_party/android_deps/set_up_android_deps.py
43+
3944
# Build CMake for Android Studio
4045
echo "build ide"
4146
gn gen --check --fail-on-unused-args out/"android_$TARGET_CPU" --args="target_os=\"android\" target_cpu=\"$TARGET_CPU\" android_ndk_root=\"$ANDROID_NDK_HOME\" android_sdk_root=\"$ANDROID_HOME\" chip_use_clusters_for_ip_commissioning=\"true\"" --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py

src/android/CHIPTool/chip-library/build.gradle

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ plugins {
22
id 'com.android.library'
33
}
44

5+
apply from: "../../../../third_party/android_deps/android_deps.gradle"
6+
57
android {
68
compileSdkVersion 30
79

@@ -37,3 +39,12 @@ android {
3739
}
3840
}
3941
}
42+
43+
dependencies {
44+
// Dependencies for CHIP Java code are defined centrally in
45+
// android_deps.gradle. Copy deps from that file into our implementation
46+
// config, instead of maintaining two separate lists of dependencies.
47+
configurations.chipDeps.dependencies.each {
48+
project.dependencies.add("implementation", "${it.group}:${it.name}:${it.version}")
49+
}
50+
}

src/controller/java/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ shared_library("jni") {
6060
android_library("java") {
6161
output_name = "CHIPController.jar"
6262

63-
deps = [ ":android" ]
63+
deps = [
64+
":android",
65+
"${chip_root}/third_party/android_deps:annotation",
66+
]
6467

6568
data_deps = [
6669
":jni",

src/controller/java/src/chip/devicecontroller/ChipDeviceController.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import android.bluetooth.BluetoothGatt;
2121
import android.bluetooth.BluetoothGattCallback;
2222
import android.util.Log;
23+
import androidx.annotation.Nullable;
2324
import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCallback;
2425
import chip.devicecontroller.mdns.ChipMdnsCallback;
2526
import chip.devicecontroller.mdns.ServiceResolver;
2627

2728
/** Controller to interact with the CHIP device. */
2829
public class ChipDeviceController {
2930
private static final String TAG = ChipDeviceController.class.getSimpleName();
30-
3131
private long deviceControllerPtr;
3232
private int connectionId;
3333
private BluetoothGatt bleGatt;
@@ -66,7 +66,7 @@ public void pairDevice(BluetoothGatt bleServer, long deviceId, long setupPincode
6666
* generated CSR nonce.
6767
*/
6868
public void pairDevice(
69-
BluetoothGatt bleServer, long deviceId, long setupPincode, byte[] csrNonce) {
69+
BluetoothGatt bleServer, long deviceId, long setupPincode, @Nullable byte[] csrNonce) {
7070
if (connectionId == 0) {
7171
bleGatt = bleServer;
7272

@@ -87,7 +87,12 @@ public void pairDevice(
8787
}
8888

8989
public void pairDeviceWithAddress(
90-
long deviceId, String address, int port, int discriminator, long pinCode, byte[] csrNonce) {
90+
long deviceId,
91+
String address,
92+
int port,
93+
int discriminator,
94+
long pinCode,
95+
@Nullable byte[] csrNonce) {
9196
pairDeviceWithAddress(
9297
deviceControllerPtr, deviceId, address, port, discriminator, pinCode, csrNonce);
9398
}
@@ -225,7 +230,11 @@ private native long newDeviceController(
225230
KeyValueStoreManager manager, ServiceResolver resolver, ChipMdnsCallback chipMdnsCallback);
226231

227232
private native void pairDevice(
228-
long deviceControllerPtr, long deviceId, int connectionId, long pinCode, byte[] csrNonce);
233+
long deviceControllerPtr,
234+
long deviceId,
235+
int connectionId,
236+
long pinCode,
237+
@Nullable byte[] csrNonce);
229238

230239
private native void pairDeviceWithAddress(
231240
long deviceControllerPtr,
@@ -234,7 +243,7 @@ private native void pairDeviceWithAddress(
234243
int port,
235244
int discriminator,
236245
long pinCode,
237-
byte[] csrNonce);
246+
@Nullable byte[] csrNonce);
238247

239248
private native void unpairDevice(long deviceControllerPtr, long deviceId);
240249

third_party/android_deps/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.gradle/
2+
.settings/
3+
buildSrc/.gradle/
4+
buildSrc/build/
5+
artifacts/

third_party/android_deps/BUILD.gn

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2021 Project CHIP Authors
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+
# === This BUILD file is auto-generated, modify the GnBuildGenerator task instead of editing this file manually. ===
16+
17+
import("//build_overrides/chip.gni")
18+
import("${chip_root}/build/chip/java/rules.gni")
19+
20+
java_prebuilt("annotation") {
21+
jar_path = "artifacts/annotation-1.1.0.jar"
22+
}

third_party/android_deps/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Third party Android dependencies
2+
3+
This directory contains files relevant to specifying Android dependencies,
4+
downloading them, and generating GN build targets from them.
5+
6+
To add a new Android dependency, add a Gradle dependency to the `chipDeps`
7+
configuration in `android_deps.gradle`. Then, run
8+
9+
```shell
10+
./set_up_android_deps.py
11+
```
12+
13+
to download dependencies to `artifacts/`, and generate GN build targets in
14+
`BUILD.gn`.
15+
16+
The application code can then depend on
17+
`${chip_root}/third_party/android_deps:my_dep`. The target name `my_dep` will be
18+
generated based on the Gradle dependency name. For example,
19+
"androidx.annotation:annotation:1.1.0" in `build.gradle` becomes the target name
20+
"annotation" in `BUILD.gn`.
21+
22+
Changes to `BUILD.gn` should be committed to the repository, so developers can
23+
track where dependencies lead. `artifacts/` should not, as it contains binaries.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file is kept separate from build.gradle, so that the chipDeps configuration it defines can be shared.
2+
3+
apply plugin: 'base'
4+
5+
repositories {
6+
google()
7+
mavenCentral()
8+
}
9+
10+
configurations {
11+
chipDeps
12+
}
13+
14+
dependencies {
15+
chipDeps "androidx.annotation:annotation:1.1.0"
16+
}

third_party/android_deps/build.gradle

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apply plugin: 'base'
2+
apply from: 'android_deps.gradle'
3+
4+
task setUpAndroidDeps(type: GnBuildGenerator) {}
5+
6+
task copyArtifacts(type: Copy) {
7+
// Defined in android_deps.gradle
8+
from configurations.chipDeps
9+
into 'artifacts'
10+
}
11+
12+
clean {
13+
delete 'artifacts'
14+
}
15+
16+
setUpAndroidDeps.dependsOn copyArtifacts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.regex.Pattern
2+
import org.gradle.api.DefaultTask
3+
import org.gradle.api.tasks.TaskAction
4+
import org.gradle.api.artifacts.ResolvedDependency
5+
6+
/** Task that generates a BUILD.gn file based on dependencies in the chipDeps configuration. */
7+
class GnBuildGenerator extends DefaultTask {
8+
private static final GENERATED_WARNING =
9+
"# === This BUILD file is auto-generated, modify the GnBuildGenerator task instead of editing this file manually. ==="
10+
private static final COPIED_ARTIFACTS_DIR = "artifacts"
11+
private static final ANDROID_DEPS_DIR = "/third_party/android_deps"
12+
13+
@TaskAction
14+
void main() {
15+
def chipDepsConfiguration = project.configurations.chipDeps
16+
def chipRoot = System.getenv("PW_PROJECT_ROOT");
17+
def absoluteAndroidDepsDir = "$chipRoot/$ANDROID_DEPS_DIR";
18+
19+
def buildFile = new File("$absoluteAndroidDepsDir/BUILD.gn");
20+
if (!buildFile.exists()) {
21+
buildFile.createNewFile()
22+
}
23+
24+
def stringBuilder = new StringBuilder()
25+
def copyrightHeader = new File("$absoluteAndroidDepsDir/copyright_header.txt")
26+
stringBuilder.append(copyrightHeader.text + "\n\n")
27+
28+
stringBuilder.append(GENERATED_WARNING + "\n\n")
29+
30+
stringBuilder.append("""\
31+
import("//build_overrides/chip.gni")
32+
import("\${chip_root}/build/chip/java/rules.gni")
33+
""".stripIndent())
34+
35+
def allDeps = new HashSet<ResolvedDependency>()
36+
chipDepsConfiguration.resolvedConfiguration.firstLevelModuleDependencies.each {
37+
allDeps += it
38+
it.children.each {
39+
allDeps += it
40+
}
41+
}
42+
43+
allDeps.each {
44+
def artifact = it.moduleArtifacts[0]
45+
if (it.moduleArtifacts.size() > 1 || artifact.extension != "jar") {
46+
throw new IllegalStateException(
47+
"Multiple top-level module artifacts or unsupported"
48+
+ " artifact extension (artifact count = $it.moduleArtifacts.size(), extension"
49+
+ " = $artifact.extension).")
50+
}
51+
52+
def deps = []
53+
it.children.each {
54+
deps += "\":$it.module.id.name\""
55+
}
56+
57+
stringBuilder.append("\n")
58+
stringBuilder.append("""\
59+
java_prebuilt("$it.module.id.name") {
60+
jar_path = "$COPIED_ARTIFACTS_DIR/${artifact.file.name}"
61+
""".stripIndent())
62+
63+
if (!deps.isEmpty()) {
64+
stringBuilder.append(" deps = $deps\n")
65+
}
66+
stringBuilder.append("}\n")
67+
}
68+
69+
buildFile.write(stringBuilder.toString())
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2021 Project CHIP Authors
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.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)