Send .dex file via post request android studio -
for own purposes, want send other app's dex file remote server , answer.
i've used answers found here. tried create simple example @ first, connect server , upload dex file. far haven't managed extract dex other apps , thought of using dex file have. i've read, not common files should stored either "/res/raw" or "assets" tried many ways load file none of them worked . path used in cases found in right click on file -> copy reference.
create res folder under /raw ,
file f = new file("res/raw/filename.dex");
create new assets folder under /app
file f = new file("filename.dex");
create assets folder under /main
file f = new file("main/assets/filename.dex");
and on.
the way managed using inputstream
inputstream in = getresources().openrawresources(r.raw.filename_without_dex)
but couldn't cast file, dropped solution.i want have file cause following post request must multipart/form.
in java, way "load" file straightforward. why not in android ?
a ugly, quick , dirty solution.
no error check or cleanup.
works.
import android.content.pm.applicationinfo; import android.content.pm.packagemanager; import android.os.bundle; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.randomaccessfile; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.menu; import android.view.menuitem; import java.util.arraylist; import java.util.list; import java.util.zip.zipentry; import java.util.zip.zipfile; import java.util.zip.zipinputstream; public class mainactivity extends actionbaractivity { final string tag = "getdex"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getapppaths(); // list packages string s = "/data/app/softkeyboard/softkeyboard.apk"; // sample package byte[] b = getfile(s); byte[] dex = unzipdex(b, s); log.d(tag, string.format("dex size %d", dex.length)); } list<string> getapppaths() { final packagemanager pm = getpackagemanager(); //get list of installed apps. list<applicationinfo> packages = pm.getinstalledapplications(packagemanager.get_meta_data); list<string> paths = new arraylist<>(); (applicationinfo packageinfo : packages) { log.d(tag, "installed package :" + packageinfo.packagename); log.d(tag, "apk file path:" + packageinfo.sourcedir); paths.add(packageinfo.sourcedir); } return paths; } byte[] getfile(string filename) { try { randomaccessfile f = new randomaccessfile(filename, "r"); byte[] b = new byte[(int)f.length()]; f.readfully(b); return b; } catch(ioexception exception) { exception.printstacktrace(); } return null; } public byte[] unzipdex(byte[] bytes, string filename) { try{ zipfile zipfile = new zipfile(filename); zipinputstream zis = new zipinputstream(new bytearrayinputstream(bytes)); zipentry ze = zis.getnextentry(); while(ze!=null){ string entryname = ze.getname(); if(!entryname.equals("classes.dex")) { ze = zis.getnextentry(); continue; } inputstream = zipfile.getinputstream(ze); bytearrayoutputstream buffer = new bytearrayoutputstream(); int nread; byte[] data = new byte[16384]; while ((nread = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nread); } buffer.flush(); return buffer.tobytearray(); } system.out.println("done"); }catch(ioexception ex){ ex.printstacktrace(); } return null; }
}
Comments
Post a Comment