-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·145 lines (122 loc) · 4.88 KB
/
build.sh
File metadata and controls
executable file
·145 lines (122 loc) · 4.88 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
#!/bin/bash
# Load .env; this holds the following for signing:
# * KEYSTORE_PATH
# * KEY_ALIAS
# * KEYSTORE_PASS
# * KEY_PASS
set -a && source ./.env && set +a
set -e
# Configuration variables
APP_NAME="MinimalAndroidWorkflowWithoutJava"
PACKAGE_NAME="com.example.minimal_android_workflow_without_java"
RUST_PACKAGE_NAME="minimal_android_workflow_without_java"
RUST_ANDROID_TARGET="aarch64-linux-android" # Can be adjusted (e.g., armv7, x86_64)
ANDROID_TARGET="arm64-v8a"
SDK_PATH="$HOME/android"
# NDK_VERSION="26.2.11394342" # Adjust as needed
NDK_VERSION="23.1.7779620"
OUTPUT_DIR="./output"
MIN_SDK_VERSION="28"
TARGET_SDK_VERSION="34"
# Paths to Android tools (ensure they're correct for your environment)
AAPT2="$SDK_PATH/build-tools/34.0.0/aapt2"
ZIPALIGN="$SDK_PATH/build-tools/34.0.0/zipalign"
PLATFORM="$SDK_PATH/platforms/android-31/android.jar"
APKSIGNER="$SDK_PATH/build-tools/34.0.0/apksigner"
# NativeActivity path from NDK
NATIVE_ACTIVITY_PATH="$SDK_PATH/ndk/$NDK_VERSION/sources/android/native_app_glue"
# Ensure the output directory exists
mkdir -p $OUTPUT_DIR
rm $OUTPUT_DIR/*.apk -f
# Step 1: Add Android target for Rust if it's not already added
rustup target add $RUST_ANDROID_TARGET
# Step 2: Build the Rust project for the specified Android target
cargo ndk -t $RUST_ANDROID_TARGET --platform $TARGET_SDK_VERSION build --release
# Step 3: Create the APK directory structure
mkdir -p $OUTPUT_DIR/app/lib/$ANDROID_TARGET
mkdir -p $OUTPUT_DIR/app/res
mkdir -p $OUTPUT_DIR/app/values
mkdir -p $OUTPUT_DIR/app/META-INF
echo "dummy thing" > $OUTPUT_DIR/app/res/textfile.txt
# Step 4: Copy the Rust compiled shared library to the APK lib folder
cp "target/$RUST_ANDROID_TARGET/release/lib$RUST_PACKAGE_NAME.so" "$OUTPUT_DIR/app/lib/$ANDROID_TARGET/"
# <activity android:name="android.app.NativeActivity"
# android:exported="true"
# android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
# android:configChanges="orientation|keyboardHidden">
# <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31"/>
# package="$PACKAGE_NAME"
# android:versionCode="1"
# android:versionName="1.0">
# <?xml version="1.0" encoding="utf-8"?>
# <meta-data android:name="android.app.lib_name" android:value="$RUST_PACKAGE_NAME"/>
# Step 5: Create a minimal AndroidManifest.xml
cat <<EOL > $OUTPUT_DIR/app/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="$PACKAGE_NAME"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="$MIN_SDK_VERSION" android:targetSdkVersion="$TARGET_SDK_VERSION" />
<application
android:label="$APP_NAME"
android:hasCode="false"
>
<activity android:name="android.app.NativeActivity"
android:exported="true"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="$RUST_PACKAGE_NAME"/>
</activity>
</application>
</manifest>
EOL
# Step 6: Create the resources (minimal example, adjust as needed)
cat <<EOL > $OUTPUT_DIR/app/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">$APP_NAME</string>
</resources>
EOL
# Step 7: Compile resources using aapt2
echo "Compiling resources..."
$AAPT2 compile --dir $OUTPUT_DIR/app/res -o $OUTPUT_DIR/resources.zip
# echo "Compiling manifest..."
# $AAPT2 compile $OUTPUT_DIR/AndroidManifest.xml -o $OUTPUT_DIR/app/AndroidManifest.xml
echo "Linking into apk"
# Step 8: Link the resources into a binary APK format
$AAPT2 link \
-o $OUTPUT_DIR/$APP_NAME-unaligned.apk \
-I $PLATFORM \
-R $OUTPUT_DIR/resources.zip \
--manifest $OUTPUT_DIR/app/AndroidManifest.xml \
--auto-add-overlay
# --proto-format
# --java $OUTPUT_DIR/app \
# --manifest $OUTPUT_DIR/compiled_manifest.zip \
# $AAPT2 convert
rm $OUTPUT_DIR/app/AndroidManifest.xml
echo "Zipping rust library into APK"
# Step 9: Add the compiled Rust library to the APK
cd $OUTPUT_DIR/app
zip -r ../$APP_NAME-unaligned.apk ./*
cd -
# Step 10: Align the APK using zipalign
$ZIPALIGN -v -p 4 $OUTPUT_DIR/$APP_NAME-unaligned.apk $OUTPUT_DIR/$APP_NAME.apk
echo "Signing..."
# Signing the APK using apksigner
$APKSIGNER sign \
--ks $KEYSTORE_PATH \
--ks-key-alias $KEY_ALIAS \
--ks-pass pass:$KEYSTORE_PASS \
--key-pass pass:$KEY_PASS \
--out $OUTPUT_DIR/$APP_NAME-signed.apk \
--min-sdk-version $MIN_SDK_VERSION \
$OUTPUT_DIR/$APP_NAME.apk
# Final output
echo "APK created successfully: $OUTPUT_DIR/$APP_NAME-signed.apk"