Skip to content

Commit

Permalink
Add Gradle repository in third_party/android_deps.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 committed Sep 27, 2021
1 parent bb7e53f commit 9b828fa
Show file tree
Hide file tree
Showing 19 changed files with 582 additions and 6 deletions.
1 change: 1 addition & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ exclude:
- "third_party/cirque/repo/**/*"
- "third_party/nanopb/repo/**/*"
- "src/android/CHIPTool/gradlew" # gradle wrapper generated file
- "third_party/android_deps/gradlew" # gradle wrapper generated file
- "src/controller/python/chip/clusters/CHIPClusters.py" # generated file


Expand Down
4 changes: 4 additions & 0 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def generate(self):
'python3', 'build/chip/java/tests/generate_jars_for_test.py'
], title='Generating JARs for Java build rules test')

self._Execute([
'python3', 'third_party/android_deps/set_up_android_deps.py'
], title='Setting up Android deps through Gradle')

if not os.path.exists(self.output_dir):
# NRF does a in-place update of SDK tools
if not self._runner.dry_run:
Expand Down
12 changes: 12 additions & 0 deletions scripts/build/expected_all_platform_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ west build --cmake-only -d {out}/nrf-nrf5340-pump_controller -b nrf5340dk_nrf534
# Generating JARs for Java build rules test
python3 build/chip/java/tests/generate_jars_for_test.py

# Setting up Android deps through Gradle
python3 third_party/android_deps/set_up_android_deps.py

# Generating android-arm-chip_tool
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"'

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

# Setting up Android deps through Gradle
python3 third_party/android_deps/set_up_android_deps.py

# Generating android-arm64-chip_tool
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"'

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

# Setting up Android deps through Gradle
python3 third_party/android_deps/set_up_android_deps.py

# Generating android-x64-chip_tool
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"'

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

# Setting up Android deps through Gradle
python3 third_party/android_deps/set_up_android_deps.py

# Generating android-x86-chip_tool
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"'

Expand Down
5 changes: 5 additions & 0 deletions scripts/examples/android_app_ide.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ if [ -z "$TARGET_CPU" ]; then
fi

source scripts/activate.sh

# Set up JARs
python3 build/chip/java/tests/generate_jars_for_test.py
python3 third_party/android_deps/set_up_android_deps.py

# Build CMake for Android Studio
echo "build ide"
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
11 changes: 11 additions & 0 deletions src/android/CHIPTool/chip-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id 'com.android.library'
}

apply from: "../../../../third_party/android_deps/android_deps.gradle"

android {
compileSdkVersion 30

Expand Down Expand Up @@ -37,3 +39,12 @@ android {
}
}
}

dependencies {
// Dependencies for CHIP Java code are defined centrally in
// android_deps.gradle. Copy deps from that file into our implementation
// config, instead of maintaining two separate lists of dependencies.
configurations.chipDeps.dependencies.each {
project.dependencies.add("implementation", "${it.group}:${it.name}:${it.version}")
}
}
5 changes: 4 additions & 1 deletion src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ shared_library("jni") {
android_library("java") {
output_name = "CHIPController.jar"

deps = [ ":android" ]
deps = [
":android",
"${chip_root}/third_party/android_deps:annotation",
]

data_deps = [
":jni",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.util.Log;
import androidx.annotation.Nullable;
import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCallback;
import chip.devicecontroller.mdns.ChipMdnsCallback;
import chip.devicecontroller.mdns.ServiceResolver;

/** Controller to interact with the CHIP device. */
public class ChipDeviceController {
private static final String TAG = ChipDeviceController.class.getSimpleName();

private long deviceControllerPtr;
private int connectionId;
private BluetoothGatt bleGatt;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void pairDevice(BluetoothGatt bleServer, long deviceId, long setupPincode
* generated CSR nonce.
*/
public void pairDevice(
BluetoothGatt bleServer, long deviceId, long setupPincode, byte[] csrNonce) {
BluetoothGatt bleServer, long deviceId, long setupPincode, @Nullable byte[] csrNonce) {
if (connectionId == 0) {
bleGatt = bleServer;

Expand All @@ -87,7 +87,12 @@ public void pairDevice(
}

public void pairDeviceWithAddress(
long deviceId, String address, int port, int discriminator, long pinCode, byte[] csrNonce) {
long deviceId,
String address,
int port,
int discriminator,
long pinCode,
@Nullable byte[] csrNonce) {
pairDeviceWithAddress(
deviceControllerPtr, deviceId, address, port, discriminator, pinCode, csrNonce);
}
Expand Down Expand Up @@ -219,7 +224,11 @@ private native long newDeviceController(
KeyValueStoreManager manager, ServiceResolver resolver, ChipMdnsCallback chipMdnsCallback);

private native void pairDevice(
long deviceControllerPtr, long deviceId, int connectionId, long pinCode, byte[] csrNonce);
long deviceControllerPtr,
long deviceId,
int connectionId,
long pinCode,
@Nullable byte[] csrNonce);

private native void pairDeviceWithAddress(
long deviceControllerPtr,
Expand All @@ -228,7 +237,7 @@ private native void pairDeviceWithAddress(
int port,
int discriminator,
long pinCode,
byte[] csrNonce);
@Nullable byte[] csrNonce);

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

Expand Down
5 changes: 5 additions & 0 deletions third_party/android_deps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.gradle/
.settings/
buildSrc/.gradle/
buildSrc/build/
artifacts/
22 changes: 22 additions & 0 deletions third_party/android_deps/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# === This BUILD file is auto-generated, modify the GnBuildGenerator task instead of editing this file manually. ===

import("//build_overrides/chip.gni")
import("${chip_root}/build/chip/java/rules.gni")

java_prebuilt("annotation") {
jar_path = "artifacts/annotation-1.1.0.jar"
}
23 changes: 23 additions & 0 deletions third_party/android_deps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Third party Android dependencies

This directory contains files relevant to specifying Android dependencies,
downloading them, and generating GN build targets from them.

To add a new Android dependency, add a Gradle dependency to the `chipDeps`
configuration in `android_deps.gradle`. Then, run

```shell
./set_up_android_deps.py
```

to download dependencies to `artifacts/`, and generate GN build targets in
`BUILD.gn`.

The application code can then depend on
`${chip_root}/third_party/android_deps:my_dep`. The target name `my_dep` will be
generated based on the Gradle dependency name. For example,
"androidx.annotation:annotation:1.1.0" in `build.gradle` becomes the target name
"annotation" in `BUILD.gn`.

Changes to `BUILD.gn` should be committed to the repository, so developers can
track where dependencies lead. `artifacts/` should not, as it contains binaries.
16 changes: 16 additions & 0 deletions third_party/android_deps/android_deps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This file is kept separate from build.gradle, so that the chipDeps configuration it defines can be shared.

apply plugin: 'base'

repositories {
google()
mavenCentral()
}

configurations {
chipDeps
}

dependencies {
chipDeps "androidx.annotation:annotation:1.1.0"
}
16 changes: 16 additions & 0 deletions third_party/android_deps/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'base'
apply from: 'android_deps.gradle'

task setUpAndroidDeps(type: GnBuildGenerator) {}

task copyArtifacts(type: Copy) {
// Defined in android_deps.gradle
from configurations.chipDeps
into 'artifacts'
}

clean {
delete 'artifacts'
}

setUpAndroidDeps.dependsOn copyArtifacts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.regex.Pattern
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.api.artifacts.ResolvedDependency

/** Task that generates a BUILD.gn file based on dependencies in the chipDeps configuration. */
class GnBuildGenerator extends DefaultTask {
private static final GENERATED_WARNING =
"# === This BUILD file is auto-generated, modify the GnBuildGenerator task instead of editing this file manually. ==="
private static final COPIED_ARTIFACTS_DIR = "artifacts"
private static final ANDROID_DEPS_DIR = "/third_party/android_deps"

@TaskAction
void main() {
def chipDepsConfiguration = project.configurations.chipDeps
def chipRoot = System.getenv("PW_PROJECT_ROOT");
def absoluteAndroidDepsDir = "$chipRoot/$ANDROID_DEPS_DIR";

def buildFile = new File("$absoluteAndroidDepsDir/BUILD.gn");
if (!buildFile.exists()) {
buildFile.createNewFile()
}

def stringBuilder = new StringBuilder()
def copyrightHeader = new File("$absoluteAndroidDepsDir/copyright_header.txt")
stringBuilder.append(copyrightHeader.text + "\n\n")

stringBuilder.append(GENERATED_WARNING + "\n\n")

stringBuilder.append("""\
import("//build_overrides/chip.gni")
import("\${chip_root}/build/chip/java/rules.gni")
""".stripIndent())

def allDeps = new HashSet<ResolvedDependency>()
chipDepsConfiguration.resolvedConfiguration.firstLevelModuleDependencies.each {
allDeps += it
it.children.each {
allDeps += it
}
}

allDeps.each {
def artifact = it.moduleArtifacts[0]
if (it.moduleArtifacts.size() > 1 || artifact.extension != "jar") {
throw new IllegalStateException(
"Multiple top-level module artifacts or unsupported"
+ " artifact extension (artifact count = $it.moduleArtifacts.size(), extension"
+ " = $artifact.extension).")
}

def deps = []
it.children.each {
deps += "\":$it.module.id.name\""
}

stringBuilder.append("\n")
stringBuilder.append("""\
java_prebuilt("$it.module.id.name") {
jar_path = "$COPIED_ARTIFACTS_DIR/${artifact.file.name}"
""".stripIndent())

if (!deps.isEmpty()) {
stringBuilder.append(" deps = $deps\n")
}
stringBuilder.append("}\n")
}

buildFile.write(stringBuilder.toString())
}
}
13 changes: 13 additions & 0 deletions third_party/android_deps/copyright_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 9b828fa

Please sign in to comment.