Android treats ARM executable as a script - android

I am trying to execute an ARM binary on an Android emulator using adb, but it gives me an error as if it were trying to execute it as a script. Hello.c is a simple hello world program cross compiled for Android. How do I execute it properly? Below is what I did in my terminal. Thanks a lot.
arm-linux-androideabi-gcc --sysroot=/home/lotus/android-ndk-r13/platforms/android-9/arch-arm -o hello.o hello.c
adb -s emulator-5554 push hello.o /data/bin/hello
100 KB/s (6284 bytes in 0.061s)
adb -s emulator-5554 shell chmod 777 /data/bin/hello
adb -s emulator-5554 shell /data/bin/hello
/data/bin/hello: 1: Syntax error: word unexpected (expecting ")")

Related

Packaging error: Command "adb.exe -s emulator-5554 pull /system/bin/app_process" failed.Exit code: 1

I'm trying to build an empty new project project for Android target from Qt Creator, but I'm getting this error:
12:51:05: Packaging error: Command "C:/adt/sdk/platform-tools/adb.exe -s emulator-5554 pull /system/bin/app_process C:/Workspace/NewProjects/build-untitled18-Android_for_x86_GCC_4_9_Qt_5_4_2-Debug/app_process" failed.Exit code: 1
Screenshot:
And the full log, in case needed.
How do I fix that error?
After reading the answer to a related question:
As mentioned before, Android 5.0 has /system/bin/app_process as a symlink to /system/bin/app_process32. Since a symlink cannot be pulled with adb pull, the ndk-gdb script will not be able to work as-is.
I just logged into the android emulator VM and replaced the symlink with a copy of the file:
C:\Users\sasho>adb shell
root#generic_x86:/ # mount -o rw,remount /system
root#generic_x86:/ # cd /system/bin
root#generic_x86:/system/bin # rm app_process
root#generic_x86:/system/bin # cp app_process32 app_process
And it worked after that!

How to install an app in system/app while developing from android studio

Is there a way to make an app install directly in the system/app folder while developing on Android Studio (the device is rooted)?
Meaning, when I press on the 'Run app' button, I want the apk to be placed in system/app.
If this is not possible, what is the recommended most convenient way to work on building and testing a system app?
Deploy automatically system app from AS
You can create a script that will do the job, and run it automatically each time you hit run in AS.
1. Create the script
You can adapt this script that I've created from my needs. Place it in: project_directory/installSystem.sh
#!/bin/bash
# CHANGE THESE FOR YOUR APP
app_package="com.example"
dir_app_name="MySysApp"
MAIN_ACTIVITY="SysAppMainActivity"
ADB="adb" # how you execute adb
ADB_SH="$ADB shell" # this script assumes using `adb root`. for `adb su` see `Caveats`
path_sysapp="/system/priv-app" # assuming the app is priviledged
apk_host="./app/build/outputs/apk/app-debug.apk"
apk_name=$dir_app_name".apk"
apk_target_dir="$path_sysapp/$dir_app_name"
apk_target_sys="$apk_target_dir/$apk_name"
# Delete previous APK
rm -f $apk_host
# Compile the APK: you can adapt this for production build, flavors, etc.
./gradlew assembleDebug || exit -1 # exit on failure
# Install APK: using adb root
$ADB root 2> /dev/null
$ADB remount # mount system
$ADB push $apk_host $apk_target_sys
# Give permissions
$ADB_SH "chmod 755 $apk_target_dir"
$ADB_SH "chmod 644 $apk_target_sys"
#Unmount system
$ADB_SH "mount -o remount,ro /"
# Stop the app
$ADB shell "am force-stop $app_package"
# Re execute the app
$ADB shell "am start -n \"$app_package/$app_package.$MAIN_ACTIVITY\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
2. Bind it with AS Run
Go to Run -> Edit Configurations
Do the following changes on General tab (of your module)
Installation Options->Deplay: Nothing
Launch Options->Launch: Nothing
Before launch: press +, then Run External Tool, to select your script.
In the new dialog:
set any name.
On 'Tool Settings'->Program: navigate to the project's dir, and select your script
Caveats :
First installation
The device needs to be restarted (adb reboot) only once, on the very first installation of your app. Afterwards, you can simply press Run and everything will happen automatically.
This is because the host compiler (dex2oat) is not invoked automatically. Somehow the OS is not yet informed for this new system app. Calling dex2oat manually should solve this, but I had no luck. If anyone solves it please share.
adb root issues
Sometimes (usually the initial execution after the restart) the call to adb root does not find the device. You can simply re-play from AStudio, or sleep for a second after a successful adb root.
using su instead of adb root
adb push won't be working despite mounting system and giving permissions. To make it work replace the ADB_SH variable and the install section of the script with the following:
..
ADB_SH="$ADB shell su -c"
..
# Install APK: using adb su
$ADB_SH "mount -o rw,remount /system"
$ADB_SH "chmod 777 /system/lib/"
$ADB_SH "mkdir -p /sdcard/tmp" 2> /dev/null
$ADB_SH "mkdir -p $apk_target_dir" 2> /dev/null
$ADB push $apk_host /sdcard/tmp/$apk_name 2> /dev/null
$ADB_SH "mv /sdcard/tmp/$apk_name $apk_target_sys"
$ADB_SH "rmdir /sdcard/tmp" 2> /dev/null
Windows script for those interested:
Store this file the same way: in the root of your project directory (installSysPrivApp.bat)
::WIN BATCH SCRIPT
:: CHANGE THESE
set app_package=com.example.package
set dir_app_name=app
set MAIN_ACTIVITY=MainActivity
set ADB="adb"
::ADB_SH="%ADB% shell" # this script assumes using `adb root`. for `adb su`
see `Caveats`
set path_sysapp=/system/priv-app
set apk_host=.\Application\build\outputs\apk\Application-debug.apk
set apk_name=%dir_app_name%.apk
set apk_target_dir=%path_sysapp%/%dir_app_name%
set apk_target_sys=%apk_target_dir%/%apk_name%
:: Delete previous APK
del %apk_host%
:: Compile the APK: you can adapt this for production build, flavors, etc.
call gradlew assembleDebug
set ADB_SH=%ADB% shell su -c
:: Install APK: using adb su
%ADB_SH% mount -o rw,remount /system
%ADB_SH% chmod 777 /system/lib/
%ADB_SH% mkdir -p /sdcard/tmp
%ADB_SH% mkdir -p %apk_target_dir%
%ADB% push %apk_host% /sdcard/tmp/%apk_name%
%ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys%
%ADB_SH% rmdir /sdcard/tmp
:: Give permissions
%ADB_SH% chmod 755 %apk_target_dir%
%ADB_SH% chmod 644 %apk_target_sys%
::Unmount system
%ADB_SH% mount -o remount,ro /
:: Stop the app
%ADB% shell am force-stop %app_package%
:: Re execute the app
%ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
To bypass reboot issue from the #paschalis answer reinstall application with a help of package manager before remounting system to read only:
# Reinstall app
$ADB_SH "pm install -r $apk_target_sys"
# Unmount system
$ADB_SH "mount -o remount,ro /"
Package manager will invoke dex2oat by itself.
(Android Q >> Windows)
::WIN BATCH SCRIPT
::setup emulator https://stackoverflow.com/a/64397712/13361987
:: CHANGE THESE
set app_package=com.project.package
set dir_app_name=NewApkName
set MAIN_ACTIVITY=Package.MainActivity
set ADB="adb"
set path_sysapp=/system/priv-app
set apk_host=.\app\build\outputs\apk\debug\app-debug.apk
set apk_name=%dir_app_name%.apk
set apk_target_dir=%path_sysapp%/%dir_app_name%
set apk_target_sys=%apk_target_dir%/%apk_name%
:: Delete previous APK
del %apk_host%
:: Compile the APK: you can adapt this for production build, flavors, etc.
call gradlew assembleDebug
set ADB_SH=%ADB% shell su 0
:: Install APK: using adb su
%ADB_SH% mount -o remount,rw /system
%ADB_SH% chmod 777 /system/lib/
%ADB_SH% mkdir -p /sdcard/tmp
%ADB_SH% mkdir -p %apk_target_dir%
%ADB% push %apk_host% /sdcard/tmp/%apk_name%
%ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys%
%ADB_SH% rm -r /sdcard/tmp
:: Give permissions
%ADB_SH% chmod 755 %apk_target_dir%
%ADB_SH% chmod 644 %apk_target_sys%
:: Unmount system
%ADB_SH% mount -o remount,ro /
:: Stop the app
%ADB% shell am force-stop %app_package%
:: Re execute the app
%ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
:: from >> https://stackoverflow.com/questions/28302833/how-to-install-an-app-in-system-app-while-developing-from-android-studio
For mac
By using the script of #Paschalis I got 3 problems first I couldn't mount the system from adb so I did it with "terminal emulator for android" from jack palevich only (needed once) https://play.google.com/store/apps/details?id=jackpal.androidterm
mount -o rw,remount /system
the second problem was the JRE that was not the same as Android studio.
so I added
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/
to have the same version of java that android studio was using ("ctrl + ;" in android studio to get this path)
And the last problem because of adb root that can not run in production build so I flashed this zip with magisk
https://github.com/evdenis/adb_root
Android: adbd cannot run as root in production builds
but then my phone was not detected anymore so I removed adb root and this time all works well.
Also try to run the script manually line by line in a terminal to debug this script android studio does not give all the error.
I think adb push *.apk /system/app/*.apk should do just fine.
I don't know about Android Studio, but if you are on Linux you can get try to create an alias for
adb install
that points to that command, It should work!

Android NDK, Native app debugging

I am implementing a Native C++ program ( hello world ) w/o JNI
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
}
I am able to compile, run and attach a debugger using the following set of adb commands:
adb.exe -d shell am kill "/data/local/tmp/HellowWorld"
adb.exe -d push ./libs/armeabi/HellowWorld /data/local/tmp/HellowWorld
adb.exe -d shell /system/bin/chmod 0777 /data/local/tmp/HellowWorld
start /B adb.exe -d shell /data/local/tmp/HellowWorld
adb.exe -d push ./libs/armeabi/gdbserver /data/local/tmp/gdbserver
adb.exe -d shell /system/bin/chmod 0777 /data/local/tmp/gdbserver
adb.exe forward tcp:5039 tcp:5039
adb.exe -d shell ps
adb.exe -d shell /data/local/tmp/gdbserver tcp:5039 --attach %PID%
I define a new c++ Debug Configuration # eclipse setting up "\toolchains\arm-linux-androideabi-4.6\prebuilt\windows-x86_64\bin\arm-linux-androideabi-gdb.exe" as the debug client, Then I connect to the gdbserver running on the device
Unfortunetly, none of the breakpoints I set with Eclipse is actually Identified by gdb, info sharedLibrary shows the following where my main exe module is not found ( should it? ):
311-interpreter-exec console "info sharedLibrary"
~"From To Syms Read Shared Object Library\n"
From To Syms Read Shared Object Library
~" No /system/bin/linker\n"
No /system/bin/linker
~" No libc.so\n"
No libc.so
~" No libstdc++.so\n"
No libstdc++.so
~" No libm.so\n"
No libm.so
This is what I get from adb while setting a break-point:
355-break-insert D:/Develop/Android/EclipseProjects/ScreenCapSvc/jni/main.cpp:312
355^error,msg="No symbol table is loaded. Use the \"file\" command."
(gdb) file
365-interpreter-exec console file
=library-unloaded,id="/system/bin/linker",target-name="/system/bin/linker",host-name="/system/bin/linker",thread-group="i1"
=library-unloaded,id="libc.so",target-name="libc.so",host-name="libc.so",thread-group="i1"
=library-unloaded,id="libstdc++.so",target-name="libstdc++.so",host-name="libstdc++.so",thread-group="i1"
=library-unloaded,id="libm.so",target-name="libm.so",host-name="libm.so",thread-group="i1"
I use "APP_OPTIM := debug" and "LOCAL_CFLAGS += -gstabs+" at my make files.
Why can't I see my executable module when using "info sharedLibrary" ? Why can't I set breakpoints on my native NDK executable?

Python script unable to run "android" command

I am trying to create "avd" from python script using command :
android create avd -n avd1 -t android-19 -s QVGA -b x86
Everytime i run this command through Terminal i get avd created but if i am running same command from python script i a getting error which says that "android" command was not found.
Python script:
#!/usr/bin/env python
import sys, os
def main():
cmd = "android create avd -n avd1 -t android-19 -s QVGA -b x86"
print cmd
os.system(cmd)
main()
What could be the possible error.
Environment : Ubuntu 13.04
Path variable set to: export PATH=$PATH:~/android-sdks/tools/:~/android-sdks/build-tools/:~/android-sdks/platform-tools/:~/android-sdks/
From Python's documentation:
Changes to sys.stdin, etc. are not reflected in the environment of the
executed command.
os.system has limitations. The subprocess module is preferable.
Try:
import subprocess
cmd = "android create avd -n avd1 -t android-19 -s QVGA -b x86"
subprocess.call(cmd.split())

How do you push Android Instrumentation Tests to Emulator/Device?

I'm trying to run the Webkit Layout Tests on the Android emulator using the command line shell in Ubuntu 9.04.
adb -s emulator-5554 shell am instrument -w \
com.android.dumprendertree/com.android.dumprendertree.LayoutTestsAutoRunner
I get this error:
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.android.dumprendertree/com.android.dumprendertree.LayoutTestsAutoRunner}
I found that you need to use development/testrunner/runtest.py to push tests in android/frameworks/base/tests.
I've tried this to push the AndroidTests:
. ./build/envsetup.sh
emulator&
./development/testrunner/runtest.py android
But it gives me this error:
Error: ANDROID_PRODUCT_OUT not defined. Please run envsetup.sh
I did run envsetup.sh! Anyone know how to do this?
I've found an answer at Android - Instrumentation Testing
. build/envsetup.sh
(cd frameworks/base/tests/DumpRenderTree/ && mm) && \
adb install out/target/product/generic/data/app/DumpRenderTree.apk
Then you can run the instrumentation tests:
adb -s emulator-5554 shell am instrument -w \
com.android.dumprendertree/com.android.dumprendertree.LayoutTestsAutoRunner
once you launch adb shell in command line:
go with a command stating the package(com.ni.Keyboard) say of Keyboard application..
am instrument -w com.ni.Keyboard.test/android.test.InstrumentationTestRunner

Categories

Resources