mirror of
https://github.com/IllusionMan1212/android-native-example-odin.git
synced 2026-10-24 10:12:33 +00:00
Compare commits
9
Commits
abb67bb33d
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c657f7abc4
|
||
|
|
147b0ec325
|
||
|
|
b643b0b3c2
|
||
|
|
ff674affbd
|
||
|
|
135171813f
|
||
|
|
a7dca34510
|
||
|
|
55ce9659a7
|
||
|
|
84dbfb9501 | ||
|
|
2afd57f64d |
@@ -4,7 +4,12 @@ This requires that you write some basic bindings to the Android NDK (or use mine
|
||||
|
||||
# Building
|
||||
|
||||
First you must adjust `build.sh` and `launch.sh` to have correct paths to Android SDK, Android NDK and the Jetbrains RT.
|
||||
Then set `APK` variable to the desired apk package name. Use Android SDK to install build-tools and platform SDK.
|
||||
### Prerequisites
|
||||
- The Android SDK must be installed and the appropriate Odin environment variables must be set.
|
||||
- The `keytool` binary must be in your PATH
|
||||
|
||||
Now you can use `build.sh` to build and install the app, and `launch.sh` to run the application:
|
||||
### Steps
|
||||
Run `build.sh` to build and install the app, and `launch.sh` to run the application:
|
||||
|
||||
# License
|
||||
The code is licensed under the Unlicense license. I do not own the app icon (Odin logo). The logo copyright is owned by [GingerBill](https://github.com/gingerbill)
|
||||
|
||||
|
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.5 KiB |
@@ -1,57 +1,31 @@
|
||||
#!/bin/bash
|
||||
|
||||
## TODO: handle different ABIs, currently only builds for arm64
|
||||
|
||||
APK=native_example.apk
|
||||
ANDROID_SDK_ROOT=/home/illusion/Android/Sdk
|
||||
ANDROID_NDK=$ANDROID_SDK_ROOT/ndk/26.1.10909125
|
||||
ANDROID_TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
|
||||
ANDROID_JBR=/home/illusion/android-studio/jbr
|
||||
BUILD_TOOLS=34.0.0
|
||||
PLATFORM=34
|
||||
|
||||
# Create required dirs
|
||||
mkdir -p lib/lib/arm64-v8a
|
||||
mkdir -p bin
|
||||
|
||||
# Compile the glue code that comes with the NDK to an object using the aarch64 crosscompiler.
|
||||
$ANDROID_TOOLCHAIN/bin/aarch64-linux-android24-clang -c $ANDROID_NDK/sources/android/native_app_glue/android_native_app_glue.c -o android_native_app_glue.o -I $ANDROID_TOOLCHAIN/sysroot/usr/include/
|
||||
|
||||
# Create a static lib from the glue code object
|
||||
$ANDROID_TOOLCHAIN/bin/llvm-ar rcs libandroid_native_app_glue.a android_native_app_glue.o
|
||||
|
||||
# Compile our odin code to an object. relocation mode is set to PIC because the linker complains.
|
||||
# freestanding_arm64 should probably be preferred but raylib projects need linux_arm64, otherwise the android dynamic linker fails to find our main function.
|
||||
odin build jni/ -build-mode:obj -target:linux_arm64 -reloc-mode:pic || exit 1
|
||||
|
||||
# Link the static library with our object and create the shared library that will hold all our native code.
|
||||
## We pass "-u ANativeActivity_onCreate" to the linker here because that symbol gets stripped by the linker even though it's required
|
||||
## This is exactly what the official ndk-build building process does, see: https://github.com/android/ndk/issues/381
|
||||
$ANDROID_TOOLCHAIN/bin/aarch64-linux-android24-clang -shared -o libmain.so jni.o libandroid_native_app_glue.a -L $ANDROID_TOOLCHAIN/sysroot/usr/lib/aarch64-linux-android/$PLATFORM/ -landroid -llog -lEGL -lGLESv3 -u ANativeActivity_onCreate
|
||||
|
||||
# Move the shared lib to the lib directory
|
||||
cp libmain.so lib/lib/arm64-v8a
|
||||
|
||||
# Pack the assets of the app using a basic android jar that comes with the SDK
|
||||
## If there are asset files that need to be loaded during runtime then they should be put in an `assets` directory
|
||||
## and "-A assets" should be appended after the -I option.
|
||||
$ANDROID_SDK_ROOT/build-tools/$BUILD_TOOLS/aapt package -f -M AndroidManifest.xml -S res -I $ANDROID_SDK_ROOT/platforms/android-$PLATFORM/android.jar -A assets -F "bin/$APK.build" lib
|
||||
|
||||
# Create a debug signing key to sign our application.
|
||||
if [ ! -f .keystore ]; then
|
||||
"$ANDROID_JBR/bin/keytool" -genkey -dname "CN=Android Debug, O=Android, C=US" -keystore .keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validity 30000
|
||||
keytool -genkey -dname "CN=Android Debug, O=Android, C=US" -keystore .keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validity 30000
|
||||
if [ $? -ne 0 ]; then exit 1; fi
|
||||
fi
|
||||
|
||||
# Sign the jar
|
||||
$ANDROID_JBR/bin/jarsigner -storepass android -keystore .keystore "bin/$APK.build" androiddebugkey > /dev/null
|
||||
# Build the shared lib
|
||||
if [[ $1 == "release" ]]; then
|
||||
odin build . -target:linux_arm64 -subtarget:android -build-mode:shared -o:speed -define:RELEASE_BUILD=true
|
||||
else
|
||||
odin build . -target:linux_arm64 -subtarget:android -build-mode:shared -debug -show-system-calls
|
||||
fi
|
||||
|
||||
# Make it into an apk
|
||||
$ANDROID_SDK_ROOT/build-tools/$BUILD_TOOLS/zipalign -f 4 "bin/$APK.build" "bin/$APK"
|
||||
name=$(basename "$PWD")
|
||||
|
||||
# Create the proper lib directories and move the shared lib to them
|
||||
# Rename the shared lib to match what we specified in the AndroidManifest.xml file
|
||||
# for the android.app.lib_name metadata value
|
||||
mkdir -p android/lib/lib/arm64-v8a
|
||||
mv $name.so android/lib/lib/arm64-v8a/libmain.so || exit 1
|
||||
|
||||
# Bundle the directory into an apk
|
||||
odin bundle android android -android-keystore:.keystore -android-keystore-password:"android"
|
||||
|
||||
cleanup() {
|
||||
rm jni.o android_native_app_glue.o libandroid_native_app_glue.a bin/$APK.build
|
||||
rm test.apk-build
|
||||
}
|
||||
|
||||
cleanup
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
APK=native_example.apk
|
||||
ANDROID_SDK_ROOT=/home/illusion/Android/Sdk
|
||||
ANDROID_NDK=$ANDROID_SDK_ROOT/ndk/26.1.10909125
|
||||
ANDROID_TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
|
||||
ANDROID_JBR=/home/illusion/android-studio/jbr
|
||||
BUILD_TOOLS=34.0.0
|
||||
|
||||
get_package_activity() {
|
||||
PACKAGE=$("$ANDROID_SDK_ROOT/build-tools/$BUILD_TOOLS/aapt" dump badging "bin/$APK" | grep package | awk -F"'" '{print $2}')
|
||||
ACTIVITY=$("$ANDROID_SDK_ROOT/build-tools/$BUILD_TOOLS/aapt" dump badging "bin/$APK" | grep launchable-activity | awk -F"'" '{print $2}')
|
||||
}
|
||||
|
||||
launch() {
|
||||
get_package_activity
|
||||
"$ANDROID_SDK_ROOT/platform-tools/adb" shell am start -n "$PACKAGE/$ACTIVITY"
|
||||
if [ $? -ne 0 ]; then exit 1; fi
|
||||
}
|
||||
|
||||
launch
|
||||
@@ -23,7 +23,8 @@ engine :: struct {
|
||||
shader: u32,
|
||||
}
|
||||
|
||||
engine_init_display :: proc(engine: ^engine) {
|
||||
engine_init_display :: proc "contextless" (engine: ^engine) {
|
||||
context = runtime.default_context()
|
||||
attribs := []i32{
|
||||
egl.SURFACE_TYPE, egl.WINDOW_BIT,
|
||||
egl.RENDERABLE_TYPE, egl.OPENGL_ES3_BIT,
|
||||
@@ -35,7 +36,7 @@ engine_init_display :: proc(engine: ^engine) {
|
||||
|
||||
display := egl.GetDisplay(egl.DEFAULT_DISPLAY)
|
||||
if display == egl.NO_DISPLAY {
|
||||
LOG(.INFO, "NativeExampleOdin", "Naterror with eglGetDisplay")
|
||||
LOG(.INFO, "NativeExampleOdin", "error with eglGetDisplay")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,11 +107,11 @@ engine_init_display :: proc(engine: ^engine) {
|
||||
engine.height = h
|
||||
|
||||
vasset := android.AAssetManager_open(engine.app.activity.assetManager, "vertex.glsl", .BUFFER)
|
||||
defer android.AAsset_close(vasset)
|
||||
if vasset == nil {
|
||||
LOG(.INFO, "NativeExampleOdin", "error opening vertex.glsl")
|
||||
return
|
||||
}
|
||||
defer android.AAsset_close(vasset)
|
||||
vsrc := cstring(android.AAsset_getBuffer(vasset))
|
||||
vlen := i32(android.AAsset_getLength(vasset))
|
||||
|
||||
@@ -119,11 +120,11 @@ engine_init_display :: proc(engine: ^engine) {
|
||||
gl.CompileShader(v)
|
||||
|
||||
fasset := android.AAssetManager_open(engine.app.activity.assetManager, "fragment.glsl", .BUFFER)
|
||||
defer android.AAsset_close(fasset)
|
||||
if fasset == nil {
|
||||
LOG(.INFO, "NativeExampleOdin", "error opening fragment.glsl")
|
||||
return
|
||||
}
|
||||
defer android.AAsset_close(fasset)
|
||||
fsrc := cstring(android.AAsset_getBuffer(fasset))
|
||||
flen := i32(android.AAsset_getLength(fasset))
|
||||
|
||||
@@ -157,11 +158,9 @@ engine_init_display :: proc(engine: ^engine) {
|
||||
|
||||
engine.buffer = b
|
||||
engine.shader = p
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
engine_draw_frame :: proc(engine: ^engine) {
|
||||
engine_draw_frame :: proc "contextless" (engine: ^engine) {
|
||||
if engine.display == nil {
|
||||
return
|
||||
}
|
||||
@@ -182,7 +181,7 @@ engine_draw_frame :: proc(engine: ^engine) {
|
||||
egl.SwapBuffers(engine.display, engine.surface)
|
||||
}
|
||||
|
||||
engine_term_display :: proc(engine: ^engine) {
|
||||
engine_term_display :: proc "contextless" (engine: ^engine) {
|
||||
if engine.display != egl.NO_DISPLAY {
|
||||
gl.DeleteProgram(engine.shader)
|
||||
gl.DeleteBuffers(1, &engine.buffer)
|
||||
@@ -202,11 +201,11 @@ engine_term_display :: proc(engine: ^engine) {
|
||||
engine.surface = egl.NO_SURFACE
|
||||
}
|
||||
|
||||
engine_handle_input :: proc(app: ^android.android_app, event: ^android.AInputEvent) -> i32 {
|
||||
engine_handle_input :: proc "c" (app: ^android.android_app, event: ^android.AInputEvent) -> i32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
engine_handle_cmd :: proc(app: ^android.android_app, cmd: android.AppCmd) {
|
||||
engine_handle_cmd :: proc "c" (app: ^android.android_app, cmd: android.AppCmd) {
|
||||
engine := cast(^engine)app.userData
|
||||
|
||||
#partial switch (cmd) {
|
||||
Reference in New Issue
Block a user