How to get access to application classes.dex file - android

I believe that every application can get access to its own files, for example - to classes.dex file.
I wonder how the application get access to its files?
and another question, can I fake this access and get access to another application.?

you can point to the apk file of your application using ApplicationInfo.publicSourceDir
File yourApk = new File(this.getApplicationInfo().publicSourceDir);
//create the destination dir
File tempDir = new File(getFilesDir(), "temp_dir");
//and then unzip the apk to that dir
unzip(yourApk , tempDir);
//now the tempDir will contain all the resources including the dex file and its accessible
this file is a zip file you can decompress that file using this link
How to unzip files programmatically in Android?
so you will have the access to all resources plus the classes.dex
hope this will help

Related

File hierarchy with Android native AAssetManager

Issue
I wonder how to make file hierarchy of assets folder in Android from native code. I'm using AAssetManager_openDir but AAssetDir_getNextFileName doesn't return any directory names so basically I have no way to dive deeper into hierarchy because I can't obtain names of subfolders. Is there any way to resolve this issue with AAssetManager or should I implement this in Java?
Example
Assets looks like
a/
file1
file2
b/
file3
c/
file4
C++ code
AAssetDir* assetDir = AAssetManager_openDir(env, "");
// filename is NULL since there is only folders at the root folder
const char* filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
AAssetDir* assetDir = AAssetManager_openDir(env, "a");
// filename is "file1"
const char* filename = AAssetDir_getNextFileName(assetDir);
// filename is "file2"
filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
EDIT:
I found out that AAssetDir_getNextFileName implementation literally filters directories from output.
Link to source code
You can achieve your goal in three steps, each requires some programming. The bottom line is, your assets all live in the APK file, and your app can find and read the APK file, and its format is a ZIP archive.
Find your APK file name.
Open this file for read as zip archive.
Iterate the zip contents for assets/<whatever>.
For 1), you can use Java or if you must stay in c++, filter /proc/self/fd. You may find some system APK files (framework), but your APK will be the only one that is not system.
For 2), see use zlib to list directory structure of apk file in android. Note that zlib is part of public NDK libraries.

Deleting file from internal storage

How do I delete a file or folder on the internal storage in android with Delphi for android
You should use the TFile and TDirectory classes in the System.IOUtils unit.
For example:
TDirectory.Delete(<YOUR DIR PATH>);
or
TFile.Delete(<YOUR FILE PATH>);
Look in Embarcadero's documentation to get the right path of your files and folders on the various platforms:
Standard RTL Path Functions across the Supported Target Platforms
Referred from :
https://stackoverflow.com/a/27047502/5193608
https://stackoverflow.com/a/3554949/5193608
Main Part from both links:
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following: All files you saved on internal storage All files you saved on external storage using getExternalFilesDir(). However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.
Source : http://developer.android.com/training/basics/data-storage/files.html
Directly you can do is :
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
Hope,it helps!

Android: find a file created in AVD

My application has a function of creating an xls file. After that I need to access that file manually. Now, the problem is, when i run the app on my device, I am able to locate the directories and my xls file in internal storage.
But when I try to run that in the AVD, I am not able to find the file. I also viewed file explorer but there also, I wasn't able to find my file.
Where does the file exists when created in AVD?
Below is the code :
File myDir = new File(Environment.getExternalStorageDirectory()+ "/PB/automation/mydir/");
myFolder.mkdirs();
File file = new File(myFolder, "demo.xls");
Trust me, there is nothing wrong with the code.
Below is the file explorer view :

open assets or raw file as File

Is there a way to open assets or raw resource as File ? I have method that needs open file as File but since i want store my file in assets or in raw folder i cannot access them.
I tried open it with file:///android_asset/filename.xml but received FileNotFound exception.
File file = new File("....");
x.load(file);
Is there a way to open assets or raw resource as File ?
No, sorry. They do not exist as files. You can only get an InputStream.
If you're willing to zip up the file first, you can do this:
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.texfilezip), dir, true);
File textFile = new File(dir, "textfile.txt");

Get folders name from Assets directory

I'm trying to get the names of my folders in "assets". I can get the names of the files with an AssetManager by using the method assetManager.list(). But the problem is that it return only files' name and not folders' names. So I'm trying to use the listFiles() method but i can't access to the Assets directory; I've try the following :
File dir = new File ("file:///android_asset/");
File[] files= dir.listFiles();
But it doesn't work :( ... Is there a way to get the folders' names contained in the Assets directory ?
The URL "file:///android_asset/" doesn't point to a particular directory, it is only used by WebView to address assets. Assets are read-only and defined at compile time, so it's assumed their directory structure is known to the programmer. It's inconvenient, but that's the way it's designed

Categories

Resources