Android 执行Runtime.getRuntime().exec报错Permission denied

在Android上调用Runtime.getRuntime().exec()函数执行命令时,报错了。

java.io.IOException: Cannot run program "/data/data/com.example.testhband/outTest": error=13, Permission denied

使用命令 run-as com.example.testhband进入APK安装目录,发现outTest存在,权限也是正常的。

再查看应用程序的AndroidManifest.xml,也没发现什么特别的权限;

在网上查到资料,Android API 29以后,就不能在/data/data/安装包目录/ 下直接执行程序了,需要对程序打包方式进行处理,但是如果是 apk的 targetSdkVersion 小于29,还是可以兼容运行的。我把targetSdkVersion改为23,使用这个版本的SDK编译就可以了。

hen targeting API 29 (Android 10 / Q) or above, it is not possible anymore to have execute permission for files stored within the app's home directory (data), which is exactly what you are describing (/data/data/%package%/).

This Android 10 modification was introduced in commit: https://android-review.googlesource.com/c/platform/system/sepolicy/+/804149 and was then confirmed officially by Google here: https://issuetracker.google.com/issues/128554619

This significant change is being discussed by various projects, in termux/termux-app#1072 for instance. One recommended and (hopefully) future-proof way is to extract program binaries into the application's native lib directory (with android:extractNativeLibs=true), where files can still be executed but are stored read-only for improved security.

Here are examples showing how to handle this change, in Termux with commit f6c3b6f in the android-10 branch or in this other project showing how to run the Erlang runtime on Android by exctrating all the files from a .zip archive into the jniLibs/"abi" subdirectory ("abi" being arm64-v8a for 64-bit ARM, armeabi-v7a for 32-bit ARM, etc.) with the imposed lib___.so filename format expected by the Android platform. Executable files can simply be moved in the right project folder manually, the important part is to use the lib___.so format for the filenames.

In the Android Manifest file, setting the attribute android:extractNativeLibs="true" will get these lib___.so files extracted at installation time in the right native lib directory, with support for execute permission. Symlinks can finally be created if needed in the usual app directory to use the regular executable names, instead of the harder-to-manipulate lib___.so versions.
 

参考链接

https://stackoverflow.com/questions/64786837/what-path-to-put-executable-to-run-on-android-29/64792194#64792194
https://github.com/JeromeDeBretagne/erlanglauncher/issues/2
https://developer.android.com/about/versions/10/behavior-changes-10#execute-permission

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用提到了Runtime.getRuntime().exec()方法,该方法可以在Android执行命令或创建进程。根据引用,您可以使用这种方法从Java代码中调用Python脚本。但需要注意的是,引用指出正确的写法应该是Runtime.getRuntime()而不是getruntime。因此,根据您的问题"android8 Runtime.getRuntime().exec",您可以使用以下代码来执行命令或创建进程: ```java Runtime runtime = Runtime.getRuntime(); String command = "your_command_here"; try { Process process = runtime.exec(command); // 处理进程的输出或其他操作 } catch (IOException e) { e.printStackTrace(); } ``` 在上述代码中,将"your_command_here"替换为您要执行的具体命令。这样,您就可以在Android 8及以上版本中使用Runtime.getRuntime().exec()方法执行命令或创建进程了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [使用runtime.getruntime.exec从Java调用python脚本(Calling python script from Java using runtime....](https://blog.csdn.net/weixin_39618730/article/details/110567660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Android Runtime.getRuntime().exec()](https://blog.csdn.net/xianrenli38/article/details/117484708)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值