Android Q+
(Personally tested with Android 11
). Since got released new policy changes towards android system safety, you can't execute normally your binary executables just from /data/data/package/files/...
folders.
W^X violation details:
https://developer.android.com/about/versions/10/behavior-changes-10#execute-permission
Solution:
1. Add your executables to the native libraries folder in Android studio eg. app/libs/armeabi/executable
.
File structure here
2. Also add this line to your manifest (Application layer):
<Application
...
android:extractNativeLibs="true">
...
</application>
If you are using newer version of the Gradle plugin, you may face issues when
your executable not being packaged into apk or extracted in.
/data/app/package/lib/.../...
You need then trick Android Gradle plugin a bit.
Solution:
1. Create a folder structure in your file explorer lib/armeabi/executable
(I use armeabi just as an example, it can be your architecture)
2. Now zip this lib
folder and rename it to yournaming.jar
3. Copy and paste in your Android Studio libs
directory
should look like this
4. Go to your build.grade(app)
, in dependencies
section add following line
for compiling .jar
.
dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
...
}
5 Build your .apk
and check with analyze tool
, folder should be now created in .apk
itself
If all these steps done, now can try do exec command to run binary executable from android app:
try {
// creating exec process
Process process =
Runtime.getRuntime().exec(getApplicationInfo().nativeLibraryDir +
"/executable");
// reading and printing executable outcome
byte[] buffer = new byte[2048];
process.waitFor();
process.getInputStream().read(buffer, 0 , buffer.length);
System.out.println(new String(buffer));
} catch (IOException | InterruptedException e)
{
e.printStackTrace();
}
Hopefully helped to solve your case