L.Nam
L.Nam

Reputation: 185

Proguard rules for Jetpack GameActivity

I'm trying to use Jetpack GameActivity in my project, but I encounter a strange issue with Proguard. My project has 2 modules, app and my-lib:

When I built my project in Release build and had Proguard enabled, I got a native crash:

=> I tried inspecting GameActivity class from my APK, setWindowFlags method didn't exist at all, which means Proguard has removed it, that's why the native part of the GameActivity library couldn't find the method and it threw an error.

Then, I tried to fix it, by adding Proguard rule like this:

But unfortunately, I got another native crash:

=> Why does this happen? I tried inspecting the APK again, but have no idea why

Anyway, how can I deal with this situation? Does Jetpack GameActivty need to add Proguard rules to keep methods that will be used by the native part?

Upvotes: 2

Views: 486

Answers (2)

Gerry
Gerry

Reputation: 1233

There is a know issue in version 1.2.1 and before: the java functions called by native code only were stripped out by proguard for the release build. Version 1.2.2-alpha01 has fixed that and a few other important potential issues, including the static library release in the AAR. To use the 1.2.2-alpha01+:

  1. Add the latest version to the dependency: 1.2.2-alpha01+

  2. Use C/C++ static lib release in the AAR.
    add the following or something similar to your project's existing CMakeLists.txt

    find_package(game-activity REQUIRED CONFIG)
       add_library(${PROJECT_NAME} SHARED sth.cpp) #<=== this is your own.
    target_link_libraries(${PROJECT_NAME} game-activity::game-activity_static
    # optional: does not hurt to add it; refer to the official doc for update.
    set(CMAKE_SHARED_LINKER_FLAGS
     "${CMAKE_SHARED_LINKER_FLAGS} -u \
     Java_com_google_androidgamesdk_GameActivity_initializeNativeCode")
    
  3. If want to use the source code directly(not recommended), do something similar to this:

    # Find GameActivity sources to build.
    find_package(game-activity REQUIRED CONFIG)
    
    get_target_property(GAME_ACTIVITY_INCLUDE_PATH
        game-activity::game-activity
        INTERFACE_INCLUDE_DIRECTORIES)
    string(REPLACE "include" "" GAME_ACTIVITY_SRCS ${GAME_ACTIVITY_INCLUDE_PATH})
    file(GLOB_RECURSE GAME_ACTIVITY_SRCS
        ${GAME_ACTIVITY_INCLUDE_PATH}/*.c*)
    add_library(${PROJECT_NAME} SHARED
        sth.cpp.   #<== this is your own file
        ${GAME_ACTIVITY_SRCS})
    target_link_libraries(${PROJECT_NAME} PUBLIC
     android log
     game-activity::game-activity
     # other libs
     )
    

    Note: it is still just 3 source files now, and you can unzip the latest AAR and list them explicitly like:

    add_library(${PROJECT_NAME} SHARED
        sth.cpp.   #<== this is your own file
        ${GAME_ACTIVITY_INCLUDE_PATH}/game-activity/GameActivity.cpp
        ${GAME_ACTIVITY_INCLUDE_PATH}/game-activity/native_app_glue/android_native_app_glue.c
        ${GAME_ACTIVITY_INCLUDE_PATH}/game-text-input/gametextinput.cpp)
    

    The glob way might be better for future compatibility reasons; but it is not cmake recommended way to do things.

  4. Handle the the "back" button press to exit app either from Kotlin/Java side(handle KEYCODE_BACK in onKeyDown()) or native code (AKEYCODE_BACK in input key event processing).

Referring to the documentation page and probably the list above, you should be able to get your project going. The simple fact is this: all of the C/C++ things are inside AAR, under the prefab sub-directory, you even can copy it out and directly put into your own source tree. This Pull Request might help (but not totally sure).

If you see something or like some new things, please create a bug.

Upvotes: 2

vspyder
vspyder

Reputation: 31

I had the same issues as you and I solved this by adding the following line to my proguard-rules.pro file:

-keep class com.google.androidgamesdk.** { *; }

Of course then I had other proguard related errors so I had to add these additional lines:

-keep class androidx.core.graphics.Insets { *; }

-keep class androidx.core.view.** { *; }

-keep class org.fmod.** { *; }

Of course you will have different errors in your build, but hopefully this will help you.

Upvotes: 0

Related Questions