Exporting to Android with encrypted scripts does not work with custom build enabled.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blurrred

I am using Godot 3.4.2 on Manjaro KDE. I have also tested Godot 3.3.4 and the same issue persists.
These are the steps I followed starting with: Compiling with PCK encryption key — Godot Engine (stable) documentation in English

  1. Generate a 256bit AES key using the aes-256-cbc variant from https://asecuritysite.com/encryption/keygen
  • The documentation specifies “a 256bit AES key in HEX”, but only the Base-64 section of the keygen service has any output.
  1. Set this key as environment variable with: export SCRIPT_AES256_ENCRYPTION_KEY="generatedencryptionkey"
    The documentation then says I need to “Compile Godot export templates and set them as custom export templates in the export preset options.”
    Next documentation link: Compiling for Android — Godot Engine (stable) documentation in English
    This page says “Also, you still need to follow the steps mentioned in the Exporting for Android tutorial before attempting to build a custom export template.”
    Next documentation link: Exporting for Android — Godot Engine (stable) documentation in English
  2. Download Linux command-line tools from Fazer o download do Android Studio e das ferramentas de apps: desenvolvedores Android  |  Android Developers
  3. Run ./sdkmanager --sdk_root=<android_sdk_path> "platform-tools" "build-tools;30.0.3" "platforms;android-30" "cmdline-tools;latest" "cmake;3.10.2.4988404" "ndk;21.4.7075529"
  • The documentation says to install “platforms;android-29”, but that needs to be changed to “platforms;android-30” to match the default compileSdk and targetSdk in a project’s android/build/config.gradle file.
  • This command is also very similar to the one present in: Compiling for Android — Godot Engine (stable) documentation in English. The only part that is omitted is the “ndk;21.4.7075529”
  • This page also does not have the command sdkmanager --sdk_root=<android_sdk_path> --licenses that is present on the compiling page. I am not sure if it is necessary to run the license command first, as the exporting page does not specify it. I figured it probably wouldn’t hurt, so I ran the license command first.
  1. Verify that openJDK 11 is installed with java --version
  2. Create a debug keystore with keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999 -deststoretype pkcs12
  3. Set the Android SDK path and debug keystore information in the Editor Settings.
    Going back to: Compiling for Android — Godot Engine (stable) documentation in English
  4. Download Godot source code from Releases · godotengine/godot · GitHub to match my release, 3.4.2-stable.
  5. Set the environment variable ANDROID_SDK_ROOT to point to the Android SDK: export ANDROID_SDK_ROOT="/home/user/Android/cmdline-tools"
  6. Run the release/debug SCons commands:
scons platform=android target=release android_arch=armv7
scons platform=android target=release android_arch=arm64v8
cd platform/android/java
./gradlew generateGodotTemplates

scons platform=android target=release_debug android_arch=armv7
scons platform=android target=release_debug android_arch=arm64v8
cd platform/android/java
./gradlew generateGodotTemplates

1 1. Copy the templates to $HOME/.local/share/godot/templates//

  • The documentation says “However, if you are writing your custom modules or custom C++ code, you might instead want to configure your APKs as custom export templates here: (in the editor)”
    I am not sure why this would be specific to custom modules or custom C++ code and not other reasons to use a custom template (such as for encrypting scripts.)
    But seeing as I am not using custom modules or custom C++ code in this test project, I will do the more difficult task of copying the export templates to a hidden folder rather than linking to them in the editor.
  1. Set the encryption key in the Script tab of the export preset.
  2. Export with any arbitrary script.
  • If custom build is enabled, the game crashes.
  • If custom build is disabled, the game runs as normal.

Does anyone know what could be causing this? Has anyone successfully exported to Android with custom build and encrypted scripts enabled?

I think I have finally found a solution here: Android Export : 'Use Custom Build' option DOES NOT USE 'Custom Template' setttings, for android_debug.apk and android_release.apk · Issue #43817 · godotengine/godot · GitHub

I am not sure if this will break compatibility with any plugins, but so far all of the ones I am using work.

blurrred | 2021-12-27 15:32