Android- Using DexClassLoader to load apk file

后端 未结 3 1373
礼貌的吻别
礼貌的吻别 2020-12-02 12:38

I\'ve hit a bit of a wall. Any help would be appreciated. I have an app that I want to use DexClassLoader to load another apk file.

Here is my code:

         


        
相关标签:
3条回答
  • 2020-12-02 13:04

    Although the question is old, I will answer because I struggled a bit to find a clear answer for your same question for myself. First, I would like to highlight that a clear requirement in your question is to load a class from an .apk that is not already installed on the device. Therefore, calling the package manager using getPackageManager() and providing it with the package path will clearly lead to NameNotFoundException because the .apk that has the package is not installed on the device.

    So, the way to go about loading classes from an .apk file that is not installed on the device (i.e. you only have the .apk stored in a directory on your SDCARD) is by using DexClassLoader as follows:

    1- Make sure you have the .apk file in a directory on your SDCARD. I've mine Offloadme.apk in the Download folder on my SDCARD.

    2- Add read permission in your AndroidManifest.xml to allow your app to read from the manifest.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    3- Use the following definitions to define the path of the .apk, the class name inside the apk, and method name in that class that you would like to invoke:

    final String apkFile =Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/Offloadme.apk";
    String className = "com.khaledalanezi.offloadme.SimpleCalculator";
    String methodToInvoke = "add"; 
    

    4- Use the DexClassLoader to load the .apk and call the add method in the SimpleCalculator class using reflection as follows:

        final File optimizedDexOutputPath = getDir("outdex", 0);
    
        DexClassLoader dLoader = new DexClassLoader(apkFile,optimizedDexOutputPath.getAbsolutePath(),
                null,ClassLoader.getSystemClassLoader().getParent());
    
        try {
            Class<?> loadedClass = dLoader.loadClass(className);
            Object obj = (Object)loadedClass.newInstance();
            int x =5;
            int y=6;
            Method m = loadedClass.getMethod(methodToInvoke, int.class, int.class);
            int z = (Integer) m.invoke(obj, y, x);              
            System.out.println("The sum of "+x+" and "+"y="+z);
    
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    Note that in my simple example, I added two numbers using the add method available in the SimpleCalculator class loaded from the Offloadme.apk file stored on my SDCARD and I was able to print the correct answer which is 11.

    0 讨论(0)
  • 2020-12-02 13:19

    Try using Android's PathClassLoader:

        String packagePath = "com.mypackage";
        String classPath = "com.mypackage.ExternalClass";
    
        String apkName = null;
        try {
            apkName = getPackageManager().getApplicationInfo(packagePath,0).sourceDir;
        } catch (PackageManager.NameNotFoundException e) {
            // catch this
        }
    
        // add path to apk that contains classes you wish to load
        String extraApkPath = apkName + ":/path/to/extraLib.apk" 
    
        PathClassLoader pathClassLoader = new dalvik.system.PathClassLoader(
                apkName,
                ClassLoader.getSystemClassLoader());
    
        try {
            Class<?> handler = Class.forName(classPath, true, pathClassLoader);
        } catch (ClassNotFoundException e) {
            // catch this
        }
    
    0 讨论(0)
  • 2020-12-02 13:20

    You can't do that. Even if you're able to access classes from external file, Android still does not know them. And you don't run activities directly, but by requesting Android to run them, so they have to be registered/installed into system.

    0 讨论(0)
提交回复
热议问题