Android Pre-installing NDK Application - android

We are trying to pre-install a NDK Application into the /system/app directory. If I open the apk file in a ZIP file manager, the .so file is inside the lib directory. However, when we preinstall the apk file, the apk's .so file is not copied to system/lib directory, causing for the application to fail when we launched it in the device.
Can anyone please tell me what should be set in the Android.mk for the APK file so that the .so file will be extracted from the APK file and copied to system/lib directory? We need to include the application in the system image.
Any feedback will be greatly appreciated.
Thanks,
artsylar

I had the same need and after 2 days of heavy research, I came up with a solution to this problem. It is not simple and requires you to be able to modify the Android System code as well.
Basically PackageManagerService prevents system applications to unpack their native binaries (.so files), unless they have been updated. So the only way to fix this is by modifying PMS.java (aptly named since trying to solve this problem put me in a terrible mood).
On the system's first boot, I check every system package for native binaries by writing a isPackageNative(PackageParser.Package pkg) function:
private boolean isPackageNative(PackageParser.Package pkg) throws IOException {
final ZipFile zipFile = new ZipFile(pkg.mPath);
final Enumeration<? extends ZipEntry> privateZipEntries = zipFile.entries();
while (privateZipEntries.hasMoreElements()) {
final ZipEntry zipEntry = privateZipEntries.nextElement();
final String zipEntryName = zipEntry.getName();
if(true) Log.e(TAG, " Zipfile entry:"+zipEntryName);
if (zipEntryName.endsWith(".so")) {
zipFile.close();
return true;
}
}
zipFile.close();
return false;
}
This function checks every package for a native library and if it has one, I unpack it. PMS does this check in scanPackageLI(....). Search for the following code in the method:
if (isSystemApp(pkg) && !isUpdatedSystemApp(pkg))
and add the isPackageNative(pkg) check. There are other small modifications required but you'll probably figure it out once you have this direction. Hope it helps!

I think you cannot do it by default as Android's /system partition is mounted as read-only! You need a rooted phone so as to mount the /system with write privileges through this command:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system.
So, if you have a rooted phone you can add in your application this code:
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
// gain root privileges
os.writeBytes("mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system\n");
// do here the copy operation you want in /system/lib file, for example:
os.writeBytes("mv /sdcard/mylib.so /system/lib/\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
toastMessage("could not get root access");
}
Otherwise, you have to follow the solution that digitalmouse12 gave..

You will have to "adb push" the .so file yourself. Also, you don't necessarily have to push your library into system/lib (the folder might deny you permission anyway). Most push it to data/app and then load by issuing
System.load("/data/app/<libName>.so");

There's probably documentation somewhere, but if you cannot find that, I would suggest identifying a pre-installed app with an associated jni library .so and examining the android sources or corresponding system image or update.zip to see how it's handled.
In other words, programming by example...

Related

Config file on Android

New to Android, working on an app for Vuzix M300s. The app needs to access a file that contains the IP address and port of a web server.
I believe I will need to manually place a pre-configured file on the M300s using adb shell, but I cannot figure out where on the device to place it so that the app can find it.
Via Android Studio 3.1.3, I have placed a file in the assets folder which I can open & read, but using adb shell I cannot locate it. (I get permission denied for a lot of actions like ls).
How do I get a file on there? Or is there a better way?
Note that the assets folder in your project only exists on your development machine. The contents of this folder are packaged into the APK file when you build your app. In order to read any of these files, you need to use Context.getAssets() as explained in read file from assets.
Figured it out.
To move/copy a file to the M300s for an application
move the file to the device (in the sdcard folder)
.\adb push C:\temp\file.cfg /sdcard/
move the file from /sdcard/ to the desired location
a) go into the shell
'> .\adb shell
b) change to the application's permissions
$ run-as com.foobar.appname
c) copy the file into the app's 'files' folder
$ cp /sdcard/file.cfg files/
Within my app, I was able to read this with
FileInputStream fin = openFileInput("file.cfg");
InputStreamReader rdr = new InputStreamReader(fin);
char[] inputBuffer = new char[100];
int charsRead = rdr.read(inputBuffer);
String fileContents = new String(inputBuffer);
rdr.close();
Log.i(method, "charsRead: " + charsRead);
Log.i(method, "fileContents: " + fileContents);

Install APK using root, handling new limitations of "/data/local/tmp/" folder

Background
So far, I was able to install APK files using root (within the app), via this code:
pm install -t -f fullPathToApkFile
and if I want to (try to) install to sd-card :
pm install -t -s fullPathToApkFile
The problem
Recently, not sure from which Android version (issue exists on Android P beta, at least), the above method fails, showing me this message:
avc: denied { read } for scontext=u:r:system_server:s0 tcontext=u:object_r:sdcardfs:s0 tclass=file permissive=0
System server has no access to read file context u:object_r:sdcardfs:s0 (from path /storage/emulated/0/Download/FDroid.apk, context u:r:system_server:s0)
Error: Unable to open file: /storage/emulated/0/Download/FDroid.apk
Consider using a file under /data/local/tmp/
Error: Can't open file: /storage/emulated/0/Download/FDroid.apk
Exception occurred while executing:
java.lang.IllegalArgumentException: Error: Can't open file: /storage/emulated/0/Download/FDroid.apk
at com.android.server.pm.PackageManagerShellCommand.setParamsSize(PackageManagerShellCommand.java:306)
at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:884)
at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:138)
at android.os.ShellCommand.exec(ShellCommand.java:103)
at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21125)
at android.os.Binder.shellCommand(Binder.java:634)
at android.os.Binder.onTransact(Binder.java:532)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2806)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3841)
at android.os.Binder.execTransact(Binder.java:731)
This seems to also affect popular apps such as "Titanium backup (pro)", which fails to restore apps.
What I've tried
Looking at what's written, it appears it lacks permission to install APK files that are not in /data/local/tmp/.
So I tried the next things, to see if I can overcome it:
set the access to the file (chmod 777) - didn't help.
grant permissions to my app, of both storage and REQUEST_INSTALL_PACKAGES (using ACTION_MANAGE_UNKNOWN_APP_SOURCES Intent) - didn't help.
create a symlink to the file, so that it will be inside the /data/local/tmp/, using official API:
Os.symlink(fullPathToApkFile, symLinkFilePath)
This didn't do anything.
create a symlink using this :
ln -sf $fullPathToApkFile $symLinkFilePath
This partially worked. The file is there, as I can see it in Total Commander app, but when I try to check if it exists there, and when I try to install the APK from there, it fails.
Copy/move (using cp or mv) the file to the /data/local/tmp/ path, and then install from there. This worked, but it has disadvantages: moving is risky because it temporarily hides the original file, and it changes the timestamp of the original file. Copying is bad because of using extra space just for installing (even temporarily) and because it wastes time in doing so.
Copy the APK file, telling it to avoid actual copy (meaning hard link), using this command (taken from here) :
cp -p -r -l $fullPathToApkFile $tempFileParentPath"
This didn't work. It got me this error:
cp: /data/local/tmp/test.apk: Cross-device link
Checking what happens in other cases of installing apps. When you install via via the IDE, it actually does create the APK file in this special path, but if you install via the Play Store, simple APK install (via Intent) or adb (via PC), it doesn't.
Wrote about this here too: https://issuetracker.google.com/issues/80270303
The questions
Is there any way to overcome the disadvantages of installing the APK using root on this special path? Maybe even avoid handling this path at all?
Why does the OS suddenly require to use this path? Why not use the original path instead, just like in the other methods of installing apps? What do the other methods of installing apps do, that somehow avoids using the spacial path?
One solution, in case you don't mind the moving procedure, is to also save&restore the timestamp of the original file, as such:
val tempFileParentPath = "/data/local/tmp/"
val tempFilePath = tempFileParentPath + File(fullPathToApkFile).name
val apkTimestampTempFile = File(context.cacheDir, "apkTimestamp")
apkTimestampTempFile.delete()
apkTimestampTempFile.mkdirs()
apkTimestampTempFile.createNewFile()
root.runCommands("touch -r $fullPathToApkFile ${apkTimestampTempFile.absolutePath}")
root.runCommands("mv $fullPathToApkFile $tempFileParentPath")
root.runCommands("pm install -t -f $tempFilePath")
root.runCommands("mv $tempFilePath $fullPathToApkFile")
root.runCommands("touch -r ${apkTimestampTempFile.absolutePath} $fullPathToApkFile")
apkTimestampTempFile.delete()
It's still a bit dangerous, but better than copying files...
EDIT: Google has shown me a nice workaround for this (here) :
We don't support installation of APKs from random directories on the device. They either need to be installed directly from the host using 'adb install' or you have to stream the contents to install --
$ cat foo.apk | pm install -S APK_SIZE
While I think this is incorrect that they don't support installing of APK files from random paths (always worked before), the workaround does seem to work. All I needed to change in the code of installing an APK file is as such:
val length = File(fullPathToApkFile ).length()
commands.add("cat $fullPathToApkFile | pm install -S $length")
Thing is, now I have some other questions about it :
Does this workaround avoid the moving/copying of the APK into storage, and without affecting the original file ? - seems it does
Will this support any APK file, even large ones? - seems it succeeds in doing it for an APK that takes 433MB, so I think it's safe to use for all sizes.
This is needed only from Android P, right? - so far seems so.
Why does it need the file size as a parameter ? - No idea, but if I remove it, it won't work
Thanks for the answers! I looked everywhere else as well to get a whole setup for OTA to work for Android 10 and so on. It 100% works on Samsung Galaxy Tab 10.1 running Android 10.
Here is a medium article with the code:
https://medium.com/#jnishu1996/over-the-air-ota-updates-for-android-apps-download-apk-silent-apk-installation-auto-launch-8ee6f342197c
The magic is running this command with root access:
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
// Get all file permissions
dataOutputStream.writeBytes("chmod 777 " + file.getPath() + "\n");
// Perform silent installation command, all flags are necessary for some reason, only this works reliably post Android 10
String installCommand = "cat " + file.getAbsolutePath() + "| pm install -d -t -S " + file.length();
// Data to send to the LaunchActivity to the app knows it got updated and performs necessary functions to notify backend
// es stands for extraString
// In LaunchActivity onCreate(), you can get this data by running -> if (getIntent().getStringExtra("OTA").equals("true"))
String launchCommandIntentArguments = "--es OTA true --es messageId " + MyApplication.mLastSQSMessage.receiptHandle();
// Start a background thread to wait for 8 seconds before reopening the app's LaunchActivity, and pass necessary arguments
String launchCommand = "(sleep 8; am start -n co.getpresso.Presso/.activities.LaunchActivity " + launchCommandIntentArguments + ")&";
// The entire command is deployed with a ";" in the middle to launchCommand run after installCommand
String installAndLaunchCommand = installCommand + "; " + launchCommand;
// begins the installation
dataOutputStream.writeBytes(installAndLaunchCommand);
dataOutputStream.flush();
// Close the stream operation
dataOutputStream.close();
out.close();
int value = process.waitFor();

Can I copy files from another app /data folder?

Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount rw /data");
os.writeBytes("chmod 777 /data/data/com.a.aas/a.jpg");
os.writeBytes("dd if=/data/data/com.a.aas/a.jpg of=mnt/sdcard/b.jpg");
I can't copy this. How can I be done ? I added write permission to ext.storage !
os.writeBytes("cat /data/data/com.a.aas/a.jpg > mnt/sdcard/b.jpg");
also doesn't work .. But Terminal Emulator can done it. Help me please !
Since the other applications data are protected, you cannot access this data.
The permission you refer to about ext.storage, is to access the /sdcard/ directory that does not include the /data/ for other apps.
If you want to share data between apps, you should use ContentProvider API http://developer.android.com/guide/topics/providers/content-providers.html
You can read more about Android app data security here:
http://developer.android.com/guide/topics/data/data-storage.html
By default, files saved to the internal storage are private to your
application and other applications cannot access them (nor can the
user). When the user uninstalls your application, these files are
removed.
/data/ is not on the sdcard and therefore won't be affected by ext.storage permission flag in your metadata.
How about using
p = Runtime.getRuntime().exec("su -c cp /data/data/com.a.aas/a.jpg /mnt/sdcard/b.jpg");
p.waitFor();
with
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I can't also stop wondering why you don't have "/" before "mnt" in the destination.
Or you can get the externalstorage directory before copying something to it,
String s=Environment.getExternalStorageDirectory().toString();
p = Runtime.getRuntime().exec("su -c cp /data/data/com.a.aas/a.jpg "+ s +"/b.jpg");
p.waitFor(); // it is good practice to wait

How to access data/data folder in Android device?

I am developing an app and I know my database *.db will appear in data/data/com.****.***
I can access this file from AVD in Eclipse with help of sqlite manager
But I can't access this file in my Android phone.
I googled it and it says I need to root my phone to do it, but I don't want to do that.
How can I access my data/data/..... directory in my Android phone "without rooting it"?
Can I change user permissions for the directory data/data..... without rooting it?
Accessing the files directly on your phone is difficult, but you may be able to copy them to your computer where you can do anything you want with it.
Without rooting you have 2 options:
If the application is debuggable you can use the run-as command in adb shell
adb shell
run-as com.your.packagename
cp /data/data/com.your.packagename/
Alternatively you can use Android's backup function.
adb backup -noapk com.your.packagename
You will now be prompted to 'unlock your device and confirm the backup operation'. It's best NOT to provide a password, otherwise it becomes more difficult to read the data. Just click on 'backup my data'. The resulting 'backup.ab' file on your computer contains all application data in android backup format. Basically it's a compressed tar file. This page explains how you can use OpenSSL's zlib command to uncompress it.
You can use the adb restore backup.db command to restore the backup.
If you are using Android Studio 3.0 or later version then follow these steps.
Click View > Tool Windows > Device File Explorer.
Expand /data/data/[package-name] nodes.
You can only expand packages which runs in debug mode on non-rooted device.
You could also try fetching the db using root explorer app. And if that does not work then you can try this:
Open cmd
Change your directory and go into 'Platform tools'
Type 'adb shell'
su
Press 'Allow' on device
chmod 777 /data /data/data /data/data/com.application.package /data/data/com.application.package/*
Open DDMS view in Eclipse and from there open 'FileExplorer' to get your desired file
After this you should be able to browse the files on the rooted device.
To do any of the above (i.e. access protected folders from within your phone itself), you still need root. (That includes changing mount-permissions on the /data folder and accessing it)
Without root, accessing the /data directly to read except from within your application via code isn't possible. So you could try copying that file to sdcard or somewhere accessible, and then, you should be able to access it normally.
Rooting won't void your warranty if you have a developer device. I'm sorry, there isn't any other way AFAIK.
The easiest way (just one simple step) to pull a file from your debuggable application folder (let's say /data/data/package.name/databases/file) on an unrooted Android 5.0+ device is by using this command:
adb exec-out run-as package.name cat databases/file > file
Open your command prompt
Change directory to E:\Android\adt-bundle-windows-x86_64-20140702\adt-bundle-windows-x86_64-20140702\sdk\platform-tools
Enter below commands
adb -d shell
run-as com.your.packagename cat databases/database.db > /sdcard/database.db
Change directory to cd /sdcard to make sure database.db is there.
adb pull /sdcard/database.db or simply you can copy database.db from device .
Use File Explorer in eclipse. Select Windows => Show View => Other ... => File Explorer.
An another way is pull the file via adb:
adb pull /system/data/data/<yourpackagename>/databases/<databasename> /sdcard
To backup from Android to Desktop
Open command line cmd and run this:
adb backup -f C:\Intel\xxx.ab -noapk your.app.package.
Do not enter password and click on Backup my data.
Make sure not to save on drive C root. You may be denied.
This is why I saved on C:\Intel.
To extract the *.ab file
Go here and download: https://sourceforge.net/projects/adbextractor/
Extract the downloaded file and navigate to folder where you extracted.
run this with your own file names: java -jar abe.jar unpack c:\Intel\xxx.ab c:\Intel\xxx.tar
I had also the same problem once. There is no way to access directly the file within android devices except adb shell or rooting device.
Beside here are 02 alternatives:
1)
public void exportDatabse(String databaseName)
{
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "backupname.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
}
}
2) Try this: https://github.com/sanathp/DatabaseManager_For_Android
On a rooted device, the correct solution is this:
Open cmd
Change your directory and go into 'Platform tools'
Type 'adb shell'
su
Press 'Allow' on device
chmod 777 /data /data/data /data/data/*
Open DDMS view in Eclipse/IntelliJ and from there open 'FileExplorer' to get your desired file
The original solution worked, but the chmod would return unknown directory. Changing the chmod command to /data/data/* gave access to all subfolders in the data directory from DDMS in Intellij. I assume the same solution is true for Eclipse DDMS.
UPDATE
So, what I've found is strange. I'm running a Nexus 6 using DDMS in IntelliJ (Android Device Monitor). I have built a little starter app. Said app saves data to a .csv file in data/data/com.example.myapp/files
When I first started to try to access this file on my Nexus 6, I found that I have to root the device.. I could see the data folder, but trying to open it would not work. As mentioned online in other places, the expand + would vanish then reappear shortly thereafter (note, there are solutions on the web that claim to allow access to these folders without rooting, I didn't find them till too late, and I'm not sure if I prefer not to root anyway ((I'd rather be able to do it manually than rely on an app or command prompt to give me my solutions))). I rooted my 6 and tried DDMS again.
At this point, it showed me the data folder and I could expand the folder and see the com. directories, but I could not open any of them. That is when I discovered the above solution. The initial instructions would not work on this part:
chmod 777 /data /data/data /data/data/com.application.pacakage /data/data/com.application.pacakage/*
That is when I tried the solution I posted:
chmod 777 /data /data/data /data/data/*
That solution seemed to work, but only on certain folders. I now could expand my myapp folder, but could not expand the files directory in it.
At this point, I played around for a while then figured why not just try it on the directory I need rather than trying these wildcard entries.
chmod 777 /data /data/data /data/data/com.example.myapp/*
Followed by:
chmod 777 /data /data/data /data/data/com.example.myapp/files
These commands allowed me to expand and view the files in my app's directory to confirm that the .csv was being saved correctly.
Hope this helps someone. I struggled with this for hours!
(to compound on this a tad further, oddly enough, the permissions did not pass to the .csv file that passed to the files directory. my files directory permissions read drwxrwxrwx and my log.csv file permissions read -rw-rw---- .. just fyi)
may be to access this folder you need administrative rights.
so you have two options:-
root your device and than try to access this folder
use emulator
p.s. : if you are using any of above two options you can access this folder by following these steps
open DDMS perspective -> your device ->(Select File Explorer from
right window options) select package -> data -> data -> package name
->files
and from there you can pull up your file
You can download a sigle file like that:
adb exec-out run-as debuggable.app.package.name cat files/file.mp4 > file.mp4
Before you download you might wan't to have a look at the file structure in your App-Directory. For this do the following steps THelper noticed above:
adb shell
run-as com.your.packagename
cd files
ls -als .
The Android-Studio way Shahidul mentioned (https://stackoverflow.com/a/44089388/1256697) also work. For those who don't see the DeviceFile Explorer Option in the Menu: Be sure, to open the /android-Directory in Android Studio.
E.g. react-native users have this inside of their Project-Folder right on the same Level as the /ios-Directory.
adb backup didn't work for me, so here's what I did (Xiaomi Redmi Note 4X, Android 6.0):
1. Go to Settings > Additional Settings > Backup & reset > Local backups.
2. Tap 'Back up' on the bottom of the screen.
3. Uncheck 'System' and 'Apps' checkmarks.
4. Tap detail disclosure button on the right of the 'Apps' cell to navigate to app selection screen.
5. Select the desired app and tap OK.
6. After the backup was completed, the actual file need to be located somehow. Mine could be found at /MIUI/backup/AllBackup/_FOLDER_NAMED_AFTER_BACKUP_CREATION_DATE_.
7. Then I followed the steps from this answer by RonTLV to actually convert the backup file (.bak in my case) to tar (duplicating from the original answer):
"
a) Go here and download: https://sourceforge.net/projects/adbextractor/
b) Extract the downloaded file and navigate to folder where you extracted.
c) run this with your own file names: java -jar abe.jar unpack c:\Intel\xxx.ab c:\Intel\xxx.tar
"
Simple answer is NO. On upcoming Android 13, you can't access anything in
/storage/emulated/0/Android/*
directory without Rooting your device or hooking up to a PC, certainly not in Pixel devices.
Read Android Source page for such App data access using ADB here:
https://developer.android.com/training/data-storage/manage-all-files
One of the simple way is to create your database on SD-Card. Because you cannot get access to your phone's data folder in internal memory, unless you root your phone. So why not simply create your database on SD-Card.
Moreover, if you want, you may write some file copying-code to copy your existing database file (from internal memory) to external memory without requiring any root.
you can copy this db file to somewhere in eclipse explorer (eg:sdcard or PC),and you can use sqlite to access and update this db file .
You can also try copying the file to the SD Card folder, which is a public folder, then you can copy the file to your PC where you can use sqlite to access it.
Here is some code you can use to copy the file from data/data to a public storage folder:
private void copyFile(final Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath =
context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
String backupDBPath = "data.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
SQLlite database is store on user's Phone and it's hidding under path:
/Data/Data/com.companyname.AppName/File/
you have 2 options here:
you can root your phone so that you get access to view your hidding
db3 file
this is not a solution but a work around. Why not just create test
page that display your database table in it using 'select' statment.

Native Subprocess Can't Create File in Application Folder or on SDcard

Since this turned out to be a long read, I'll start with a very truncated version:
Are native processes not allowed to create/edit files created by their spawning java process regardless of having appropriate location/ permission/ file ownership?
The full question:
My application consists of a set of native binaries compiled with the NDK toolchain and a typical java Android interface. The native programs are packaged in the /assets folder and copied by the java application out into a /data/data/com.domain.app/nativeApplication folder.
There are two native application in question. One which modifies a jpeg created by the java layer, and a second which analyzes that jpeg and returns an int as output. The issue is this: When called by the java layer, the jpeg modifier doesn't work.
I've done a good amount of troubleshooting on it already so I'll try to dissuade you from some of the obvious potential answers starting with the most obvious.
Correct Compilation:
Through adb shell I can call both of the programs in question, as is, from the phone's disk. The second image analyzer program runs without a hitch when called either via adb or as designed in the software IF I do the intermediary step manually from adb.
Permissions:
Jpg Permission - The application has permission to write to external storage. I've tried to have the file read in from both the SDcard and /data/data/com.domain.app/nativeApp/img/name.jpg; no joy. As stated the second program operates as designed when the image is in either location, if I manually run the first program through adb.
File/Folder Permissions: All of the folders in ~/nativeApplication/* have been created BY the android application itself, it is the owner and it has chmodded all of the sub-directories and files to 777.
Supporting Code:
Calling the jpg converter process:
NativeSetup.changePermission("/data/data/com.domain.program/nativeApp/img/img1.jpg");
try {
Process jpegConvert = Runtime.getRuntime().exec("/data/data/com.domain.program/nativeApp/bin/jpegtransformer -opts /data/data/com.domain.program/nativeApp/img/img1.jpg > /data/data/com.domain.program/nativeApp/img/img2.jpg");
jpegConvert.waitFor();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Call used to create the files in ~/nativeApp/*
private static void copyFile(String assetPath, String localPath, Context context) {
try {
InputStream in = context.getAssets().open(assetPath);
FileOutputStream out = new FileOutputStream(localPath);
int read;
byte[] buffer = new byte[4096];
while ((read = in.read(buffer)) > 0) {
out.write(buffer, 0, read);
}
chmod call: (it works according to ls -l through adb)
static void changePermission(String localPath) {
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +localPath);
chmod.waitFor();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
out.close();
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Permissions of the file the program can't edit and then output a changed copy:
~/nativeApp/img $ ls -l
-rwxrwxrwx app_89 app_89 93061 2011-07-23 18:38 img1.jpg
Permission of the application that can't edit the above jpg:
~/nativeApp/bin $ ls -l jpegtransformer
-rwxrwxrwx app_89 app_89 95268 2011-07-23 18:01 jpegtransformer
Permissions of folders in ~/nativeApp/
~/nativeApp $ ls -l
drwxrwxrwx app_89 app_89 2011-07-23 17:57 bin
drwxrwxrwx app_89 app_89 2011-07-23 18:27 img
Theories:
At this point my only viable theory is that there is some android policy not governed by chmod which is keeping the jpegtransformer from editing the image when called by the java layer. Since the image analyzer doesn't change the image I imagine it skirts this? Are native processes not allowed to create files? Any ideas would be great. Sorry for the long read.
-SS
When you spawn a process from your app, it runs under the same uid as you do, so all of the same Android and filesystem permissions apply. I think you need to look for something else going on, such as using bad paths.
If you want to verify, just use "adb shell ps" to see the running processes and you should see that your process has the same uid as your main app.
That said... Android really does not support spawning processes like this, and it is strongly discouraged. Please consider doing this just by loading the native code as a shared library and using JNI to call it. This will also be a lot more efficient, and communication with your native code potentially simpler since JNI is a much richer and more direct way to call in to it.
I think your problem is not related to permissions - you try to redirect the output using >, while this is a shell operator and does not work from java. grab the process output stream and write it to a file using Java standard file I/O API.
Though I'm not sure this is the problem, you shouldn't have problem to modify files in your app folder and the issue I described is an issue I met more than once.

Categories

Resources