Reflection: Accessing the app directory

ukimiku

Active Member
Licensed User
Longtime User
Using the Reflection library, how can a B4A app determine the directory path in which it is stored? I would like to calculate a checksum of the app. Maybe I will have to copy it to another location in the Android file system?

Any help greatly appreciated. Thank you.

Regards,
 

margret

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
Thank you, both for your reply and for the link. If you should find the info on the system, please post it or a link to it.

Regards,
 
Upvote 0

frapel

Active Member
Licensed User
Longtime User
Using the Reflection library, how can a B4A app determine the directory path in which it is stored? I would like to calculate a checksum of the app. Maybe I will have to copy it to another location in the Android file system?

If you mean the dir path where the binary package of an installed app (apk) is located, give a look at the following code (that use the Reflection lib):

B4X:
'retrieves android.content.pm.PackageInfo
'throws PackageManager.NameNotFoundException if a package with the given name can not be found on the system
Sub getPackageInfo(packageName As String, flags As Int) As Object
   Dim pmObj As Reflector
   pmObj.Target = getPackageManager
   Return pmObj.RunMethod3("getPackageInfo", packageName, "java.lang.String", flags, "java.lang.int")
End Sub

'retrieves field of android.content.pm.PackageInfo
Sub getPackageInfoField(packageInfo As Object, fieldName As String) As Object
   Dim piField As Reflector
   piField.Target = packageInfo
   Return piField.GetField(fieldName)
End Sub

'retrieves android.content.pm.ApplicationInfo
Sub getApplicationInfo(packageInfo As Object) As Object
   Return getPackageInfoField(packageInfo, "applicationInfo") 'ApplicationInfo
End Sub

'retrieves field of android.content.pm.ApplicationInfo
Sub getApplicationInfoField(applicationInfo As Object, fieldName As String) As Object
   Dim aiField As Reflector
   aiField.Target = applicationInfo
   Return aiField.GetField(fieldName)
End Sub

'retrieves application public source dir
Sub getApplicationPublicSourceDir(applicationInfo As Object) As String
   Return getApplicationInfoField(applicationInfo, "publicSourceDir")
End Sub

With the following call you will obtain the location of the app (the apk, for non-protected apps) whose package name is "packagenameofyourapp":

B4X:
Dim apkLocation as String
apkLocation = getApplicationPublicSourceDir(getApplicationInfo(getPackageInfo("packagenameofyourapp", 0)))

For example, you may replace "packagenameofyourapp" in the last line of code with something like:

com.android.browser (the default browser in your device)
com.google.android.gm (the gmail app)
com.rovio.angrybirds (angry birds game)

I separated the above methods and then composed the final call to give you some hints about the structure of an installed app in android operating systems.

Moreover, you'll find complete information on:
Package Index | Android Developers

regards,
Francesco
 
Last edited:
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
Hi frapel,

that was exactly what I was trying to achieve. Thank you so much for your thorough and very useful reply and your code. And, of course, for the link. I think I will challenge myself to understand all the structural aspects so I can actually understand what your code does.

Terrific, thank you!

Regards,
 
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
frapel,

after rereading your post, I noticed that you wrote "where the binary package..." Does "binary" here mean that the app has already been compiled to native code on the device? My understanding is that the Java (since Android 2.x) comes with a JIT compiler that performs the translating. Then creating a checksum routine would be quite difficult since I would expect the "binaries" of my app to differ wildly across different OS versions, processor types, etc.

?

Thank you.

Regards,
 
Upvote 0

frapel

Active Member
Licensed User
Longtime User
ukimiku,
with "binary" I intended the apk file generated with your Android IDE (Basic4android, Eclipse, and similar RAD) after compiling, signing and packaging your source code. It is the same apk file you'll publish on the market (Google Play) and that people will install on their devices.

Give a look at:
APK (file format) - Wikipedia, the free encyclopedia

If you calculate the checksum of a specific apk the result will remain the same on different Android devices where that apk could be deployed.

Regards,
Francesco
 
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
The goal is to determinr whether or not the application bundle has been modified in any way. Have you ever coded such a task?

Regards,
 
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
Frapel, thanks for the link! You have been very helpful.

Regards,
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…