Skip to content

Commit cf90640

Browse files
authored
Update Android edge app and documentation (#3651)
Updates Android edge app and documentation. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Quick tests passed locally by running `./runtest.sh`. - [ ] In-line docstrings updated. - [ ] Documentation updated.
1 parent 2e57a3b commit cf90640

File tree

5 files changed

+242
-146
lines changed

5 files changed

+242
-146
lines changed
Lines changed: 156 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,192 @@
1-
# ExecuTorch Libraries
1+
# NVFlare Android App with ExecuTorch
22

3-
This directory should contain the ExecuTorch Android libraries (.aar files) that are required for the NVFlare Android app.
3+
This guide will help you set up and build the NVFlare Android application that uses ExecuTorch for federated learning on mobile devices.
44

5-
## Required Libraries
5+
## 🎯 What You'll Build
66

7-
Based on the CIFAR-10 ExecuTorch example, you need to add the following libraries:
7+
A complete Android application that:
8+
- Runs federated learning using ExecuTorch
9+
- Trains CIFAR-10 models on Android devices
10+
- Integrates with the NVFlare federated learning framework
11+
- Supports on-device training and model updates
812

9-
1. **executorch.aar** - Main ExecuTorch library
10-
2. **executorch_training.aar** - ExecuTorch training library (if separate)
13+
## 📋 Prerequisites
1114

12-
## How to Obtain
15+
Before you begin, ensure you have the following installed:
1316

14-
1. **From ExecuTorch Release**: Download the latest ExecuTorch Android libraries from the official releases
15-
2. **From CIFAR-10 Example**: Copy the libraries from the working CIFAR-10 example project
16-
3. **Build from Source**: Build ExecuTorch from source and extract the Android libraries
17+
### Required Software
18+
- **Java Development Kit (JDK)**: OpenJDK 17 or later
19+
- **Android SDK**: API level 29+ with latest build tools
20+
- **Android NDK**: Version 29.0.13599879 or compatible
21+
- **Python**: 3.10+ (for ExecuTorch build scripts)
22+
- **Git**: For repository management
23+
- **CMake**: For building native components
1724

18-
## Installation Steps
25+
### Quick Installation
26+
```bash
27+
# macOS with Homebrew
28+
brew install openjdk@17
29+
brew install cmake
1930

20-
1. Download the required .aar files
21-
2. Place them in this `libs/` directory
22-
3. The build.gradle.kts is already configured to include all .aar files from this directory
31+
# Android SDK/NDK via Android Studio SDK Manager
32+
# Or download from: https://developer.android.com/studio
33+
```
2334

24-
## Note
35+
## 🚀 Quick Start
2536

26-
The build.gradle.kts file uses:
27-
```kotlin
28-
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
37+
Follow these steps to get your NVFlare Android app running:
38+
39+
### Step 1: Set Up Environment Variables
40+
41+
```bash
42+
# Set your Android development environment
43+
export JAVA_HOME=/opt/homebrew/Cellar/openjdk@17/17.0.15/libexec/openjdk.jdk/Contents/Home
44+
export ANDROID_HOME=/Users/$(whoami)/Library/Android/sdk
45+
export ANDROID_NDK=/Users/$(whoami)/Library/Android/sdk/ndk/29.0.13599879
46+
export ANDROID_SDK=/Users/$(whoami)/Library/Android/sdk
47+
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
2948
```
3049

31-
This will automatically include all .jar and .aar files in this directory.
50+
> **Note**: Adjust the paths above to match your system configuration.
3251
33-
## Current Status
52+
### Step 2: Build ExecuTorch Libraries
3453

35-
⚠️ **Libraries not yet added** - You need to add the ExecuTorch libraries before the app will compile and run successfully.
54+
```bash
55+
# Create Python environment
56+
python3.12 -m venv androidexecutorchenv
57+
source androidexecutorchenv/bin/activate
3658

37-
# Clone ExecuTorch repository
59+
# Clone and build ExecuTorch
3860
git clone https://github.com/pytorch/executorch.git --recurse-submodules
3961
cd executorch
4062

41-
# Setup Python environment (optional but recommended)
42-
uv venv --seed --prompt et --python 3.10
43-
source .venv/bin/activate
63+
# Update and install dependencies (clean if you have attempted installing previously)
64+
git pull
65+
./install_executorch.sh --clean
66+
git submodule sync --recursive
67+
git submodule update --init --recursive
4468

45-
# Install build tools and ExecuTorch pip wheel
46-
./install_executorch.sh
69+
# Install with training extensions
70+
CMAKE_ARGS="-DEXECUTORCH_BUILD_EXTENSION_TRAINING=ON,-DEXECUTORCH_BUILD_PYBIND=ON" ./install_executorch.sh
4771

48-
# Build ExecuTorch for Android
49-
./scripts/build_android_library.sh
72+
# Build Android library
73+
EXECUTORCH_BUILD_EXTENSION_LLM=OFF ./scripts/build_android_library.sh
74+
```
5075

51-
# Create libs directory in NVFlare Android app
52-
mkdir -p nvflare/edge/device/android/app/libs
76+
### Step 3: Copy ExecuTorch Libraries
5377

54-
# Copy the ExecuTorch AAR - make sure the app directory is what you are actually using
55-
cp ./extension/android/executorch_android/build/outputs/aar/executorch_android-debug.aar nvflare/edge/device/android/app/libs/executorch.aar
78+
```bash
79+
# Create libs directory
80+
mkdir -p /path/to/NVFlare/nvflare/edge/device/android/app/libs
5681

57-
Copy nvflare/edge/ios/NVFlareMobile/NVFlareMobile/Assets.xcassets/cifar10/data_batch_1.dataset/data_batch_1.bin to nvflare/edge/device/android/app/src/main/assets/data_batch_1.bin
82+
# Copy the ExecuTorch AAR file
83+
cp ./extension/android/executorch_android/build/outputs/aar/executorch_android-debug.aar \
84+
/path/to/NVFlare/nvflare/edge/device/android/app/libs/executorch.aar
85+
```
5886

59-
## ⚠️ Important: SDK Copy Requirement
87+
### Step 4: Set Up NVFlare SDK
6088

61-
**Critical Setup Step**: The NVFlare Android SDK contents must be copied to the app's source directory for the app to build and function correctly.
89+
**Critical**: The NVFlare Android SDK must be copied to the app's source directory.
6290

63-
### SDK Source Location
64-
The SDK source code is located at: `nvflare/edge/device/android/sdk/`
91+
```bash
92+
# Copy SDK to app source directory
93+
cp -r nvflare/edge/device/android/sdk \
94+
nvflare/edge/device/android/app/src/main/java/com/nvidia/nvflare/
95+
```
96+
97+
### Step 5: Add Training Data
98+
99+
```bash
100+
# Copy CIFAR-10 dataset
101+
cp nvflare/edge/ios/NVFlareMobile/NVFlareMobile/Assets.xcassets/cifar10/data_batch_1.dataset/data_batch_1.bin \
102+
nvflare/edge/device/android/app/src/main/assets/data_batch_1.bin
103+
```
65104

66-
This directory contains:
67-
- `core/` - Core SDK functionality
68-
- `training/` - Training-related components
69-
- `utils/` - Utility functions
70-
- `models/` - Model definitions
71-
- `config/` - Configuration files
72-
- `ETTrainerExecutor.kt` - Main training executor
105+
### Step 6: Build and Run
73106

74-
### Required Action: Copy SDK to App
75-
**You must copy the contents of the SDK directory to the app's source directory:**
107+
Open the project in Android Studio and build the app, or use the command line:
76108

77109
```bash
78-
# Copy the SDK directory to the proper package structure
79-
cp -r nvflare/edge/device/android/sdk nvflare/edge/device/android/app/src/main/java/com/nvidia/nvflare
110+
cd nvflare/edge/device/android
111+
./gradlew assembleDebug
112+
```
113+
114+
## 📁 Project Structure
115+
116+
```
117+
nvflare/edge/device/android/
118+
├── app/
119+
│ ├── libs/ # ExecuTorch libraries (.aar files)
120+
│ ├── src/main/
121+
│ │ ├── assets/ # Training data (CIFAR-10)
122+
│ │ └── java/com/nvidia/nvflare/
123+
│ │ ├── app/ # Main application code
124+
│ │ └── sdk/ # NVFlare Android SDK (copied from sdk/)
125+
├── sdk/ # NVFlare Android SDK source
126+
│ ├── core/ # Core SDK functionality
127+
│ ├── training/ # Training components
128+
│ ├── utils/ # Utility functions
129+
│ └── models/ # Model definitions
130+
└── README.md # This file
80131
```
81132

82-
### Why This Is Required
83-
The app code in `nvflare/edge/device/android/app/src/main/java/` does not contain the SDK code. The SDK is maintained separately in the `sdk/` directory, but its contents must be copied to the app's source directory during the build process.
133+
## 🔧 Configuration
134+
135+
### Build Configuration
136+
137+
The `build.gradle.kts` file is pre-configured to automatically include all `.aar` files from the `libs/` directory:
138+
139+
```kotlin
140+
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
141+
```
142+
143+
### Environment Setup
144+
145+
Make sure your `local.properties` file contains the correct Android SDK path:
146+
147+
```properties
148+
sdk.dir=/Users/yourusername/Library/Android/sdk
149+
```
150+
151+
## 🐛 Troubleshooting
152+
153+
### Common Issues
154+
155+
**Build fails with missing classes**
156+
- Ensure the SDK has been copied to the app's source directory (Step 4)
157+
- Verify all required `.aar` files are in the `libs/` directory
158+
159+
**ExecuTorch build fails**
160+
- Check that all environment variables are set correctly
161+
- Ensure you have sufficient disk space (build requires ~10GB)
162+
- Try building with `EXECUTORCH_BUILD_EXTENSION_LLM=OFF` if LLM extensions cause issues
163+
164+
**App crashes on startup**
165+
- Verify the CIFAR-10 dataset is in the correct location
166+
- Check that all required permissions are granted in `AndroidManifest.xml`
167+
168+
### Getting Help
169+
170+
1. Check the [NVFlare documentation](https://nvflare.readthedocs.io/)
171+
2. Review the [ExecuTorch Android examples](https://github.com/pytorch/executorch/tree/main/examples)
172+
3. Open an issue in the NVFlare repository for Android-specific problems
173+
174+
## 📚 Additional Resources
175+
176+
- [NVFlare Documentation](https://nvflare.readthedocs.io/)
177+
- [ExecuTorch Documentation](https://pytorch.org/executorch/)
178+
- [Android Development Guide](https://developer.android.com/guide)
179+
- [Federated Learning Concepts](https://nvflare.readthedocs.io/en/latest/fl_introduction.html)
180+
181+
## 🎉 Next Steps
84182

85-
### What Happens If SDK Is Not Copied
86-
If the SDK contents are not copied to the app's source directory:
87-
- The app will fail to compile with missing class errors
88-
- Runtime errors will occur when trying to access SDK functionality
89-
- Training operations will fail with `TrainingError.DATASET_CREATION_FAILED` or similar errors
183+
Once your app is running:
90184

91-
### Verification
92-
To ensure the SDK is properly set up:
93-
1. Verify the `sdk/` directory exists at `nvflare/edge/device/android/sdk/`
94-
2. Copy the SDK directory to `nvflare/edge/device/android/app/src/main/java/com/nvidia/nvflare`
95-
3. Check that all required SDK files are now present at `nvflare/edge/device/android/app/src/main/java/com/nvidia/nvflare/sdk/`
96-
4. Ensure the app can compile successfully
97-
5. Test that the app can import and use SDK classes
185+
1. **Test Training**: Run a simple training session to verify everything works
186+
2. **Connect to Server**: Set up connection to an NVFlare server for federated learning
187+
3. **Customize Models**: Modify the model architecture for your specific use case
188+
4. **Deploy**: Package and deploy your app to Android devices
98189

99-
**Note**: The SDK contents must be copied to the app's source directory before building. This is a manual step that must be performed when setting up the development environment.
190+
---
100191

192+
**Happy Federated Learning! 🚀**

nvflare/edge/device/android/app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ dependencies {
6969
// ExecuTorch dependencies
7070
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
7171
// Add sources for better IDE support
72-
implementation(files("libs/executorch-sources.jar"))
72+
//implementation(files("libs/executorch-sources.jar"))
7373
// Add SoLoader dependency for PyTorch native library loading
7474
implementation("com.facebook.soloader:nativeloader:0.10.5")
7575
// Add Facebook JNI dependency required by executorch

0 commit comments

Comments
 (0)