有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android DexClassLoader错误,“优化数据目录..”。。不属于当前用户的

我正在尝试生成一个简单的安卓应用程序,它可以在运行时从SD卡加载一个DEX文件

该应用程序有两个活动。第一个活动是一个带有按钮的简单屏幕。按下按钮后,将启动第二个活动,从而调用loadDex()方法。loadDex()方法尝试在SD卡上定位jar文件并将其加载到当前应用程序中

以下是我的第一个活动代码:

package poc.example.del.customclass;

import 安卓.content.Intent;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void launchLoadClass(View view) {
        Intent intent = new Intent(MainActivity.this, LoadClass.class);
        startActivity(intent);
    }
}

下面是我的第二个活动(加载DEX文件的活动)的代码:

package poc.example.del.customclass;

import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;


public class LoadClass extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_class);
        loadDex();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_load_class, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void loadDex() {
        String dexFile = "/sdcard/output.jar";
        File jarFile = new File(dexFile);
        if (jarFile.exists()) {
            // Toast.makeText(getApplicationContext(), "It Worked!", Toast.LENGTH_LONG).show();
            DexClassLoader cl = new DexClassLoader (jarFile.toString (), "/data/test", null, ClassLoader.getSystemClassLoader());

        }
    }
}

调用DexClassLoader构造函数时会出现问题。在日志中可以找到以下错误:

03-25 10:15:48.441    1934-1934/poc.example.del.customclass E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{poc.example.del.customclass/poc.example.del.customclass.LoadClass}: java.lang.IllegalArgumentException: Optimized data directory /data/test is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at 安卓.app.ActivityThread.access$600(ActivityThread.java:141)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at 安卓.os.Handler.dispatchMessage(Handler.java:99)
            at 安卓.os.Looper.loop(Looper.java:137)
            at 安卓.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: Optimized data directory /data/test is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
            at dalvik.system.DexFile.<init>(DexFile.java:100)
            at dalvik.system.DexFile.loadDex(DexFile.java:149)
            at dalvik.system.DexPathList.loadDexFile(DexPathList.java:261)
            at dalvik.system.DexPathList.makeDexElements(DexPathList.java:229)
            at dalvik.system.DexPathList.<init>(DexPathList.java:96)
            at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:56)
            at dalvik.system.DexClassLoader.<init>(DexClassLoader.java:57)
            at poc.example.del.customclass.LoadClass.loadDex(LoadClass.java:54)
            at poc.example.del.customclass.LoadClass.onCreate(LoadClass.java:23)
            at 安卓.app.Activity.performCreate(Activity.java:5104)
            at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at 安卓.app.ActivityThread.access$600(ActivityThread.java:141)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at 安卓.os.Handler.dispatchMessage(Handler.java:99)
            at 安卓.os.Looper.loop(Looper.java:137)
            at 安卓.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

以下是日志中我认为代表问题的一行:

java.lang.IllegalArgumentException: Optimized data directory /data/test is not owned by the current user. Shared storage cannot protect your application from code injection attacks.

如果您能提供任何帮助,我将不胜感激,因为我在网上几乎没有找到关于这个问题的信息。我正在为Android 4.2 Api 17开发应用程序

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    经过几天的学习,我找到了答案。我想我会在这里发布解决方案,以防其他人有类似的问题

    出于安全原因,Android不允许应用程序将文件加载到任何随机文件夹。相反,它应该加载到应用程序环境中。下面是修改后的代码,它允许我继续这个项目。显示的代码是针对“loadDex()”方法的:

    public void loadDex() {
        // Toast the show the method has been invoked correctly
        // Toast.makeText(getApplicationContext(), "loadDex() Method invoked", Toast.LENGTH_LONG).show();
    
        // name of the DEX file
        String dexFile = "/output.jar";
    
        // Get the path to the SD card
        File f = new File(Environment.getExternalStorageDirectory().toString() + dexFile);
    
        // optimized directory, the applciation and package directory
        final File optimizedDexOutputPath = getDir("outdex", 0);
    
        // DexClassLoader to get the file and write it to the optimised directory
        DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(),
        optimizedDexOutputPath.getAbsolutePath(),null, getClassLoader());
    
        // The classpath is created for the new class
        String completeClassName = "poc.example.del.mylibrary.name";
        String methodToInvoke = "display";
    
        try {
            Class<?> myClass = classLoader.loadClass(completeClassName);
            Object obj = (Object)myClass.newInstance();
            Method m = myClass.getMethod(methodToInvoke);
            String s = ""+m.invoke(obj);
            makeToast(s);
        }
        catch (Exception e) {
            e.printStackTrace();
            makeToast("Something went wrong!");
        }
    
    
    }
    

    解决问题的特定代码行是:

    // DexClassLoader to get the file and write it to the optimized directory
    DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(),
    optimizedDexOutputPath.getAbsolutePath(),null, getClassLoader());
    

    如您所见,optimizedDexOutputPath,getAbsolutePath()方法返回应用程序可用于向其写入文件的目录

    希望这能帮助其他有类似问题的人