2012-04-12 9 views
3

アクティビティが起動されると、classes.dexファイルがシステムによってロードされ、命令の実行が開始されます。現在のアクティビティが実行されている同じアプリケーションのclasses.dexに読み取り専用でアクセスする必要があります。Androidアプリケーションのclasses.dexにアクセスするには?

ネットで何時間も検索したところ、Androidセキュリティシステムはアプリケーションサンドボックスにアクセスできないと推測できました。

しかし、私は自分の仕事を達成するために、classes.dexファイルへの読み取り専用アクセスが必要です。

これについての洞察はありますか?

ありがとうございます!

+0

これを達成できましたか?私は同じものが必要であり、手がかりを見つけることができません。あなたが私をこのように導くことができれば、大きな助けになるでしょう。 – Sathish

答えて

4

は、あなたがやろうとしているが、あなたはDexFileにアクセスできるかに依存します。

String sourceDir = context.getApplicationInfo().sourceDir; 
DexFile dexFile = new DexFile(sourceDir); 

それはあなたが列挙、およびからクラスをロードすることができhttp://developer.android.com/reference/dalvik/system/DexFile.htmlを提供します。

+0

基本的に、ファイルのInputStreamへのアクセスが必要なclasses.dexのMD5ハッシュを計算しようとしています。しかし、リードしてくれてありがとう! –

+0

おそらくまだApplicationInfo.sourceDirを調べることができます。 Dexファイルはおそらくそこにあります(またはディレクトリの場合はディレクトリにあります)。 – njzk2

+0

簡単な質問ですが、何のためにmd5が必要ですか? – njzk2

2

次のように「classes.dex」のためのInputStreamを取得することができる場合があります

  1. は、アプリケーションのAPKコンテナへのパスを取得します。
  2. JarFileクラスのおかげで、あなたのapkコンテナ内の "classes.dex"エントリを取得します。
  3. 入力ストリームを取得してください。ここで

例示するためのコードの抜粋です:

 // Get the path to the apk container. 
     String apkPath = getApplicationInfo().sourceDir; 
     JarFile containerJar = null; 

     try { 

      // Open the apk container as a jar.. 
      containerJar = new JarFile(apkPath); 

      // Look for the "classes.dex" entry inside the container. 
      ZipEntry ze = containerJar.getEntry("classes.dex"); 

      // If this entry is present in the jar container 
      if (ze != null) { 

       // Get an Input Stream for the "classes.dex" entry 
       InputStream in = containerJar.getInputStream(ze); 

       // Perform read operations on the stream like in.read(); 
       // Notice that you reach this part of the code 
       // only if the InputStream was properly created; 
       // otherwise an IOException is raised 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (containerJar != null) 
       try { 
        containerJar.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     } 

はそれが役に立てば幸い!

関連する問題