Skip to content

Commit

Permalink
[Android] Build in release mode (#46)
Browse files Browse the repository at this point in the history
* chore(android): update script for building in release/debug mode

* chore(android): add init function in example app
  • Loading branch information
vivianjeng authored Dec 7, 2023
1 parent 4591653 commit 6bb9bb6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ jobs:
cargo install --path circom
- name: Prepare CI
run: ./scripts/prepare_ci.sh
- name: Build
- name: Build for iOS
run: ./scripts/build_ios.sh x86_64 debug
- name: Build for android
run: ./scripts/build_android.sh arm64 debug
- name: Run core tests
run: cd mopro-core && cargo test -- --nocapture
- name: Run ffi tests
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ To update bindings, run `./scripts/update_bindings.sh simulator|device debug|rel

#### Build and Update Bindings

To build bindings for android simulator, run
To build bindings for android simulator debug mode, run

```sh
./scripts/build_android.sh
./scripts/build_android.sh arm64 debug
```

- **Device types:** `x86_64`, `x86`, `arm`, `arm64`
Check your device architecture here: https://doc.e.foundation/devices
For Android Studio simulator (Pixel series), choose `arm64`
- **Mode:** `debug`, `release`

## Community and Talks

Join the Telegram group [here](https://t.me/zkmopro).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MainActivity : ComponentActivity() {

@Composable
fun ProofComponent() {
var initTime by remember { mutableStateOf("init time:") }
var provingTime by remember { mutableStateOf("proving time:") }
var verifyingTime by remember { mutableStateOf("verifying time: ") }
var valid by remember { mutableStateOf("valid:") }
Expand Down Expand Up @@ -293,11 +294,37 @@ fun ProofComponent() {
Box(modifier = Modifier.fillMaxSize().padding(16.dp), contentAlignment = Alignment.Center) {
Button(
onClick = {
val startTime = System.currentTimeMillis()
res = uniffi.mopro.generateProof2(inputs)
val endTime = System.currentTimeMillis()
provingTime = "proving time: " + (endTime - startTime).toString() + " ms"
}
Thread(
Runnable {
val startTime = System.currentTimeMillis()
uniffi.mopro.initializeMopro()
val endTime = System.currentTimeMillis()
initTime =
"init time: " +
(endTime - startTime).toString() +
" ms"
}
)
.start()
},
modifier = Modifier.padding(bottom = 80.dp)
) { Text(text = "init") }
Button(
onClick = {
Thread(
Runnable {
val startTime = System.currentTimeMillis()
res = uniffi.mopro.generateProof2(inputs)
val endTime = System.currentTimeMillis()
provingTime =
"proving time: " +
(endTime - startTime).toString() +
" ms"
}
)
.start()
},
modifier = Modifier.padding(top = 20.dp)
) { Text(text = "generate proof") }
Button(
onClick = {
Expand All @@ -306,16 +333,17 @@ fun ProofComponent() {
val endTime = System.currentTimeMillis()
verifyingTime = "verifying time: " + (endTime - startTime).toString() + " ms"
},
modifier = Modifier.padding(top = 100.dp)
modifier = Modifier.padding(top = 120.dp)
) { Text(text = "verify proof") }
Text(
text = "Keccak256 proof",
modifier = Modifier.padding(bottom = 80.dp),
modifier = Modifier.padding(bottom = 180.dp),
fontWeight = FontWeight.Bold
)

Text(text = valid, modifier = Modifier.padding(top = 250.dp).width(200.dp))
Text(text = provingTime, modifier = Modifier.padding(top = 300.dp).width(200.dp))
Text(text = initTime, modifier = Modifier.padding(top = 200.dp).width(200.dp))
Text(text = provingTime, modifier = Modifier.padding(top = 250.dp).width(200.dp))
Text(text = valid, modifier = Modifier.padding(top = 300.dp).width(200.dp))
Text(text = verifyingTime, modifier = Modifier.padding(top = 350.dp).width(200.dp))
}
}
87 changes: 69 additions & 18 deletions scripts/build_android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,81 @@ PROJECT_DIR=$(pwd)
# Color definitions
DEFAULT='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'

# Function to handle exit
handle_exit() {
# $? is a special variable that holds the exit code of the last command executed
if [ $? -ne 0 ]; then
echo -e "\n${RED}Script did not finish successfully!${DEFAULT}"
fi
}

# Set the trap
trap handle_exit EXIT

print_action() {
printf "\n${GREEN}$1${DEFAULT}\n"
}

print_warning() {
printf "\n${YELLOW}$1${DEFAULT}\n"
}

# Check for the device type argument
if [[ "$1" == "x86_64" ]]; then
ARCHITECTURE="x86_64-linux-android"
FOLDER="x86_64"
elif [[ "$1" == "x86" ]]; then
ARCHITECTURE="i686-linux-android"
FOLDER="x86"
elif [[ "$1" == "arm" ]]; then
ARCHITECTURE="armv7-linux-androideabi"
FOLDER="armeabi-v7a"
elif [[ "$1" == "arm64" ]]; then
ARCHITECTURE="aarch64-linux-android"
FOLDER="arm64-v8a"
else
echo -e "${RED}Error: Please specify either 'x86_64', 'x86', 'arm' or 'arm64' as the first argument.${DEFAULT}"
exit 1
fi

# Check for the build mode argument
if [[ "$2" == "debug" ]]; then
BUILD_MODE="debug"
LIB_DIR="debug"
COMMAND=""
elif [[ "$2" == "release" ]]; then
BUILD_MODE="release"
LIB_DIR="release"
COMMAND="--release"
else
echo -e "${RED}Error: Please specify either 'debug' or 'release' as the second argument.${DEFAULT}"
exit 1
fi

cd ${PROJECT_DIR}/mopro-ffi

cargo build --lib \
--target x86_64-linux-android \
--target i686-linux-android \
--target armv7-linux-androideabi \
--target aarch64-linux-android
# Print appropriate message based on device type
print_action "Using $ARCHITECTURE libmopro_ffi.a ($LIB_DIR) static library..."
print_warning "This only works on $FOLDER devices!"

print_action "[android] Build target in $BUILD_MODE mode"
cargo build --lib ${COMMAND} --target ${ARCHITECTURE}

print_action "[android] Copy files in mopro-android/Example/jniLibs/"
for binary in target/*/*/libmopro_ffi.so; do file $binary; done

mkdir -p jniLibs/arm64-v8a/ && \
cp target/aarch64-linux-android/debug/libmopro_ffi.so jniLibs/arm64-v8a/libuniffi_mopro.so && \
mkdir -p jniLibs/armeabi-v7a/ && \
cp target/armv7-linux-androideabi/debug/libmopro_ffi.so jniLibs/armeabi-v7a/libuniffi_mopro.so && \
mkdir -p jniLibs/x86/ && \
cp target/i686-linux-android/debug/libmopro_ffi.so jniLibs/x86/libuniffi_mopro.so && \
mkdir -p jniLibs/x86_64/ && \
cp target/x86_64-linux-android/debug/libmopro_ffi.so jniLibs/x86_64/libuniffi_mopro.so
mkdir -p jniLibs/${FOLDER}/ && \
cp target/${ARCHITECTURE}/${LIB_DIR}/libmopro_ffi.so jniLibs/${FOLDER}/libuniffi_mopro.so

cargo run --features=uniffi/cli \
--bin uniffi-bindgen \
generate src/mopro.udl \
--language kotlin
print_action "[android] Generating Kotlin bindings in $BUILD_MODE mode..."
cargo run --features=uniffi/cli ${COMMAND} \
--bin uniffi-bindgen \
generate src/mopro.udl \
--language kotlin

print_action "[android] Copy Kotlin bindings to mopro-android/Example"
cp -r ${PROJECT_DIR}/mopro-ffi/jniLibs/ ${PROJECT_DIR}/mopro-android/Example/app/src/main/jniLibs/
cp -r ${PROJECT_DIR}/mopro-ffi/src/uniffi/ ${PROJECT_DIR}/mopro-android/Example/app/src/main/java/uniffi/
cp -r ${PROJECT_DIR}/mopro-ffi/src/uniffi/ ${PROJECT_DIR}/mopro-android/Example/app/src/main/java/uniffi/

0 comments on commit 6bb9bb6

Please sign in to comment.