diff --git a/AndroidManifest.xml b/android/AndroidManifest.xml similarity index 100% rename from AndroidManifest.xml rename to android/AndroidManifest.xml diff --git a/assets/fragment.glsl b/android/assets/fragment.glsl similarity index 100% rename from assets/fragment.glsl rename to android/assets/fragment.glsl diff --git a/assets/vertex.glsl b/android/assets/vertex.glsl similarity index 100% rename from assets/vertex.glsl rename to android/assets/vertex.glsl diff --git a/res/drawable-v24/ic_launcher_background.xml b/android/res/drawable-v24/ic_launcher_background.xml similarity index 100% rename from res/drawable-v24/ic_launcher_background.xml rename to android/res/drawable-v24/ic_launcher_background.xml diff --git a/res/drawable-v24/ic_launcher_foreground.xml b/android/res/drawable-v24/ic_launcher_foreground.xml similarity index 100% rename from res/drawable-v24/ic_launcher_foreground.xml rename to android/res/drawable-v24/ic_launcher_foreground.xml diff --git a/res/mipmap-anydpi-v26/ic_launcher.xml b/android/res/mipmap-anydpi-v26/ic_launcher.xml similarity index 100% rename from res/mipmap-anydpi-v26/ic_launcher.xml rename to android/res/mipmap-anydpi-v26/ic_launcher.xml diff --git a/res/mipmap-anydpi-v26/ic_launcher_round.xml b/android/res/mipmap-anydpi-v26/ic_launcher_round.xml similarity index 100% rename from res/mipmap-anydpi-v26/ic_launcher_round.xml rename to android/res/mipmap-anydpi-v26/ic_launcher_round.xml diff --git a/res/mipmap-hdpi/ic_launcher.png b/android/res/mipmap-hdpi/ic_launcher.png similarity index 100% rename from res/mipmap-hdpi/ic_launcher.png rename to android/res/mipmap-hdpi/ic_launcher.png diff --git a/res/mipmap-hdpi/ic_launcher_round.png b/android/res/mipmap-hdpi/ic_launcher_round.png similarity index 100% rename from res/mipmap-hdpi/ic_launcher_round.png rename to android/res/mipmap-hdpi/ic_launcher_round.png diff --git a/build.sh b/build.sh index c701676..3175bb1 100755 --- a/build.sh +++ b/build.sh @@ -2,40 +2,7 @@ ## 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 @@ -43,15 +10,20 @@ if [ ! -f .keystore ]; then 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 +odin build jni -target:linux_arm64 -subtarget:android -build-mode:shared -# Make it into an apk -$ANDROID_SDK_ROOT/build-tools/$BUILD_TOOLS/zipalign -f 4 "bin/$APK.build" "bin/$APK" +# 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 jni.so android/lib/lib/arm64-v8a/libmain.so +# 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 diff --git a/jni/main.odin b/jni/main.odin index 075f507..c590c19 100644 --- a/jni/main.odin +++ b/jni/main.odin @@ -107,11 +107,11 @@ engine_init_display :: proc "contextless" (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)) @@ -120,11 +120,11 @@ engine_init_display :: proc "contextless" (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)) @@ -158,8 +158,6 @@ engine_init_display :: proc "contextless" (engine: ^engine) { engine.buffer = b engine.shader = p - - return } engine_draw_frame :: proc "contextless" (engine: ^engine) {