How to Package Native Commandline Application in APK

How to package native commandline application in apk?

See Android NDK build with ANT script.

You can add the binary to APK using aapt if Ant does not copy it automatically from libs/armeabi, see Documentation for aapt element in Ant script.

I believe that the file will be correctly extracted to /data/data/your.package.full.name/lib, with executable permissions.

Ant build involves a step called "ApkBuilder", which only adds files from libs/armeabi that match pattern ^.+\.so$ or gdbserver - the latter for debug build only.

But it is not enough to rename the executable to "myexecutable.so": this file will not be extracted by the APK installer on the device.

It is enough to rename the executable to "libmyexecutable.so": this file will be extracted by the APK installer Package Manager on the device to /data/data/your.package.full.name/lib, with executable permissions.

I personally like to give this not-actually-a-library some special name, e.g. lib...ffmpeg...so.

UPDATE 6 years and 12 API levels later, this approach is still valid. But with Android App Bundle, the default behavior of the package manager is not to extract the native libraries from the APK, using them in-place. Here and in comments to this answer, you will find some workarounds for that. The one that is especially attractive, is to refer to the executable as "/path/to/MyApp.apk!lib...ffmpeg...so", but I have not tested this notation does not work with System.exec(). See IssueTracker for discussion.

Note that now there is a special exception for wrap.sh.

Package Android apk with additional executables

You can put it into assets and copy it to the app's private directory on first run. After you set the executable bit, you should be able to run it.

Install an apk file from command prompt?

You can use the code below to install application from command line

adb install example.apk

this apk is installed in the internal memory of current opened emulator.

adb install -s example.apk

this apk is installed in the sd-card of current opened emulator.

You can also install an apk to specific device in connected device list to the adb.

adb -s emulator-5554 install myapp.apk

Refer also to adb help for other options.

Create native android apk via command line only (makefile)

The path you specify here seems like the culprit:

aapt add hellojni.apk ./lib/arm64/libhello.so

I tried adding a bogus file to an APK using the same type of path and then listing the APK contents:

./aapt.exe add test.apk ./lib/libbcc.dll
./aapt.exe list test.apk

And got this in the output:

./lib/libbcc.dll

I.e. I ended up with a directory named . in the root of the APK, inside of which a lib directory was created, inside of which the libbcc.dll file was placed (I confirmed this by opening the APK with 7zip).

If I remove the ./:

./aapt.exe add test.apk lib/libbcc.dll
./aapt.exe list test.apk

I get the correct results:

lib/libbcc.dll

(I tested this both via Windows cmd and MSys2, with the same results)

How to Create Native Android Application using CMD line from Native Folder of MobileFirst

Edit: if what you're asking is "how to generate the APK for my hybrid application from command line?", then this is not related to MFP.

For a Hybrid application, MFP generates the native folder for you. You then need to use Android tools such as Android Studio or the Android ADT command line to further work with the Android project.

See here: Building and Running from the command line / from Android Studio.

Note that Google's documentation assumes your project supports Gradle, which MFP projects (7.0 and below) do not support at this time. You should probably use the Eclipse ADT UI or Android Studio (do not select to upgrade the project with Gradle support) rather than command line.

These instructions are older (so no mention of Gradle), you can try those.

http://codeseekah.com/2012/02/09/command-line-android-development-basics/


That does not look like a native Android component generated by MFP. It looks like a Hybrid application with the Android environment. Not the same.

To create a native Android component (= the MFP SDK for Native Android applications), you need to generate the NativeAPI and associated artifacts that you then need to copy over into your own, separately created by you (using Android ADT or Android Studio), native Android project.

You can do this using the following set of CLI commands (or via the MFP Studio):

mfp create MyProject  
cd MyProject
mfp add api MyAndroidFramework -e android

Read more here: Using CLI to create, build, and manage MobileFirst project artifacts

How to create an Android executable for launch from the shell?

For me, I didn't need eclipse nor any java code in my application. My application that was being ported from Linux, already had a set of makefiles already in place. I was able to use my existing makefile system by way of the standalone tool chain portion of the NDK.

This answer got me going with the standalone tool chain. In my case, here is how I invoked it where I am cross compiling from my Macintosh, having already installed the NDK at /Users/me/Development/android-ndk-r10d:

ANDROID_NDK=/Users/me/Development/android-ndk-r10d
SYSROOT=$(ANDROID_NDK)/platforms/android-19/arch-arm
${ANDROID_NDK}/build/tools/make-standalone-toolchain.sh --platform=android-19 --install-dir=${ANDROID_NDK}/myapp-android-toolchain --ndk-dir=/Users/me/Development/android-ndk-r10d --toolchain=arm-linux-androideabi-4.8 --system=darwin-x86_64

With this setup, I launched my build like this:

cd ~/base/of/myapp/src/
ANDROID_NDK=/Users/me/Development/android-ndk-r10d PATH=${ANDROID_NDK}/myapp-android-toolchain/bin:$PATH make

After I fixed some issues with config for my cross-compilation in some third-party libraries I'm using, I had a working build. Next, I wanted to install it on my phone:

# Push to a location with write permission (sdcard):
cd ~/base/of/myapp/bin/ # My build puts the executable here.
adb push myapp /sdcard/
# Go into phone and complete the install manually:
adb shell
su
mount -oremount,rw rootfs /system # /vendor/bin is in /system mount.
# One time creation of /vendor/bin (that is in the PATH by default):
mkdir /vendor/bin
# Copy the myapp executable over
cp /sdcard/myapp /vendor/bin/myapp

Now, I can invoke myapp from the shell on my phone, like a normal command line interface Linux application. The argc and argv from my original Linux code work as written in the Android build by this process.

Create android apk manually via command line (Makefile)

ok, I figured it out. I placed my sample code on https://github.com/skanti/Android-Manual-Build-Command-Line

Hope it helps you guys too.

Manually (aapt) add native library .so to apk

Thanks to apktool (http://ibotpeaches.github.io/Apktool/) I found out what was wrong.

The makefile that generates the final .apk bundle had an error. Indeed the aapt add command contained \ in the path instead of /. As I'm generating on Windows, all my paths use \ as a separator. That's why I used \ for aapt add. However as this path is used during the installation of the .apk on Android (Unix), then I should have used / separator.

How can I generate an apk that can run without server with react-native?

Following Aditya Singh's answer the generated (unsigned) apk would not install on my phone. I had to generate a signed apk using the instructions here.

The following worked for me:

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

Place the my-release-key.keystore file under the android/app
directory in your project folder. Then edit the file
~/.gradle/gradle.properties and add the following (replace ****
with the correct keystore password, alias and key password)

MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=****
MYAPP_RELEASE_KEY_PASSWORD=****

If you're using MacOS, you can store your password in the keychain using the instructions here instead of storing it in plaintext.

Then edit app/build.gradle and ensure the following are there (the sections with signingConfigs signingConfig may need to be added) :

...
android {
...
defaultConfig { ... }
signingConfigs {
release {
if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
...

Then run the command cd android && ./gradlew assembleRelease ,

For Windows 'cd android' and then run gradlew assembleRelease command , and find your signed apk under android/app/build/outputs/apk/app-release.apk, or android/app/build/outputs/apk/release/app-release.apk



Related Topics



Leave a reply



Submit