Android APK崩溃与ProGuard和JSON

时间:2015-08-03 22:58:14

标签: java android json libgdx proguard

我已经将我的LibGDX项目导出为带有ProGuard的Android APK,并且在我的手机(三星Galaxy S4)上运行时,它在启动时崩溃。

据我所知,问题似乎是使用ProGruard并尝试从JSON文件加载以前保存的数据。每次程序退出时,它都会将程序的当前状态保存到JSON文件中,当程序再次启动时,它会从JSON文件中读取数据并将程序重置为以前的状态。

从Eclipse中运行它时程序不会崩溃(因为当时没有使用ProGuard)而且我不确定如何从我手机上安装的APK获取崩溃日志...这使得这么多更难排除故障。但我已经确定了:

  • 如果在禁用ProGuard的情况下创建APK,则不会发生崩溃。
  • 如果启用了ProGuard,也不会发生崩溃,但我绕过从JSON文件加载数据,而只是使用默认值。

这是我的代码:

Logic.java

public class Logic{
    private RecallLastPreferences recall;
    public DataMain dataMain;

    public Logic(){
        //. . .
        recall = new RecallLastPreferences(this);
        dataMain = recall.loadPreferences();
        //. . .
    }
}

RecallLastPreferences.java

public class RecallLastPreferences {
    private Logic logic;    
    private SaveManager saveManager;

    public RecallLastPreferences(Logic logic){
        this.logic = logic;
        saveManager = new SaveManager();
    }

    public void savePreferences(){      
        saveManager.saveDataValue("main", logic.dataMain);
    }

    public <T> T loadPreferences(){
        return saveManager.loadDataValue("main", DataMain.class);
    }
}

SaveManager.java

public class SaveManager {
    private ObjectMap<String, Object> data = new ObjectMap<String, Object>();
    private FileHandle file = Gdx.files.local("save.json");

    @SuppressWarnings("unchecked")
    public SaveManager(){
        if(file.exists()){
            Json json = new Json();
            data = json.fromJson(ObjectMap.class, file.readString());
        }
    }

    private void saveToJson(){
        Json json = new Json();
        json.setOutputType(OutputType.json);
        file.writeString(json.prettyPrint(data), false);
    }

    @SuppressWarnings("unchecked")
    public <T> T loadDataValue(String key, Class<?> type){      
        if(data.containsKey(key)){
            return (T)data.get(key);
        }
        return null;
    }

    public void saveDataValue(String key, Object object){
        data.put(key, object);
        saveToJson();
    }

    public ObjectMap<String, Object> getAllData(){
        return data;
    }
}

proguard的-project.txt

-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

-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);
}

-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, float, float, float);
}

0 个答案:

没有答案