-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_android.sh
More file actions
executable file
·134 lines (109 loc) · 3.87 KB
/
build_android.sh
File metadata and controls
executable file
·134 lines (109 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
echo "Starting Android build process..."
# Define output directories
ANDROID_LIB_DIR="./bindings/android"
BASE_DIR="$ANDROID_LIB_DIR/src/main/kotlin/com/synonym/vssclient"
JNILIBS_DIR="$ANDROID_LIB_DIR/src/main/jniLibs"
# Create output directories
mkdir -p "$BASE_DIR"
mkdir -p "$JNILIBS_DIR"
# Remove previous build
echo "Removing previous build..."
rm -rf "$BASE_DIR"/*
rm -rf "$JNILIBS_DIR"/*
# Cargo Build
echo "Building Rust libraries..."
cargo build
# Temporarily set crate-type for Android (restored at end)
cp Cargo.toml Cargo.toml.bak
sed -i '' 's/crate-type = .*/crate-type = ["cdylib"]/' Cargo.toml
trap 'mv Cargo.toml.bak Cargo.toml' EXIT
# Build release
echo "Building release version..."
cargo build --release
# Install cargo-ndk if not already installed
if ! command -v cargo-ndk &> /dev/null; then
echo "Installing cargo-ndk..."
cargo install cargo-ndk
fi
# Check if Android NDK is available
if [ -z "$ANDROID_NDK_ROOT" ] && [ -z "$NDK_HOME" ]; then
echo "Warning: ANDROID_NDK_ROOT or NDK_HOME not set. Attempting to find NDK..."
# Common NDK locations
POSSIBLE_NDK_PATHS=(
"$HOME/Library/Android/sdk/ndk-bundle"
"$HOME/Android/Sdk/ndk-bundle"
"/usr/local/android-ndk"
"/opt/android-ndk"
)
for path in "${POSSIBLE_NDK_PATHS[@]}"; do
if [ -d "$path" ]; then
export ANDROID_NDK_ROOT="$path"
echo "Found NDK at: $ANDROID_NDK_ROOT"
break
fi
done
if [ -z "$ANDROID_NDK_ROOT" ]; then
echo "Error: Android NDK not found. Please install Android NDK and set ANDROID_NDK_ROOT"
echo "You can install it via Android Studio or download from https://developer.android.com/ndk/downloads"
exit 1
fi
fi
# Add Android targets
echo "Adding Android targets..."
rustup target add \
aarch64-linux-android \
armv7-linux-androideabi \
i686-linux-android \
x86_64-linux-android
# Build for all Android architectures
echo "Building for Android architectures..."
cargo ndk \
-o "$JNILIBS_DIR" \
--manifest-path ./Cargo.toml \
-t armeabi-v7a \
-t arm64-v8a \
-t x86 \
-t x86_64 \
build --release
# Generate Kotlin bindings
echo "Generating Kotlin bindings..."
LIBRARY_PATH="./target/release/libvss_rust_client_ffi.dylib"
# Check if the library file exists
if [ ! -f "$LIBRARY_PATH" ]; then
echo "Error: Library file not found at $LIBRARY_PATH"
echo "Available files in target/release:"
ls -l ./target/release/
exit 1
fi
# Create a temporary directory for initial generation
TMP_DIR=$(mktemp -d)
# Generate the bindings to temp directory first
cargo run --bin uniffi-bindgen generate \
--library "$LIBRARY_PATH" \
--language kotlin \
--out-dir "$TMP_DIR"
# Move the Kotlin file from the nested directory to the final location
echo "Moving Kotlin file to final location..."
find "$TMP_DIR" -name "vss_rust_client_ffi.kt" -exec mv {} "$BASE_DIR/" \;
# Clean up temp directory and any remaining uniffi directories
echo "Cleaning up temporary files..."
rm -rf "$TMP_DIR"
rm -rf "$ANDROID_LIB_DIR/uniffi"
# Verify the file was moved correctly
if [ ! -f "$BASE_DIR/vss_rust_client_ffi.kt" ]; then
echo "Error: Kotlin bindings were not moved correctly"
echo "Contents of $BASE_DIR:"
ls -la "$BASE_DIR"
exit 1
fi
# Sync version
echo "Syncing version from Cargo.toml..."
CARGO_VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/' | head -1)
sed -i.bak "s/^version=.*/version=$CARGO_VERSION/" "$ANDROID_LIB_DIR/gradle.properties"
rm -f "$ANDROID_LIB_DIR/gradle.properties.bak"
# Verify android library publish
echo "Testing android library publish to Maven Local..."
"$ANDROID_LIB_DIR"/gradlew --project-dir "$ANDROID_LIB_DIR" clean publishToMavenLocal
echo "Android build process completed successfully!"