探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法

前言
相信这样一个问题,大家都不会陌生,
“有什么的方法可以使Android的程序APK不用安装,而能够直接启动”。
发现最后的结局都是不能实现这个美好的愿望,而腾讯Android手机游戏平台却又能实现这个功能,下载的连连看,五子棋都没有安装过程,但是都能直接运行,这其中到底有什么“玄机”呢,也有热心童鞋问过我这个问题,本文就为大家来揭开这个谜团。
实践
我实现了一个小小的Demo,麻雀虽小五脏俱全,为了突出原理,我就尽量简化了程序,通过这个实例来让大家明白后台的工作原理。
下载demo的apk程序apks,其中包括了两个apk,分别是A和B
这两个APK可分别安装和运行,A程序界面只显示一个Button,B程序界面会动态显示当前的时间
下面的三幅图片分别为直接启动运行A程序(安装TestA.apk),直接启动运行B程序(安装TestB.apk)和由A程序动态启动B程序(安装TestA.apk,TestB.apk不用安装,而是放在/mnt/sdcard/目录中,即SD卡上)的截图,细心的同学可以停下来观察一下他们之间的不同
加载中...加载中...加载中...
后两幅图片的不同,也即Title的不同,则解释出了我们将要分析的后台实现原理的机制
实现原理
最能讲明白道理的莫过于源码了,下面我们就来分析一下A和B的实现机制,首先来分析TestA.apk的主要代码实现:

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                Bundle paramBundle = new Bundle();
                paramBundle.putBoolean("KEY_START_FROM_OTHER_ACTIVITY", true);
                String dexpath = "/mnt/sdcard/TestB.apk";
                String dexoutputpath = "/mnt/sdcard/";
                LoadAPK(paramBundle, dexpath, dexoutputpath);
            }
        });
    }
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btn = (Button) findViewById(R.id.btn);
  btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Bundle paramBundle = new Bundle();
    paramBundle.putBoolean("KEY_START_FROM_OTHER_ACTIVITY", true);
    String dexpath = "/mnt/sdcard/TestB.apk";
    String dexoutputpath = "/mnt/sdcard/";
    LoadAPK(paramBundle, dexpath, dexoutputpath);
   }
  });
 }
代码解析:这就是OnCreate函数要做的事情,装载view界面,绑定button事件,大家都熟悉了,还有就是设置程序B的放置路径,因为我程序中代码是从/mnt/sdcard/TestB.apk中动态加载,这也就是为什么要让大家把TestB.apk放在SD卡上面的原因了。关键的函数就是最后一个了LoadAPK,它来实现动态加载B程序。

    public void LoadAPK(Bundle paramBundle, String dexpath, String dexoutputpath) {
        ClassLoader localClassLoader = ClassLoader.getSystemClassLoader();
        DexClassLoader localDexClassLoader = new DexClassLoader(dexpath,
                dexoutputpath, null, 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[] {});
                Object instance = localConstructor.newInstance(new Object[] {});
                Log.d(TAG, "instance = " + instance);
 
                Method localMethodSetActivity = localClass.getDeclaredMethod(
                        "setActivity", new Class[] { Activity.class });
                localMethodSetActivity.setAccessible(true);
                localMethodSetActivity.invoke(instance, 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();
        }
    }
 public void LoadAPK(Bundle paramBundle, String dexpath, String dexoutputpath) {
  ClassLoader localClassLoader = ClassLoader.getSystemClassLoader();
  DexClassLoader localDexClassLoader = new DexClassLoader(dexpath,
    dexoutputpath, null, 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[] {});
    Object instance = localConstructor.newInstance(new Object[] {});
    Log.d(TAG, "instance = " + instance);

    Method localMethodSetActivity = localClass.getDeclaredMethod(
      "setActivity", new Class[] { Activity.class });
    localMethodSetActivity.setAccessible(true);
    localMethodSetActivity.invoke(instance, 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();
  }
 }
代码解析:这个函数要做的工作如下:加载B程序的APK文件,通过类加载器DexClassLoader来解析APK文件,这样会在SD卡上面生成一个同名的后缀为dex的文件,例如/mnt/sdcard/TestB.apk==>/mnt/sdcard/TestB.dex,接下来就是通过java反射机制,动态实例化B中的Activity对象,并依次调用了其中的两个函数,分别为setActivity和onCreate.看到这里,大家是不是觉得有点奇怪,Activity的启动函数是onCreate,为什么要先调用setActivity,而更奇怪的是setActivity并不是系统的函数,确实,那是我们自定义的,这也就是核心的地方。
好了带着这些疑问,我们再来分析B程序的主代码:

 public class TestBActivity extends Activity {
    private static final String TAG = "TestBActivity";
    private Activity otherActivity;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        boolean b = false;
        if (savedInstanceState != null) {
            b = savedInstanceState.getBoolean("KEY_START_FROM_OTHER_ACTIVITY", false);
            if (b) {
                this.otherActivity.setContentView(new TBSurfaceView(
                        this.otherActivity));
            }
        }
        if (!b) {
            super.onCreate(savedInstanceState);
            // setContentView(R.layout.main);
            setContentView(new TBSurfaceView(this));
        }
    }
 
    public void setActivity(Activity paramActivity) {
        Log.d(TAG, "setActivity..." + paramActivity);
        this.otherActivity = paramActivity;
    }
}
public class TestBActivity extends Activity {
 private static final String TAG = "TestBActivity";
 private Activity otherActivity;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  boolean b = false;
  if (savedInstanceState != null) {
   b = savedInstanceState.getBoolean("KEY_START_FROM_OTHER_ACTIVITY", false);
   if (b) {
    this.otherActivity.setContentView(new TBSurfaceView(
      this.otherActivity));
   }
  }
  if (!b) {
   super.onCreate(savedInstanceState);
   // setContentView(R.layout.main);
   setContentView(new TBSurfaceView(this));
  }
 }

 public void setActivity(Activity paramActivity) {
  Log.d(TAG, "setActivity..." + paramActivity);
  this.otherActivity = paramActivity;
 }
}
代码解析:看完程序B的实现机制,大家是不是有种恍然大悟的感觉,这根本就是“偷梁换柱”嘛,是滴,程序B动态借用了程序A的上下文执行环境,这也就是上面后两幅图的差异,最后一幅图运行的是B的程序,但是title表示的却是A的信息,而没有重新初始化自己的,实际上这也是不可能的,所以有些童鞋虽然通过java的反射机制,正确呼叫了被调程序的onCreate函数,但是期望的结果还是没有出现,原因就是这个上下文环境没有正确建立起来,但是若通过startActivity的方式来启动APK的话,android系统会替你建立正确的执行时环境,所以就没问题。至于那个TBSurfaceView,那就是自定义的一个view画面,动态画当前的时间

 public class TBSurfaceView extends SurfaceView implements Callback, Runnable {
    private SurfaceHolder sfh;
    private Thread th;
    private Canvas canvas;
    private Paint paint;
 
    public TBSurfaceView(Context context) {
        super(context);
        th = new Thread(this);
        sfh = this.getHolder();
        sfh.addCallback(this);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        this.setKeepScreenOn(true);
    }
 
    public void surfaceCreated(SurfaceHolder holder) {
        th.start();
    }
 
    private void draw() {
        try {
            canvas = sfh.lockCanvas();
            if (canvas != null) {
                canvas.drawColor(Color.WHITE);
                canvas.drawText("Time: " + System.currentTimeMillis(), 100,
                        100, paint);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (canvas != null) {
                sfh.unlockCanvasAndPost(canvas);
            }
        }
    }
 
    public void run() {
        while (true) {
            draw();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }
 
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
}
public class TBSurfaceView extends SurfaceView implements Callback, Runnable {
 private SurfaceHolder sfh;
 private Thread th;
 private Canvas canvas;
 private Paint paint;

 public TBSurfaceView(Context context) {
  super(context);
  th = new Thread(this);
  sfh = this.getHolder();
  sfh.addCallback(this);
  paint = new Paint();
  paint.setAntiAlias(true);
  paint.setColor(Color.RED);
  this.setKeepScreenOn(true);
 }

 public void surfaceCreated(SurfaceHolder holder) {
  th.start();
 }

 private void draw() {
  try {
   canvas = sfh.lockCanvas();
   if (canvas != null) {
    canvas.drawColor(Color.WHITE);
    canvas.drawText("Time: " + System.currentTimeMillis(), 100,
      100, paint);
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   if (canvas != null) {
    sfh.unlockCanvasAndPost(canvas);
   }
  }
 }

 public void run() {
  while (true) {
   draw();
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }

 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
 }

 public void surfaceDestroyed(SurfaceHolder holder) {
 }
}
腾讯游戏平台解析
说了这么多,都是背景,O(∩_∩)O哈哈~
其实腾讯游戏平台就是这么个实现原理,我也是通过它才学习到这种方式的,还得好好感谢感谢呢。
腾讯Android游戏平台的游戏分成两类,第一类是腾讯自主研发的,像斗地主,五子棋,连连看什么的,所以实现机制就如上面的所示,A代表游戏大厅,B代表斗地主类的小游戏。第二类是第三方软件公司开发的,可就不能已这种方式来运作了,毕竟腾讯不能限制别人开发代码的方式啊,所以腾讯就开放了一个sdk包出来,让第三方应用可以和游戏大厅相结合,具体可参见QQ游戏中心开发者平台,但这同时就损失了一个优点,那就是第三方开发的游戏要通过安装的方式才能运行。
结论
看到这里,相信大家都比较熟悉这个背后的原理了吧,也希望大家能提供更好的反馈信息!
程序源码下载source:http://up.2cto.com/2012/0429/20120429095938970.zip

posted @ 2015-06-08 10:02  V青山绿水  阅读(365)  评论(0编辑  收藏  举报