2

What I want is to run a libgdx game in a host android app. The game is hoding in jar or apk file, and loaded by the host with classloader dynamiclly. Notice that the game is not installed.

I have written a test host app and it could load the mainActivity of the game. But the problem is the classload load the class like a normal java class but not an android activity. I can even pass the host context to the game class but I don't know how to initialize libgdx application with it.

Here is part of code, and the game is hoding in an 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);
    }
}

Override all the libgdx-backend-android interface may fix my problem, but it's really too ugly.

Daahrien
  • 10,190
  • 6
  • 39
  • 71

0 Answers0