微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一旦在Google Play商店中发布了具有libgdx游戏的android应用,要使其正常运行,必须遵循哪些R8规则?

如何解决一旦在Google Play商店中发布了具有libgdx游戏的android应用,要使其正常运行,必须遵循哪些R8规则?

我正在尝试发布一个包含libgdx迷你游戏的Android应用,在调试模式下应用R8(minifyEnabled为true)后,它可以正常工作,但是当我在发布模式下将其发布并在Play商店中发布时,该应用崩溃当我按下打开libgdx游戏的按钮时。我使用的是Android Studio版本4.0

我用不同的R8规则尝试了几次,但是在生产时它们都不起作用。

我在Android模块中的代码

build.gradle(:android)

        debug {
            minifyEnabled true 
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }

proguard-rules.pro

-verbose

-dontwarn android.support.**
-dontwarn com.badlogic.gdx.backends.android.AndroidFragmentApplication
-dontwarn com.badlogic.gdx.utils.GdxBuild
-dontwarn com.badlogic.gdx.physics.Box2d.utils.Box2DBuild
-dontwarn com.badlogic.gdx.jnigen.BuildTarget*
-dontwarn com.badlogic.gdx.graphics.g2d.freetype.FreetypeBuild




#-keep class com.badlogic.gdx.controllers.android.AndroidControllers
# tried changing AndroidControllers with line:
-keep class com.badlogic.gdx.controllers.android.*

# tried adding this one
#-keep class com.ongngo.game.AndroidLauncher  

#    -keepclassmembers class com.badlogic.gdx.backends.android.AndroidInput* {
#       <init>(com.badlogic.gdx.Application,android.content.Context,java.lang.Object,com.badlogic.gdx.backends.android.AndroidApplicationConfiguration);
#    }

# tried separating AndroidInputs
    -keepclassmembers class com.badlogic.gdx.backends.android.AndroidInput {
       <init>(com.badlogic.gdx.Application,com.badlogic.gdx.backends.android.AndroidApplicationConfiguration);
    }
    -keepclassmembers class com.badlogic.gdx.backends.android.AndroidInputThreePlus {
       <init>(com.badlogic.gdx.Application,com.badlogic.gdx.backends.android.AndroidApplicationConfiguration);
    }

# tried with this
-keepclassmembers class com.ongngo.game.CoreAndroidInterface
-keepclassmembers class com.ongngo.game.AndroidLauncher
-dontwarn com.badlogic.gdx.**
-dontnote com.badlogic.gdx.**
-keepclassmembers class com.badlogic.gdx.graphics.CubemaP* { *; }
-keepclassmembers class com.badlogic.gdx.graphics.GL* { *; }
#


-keepclassmembers class com.badlogic.gdx.physics.Box2d.World {
   boolean contactFilter(long,long);
   void    beginContact(long);
   void    endContact(long);
   void    preSolve(long,long);
   void    postSolve(long,long);
   boolean reportFixture(long);
   float   reportRayFixture(long,float,float);
}

MainActivity.kt:

onCreate(...) 
        ...
        coreAndroidInterface = object : CoreAndroidInterface {
            override fun updateLifes(lifes: Int) {
                var lifesVar = lifes
                if (lifes < 0) {
                    lifesVar = 0
                }

                MainActivity.LIFES = lifesVar
                prefs.lifes = lifesVar
            }
        } 

一个片段中的Button(取决于MainActivity)启动AndroidLauncher.k:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val config = AndroidApplicationConfiguration()
        config.useAccelerometer = false
        config.useCompass = false
        initialize(MyGame(MainActivity.LIFES!!,MainActivity.coreAndroidInterface),config)
    }
} 

核心模块中的代码

CoreAndroidInterface.kt

interface CoreAndroidInterface {
    fun updateLifes(lifes : Int)
} 

MyGame.java

public class MyGame extends Game implements ApplicationListener {
private final CoreAndroidInterface coreAndroidInterface;
...
    public MyGame(int vidas,CoreAndroidInterface coreAndroidInterface) {//constructor
        this.LIFES = lifes;
        this.coreAndroidInterface = coreAndroidInterface;
    }

应用崩溃时我从Google Play控制台收到的错误是:

 java.lang.classNotFoundException
com.ongngo.game.AndroidLauncher.onCreate 

类型: java.lang.RuntimeException

Stacktrace:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performlaunchActivity (ActivityThread.java:2795)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2873)
  at android.app.ActivityThread.-wrap11 (UnkNown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1602)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6543)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
Caused by: java.lang.RuntimeException: 
  at com.badlogic.gdx.backends.android.AndroidInputFactory.newAndroidInput (AndroidInputFactory.java:65)
  at com.badlogic.gdx.backends.android.AndroidApplication.init (AndroidApplication.java:34)
  at com.badlogic.gdx.backends.android.AndroidApplication.initialize (AndroidApplication.java:1)
  at com.ongngo.game.AndroidLauncher.onCreate (AndroidLauncher.java:36)
  at android.app.Activity.performCreate (Activity.java:7023)
  at android.app.Activity.performCreate (Activity.java:7014)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1215)
  at android.app.ActivityThread.performlaunchActivity (ActivityThread.java:2748)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2873)
  at android.app.ActivityThread.-wrap11 (UnkNown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1602)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6543)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
Caused by: java.lang.classNotFoundException: 
  at java.lang.class.classForName (Class.java)
  at java.lang.class.forName (Class.java:453)
  at java.lang.class.forName (Class.java:378)
  at com.badlogic.gdx.backends.android.AndroidInputFactory.newAndroidInput (AndroidInputFactory.java:8)
  at com.badlogic.gdx.backends.android.AndroidApplication.init (AndroidApplication.java:34)
  at com.badlogic.gdx.backends.android.AndroidApplication.initialize (AndroidApplication.java:1)
  at com.ongngo.game.AndroidLauncher.onCreate (AndroidLauncher.java:36)
  at android.app.Activity.performCreate (Activity.java:7023)
  at android.app.Activity.performCreate (Activity.java:7014)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1215)
  at android.app.ActivityThread.performlaunchActivity (ActivityThread.java:2748)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2873)
  at android.app.ActivityThread.-wrap11 (UnkNown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1602)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6543)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
Caused by: java.lang.classNotFoundException: 
  at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:125)
  at java.lang.classLoader.loadClass (ClassLoader.java:379)
  at java.lang.classLoader.loadClass (ClassLoader.java:312)
  at java.lang.class.classForName (Class.java)
  at java.lang.class.forName (Class.java:453)
  at java.lang.class.forName (Class.java:378)
  at com.badlogic.gdx.backends.android.AndroidInputFactory.newAndroidInput (AndroidInputFactory.java:8)
  at com.badlogic.gdx.backends.android.AndroidApplication.init (AndroidApplication.java:34)
  at com.badlogic.gdx.backends.android.AndroidApplication.initialize (AndroidApplication.java:1)
  at com.ongngo.game.AndroidLauncher.onCreate (AndroidLauncher.java:36)
  at android.app.Activity.performCreate (Activity.java:7023)
  at android.app.Activity.performCreate (Activity.java:7014)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1215)
  at android.app.ActivityThread.performlaunchActivity (ActivityThread.java:2748)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2873)
  at android.app.ActivityThread.-wrap11 (UnkNown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1602)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6543)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)

解决方法

问题跟踪器上有一个issue for this exact problem。您要么更新到libgdx 1.9.11,要么应该禁用R8,直到您这样做。您可以改用Proguard。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。