minifyEnabled casues AssetManager to crash when opening asset - android

I am using pytorch and a Python model located in my apps asset folder using the below code to get the file path for the model file
fun assetFilePath(context: Context, assetName: String): String? {
val file = File(context.filesDir, assetName)
if (file.exists() && file.length() > 0) {
return file.absolutePath
}
context.assets.open(assetName).use { inputStream ->
FileOutputStream(file).use { os ->
val buffer = ByteArray(4 * 1024)
var read: Int
while (inputStream.read(buffer).also { read = it } != -1) {
os.write(buffer, 0, read)
}
os.flush()
}
return file.absolutePath
}
}
which is then loaded into a Module using
module = LiteModuleLoader.load(context?.let { assetFilePath(it, "model0210.ptl") })
However my problem arises when creating a release build for the app and setting minifyEnabled = true which causes a FileNotFoundException when running the release build. This does not occur when minifyEnabled is set to false however I would like to be able to run my release build with minifyEnabled to reduce the overall size of my app. Does anyone have any insight onto how this might be achieved?
I have tried using AnalyzeAPK to check that the asset file still exists in the release build and as seen in the image below it still exists however AssetManager.open() still causes this issue to occur.

Related

accessing file system on rooted device android

I have made a simple app to populate recycler view with video or audio files. The recycler view get populated when installing app on any android device through android studio but I have a mobile device which is rooted. On this device, no audio or video file is detected nor recycler view get populated. Any solution for this problem please?
#Klox
private var videosPaths:ArrayList<File> = ArrayList()
private lateinit var pathsViewModel: PathsViewModel
private val videoFormat = arrayOf(".mp4", ".MP4", ".mkv", ".MKV")
// here I'm inserting video/audio files in database created
// room database files are not shown here
val pathsDao = PathsDatabase.getPathsInstance(applicationContext).pathsDao()
val pathsRepository = PathsRepository(pathsDao)
val pathsMainDatabase = PathsMainDatabase()
pathsViewModel = ViewModelProvider(this, PathsViewModelFactory(pathsRepository))[PathsViewModel::class.java]
// I have created database with name "video_paths"
if(!applicationContext.getDatabasePath("videos_paths").exists()) {
videosPaths = getVideoFolders()
// here videoPaths returns 0 video folders on rooted device and as app
// opens, it gives toast message: "shell granted superuser access"
videosPaths.forEach{ file ->
pathsViewModel.insertPath(Paths(0, file.absolutePath))
}
}
// my custom code for extracting video/audio files in device w/o using cursor library
//getting video folders
fun getVideoFolders() : ArrayList<File> {
searchVideoFiles(Environment.getExternalStorageDirectory())
return videoFolders
}
// a recursive function to scan video files in device all folders
private fun searchVideoFiles(file: File) {
val dir = file.listFiles() ?: return
for (f in dir) {
if (f.isDirectory) {
searchVideoFiles(f)
if (isVideoFolder(f)) videoFolders.add(f)
}
}
}
// return true if at least one video file is found
private fun isVideoFolder(file: File?): Boolean {
assert(file != null)
assert(file!!.isDirectory)
val dir = file.listFiles() ?: return false
for (f in dir) {
val path = f.absolutePath
for(v in videoFormat) {
if(path.endsWith(v)) return true
}
}
return false
}

FileObserver not working on Android SDK 30

I am working on an android application where i have used FileObserver to get data changes on a specific folder. The folder is WhatsApp Images and whenever there is a new file added to that folder i perform my further work when onEvent of FileObserver is triggered.
Everything works fine until i set targetSdkVersion=29 but as i am migrating my project to targetSdkVersion=30 FileObserver stopped working.
Below is my code for FileObserver
import android.os.Environment
import android.os.FileObserver
import android.util.Log
import java.io.File
class WhatsAppImageObserver11(var notify:(msg:String)->Unit) : FileObserver(
File(
Environment.getExternalStorageDirectory().toString(),
Constants.whatsapp_images_path11).toString(), ALL_EVENTS
) {
init {
Log.d("WhatsAppImageObserver11", "start")
}
override fun onEvent(i: Int, str: String?) {
val str2 = "WhatsAppImageObserver11"
if (i == CREATE || i == MOVED_TO && str != ".probe") {
val sb = StringBuilder()
sb.append("create File path--> ")
sb.append(str)
Log.d(str2, sb.toString())
try {
val whatsDeleted = File(
Environment.getExternalStorageDirectory().path,
Constants.whatsapp_reserved_media
)
if(!whatsDeleted.exists()) {
whatsDeleted.mkdirs()
}
val srcFile = File(
Environment.getExternalStorageDirectory(),
Constants.whatsapp_images_path11+str)
val destFile = File(Environment.getExternalStorageDirectory(), Constants.whatsapp_reserved_media+str)
if (srcFile.exists()){
srcFile.copyTo(target = destFile, overwrite = false, bufferSize = DEFAULT_BUFFER_SIZE)
}
} catch (e: Exception) {
val sb2 = StringBuilder()
sb2.append("create error: ")
sb2.append(e.toString())
Log.d(str2, sb2.toString())
}
}
if (i and 512 != 0 || i and 1024 != 0) {
val sb3 = StringBuilder()
sb3.append("dlete File path--> ")
sb3.append(str)
Log.d(str2, sb3.toString())
try {
val whatsDeleted = File(
Environment.getExternalStorageDirectory().path,
Constants.new_whatsapp_deleted_media
)
if(!whatsDeleted.exists()) {
whatsDeleted.mkdirs()
}
val srcFile = File(Environment.getExternalStorageDirectory().path, Constants.whatsapp_reserved_media+str)
val destFile = File(Environment.getExternalStorageDirectory().path, Constants.new_whatsapp_deleted_media+str)
if (srcFile.exists()){
srcFile.copyTo(target = destFile, overwrite = false, bufferSize = DEFAULT_BUFFER_SIZE)
srcFile.delete()
notify(destFile.absolutePath)
}
} catch (e2: Exception) {
val sb4 = StringBuilder()
sb4.append("del error: ")
sb4.append(e2.toString())
Log.d(str2, sb4.toString())
}
}
}
}
Here in this FileObserver i am copying media from WhatsApp Reserved Media to My own WhatsAppDeleted folder for media recovery feature.
What i have tried?
1- As i know Environment.getExternalStorageDirectory() is deprecated i tried replacing it with mContext.getExternalFilesDir(null).getAbsolutePath()
2- Also checked using android:preserveLegacyExternalStorage="true" in Manifest.
3- Tried added ignore attribute in
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage"/>
What is Required?
1- FileObserver on path "/Android/media/" + WHATSAPP_PKG_NAME + "/WhatsApp/Media/WhatsApp Images/" should trigger onEvent when i set targetSdkVersion-30
Can somebody please help me out with this? Any help will be appreciated.
Thank you
As written here:
Apps that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag.
Basically, you don't have permission to "listen" to external directories in the device, but only to your own internal/external directories.
You should use the MediaStore API in order to access media files which have been scanned by the OS (Including WhatsApp's files).
You can use components like JobService to register for the event of a new media been scanned.
Enjoy

How to parse an APK after converting it to a minimal one?

Background
I want to be able to parse APK files from various sources, or various kinds. I want to get only very specific, basic information about them:
package-name
version-code
version-name
label (app name)
icon
minSdkVersion
whether it's a split APK or not (available when parsing the manifest)
The Android framework has a single function to parse APK files (PackageManager.getPackageArchiveInfo), but it has 2 disadvantages:
It requires a file-path. This isn't always available, as you might have a Uri to deal with, or you have the APK file being inside some ZIP file.
It can't handle split APK files. Only the base one of them. If you try it on split APK files, you just get null.
Because of this, I've tried to find a reliable, Java&Kotlin solution that can handle them easily.
The problem
Each solution I've found has its own disadvatanges. Some libraries I couldn't even find how to use, and some don't have any way to parse APK files without having a real file path. Some of them just create new files instead of returning you objects in Java/Kotlin. Some are even in other programming languages, making me wonder if it's possible to use them.
So far, the only one that I've found as good enough, is "hsiafan" apk-parser, which needs only 2 files within the APK: manifest and resources files. It has some issues, but usually it can get you the information I've mentioned.
Thing is, as I wrote, it's not always working, so I want to return to the basics, at least when I notice it fails to parse, at least for the case that it's a normal APK of base of split-APK files. Some cases it couldn't handle well are:
Sometimes the app name couldn't be fetched correctly (here).
Some resources are somehow hidden in some apps (here). Even tools such as Jadx fail to show them, but others can.
Adaptive icon is hard to parse, including drawable XML files. I succeed parsing VectorDrawable though (here), but it's quite a workaround.
And a few others.
What I've tried
So, I wanted to try out the Android framework again, but this time with a new idea: instead of handling the entire original APK, I could extract only what it really needs, based on the current observation of it.
For example, if I know the resources I need to handle, I can copy only them (and some key files that are needed) into a new APK, and ditch the rest. This could reduce the need to copy/download huge amount of data just to parse the APK and get information about it. If the APK file is 100MB, for example, there is no need to get it all when all we need is just a tiny portion of it.
As a start, I wanted to see how I can create a new APK that can be parsed, so for now I copied all of the entries of the original APK (currently of the current app) into a new file, and chose to parse it:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null)
return
thread {
val originalApplicationInfo = packageManager.getApplicationInfo(packageName, 0)
val filePath = originalApplicationInfo.publicSourceDir
val outputFile = File(cacheDir, "test.apk")
outputFile.parentFile!!.mkdirs()
outputFile.delete()
ZipFile(filePath).use { zipFile ->
ZipOutputStream(FileOutputStream(outputFile)).use { zipOutputStream ->
for (entry in zipFile.entries()) {
val name = entry.name
zipOutputStream.putNextEntry(ZipEntry(name))
zipFile.getInputStream(entry).use { it.copyTo(zipOutputStream.buffered()) }
zipOutputStream.closeEntry()
}
}
}
val originalLabel = originalApplicationInfo.loadLabel(packageManager)
val originalIcon: Drawable? = originalApplicationInfo.loadIcon(packageManager)
Log.d("AppLog", "originalPackageInfo: label:$originalLabel appIcon:${originalIcon?.javaClass?.simpleName}")
//
val packageArchiveInfo = packageManager.getPackageArchiveInfo(outputFile.absolutePath, 0)
val label = packageArchiveInfo?.applicationInfo?.loadLabel(packageManager)?.toString()
val appIcon = packageArchiveInfo?.applicationInfo?.loadIcon(packageManager)
Log.d("AppLog", "packageArchiveInfo!=null?${packageArchiveInfo != null} label:$label appIcon:${appIcon?.javaClass?.simpleName}")
}
}
}
The file indeed gets generated, but for some reason the Android framework failed to parse it, as packageArchiveInfo is null.
The questions
How come the sample I've made doesn't work? How come the new APK can't be parsed?
What is the minimal set of files the getPackageArchiveInfo function requires to be able to parse the APK, just for the information I've mentioned above?
If this kind of solution doesn't work, is there perhaps a library that could handle APK files of all kinds with all the information I've mentioned, no matter the source (including Uri and within zip files)?
Edit: as suggested, I could copy just those of folder "AndroidManifest.xml", "resources.arsc", "res" , but it seems it can still not always work well, as the icons don't always get to be the same:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null)
return
thread {
val installedApplications = packageManager.getInstalledPackages(0)
Log.d("AppLog", "checking ${installedApplications.size} apps")
for (originalPackageInfo in installedApplications) {
val originalApplicationInfo = originalPackageInfo.applicationInfo
val filePath = originalApplicationInfo.publicSourceDir
val outputFile = File(cacheDir, "test.apk")
outputFile.parentFile!!.mkdirs()
outputFile.delete()
val toExtract = setOf<String>("AndroidManifest.xml", "resources.arsc", "res")
ZipFile(filePath).use { zipFile ->
ZipOutputStream(FileOutputStream(outputFile)).use { zipOutputStream ->
for (entry in zipFile.entries()) {
val name = entry.name
if (toExtract.contains(name.split("/")[0])) {
zipOutputStream.putNextEntry(ZipEntry(name))
zipFile.getInputStream(entry).use { inStream ->
zipOutputStream.buffered().apply {
inStream.copyTo(this)
}.flush()
}
}
}
}
}
val packageName = originalApplicationInfo.packageName
val originalLabel = originalApplicationInfo.loadLabel(packageManager)
val originalIcon: Drawable? = originalApplicationInfo.loadIcon(packageManager)
val originalIconBitmap = originalIcon?.toBitmap()
//
val packageArchiveInfo = packageManager.getPackageArchiveInfo(outputFile.absolutePath, 0)
if (packageArchiveInfo == null) {
Log.e("AppLog", "$packageName could not parse generated APK")
continue
}
val label = packageArchiveInfo.applicationInfo.loadLabel(packageManager).toString()
val appIcon = packageArchiveInfo?.applicationInfo?.loadIcon(packageManager)
val appIconBitmap = appIcon?.toBitmap()
when {
label != originalLabel ->
Log.e("AppLog", "$packageName got wrong label $label vs $originalLabel")
packageArchiveInfo.versionName != originalPackageInfo.versionName ->
Log.e("AppLog", "$packageName got wrong versionName ${packageArchiveInfo.versionName} vs ${originalPackageInfo.versionName}")
packageArchiveInfo.versionCode != originalPackageInfo.versionCode ->
Log.e("AppLog", "$packageName got wrong versionCode ${packageArchiveInfo.versionCode} vs ${originalPackageInfo.versionCode}")
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && packageArchiveInfo.applicationInfo.minSdkVersion != originalApplicationInfo.minSdkVersion ->
Log.e("AppLog", "$packageName got wrong minSdkVersion ${packageArchiveInfo.applicationInfo.minSdkVersion} vs ${originalApplicationInfo.minSdkVersion}")
appIcon?.javaClass?.name != originalIcon?.javaClass?.name ->
Log.e("AppLog", "$packageName got different app icon type: ${appIcon?.javaClass?.simpleName} vs ${originalIcon?.javaClass?.simpleName}")
originalIconBitmap != null && appIconBitmap != null && (originalIconBitmap.width != appIconBitmap.width || originalIconBitmap.height != appIconBitmap.height) ->
Log.e("AppLog", "$packageName got wrong app icons sizes:${appIconBitmap.width}x${appIconBitmap.height} vs ${originalIconBitmap.width}x${originalIconBitmap.height}")
originalIconBitmap != null && appIconBitmap != null && !areBitmapsSame(originalIconBitmap, appIconBitmap) ->
Log.e("AppLog", "$packageName got wrong app icons content ")
(originalIconBitmap == null && appIconBitmap != null) || (originalIconBitmap != null && appIconBitmap == null) ->
Log.e("AppLog", "$packageName null vs non-null app icon: ${appIconBitmap != null} vs ${originalIconBitmap != null}")
}
}
Log.d("AppLog", "done")
}
}
fun areBitmapsSame(bitmap: Bitmap, bitmap2: Bitmap): Boolean {
if (bitmap.width != bitmap2.width || bitmap.height != bitmap2.height)
return false
for (x in 0 until bitmap.width)
for (y in 0 until bitmap.height)
if (bitmap.getPixel(x, y) != bitmap2.getPixel(x, y))
return false
return true
}
}
I think that since app icons are very complex and depend on various resources (which might even be hidden in weird ways), there is no other choice than to actually have the file on the file system.
EDIT: about getting the app icons, I actually didn't use it well for the APK file. I used something a bit different on my own app, which seems to work well here too, except for some cases that the icon is a bit different (shape/color might be different for example), probably due to different configurations. But at least it won't return you the default app icon of Android when the app clearly has a non-default app icon.
Sadly though, it didn't always get good app icon when creating the minimized APKs (sometimes returned me ColorStateListDrawable or ColorDrawable, for example), probably because sometimes the original APK has hidden resources in non-conventional paths. So this is how you can get the app icon, assuming you have the whole APK:
Before getting it, use:
packageArchiveInfo.applicationInfo.publicSourceDir = targetFilePath
packageArchiveInfo.applicationInfo.sourceDir = targetFilePath
And then call this function:
fun getAppIcon(context: Context, applicationInfo: ApplicationInfo): Drawable? {
val packageManager = context.packageManager
try {
val iconResId = applicationInfo.icon
if (iconResId != 0) {
val resources: Resources = packageManager.getResourcesForApplication(applicationInfo)
val density = context.resources.displayMetrics.densityDpi
var result = ResourcesCompat.getDrawableForDensity(resources, iconResId, density, null)
if (result != null)
return result
}
} catch (e: Exception) {
// e.printStackTrace()
}
try {
val applicationIcon = packageManager.getApplicationIcon(applicationInfo)
// Log.d("AppLog", "getApplicationIcon type:${applicationIcon.javaClass.simpleName}")
return applicationIcon
} catch (ignored: Exception) {
}
return null
}
To convert to Bitmap, you can use:
val appIconBitmap = try {
appIcon?.toBitmap(appIconSize, appIconSize)
} catch (e: Exception) {
e.printStackTrace()
null
}
And to get the app icon size, you can use:
fun getAppIconSize(context: Context): Int {
val activityManager = context.getSystemService<ActivityManager>()
val appIconSize = try {
activityManager.launcherLargeIconSize
} catch (e: Exception) {
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48f, context.resources.displayMetrics).toInt()
}
return appIconSize
}
Change the copy code from
zipFile.getInputStream(entry).use { it.copyTo(zipOutputStream.buffered()) }
to
zipFile.getInputStream(entry).use {
val bufStream = zipOutputStream.buffered()
it.copyTo(bufStream)
bufStream.flush()
}
or something similar to make sure that all the data gets written out.
I thought that the APK was failing certificate verification, but that is not the case. The foregoing will get you the label and the icon.
With the original code I was seeing the following error
chunk size is bigger than given data
Failed to load 'resources.arsc' in APK '/data/user/0/com
which indicates to me that something went awry with the output. With the aforementioned change, I see the following:
originalPackageInfo: label:APK Copy and Read appIcon:AdaptiveIconDrawable
packageArchiveInfo!=null?true label:APK Copy and Read appIcon:AdaptiveIconDrawable
If I open the created APK from the Device Explorer in Android Studio, it parses out nicely.
As for the minimum, I think, for your purposes, you will need the manifest, the resource.arsc file and the res directory. Here is how to get a reduced APK that will parse with just these elements:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null)
return
thread {
val originalApplicationInfo = packageManager.getApplicationInfo(packageName, 0)
val filePath = originalApplicationInfo.publicSourceDir
val outputFile = File(cacheDir, "test.apk")
outputFile.parentFile!!.mkdirs()
outputFile.delete()
val toExtract = setOf<String>("AndroidManifest.xml", "resources.arsc","res")
ZipFile(filePath).use { zipFile ->
ZipOutputStream(FileOutputStream(outputFile)).use { zipOutputStream ->
for (entry in zipFile.entries()) {
val name = entry.name
if (toExtract.contains(name.split("/")[0])) {
zipOutputStream.putNextEntry(ZipEntry(name))
zipFile.getInputStream(entry).use { inStream ->
zipOutputStream.buffered().apply {
inStream.copyTo(this)
}.flush()
}
}
}
}
}
val originalLabel = originalApplicationInfo.loadLabel(packageManager)
val originalIcon: Drawable? = originalApplicationInfo.loadIcon(packageManager)
Log.d(
"AppLog",
"originalPackageInfo: label:$originalLabel appIcon:${originalIcon?.javaClass?.simpleName}"
)
//
val packageArchiveInfo =
packageManager.getPackageArchiveInfo(outputFile.absolutePath, 0)
val label = packageArchiveInfo?.applicationInfo?.loadLabel(packageManager)?.toString()
val appIcon = packageArchiveInfo?.applicationInfo?.loadIcon(packageManager)
Log.d(
"AppLog",
"packageArchiveInfo!=null?${packageArchiveInfo != null} label:$label appIcon:${appIcon?.javaClass?.simpleName}"
)
}
}
}

Android storage access framework delete issue

I'm working with SAF (Storage access framework) to write down video files onto SD-CARD.
I've successfully create files, read them and moving.
But i have found the issue with deleting files.
When i delete files using SAF. Files are gone, but space isn't recover.
For example: when 1 have 2GB left out of 10GB.
And i delete file (1GB total) space left is 2GB, not 3GB.
I've used: enter code here
val file: DocumentFile = DocumentFile.fromTreeUri(ctx, treeUri)
file?.delete()
Even:
DocumentsContract.deleteDocument(
activity.contentResolver,
uri
)
and even this:
val path = "/storage/${external?.name}/root.img"
val f = File(path)
if (f.exists()) {
val deleteCmd = "rm -r $path"
val runtime = Runtime.getRuntime()
try {
runtime.exec(deleteCmd)
} catch (e: IOException) {
e.printStackTrace()
}
}
app.applicationContext.deleteFile(f.name)
f.absoluteFile.delete()
f.exists()

How to access external storage in Android 10?

As per Android 10 update, Reading and writing from external storage is restricted even we have
WRITE_EXTERNAL_STORAGE
I want to move my pdf file from internal storage (private of my android app) to DOWNLOADS folder. I tried so many approaches but nothing worked and there is no any help available on internet related to accessing external storage in android 10.
Privacy changes in Android 10  |  Android Developers
https://developer.android.com
hope below code will help you to resolve your issue and copy any file from internal to external storage.
fun copyPdfFrom(context: Context, sourceFile: File, destinationURIString: String) {
val destinationURI = Uri.parse(destinationURIString)
try {
val bufferedInputStream = BufferedInputStream(FileInputStream(sourceFile.absoluteFile))
val outPutStream = context.contentResolver.openOutputStream(destinationURI)!!
var len = 0
val brr = ByteArray(1024)
while ((bufferedInputStream.read(brr, 0, brr.size).also { len = it }) != -1) {
outPutStream.write(brr, 0, len)
}
outPutStream.flush()
outPutStream.close()
bufferedInputStream.close()
}
catch (e: Exception) {
e.printStackTrace()
}
}

Categories

Resources