-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild_android.sh
More file actions
executable file
·167 lines (136 loc) · 4.9 KB
/
build_android.sh
File metadata and controls
executable file
·167 lines (136 loc) · 4.9 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
#TODO: Eventually remove cleanup when example/main.rs builds successfully
# Define cleanup function
cleanup() {
local exit_code=$?
echo "Performing cleanup..."
# Restore example/main.rs
if [ ! -z "$MAIN_RS_CONTENT" ]; then
echo "Restoring example/main.rs..."
mkdir -p "example"
echo "$MAIN_RS_CONTENT" > "example/main.rs"
echo "example/main.rs restored"
fi
# Restore bin configuration in Cargo.toml
if [ ! -z "$BIN_CONFIG" ]; then
echo "Restoring bin configuration in Cargo.toml..."
echo "$BIN_CONFIG" >> Cargo.toml
echo "Bin configuration restored"
fi
if [ $exit_code -ne 0 ]; then
echo "Script failed with exit code $exit_code"
fi
exit $exit_code
}
# Set up trap to call cleanup function on script exit
trap cleanup EXIT
set -e # Exit immediately if a command exits with a non-zero status.
echo "Starting Android build process..."
# Install gobley-uniffi-bindgen from fork with patched version
echo "Installing gobley-uniffi-bindgen fork..."
cargo install --git https://github.com/ovitrif/gobley.git gobley-uniffi-bindgen --force
#TODO: Remove this section when example/main.rs builds successfully
# Store example/main.rs content in memory and remove the file
if [ -f "example/main.rs" ]; then
echo "Backing up example/main.rs..."
MAIN_RS_CONTENT=$(cat "example/main.rs")
rm "example/main.rs"
echo "example/main.rs temporarily removed"
fi
#TODO: Remove this section when example/main.rs builds successfully
# Backup and remove bin configuration from Cargo.toml
echo "Backing up and removing bin configuration from Cargo.toml..."
if grep -q '\[\[bin\]\]' Cargo.toml; then
# Store the bin configuration lines
BIN_CONFIG=$(awk '/\[\[bin\]\]/,/^$/' Cargo.toml)
# Remove the bin configuration section
sed -i.bak '/\[\[bin\]\]/,/^$/d' Cargo.toml
rm -f Cargo.toml.bak
echo "Bin configuration temporarily removed"
fi
# Set OpenSSL environment variables
export OPENSSL_STATIC=1
export OPENSSL_NO_VENDOR=0
# Define output directories
ANDROID_LIB_DIR="./bindings/android"
BASE_DIR="$ANDROID_LIB_DIR/lib/src/main/kotlin/com/synonym/bitkitcore"
JNILIBS_DIR="$ANDROID_LIB_DIR/lib/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
# Modify Cargo.toml
echo "Updating Cargo.toml..."
sed -i '' 's/crate_type = .*/crate_type = ["cdylib"]/' Cargo.toml
# 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
# 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/libbitkitcore.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
gobley-uniffi-bindgen --library "$LIBRARY_PATH" \
--config uniffi-android.toml \
--out-dir "$TMP_DIR"
# Move the Kotlin files from the nested directory to the final location
echo "Moving Kotlin files to final location..."
mv "$TMP_DIR"/main/kotlin/com/synonym/bitkitcore/*.kt "$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 files were moved correctly
if [ ! -f "$BASE_DIR/bitkitcore.android.kt" ] || [ ! -f "$BASE_DIR/bitkitcore.common.kt" ]; then
echo "Error: Kotlin bindings were not moved correctly"
echo "Contents of $BASE_DIR:"
ls -la "$BASE_DIR"
exit 1
fi
echo "Generated Kotlin bindings:"
ls -la "$BASE_DIR"
# 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!"