Linux command "cp" programmatically to /data/app doesn't work!

1,275 views
Skip to first unread message

Endy Silveira

unread,
Jul 10, 2012, 1:26:33 PM7/10/12
to android-...@googlegroups.com
Hello,

I'm trying to copy a .apk file from /mnt/sdcard to /data/app programmatically, but I can't get success!

What I'm doing:

proc = Runtime.getRuntime().exec("su");
                   
DataOutputStream os = new DataOutputStream(proc.getOutputStream());
os.writeBytes("chmod 777 /mnt/sdcard/Myapk.apk");
os.writeBytes("cp /mnt/sdcard/Myapk.apk /app/data/Myapk.apk");

At terminal emulator I do it and everything runs ok...

Someone know why this code doesn't work? Or another way to do it?

Thanks in advance,

Endy

Endy Silveira

unread,
Jul 10, 2012, 2:54:01 PM7/10/12
to android-...@googlegroups.com
Solved!


proc = Runtime.getRuntime().exec("su");
           
DataOutputStream os = new DataOutputStream(proc.getOutputStream());
os.writeBytes("chmod 777  777 /mnt/sdcard/Myapk.apk" +"\n");
os.writeBytes("cp /mnt/sdcard/Myapk.apk /app/data/Myapk.apk"+"\n");
os.writeBytes("exit\n");
os.flush();
os.close();

proc.waitFor();

Endy Silveira

unread,
Jul 11, 2012, 2:21:26 PM7/11/12
to android-...@googlegroups.com
Thanks for all the replys and ideas!

I really think that I must use another way, without rooting my phone, but I think that, to copy files to system folder (in this case the data/app) I need to be rooted...

If someone knows a way to copy the file without su permission, please tell me...

Thanks in advance,

Endy

Em quarta-feira, 11 de julho de 2012 12h16min50s UTC-3, <Lado Kumsiashvili> escreveu:
Hey Guys,

You should not think, these problems are not known, may be not anybody know this problem, but a part.
Or if some one uses bad way to solve a problem, he does not know a better way.
And it is your decision who you trust. There are a lot of users who root their phones and have also a good reason for it,
deleting bloat ware, modifying /etc/hosts for blocking ads, changing some limitations (disabled sip on Galaxy S2 2.3.x Version) or just for pimp a look&feel or installing
Cyanogen MOD or another Custom rom. Changing fonts was the only way for Georgian users to read/write Georgian until 4.0 Android. And even in this 4.0 version
there is a single poor font, with no bold, italic and so on. That's why I have written my only App with root as requirement, just to let the guys with rooted devices to patch the fonts easily.
My app is as example Open Source and anybody can check what it does and then compile it himself and so.

So the Theme starter had another questions, not a questions how to design his copy operation and I have tried to help him to do it in right way.
Even if it is not a good praxis to have done things in this way, sometimes it is a only solution.

Lado


On 11.07.2012 16:39, AppCoder wrote:


On Wednesday, July 11, 2012 2:51:12 AM UTC-4, <Lado Kumsiashvili> wrote:
This is true, but if you have root you do mount -o remount,rw /system [/path/to/systemdev]
This is the way how a lot of tools currently work. Some of them just copy things, others do more. So sometimes we have to do this low level stuff due to the system restrictions. So if one would like to copy or has to copy things in this way, then just in way to let it work on more devices (cp vs. cat).

Anybody allowing that type of access just might as well hand you anything they
do on the phone.   If you can do that to change fonts, there is nothing to stop
your app from changing /system/lib/libssl.so  so any data they thought was
encrypted and secure through the browser can be filtered/forwarded to where
ever they want (or libc.so if you wanted to read all socket/file i/o data) even
after they find out your application has been uninstalled.



--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-platform/-/WQK3BsrWf5wJ.
To post to this group, send email to android-platform@googlegroups.com.
To unsubscribe from this group, send email to android-platform+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.


Endy Silveira

unread,
Jul 12, 2012, 9:48:59 AM7/12/12
to android-...@googlegroups.com
Well, so I really need to root to can copy my files to there...

Thanks again and you're all amazing!

Em quarta-feira, 11 de julho de 2012 15h23min08s UTC-3, Dianne Hackborn escreveu:
You can't modify the system image.  During normal operation the system image is mounted read-only.  You can't change that without remounting it, which requires root.

This is a feature.  Not allowing the system image to change is a Good Thing.

To view this discussion on the web visit https://groups.google.com/d/msg/android-platform/-/cEu1qtMO5YwJ.

To post to this group, send email to android-platform@googlegroups.com.
To unsubscribe from this group, send email to android-platform+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.



--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Lado Kumsiashvili

unread,
Jul 10, 2012, 2:56:39 PM7/10/12
to android-...@googlegroups.com
Please note, cp ist not available on every device. Use cat filea > fileb to copy things.

Lado
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Endy Silveira <endy.s...@traceback.com.br> wrote:
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-platform/-/dAe1-McoI6EJ.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.

Dianne Hackborn

unread,
Jul 10, 2012, 7:07:21 PM7/10/12
to android-...@googlegroups.com
No don't use a shell command at all, just write the Java code to copy the file.

Lado Kumsiashvili

unread,
Jul 10, 2012, 7:12:15 PM7/10/12
to android-...@googlegroups.com, Dianne Hackborn
OK, but how to run then the code as root?

Lado

Tez

unread,
Jul 11, 2012, 12:46:57 AM7/11/12
to android-platform
re-evaluate your design. elevating to root just for a copy indicates
something is really wrong with your design.

-Earlence

On Jul 11, 4:12 am, Lado Kumsiashvili <herrl...@gmail.com> wrote:
> OK, but how to run then the code as root?
>
> Lado
>
> On 11.07.2012 01:07, Dianne Hackborn wrote:
>
>
>
>
>
>
>
> > No don't use a shell command at all, just write the Java code to copy
> > the file.
>
> > On Tue, Jul 10, 2012 at 11:56 AM, Lado Kumsiashvili
> > <herrl...@gmail.com <mailto:herrl...@gmail.com>> wrote:
>
> >     Please note, cp ist not available on every device. Use cat filea >
> >     fileb to copy things.
>
> >     Lado
> >     --
> >     Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>
> >     Endy Silveira <endy.silve...@traceback.com.br
> >     <mailto:endy.silve...@traceback.com.br>> wrote:
>
> >         Solved!
>
> >         proc = Runtime.getRuntime().exec("su");
>
> >         DataOutputStream os = new
> >         DataOutputStream(proc.getOutputStream());
> >         os.writeBytes("chmod 777  777 /mnt/sdcard/Myapk.apk" +"\n");
> >         os.writeBytes("cp /mnt/sdcard/Myapk.apk
> >         /app/data/Myapk.apk"+"\n");
> >         os.writeBytes("exit\n");
> >         os.flush();
> >         os.close();
>
> >         proc.waitFor();
>
> >         Em ter�a-feira, 10 de julho de 2012 14h26min33s UTC-3, Endy
> >         Silveira escreveu:
>
> >             Hello,
>
> >             I'm trying to copy a .apk file from /mnt/sdcard to
> >             /data/app programmatically, but I can't get success!
>
> >             What I'm doing:
>
> >             proc = Runtime.getRuntime().exec("su");
>
> >             DataOutputStream os = new
> >             DataOutputStream(proc.getOutputStream());
> >             os.writeBytes("chmod 777 /mnt/sdcard/Myapk.apk");
> >             os.writeBytes("cp /mnt/sdcard/Myapk.apk /app/data/Myapk.apk");
>
> >             At terminal emulator I do it and everything runs ok...
>
> >             Someone know why this code doesn't work? Or another way to
> >             do it?
>
> >             Thanks in advance,
>
> >             Endy
>
> >         --
> >         You received this message because you are subscribed to the
> >         Google Groups "android-platform" group.
> >         To view this discussion on the web visit
> >        https://groups.google.com/d/msg/android-platform/-/dAe1-McoI6EJ.
> >         To post to this group, send email to
> >         android-...@googlegroups.com
> >         <mailto:android-...@googlegroups.com>.
> >         To unsubscribe from this group, send email to
> >         android-platfo...@googlegroups.com
> >         <mailto:android-platform%2Bunsu...@googlegroups.com>.
> >         For more options, visit this group at
> >        http://groups.google.com/group/android-platform?hl=en.
>
> >     --
> >     You received this message because you are subscribed to the Google
> >     Groups "android-platform" group.
> >     To post to this group, send email to
> >     android-...@googlegroups.com
> >     <mailto:android-...@googlegroups.com>.
> >     To unsubscribe from this group, send email to
> >     android-platfo...@googlegroups.com
> >     <mailto:android-platform%2Bunsu...@googlegroups.com>.
> >     For more options, visit this group at
> >    http://groups.google.com/group/android-platform?hl=en.
>
> > --
> > Dianne Hackborn
> > Android framework engineer
> > hack...@android.com <mailto:hack...@android.com>

Lado Kumsiashvili

unread,
Jul 11, 2012, 1:38:04 AM7/11/12
to android-...@googlegroups.com, Tez
And how to copy files to the /system folder? I use su and cat to copy and replace the fonts in the /system/fonts folder. And yes it must be, because android fonts does not contain all national characters, at least which we need. Let me know a better designed solution to solve this problem, then I'm going to change my code to this better one.

Lado

Dianne Hackborn

unread,
Jul 11, 2012, 2:40:53 AM7/11/12
to android-...@googlegroups.com, Tez
The system partition is only modified by the low-level recovery mode when applying a system update.  During normal system operation it is mounted read-only and can not be modified.
hac...@android.com

Lado Kumsiashvili

unread,
Jul 11, 2012, 2:51:12 AM7/11/12
to android-...@googlegroups.com, Tez
This is true, but if you have root you do mount -o remount,rw /system [/path/to/systemdev]
This is the way how a lot of tools currently work. Some of them just copy things, others do more. So sometimes we have to do this low level stuff due to the system restrictions. So if one would like to copy or has to copy things in this way, then just in way to let it work on more devices (cp vs. cat).

Sure I agree, the clean and better way is to use java apis for doing, but sometimes it is not enaugh.


Lado

--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

AppCoder

unread,
Jul 11, 2012, 10:39:34 AM7/11/12
to android-...@googlegroups.com, Tez


On Wednesday, July 11, 2012 2:51:12 AM UTC-4, <Lado Kumsiashvili> wrote:
This is true, but if you have root you do mount -o remount,rw /system [/path/to/systemdev]
This is the way how a lot of tools currently work. Some of them just copy things, others do more. So sometimes we have to do this low level stuff due to the system restrictions. So if one would like to copy or has to copy things in this way, then just in way to let it work on more devices (cp vs. cat).

Lado Kumsiashvili

unread,
Jul 11, 2012, 11:16:50 AM7/11/12
to android-...@googlegroups.com
Hey Guys,

You should not think, these problems are not known, may be not anybody know this problem, but a part.
Or if some one uses bad way to solve a problem, he does not know a better way.
And it is your decision who you trust. There are a lot of users who root their phones and have also a good reason for it,
deleting bloat ware, modifying /etc/hosts for blocking ads, changing some limitations (disabled sip on Galaxy S2 2.3.x Version) or just for pimp a look&feel or installing
Cyanogen MOD or another Custom rom. Changing fonts was the only way for Georgian users to read/write Georgian until 4.0 Android. And even in this 4.0 version
there is a single poor font, with no bold, italic and so on. That's why I have written my only App with root as requirement, just to let the guys with rooted devices to patch the fonts easily.
My app is as example Open Source and anybody can check what it does and then compile it himself and so.

So the Theme starter had another questions, not a questions how to design his copy operation and I have tried to help him to do it in right way.
Even if it is not a good praxis to have done things in this way, sometimes it is a only solution.

Lado


On 11.07.2012 16:39, AppCoder wrote:
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-platform/-/WQK3BsrWf5wJ.

Dianne Hackborn

unread,
Jul 11, 2012, 2:23:08 PM7/11/12
to android-...@googlegroups.com
You can't modify the system image.  During normal operation the system image is mounted read-only.  You can't change that without remounting it, which requires root.

This is a feature.  Not allowing the system image to change is a Good Thing.

On Wed, Jul 11, 2012 at 11:21 AM, Endy Silveira <endy.s...@traceback.com.br> wrote:
To view this discussion on the web visit https://groups.google.com/d/msg/android-platform/-/cEu1qtMO5YwJ.

To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.

Dianne Hackborn

unread,
Jul 11, 2012, 2:23:43 PM7/11/12
to android-...@googlegroups.com
Also note that if you change it, you can cause future OTA updates to fail because what is on the system image doesn't match what the update should be applied against.
Reply all
Reply to author
Forward
0 new messages