Update 8 years later
On modern versions of Android, there is no way to run downloaded or generated code (a.k.a. W^X violation). We can only embed prebuilt binaries in the APK (or bundle) (using jniLibs and other settings in gradle.build) and make sure it is extracted to the native file system (explicitly set android:extractNativeLibs = "true"). Then you can run the binary from getContext().getApplicationInfo().nativeLibraryDir.
If I understand the scenario correctly, you create the script on the fly, and use /data/local/tmp
as an easy location that is both publicly writable and executable. Once, this was possible. But on recent versions of Android, security has been tightened.
Your app can execute files under /data/data/${your.package}
. You can use getContext().getFilesDir() to reliably obtain the full path. Note that you still need to use chmod 500
to ensure that the file has executable permission.
If you have some fixed executables (binaries or scripts) that must be installed with your app, there is a clever trick to let the system package installer take care of all that for you: make sure the file has a name "libsomething.so" and put it in /libs/armeabi
directory under the Eclipse project root. After installation, the files will be present in getContext().getApplicationInfo().nativeLibraryDir directory with executable permissions set.
PS You don't need the WRITE_EXTERNAL_STORAGE permission for this to work (maybe you need it for other things your app does).
PPS You can run a shell script from anywhere, including /sdcard
, and you don't need executable permission for the script. Instead, use sh -c source full.path.to.script
.