2

実行時に SD カードから DEX ファイルをロードできる単純な Android アプリケーションを作成しようとしています。

アプリケーションには 2 つのアクティビティがあります。最初のアクティビティは、ボタンのある単純な画面です。ボタンが押されると、loadDex() メソッドが呼び出される 2 番目のアクティビティが起動されます。loadDex() メソッドは、SD カード上の jar ファイルを見つけて、現在のアプリケーションにロードしようとします。

最初のアクティビティのコードは次のとおりです。

package poc.example.del.customclass;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.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);
    }
}

2 番目のアクティビティ (DEX ファイルをロードするアクティビティ) のコードは次のとおりです。

package poc.example.del.customclass;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.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 android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.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 向けのアプリケーションを開発しています。

前もって感謝します。

4

1 に答える 1

3

さまざまなチュートリアルを数日行った後、答えを見つけました。他の誰かが同様の問題を抱えている場合に備えて、ここに解決策を投稿すると思いました。

セキュリティ上の理由から、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() メソッドは、アプリケーションがファイルを書き込むために使用できるディレクトリを返します。

これが同様の問題を抱えている他の人に役立つことを願っています!

于 2015-03-27T10:52:53.327 に答える