为Android插件创建一个libgdx游戏

时间:2013-12-03 04:13:25

标签: android plugins libgdx classloader

我想要的是在主机Android应用程序中运行libgdx游戏。游戏正在jar或apk文件中,并由主机与classloader动态加载。 注意未安装游戏。

我已经编写了一个测试主机应用程序,它可以加载游戏的mainActivity。但问题是类加载类正常的java类而不是android活动。我甚至可以将主机上下文传递给游戏类,但我不知道如何用它初始化libgdx应用程序。

这是代码的一部分,游戏正在用apk进行。

HOST:

    privare void LoadAPK(Bundle paramBundle, String dexpath, String dexoutputpath, String libpath) {
            ClassLoader localClassLoader = ClassLoader.getSystemClassLoader();
            DexClassLoader localDexClassLoader = new DexClassLoader(dexpath,
                    dexoutputpath, libpath, localClassLoader);
        try {
            PackageInfo plocalObject = getPackageManager().getPackageArchiveInfo(dexpath, 1);

            if ((plocalObject.activities != null)
                    && (plocalObject.activities.length > 0)) {

                // 
                String activityname = plocalObject.activities[0].name;
                Log.d(TAG, "activityname = " + activityname);
                Class<?> localClass = localDexClassLoader.loadClass(activityname);
                Constructor<?> localConstructor = localClass.getConstructor(new Class[] {Context.class});
                // here to past host context to plugin
                Object instance = localConstructor.newInstance(new Object[] {this});

                Method methodonCreate = localClass.getDeclaredMethod("onCreate", new Class[] { Bundle.class });
                methodonCreate.setAccessible(true);
                methodonCreate.invoke(instance, new Object[] { paramBundle });
            }
            return;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
   }

GAME PLUGIN:

public class GameActivity extends AndroidApplication {
    private Game game;
        private Context hostContext;

    FetchingCoinActivity(Context context) {
        this.hostContext = context;
    } 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.game = new MyGame();
        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;

        // FIXME HERE IS THE PROBLEM, how to initialize libgdx environment using host context?
        initialize(game, cfg);
    }
}

覆盖所有libgdx-backend-android接口可能会解决我的问题,但它确实太难看了。

0 个答案:

没有答案