[GUIDE] How to build .apk file from command line - General Topics

Author: Apriorit (Device Team)
If you don’t want to install a number of programs for building your Android project, this article is for you. You will need only JDK, the Android SDK platform tools and minimum Android platform for building the project using the batch file.
1. Introduction
In this article, I will describe how to use the Android tools for building the .apk file using the command line. As a result, you can use the batch file for building your Android application. I use Windows commands in this article. But you can use the same Android commands and tools and the same program if you are working on Linux OS. The example of the script for Linux OS is included in the sample project, attached to this article.
2. Preparation
You must install Java JDK and Android SDK on the system where you will build your Android application using the Windows command line. The private keystore also must be present.
2.1 Java JDK
You can use Java JDK from Eclipse or download it from this link: oracle(dot)com/technetwork/java/javase/downloads/index.html and then install it.
2.2 Android SDK
Android SDK and its HOWTO document are available here: developer.android.com/sdk/index.html[/url].
The Android SDK Platform-tools, SDK Platform Android 2.2, and API 8 are the minimum that is needed for the test project work.
2.3 Environment Variables
If you want to have a readable batch script and a program without very long file path definitions, you should use the environment variables. I recommend you to define the following ones:
• JAVABIN path to the Java JDK bin folder. For example: C:\Program Files\Java\jdk\bin. This folder must contain the javac.exe and jarsigner.exe files.
• ANDROID-SDK path to the Android SDK folder. For example: C:\Program Files\Andoroid\android-sdk-windows.
2.4 Private Key for Signing
You can read everything about signing, creating private key, and other operations here: developer.android(dot)com/guide/publishing/app-signing.html
In this article, I describe only one important command that will generate my-release-key.keystore:
Code:
%JAVABIN%\keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
Specify your info and you will receive the my-release-key.keystore file. You must remember the password. You will need it later. Also, I recommend you to put the keystore file in the same folder as the project. In the example,It is in the keystore folder in the project directory.
2.5 Example Files Structure
You must remember that the dx Android tool requires the full path, which MUST not contain any spaces. So, check the fact, that your project path satisfies the requirement.
The file structure of the example project, which is built using the build.bat file, is the following:
Code:
SecureMessages/
assets/
keystore/
my-release-key.keystore
res/
drawable-hdpi/
icon.png
drawable-ldpi/
icon.png
drawable-mdpi/
icon.png
layout/
main.xml
values/
strings.xml
src/
org/
secure/
sms/
SecureMessagesActivity.java
StringCryptor.java
SmsReceiver.java
AndroidManifest.xml
build.bat // Windows build script
build.sh
// Linux build script
3. Command Sequence
The following commands are for the Windows OS, but you can find the script for the Linux OS project building in the attached sample.
First of all, we must save current path. Then we must change the CD variable to the path to the build.bat file:
Code:
SET PREV_PATH=%CD%
cd /d %0\..
Then, bin and gen old folders should be recreated:
rmdir "bin" /S /Q
rmdir "gen" /S /Q
mkdir "bin"
mkdir "gen"
I add some definitions. They make the batch file readable and easy to update. So, I recommend you the following definitions:
• minimum Android revision;
• the path to aapt Android tool and its arguments for adding files into the existing archive;
• the path to aapt Android tool and its arguments for packing and generating resources;
• the path to dx Android tool;
• the path to javac utility of JDK.
This list of independent definitions can be used for building the majority of Android projects (of course, with the changed Android revision value). For example:
Code:
SET ANDROID_REV=android-8
SET ANDROID_AAPT_ADD="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" add
SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
SET ANDROID_DX="%ANDROID-SDK%\platform-tools\dx.bat" --dex
SET JAVAC="%JAVABIN%\javac.exe" -classpath "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
Also I need the defined variables for my project:
• APP_NAME is the name of application that will be used for the output APK file.
• JAVAC_BUILD and JAVAC are the same commands, but there are path to the sources, generated R class, and output folder in JAVAC_BUILD.
These variables let us change the project name and paths to the sources easier.
Code:
SET APP_NAME=SecureSms
SET JAVAC_BUILD=%JAVAC% -sourcepath "src;gen" -d "bin"
And now, all preparations are finished and the application can be built. The R file will be generated using aapt tool. All resources will be packed into the resources.ap_ file:
Code:
call %ANDROID_AAPT_PACK% -M "AndroidManifest.xml" -A "assets" -S "res" -m -J "gen" -F "bin\resources.ap_"
Android manifest file, res and assets folder are the input data. Aapt will generate the R class and put it into the gen folder. Then, aapt will pack the resource files into the resourses.ap_ file.
Every folder that contains *.java file must be called with javac. In my example, there is only one folder with javac. So, I have only one command line for building sources:
Code:
call %JAVAC_BUILD% src\org\secure\sms\*.java
As you remember, the JAVAC_BUILD command has already contained the arguments that specify the bin folder. The bin folder is an output folder for the compiled sources.
At this moment, the sources have been compiled successfully and they can be packed in the special dex file:
Code:
call %ANDROID_DX% --output="%CD%\bin\classes.dex" %CD%\bin
For the application signing, the resources file must be copied to another file, which will be used for the signing:
Code:
copy "%CD%\bin\resources.ap_" "%CD%\bin\%APP_NAME%.ap_"
The classes.dex file must be added to the new file that has an ap_ extension
Code:
call %ANDROID_AAPT_ADD% "%CD%\bin\%APP_NAME%.ap_" "%CD%\bin\classes.dex"
Now, the ap_ file is a correct apk file. But it is not signed yet and it cannot be installed to the Android device.
The creation of the signed Android application from the *.ap_ file is the following (Output file name must differ from the input file name – in this case the file extension is changed to the *.apk):
Code:
call "%JAVABIN%\jarsigner" -keystore "%CD%\keystore\my-release-key.keystore" -storepass "password" -keypass "password" -signedjar "%CD%\bin\%APP_NAME%.apk" "%CD%\bin\%APP_NAME%.ap_" "alias_name"
And delete temp ap_ file:
Code:
del "bin\%APP_NAME%.ap_"
Finally, let’s return to the start folder and clear local variables:
Code:
:EXIT
cd "%PREV_PATH%"
ENDLOCAL
exit /b %ERRORLEVEL%
To test the example, you must:
• unpack it
• do the steps 2.1-2.5 from the Preparations paragraph
• run the build.bat file
You can download the sample project source at the article official page apriorit(dot)com/our-company/dev-blog/233-how-to-build-apk-file-from-command-line

Please guide me building with Android support libraries and jack without gradle or ant.
Thanks.

Related

Simple HOWTO building some linux apps and libraries from scratch on android

This howto is only draft! Sometimes i went upgrade this, or make page on my site and post link on it. Also we have a project for automated building, like buildroot and opkg repository for android.
All needed configs in attached file. If you have error on unpack - just rename my.zip in my.tar.bz2 and try to unpack. Please correct me, if needed.
----------------------------------------------------------------------------------------------------------------------------------
All work will be in home directory (/home/xvilka)
create dir build
mkdir build
1. Download the kernel (for android on your version for device) and unpack
make headers_install ARCH=arm INSTALL_HDR_PATH=~/build/kern_h/
For some optimisations you need know, which proccessor (SoC) in your android-system(gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/ARM-Options.html#ARM-Options)
Im have Motorola Milestone - SoC = TI OMAP3430 - ARM Cortex a8 (arch - armv7a)
(en.wikipedia.org/wiki/ARM_architecture)
So, if you have OMAP3430-processor - use the config files with -omap3430 suffix
If you have Quallcomm MSM7200A (HTC Hero) - use the config files with -msm7200a suffix
First, we need to define _XXFLAGS, which contain some optimisation options for your compiler:
export _XXCFLAGS=" -mcpu=cortex-a8 -march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3 -O3 -Os" (if you have Motorola Milestone/Droid or other device with TI OMAP3430)
export _XXCFLAGS=" -march=armv6j -mtune=arm1136jf-s -O3 -Os" (if you have HTC Hero or other device with Quallcomm MSM 7200A)
............................................................................................
If you don't know which chip you have in phone use _XXCFLAGS=" -O3 -Os"
2. Download crosstool-ng (ymorin.is-a-geek.org/dokuwiki/projects/crosstool),
unpack, build (you must have make,install,bash,cut,sed,grep,gcc,awk,bison,flex,automake,libtool,stat,
wget,cvs,patch,tar,gzip,bzip2,lzma,readlink,ncurses)
.configure
make
make install
3. Create directory toolchain-android, and copy config to this
XVilka-crosstool-<suffix>.config in .config
XVilka-uClibc-<suffix>.config in uClibc-0.9.30.2.config (or later version, if available)
and then run:
ct-ng menuconfig
you may change some options, if needed!
(Also use options from XXCFLAGS:
Target options -> Target optimizations ->
Architecture-level equal -march, use armv7-a
Emit assembly for CPU equal -mcpu, use cortex-a8
Tune for cpu equal -mtune, use cortex-a8
Use specific FPU equal -mfpu, use vfpv3
Floating point use hardware, because it built-in TI OMAP3430
Target CFLAGS - use -O3 -Os
) - for the Motorola Milestone/Droid
(Also use options from _XXCFLAGS:
Target options -> Target optimizations ->
Architecture-level equal -march, use armv6j
Emit assembly for CPU equal -mcpu, use nothing
Tune for cpu equal -mtune, use arm1136jf-s
Floating point use software
Target CFLAGS - use -O3 -Os
) - for the HTC Hero
Save in .config
In file uClibs-0.9.30.2.config
edit this strings:
{
UCLIBC_HAS_FLOATS=y
UCLIBC_HAS_FPU=y
# UCLIBC_HAS_SOFT_FLOAT=y
UCLIBC_EXTRA_CFLAGS=" -march=armv7-a -mcpu=cortex-a8 -mtune=cortex-a8 -mfpu=vfpv3 -O3 -Os"
} - for the Motorola Milestone/Droid
{
UCLIBC_HAS_FLOATS=y
#UCLIBC_HAS_FPU=y
UCLIBC_HAS_SOFT_FLOAT=y
UCLIBC_EXTRA_CFLAGS=" -march=armv6j -mtune=arm1136jf-s -O3 -Os"
} - for the HTC Hero
and then run
ct-ng build
4. Ok, done. We have installed cross - toolchain in directory build/x-tools.
And all programs in it with names arm-android-linux-uclibsgnueabi-*
For easy using add this directory in PATH:
PATH=~/build/x-tools/arm-android-linux-uclibcgnueabi/bin:$PATH
so we can run cross-gcc by the command:
arm-android-linux-uclibcgnueabi-gcc
Root directory of target system is:
~/build/x-tools/arm-android-linux-uclibcgnueabi/arm-android-linux-uclibcgnueabi/sys-root
But for the safe - make new root dir and copy files too.
Copy files in ~/build/cross/sys-root
chmod +w sys-root
chmod +w sys-root/usr
5. Building busybox (only static build, but if we have static busybox in any problems with system problems with libs
we can easy use busybox as repair-mode tool)
copy file XVilka-busybox-<suffix>.config in .config
make menuconfig
make ARCH=arm CC="arm-android-linux-uclibcgnueabi-gcc" CROSS_COMPILE="arm-android-linux-uclibcgnueabi-" \
CFLAGS=" -static $_XXFLAGS"
make install CONFIG_PREFIX=~/build/cross/sys-root
Copy ~/build/cross/sys-root/bin/busybox in /system/bin on device
chown root /system/bin/busybox
chmod 755 /system/bin/busybox
ln -s /system/bin/busybox /system/bin/ash
Or in other directory and you can install all programs included in busybox by the command:
busybox --install
7. Building simple but powerful text editor jupp (mirbsd.org/jupp.htm)
chmod +x configure
./configure --prefix=/system/local --disable-curses --disable-termcap --disable-termidx --host=arm-android-linux-uclibcgnueabi ARCH=arm \
CC="arm-android-linux-uclibcgnueabi-gcc" CROSS_COMPILE="arm-android-linux-uclibcgnueabi-" sysconfdir=/system/usr/local/etc sysconfjoesubdir=/joe \
CFLAGS=" -static $_XXFLAGS"
make
arm-android-linux-uclibcgnueabi-strip joe
On device:
copy joe in /system/bin
copy joerc in /system/usr/local/etc/joe
copy jupprc in /system/usr/local/etc/joe
copy jmacsrc in /system/usr/local/etc/joe
copy sintax files in /system/usr/local/etc/joe/syntax
chmod 755 /system/bin/joe
ln -s /system/bin/joe /system/bin/jupp
ln -s /system/bin/joe /system/bin/jmacs
chmod 644 /system/usr/local/etc/joe/joerc
chmod 644 /system/usr/local/etc/joe/jupprc
chmod 644 /system/usr/local/etc/joe/jmacsrc
before running joe we must define TERM
export TERM=ansi
8. How to use dinamic linking and shared libraries
(if we installed programs in local directory in sys-root,
so copy lib from local)
cd ~/build/cross/sys-root
tar -jcf lib.tar.bz2 lib/
And on device:
cd /system/usr/local
busybox tar -xcf /sdcard/lib.tar.bz2
For our programs, wich uses this non-standart library path we may create this script and run your programs by it:
#!/bin/sh
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
exec /usr/bin/local/some_program $*
9. Building libFLAC in shared library, which we can use easy and copy to /system/lib,
which have nothing in NEEDED - just kernel and you, also no any libc (bionic, or uClibc) not used.
Download latest version of libFLAC from the official site, unpack and cd to dir:
./autogen.sh --host=arm --target=arm-android-linux-uclibcgnueabi CC="arm-android-linux-uclibcgnueabi-gcc" \
CXX="arm-android-linux-uclibcgnueabi-g++" STRIP="arm-android-linux-uclibcgnueabi-strip" \
AR="arm-android-linux-uclibcgnueabi-ar" AS="arm-android-linux-uclibcgnueabi-as" LD="arm-android-linux-uclibcgnueabi-ld" \
RANLIB="arm-android-linux-uclibcgnueabi-ranlib" OBJDUMP="arm-android-linux-uclibcgnueabi-objdump" \
NM="arm-android-linux-uclibcgnueabi-nm" CROSS_COMPILE="arm-android-linux-uclibcgnueabi-" \
CFLAGS=" -nodefaultlibs -nostdlib -fPIC $_XXFLAGS" \
CXXFLAGS=" -nodefaultlibs -nostdlib -fPIC $_XXFLAGS" \
LDFLAGS=" -fPIC -static -s -L$HOME/build/cross/sys-root/usr/lib -lm -lc" --prefix="$HOME/build/cross/sys-root/usr" \
--disable-ogg --disable-oggtest --disable-rpath --disable-xmms-plugin --disable-cpplibs --disable-altivec \
--disable-3dnow --disable-thorough-tests --disable-doxygen-docs
cd src/libFLAC
make
cd .libs
arm-android-linux-uclibcgnueabi-ld -shared -s -o libFLAC.so -whole-archive libFLAC.a
And now we have shared library libFLAC.so, lybrary without any dependency, and which we can just copy in /system/lib
on device and use it from any program.
For programming on libFLAC you must see header files of libFLAC
10. Building xvid in shared library, without any dependecy, same as libFLAC.
cd src/build/generic
./configure --host=arm --target=arm-android-linux-uclibcgnueabi CC="arm-android-linux-uclibcgnueabi-gcc" \
CXX="arm-android-linux-uclibcgnueabi-g++" STRIP="arm-android-linux-uclibcgnueabi-strip" \
AR="arm-android-linux-uclibcgnueabi-ar" AS="arm-android-linux-uclibcgnueabi-as" \
LD="arm-android-linux-uclibcgnueabi-ld" RANLIB="arm-android-linux-uclibcgnueabi-ranlib" \
OBJDUMP="arm-android-linux-uclibcgnueabi-objdump" NM="arm-android-linux-uclibcgnueabi-nm" \
CROSS_COMPILE="arm-android-linux-uclibcgnueabi-" \
CFLAGS=" -nodefaultlibs -nostdlib -fPIC $_XXFLAGS" \
CXXFLAGS=" -nodefaultlibs -nostdlib -fPIC $_XXFLAGS" \
LDFLAGS=" -fPIC -static -s -L$HOME/build/cross/sys-root/usr/lib -lm -lc" --prefix="$HOME/build/cross/sys-root/usr"
cd =build
rm-android-linux-uclibcgnueabi-ld -shared -s -o libxvidcore.so -whole-archive libxvidcore.a
So we have libxvidcore.so, which using very-very simple and copy-only-use too.
11. Building nmap
svn co --username guest --password "" svn://svn.insecure.org/nmap/
cd nmap
./configure --host=arm-android-linux-uclibcgnueabi CC="arm-android-linux-uclibcgnueabi-gcc" \
CROSS_COMPILE="arm-android-linux-uclibcgnueabi-" CFLAGS=" -static $_XXFLAGS" \
CXXFLAGS=" -static $_XXFLAGS" \
--prefix="$HOME/build/cross/sys-root/system/usr/local" --with-lua=included --with-pcap=included --with-pcre=included --with-dnet=included
make
make install
12. Building dropbear
./configure --host=arm-android-linux-uclibcgnueabi CC="arm-android-linux-uclibcgnueabi-gcc" \
CROSS_COMPILE="arm-android-linux-uclibcgnueabi-" \
CFLAGS=" -static $_XXFLAGS" \
--prefix="$HOME/build/cross/sys-root/system/usr/local" --disable-zlib
make
make install
copy dbclient, dropbearconvert and dropbearkey in /system/bin
copy dropbear in /system/sbin
chown root on all copied files
chmod 755 on all copied files
Hi, I've been folowing your howto in order to build busybox but when I download the my.zip file I think it's corrupted or something because it's empty and I can't extract the config files. Could you check it please? Thanks in advance
the my.zip file is compressed strangely. I was able to open it using 7-zip then opened the "my" file in there and it showed a bunch of file within it. Can the op look into his archive and see about recompressing it in a more compliant format. It isn't a zip file.
EDIT: Actually forget it. When you download it replace the .zip with .tar.bz2 or you can just append it to the filename probably if the .zip part isn't showing for you. It's a bzip2 compressed tar archive.
shinji257 said:
the my.zip file is compressed strangely. I was able to open it using 7-zip then opened the "my" file in there and it showed a bunch of file within it. Can the op look into his archive and see about recompressing it in a more compliant format. It isn't a zip file.
EDIT: Actually forget it. When you download it replace the .zip with .tar.bz2 or you can just append it to the filename probably if the .zip part isn't showing for you. It's a bzip2 compressed tar archive.
Click to expand...
Click to collapse
worked like a charm. Thanks!!
I would like to thank you for this guide. With it (and a small alteration to my own busybox .config) I was able to build 1.18.5 and 1.19.0.git with network support working finally. The files are a bit bigger for my taste but I'll go back and see if I can get a dynamic build next.
The flash* items (flashcp, flash_lock, flash_unlock, flash_eraseall) will not build unless you get the mtd-utils package from the android repo.
EDIT: It will dynamic build but I came across a roadblock. The uClibc included on my phone already is 0.9.31 and the toolchain built 0.9.30.2 so I'm changing it up a bit... Should be fine though.
Dynamic link reduces from 1.7MB to ~1.0MB
Here is russian version of manual http://wiki.androidfan.ru/Сборка_c_нуля
Also there some updates too: https://www.droid-developers.org/wiki/Toolchain
Easy way to build jupp… if you’ve got Debian, that is.
XVilka said:
7. Building simple but powerful text editor jupp (mirbsd.org/jupp.htm)
Click to expand...
Click to collapse
Thank you very much for liking jupp and publishing him here!
If you’re running Debian on an ARM system, there’s an even easier way to get jupp.
• set up the system for building packages (or login to a Debian porterbox like harris.debian.org and switch into the “sid” chroot): install the build-essential package
• install debhelper and autotools-dev
• install dietlibc-dev (or libklibc-dev, but I tested with dietlibc-dev right now as I cannot install libklibc-dev on the porterbox by myself)
• go to packages.debian.org/source/sid/jupp and figure out the download link to the .dsc file
• download that with “dget”:
Code:
dget -d http://ftp.de.debian.org/debian/pool/main/j/jupp/jupp_3.1.23-1.dsc
(of course, adjust the link; this is the version up to date at the time of this writing)
• extract the package (adjust the name as usual):
Code:
dpkg-source -x jupp_3.1.23-1.dsc
• switch into the source directory:
Code:
cd jupp-3.1.23/
• run a compilation:
Code:
env DEB_BUILD_OPTIONS=diet fakeroot debian/rules binary
(or DEB_BUILD_OPTIONS=klibc)
Now you have in the directories ./debian/jupp/ and ./debian/joe-jupp/ the full build result. Test it by running
Code:
./debian/jupp/usr/bin/jupp
(you can press ^Kq, that is Ctrl-K followed by q, to exit it immediately; remember ^J (Ctrl-J) opens the help).
Now install the following files into their final locations:
• debian/jupp/etc/jupp/* to /etc/jupp/ (including subdirectories)
• debian/jupp/usr/bin/jupp to /usr/bin/jupp
• optionally, debian/joe-jupp/etc/jupp/ to /etc/jupp/ and the symlinks from debian/joe-jupp/usr/bin/ to /usr/bin/
Strictly speaking, for running “jupp”, no other file than the executable is needed, and that can be in any path. It includes a copy of jupprc in the executable, although without syntax highlighting.
If you have to change the paths, look at the file debian/rules (--prefix, --sysconfdir, --mandir although you don’t need the mandir) and debian/jupp.install and debian/joe-jupp.install (install paths) before compiling.
JOE finds the *rc files by looking into a path that is set at compile time (if --sysconfdir=/etc it looks into /etc/joe/ but debian/rules also sets sysconfjoesubdir=/jupp at make time, so it uses /etc/jupp/ instead), a file that’s the basename of the editor command plus “rc”, so /usr/bin/jfoo will let it load jfoorc. The klingon charmap and the syntax files are expected in subdirectories of where the configs (rc files) are.
Hope that helps!
If you want, I’m in the IRC channel #MirBSD and #!/bin/mksh on Freenode IRC network, and can help (but will assume you’re either running Debian/ARM or have your cross compiler set up correctly).

[Q] Android Scripting Q

I am trying to run a script from my nook color running CM7 (2.3.3). The script contains several wget commands designed to download pdf files from specific URLs. I have replaced the stock android busybox wget with a more powerful one that has all of the standard options. The weird thing is that when I execute the script via the command #sh run.sh, only the last line of the script executes properly...
Here is what is contained in the script file (run.sh):
wget -N -P /mnt/sdcard/wget/app/ppm --append-output=log.txt -i /mnt/sdcard/wget/ppm.txt
wget -N -P /mnt/sdcard/wget/app --append-output=log.txt -i /mnt/sdcard/wget/revision_checklist.txt
This returns the log:
/mnt/sdcard/wget/ppm.txt
: No such file or directory
No URLs found in /mnt/sdcard/wget/ppm.txt
.
[Removed Server Data Here]
Server file no newer than local file `/mnt/sdcard/wget/app/checklist.pdf' -- not retrieving.
What is weird is that if I add a third line to the run.sh file, the first two will error out and the third will execute. I've written the file in notepad++ using ANSI encoding. If I show all characters, the carriage return after all but the very last line shows a CR and LF.
I'm stumped...would sure appreciate any help.

[APP][DEV][GUIDE] Using the Android Java Internal/Hidden API classes

Using Java Reflection with Eclipse ADT to Access Internal/Hidden API classes.
Purpose
We present a way to access all the Internal and Hidden Java packages/classes
in the AOS. To do this we need to both repackage the Android.jar and hack the
Eclipse ADT plugin, to allow using these internal packages.
Posting
==================================================
Do NOT post general questions/requests on how to
do this or that, they will not be answered here.
DO post if you have additional tricks, hacks or
information that can help/benefit this tutorial.
==================================================
Background
There are two reasons one cannot use internal packages. One reason is that, if
you're using Eclipse as your development platform, those packages are
internally blocked in the Eclipse ADT plugin. Second reason is that the normal
development android.jar runtime does not contain those *.class files that
belong to the internal packages.
"There is no easy way to use com.android.internal package (internal API) or
anything marked with @hide attribute (hidden API) without using reflection.
That’s because android.jar file does not contain classes from internal and
hidden API and because of this nobody can reference those classes in compile
time."
Thus we need to first restore the "original" android.jar which will allow us
to use internal and hidden APIs. But the runtime equivalent of Android SDK’s
android.jar file is framework.jar. This file is located in the
/system/framework/ directory of your device. We will extract and use this for
our pleasure.
The general procedure:
A) Grab the "full" framwork.jar from your device
B) extract the class files
C) add them to "full" android.jar ??
D) Hack the Eclipse ADT plugin jar.
Finally, NOTHING would have been possible without the excellent step-by-step
instructions on the devmaze-blog by senior Android developer Ievgenii Nazaruk
(aka. "inazaruk"). THANK YOU Ievgenii!​References
http://stackoverflow.com/questions/...d-sdk-with-hidden-and-internal-apis-available
http://stackoverflow.com/questions/...-state-permission-for-apps-ran-on-gingerbread
http://code.google.com/p/smali/wiki/DeodexInstructions
http://code.google.com/p/adt-addons/
​
The General Procedure
NOTE: All this was performed on Windows Vista with Cygwin.(1) Grab BOOTCLASSPATH from init.rc
Find the line in your init.rc file that reads something like:
Code:
[SIZE=2]export BOOTCLASSPATH /system/framework/core.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/core-junit.jar[/SIZE]
Extract and reformat the path to:
Code:
[SIZE=2]core.jar:bouncycastle.jar:ext.jar:framework.jar:android.policy.jar:services.jar:core-junit.jar[/SIZE]
(2) Grab the "framework" from your device
Create a working directory somewhere, let's call it "_framework":
Code:
[SIZE=2]mkdir ./_framework[/SIZE]
[SIZE=2]cd _framework[/SIZE]
Grab all the framework files from your device:
Code:
[SIZE=2]adb pull /system/framework .[/SIZE]
Rename directory if needed.
NOTE-1: From now on I'll assume you know where you are!
NOTE-2: Most GB 2.3.4+ devices uses .odex'ed files,
with name pairs like: <package>.jar and <package>.odex.
These need to be converted.
(3) Use baksmali with (1)
You can also use baksmali with the switch: -d <framwork-dir>.
The general command is something like below, but in windows there may be "wrappers"
that allow you to just type "baksmali" without the "java -jar" prefix and without
the ".jar" post fix. Anyway here is the command I used:
Code:
[SIZE=2]java -Xmx1024m -jar ./../../baksmali.jar -a 10 -c :core.jar:bouncycastle.jar:ext.jar:framework.jar:android.policy.jar:services.jar:core-junit.jar -x framework.odex[/SIZE]
==> This results in all files put in the "out" sub-directory.
This directory contain 3 sub-directories (for GB 2.3.4):
Code:
[I]android [/I](Hidden APIs)
[I]com [/I](Internal APIs)
[I]javax [/I](Hidden APIs)
NOTE: If you are using Google's own API's, you will probably also need to add
those packages to the path above. (Eg. Email.jar, etc etc ?)
(4) Then use smali to create a dex file from "out" directory
Code:
java -jar smali.jar out
==> creates out.dex from "out" directory.
(5) Run dex2jar on out.dex
Code:
[SIZE=2]./dex2jar.bat out.dex[/SIZE]
==> creates out_dex2jar.jar
(This .jar contain close to 4900 files at 12 MB!)
(6) Rename "out_dex2jar.jar" to "framework-classes.zip"
Code:
[SIZE=2]mv out_dex2jar.jar framework-classes.zip
unzip framework-classes.zip[/SIZE]
(7) Find and copy your Android SDK's android.jar file
Go to: /path/to/android-sdk-windows/platforms/android-X/android.jar
where X is the API level of interest. This obviously have to match the
API level of the files you extracted from your device in: /system/framework .
This .jar file contain more than 5300 files when expanded, but missing all
the internal packages. Our job is to add them back in.
Let's first make a copy that we can use to expand and add files from (6):
Code:
cp android.jar custom-android.zip
unzip custom-android.zip
(8) Add all *.class files from (6) in to (7)
Copy and replace all existing *.class files from framework-classes.zip into
custom-android.zip:
Code:
[SIZE=2]cp -R /path/to/framework-classes/* /path/to/custom-android/.[/SIZE]
The root content of that directory should then look something like this:
Code:
[SIZE=2]android[/SIZE]
[SIZE=2]assets[/SIZE]
[SIZE=2]com[/SIZE]
[SIZE=2]dalvik[/SIZE]
[SIZE=2]java[/SIZE]
[SIZE=2]javax[/SIZE]
[SIZE=2]junit[/SIZE]
[SIZE=2]META-INF[/SIZE]
[SIZE=2]org[/SIZE]
[SIZE=2]res[/SIZE]
[SIZE=2]AndroidManifest.xml[/SIZE]
[SIZE=2]resources.arsc[/SIZE]
(9) Rename directory and create your new "classy-android.jar"
We rename it so not confuse with the original:
Code:
mv custom-android classy-android
zip classy-android
mv classy-android.zip classy-android.jar
IMPORTANT:
Make absolutely sure that the folder structure of your zip archive
is exactly the same as what you intended. To check, unzip the file
and see if it is what you (and eventually Eclipse) would expect.
(For example, if you use 7zip to zip a directory file called "test",
into "test.zip", you may end-up extracting it to ./test/test/... )
(10) Enabling & Restricting Access to classy-android.jar
Instead of just replacing the android.jar with classy-android.jar, we choose
to create a customized Android platform. This way you can enable the Internal
and Hidden API's for those projects requiring them, while other standard
projects doesn't have access to those.
(a) Go to: /path/to/android-sdk-windows/platforms/
and copy the relevant directory (for example):
Code:
cp -R android-10 android-10-internals
(b) Replace android.jar with your classy-android.jar:
Code:
cp classy-android.jar android.jar
("cp" overwrites!)
(c) Edit the build.prop file:
Edit/replace the following lines:
Code:
[SIZE=2]ro.build.version.sdk=10 ==> ro.build.version.sdk=[COLOR=Black][B]-10[/B][/COLOR][/SIZE]
[SIZE=2]ro.build.version.release=2.3.3 ==> ro.build.version.release=2.3.internal[/SIZE]
(11) Customizing the Eclipse ADT
In order to be able to use com.android.internal packages in the Eclipse ADT,
you have to disable the internal protection mechanism of the plugin, that
prevent you to use these libraries. You can see this by right-clicking on your
project package and navigate to:
Code:
[SIZE=2]==> Properties ==> Java Build Path ==> Libraries (tab) [/SIZE]
[SIZE=2]--> Android 2.x.x --> android.jar [/SIZE]
[SIZE=2]--> "Access rules: 1 rule defined": [B][COLOR=Red](X)[/COLOR][/B] [B]Forbidden: com/android/internal/**[/B][/SIZE]
This can not be removed (bug?), even though the interface allows changing, it
never persists after closing the Properties window. So we have to hack it!
The way to do it, is to hexedit the correct java class file and change the
name from "internal" to "internax". First let's find the correct file. The
plugin file is located in the ./eclipse/plugins/ directory, and its name is
something like:
Code:
com.android.ide.eclipse.adt_18.0.0.v201203301601-306762.jar
(a) make a backup copy of this (with the exact name preserved) in another directory.
(b) make a another copy of this in another directory.
(c) unzip (b) in that directory
Code:
[SIZE=2]cp com.android.ide.eclipse.adt_18.0.0.v201203301601-306762.jar hacked_adt.zip[/SIZE]
[SIZE=2]unzip hacked_adt.zip[/SIZE]
[SIZE=2]cd hacked_adt[/SIZE]
This is a huge directory system, so forget poking around in it,
just go to the correct sub-directory:
Code:
[SIZE=2]cd ./com/android/ide/eclipse/adt/internal/project/[/SIZE]
Then find the correct file and the approximate string location within that file:
Code:
[SIZE=2]strings.exe -f -4 -t x ./*.class |grep "android\/internal"[/SIZE]
It happens to be in "AndroidClasspathContainerInitializer.class". Now, use a
hexeditor to find and change the string "com/android/internal/**"
to "com/android/internax/**". That will do it!
Now zip-up your hacked jar directory and copy it over the old one.
(Remember that "cp" overwrites without warning!)
Code:
[SIZE=2]zip hacked_adt[/SIZE]
[SIZE=2]cp hacked_adt.zip /path/to/eclipse/plugins/com.android.ide.eclipse.adt_18.0.0.v201203301601-306762.jar[/SIZE]
You Are Done!
Enjoy your newly hacked Eclipse! ​Errors
If you get any errors;
1. make sure you have zipped up everything properly as warned before.
2. make sure you have included Google API packages in your BOOTCLASSPATH in step (3).
3. Try to "clean-up" the Java by: "Right-Click" ==> Source ==> "Clean Up...".
4. Google them
5. Ignore them
6. Give up. Not! But I can't help you!
If it still doesn't work, try to download inazaruk's pre-compiled set of internal android.jar's from here.
(For android 7,8,10,15.)
​
WIP! <here be dragons2>
For a project using internal package imports, see my thread:
"[TOOL][APP][WIP] Native AT Command Injector"
<here be more dragons>
Following the instructions in posts 1-2 above, may not always work. It is not known to me at this time, why it shouldn't. One theory is that it can have something to do with how Eclipse and Android.jar is packaging their files and the resulting sizes.
This was mentioned in this Stackoverflow post:
"Jar files: why does extracting then compression a jar file create a file of a different size to the original?"
Then reading the man pages for "jar" we can inform ourselves with:
Code:
[SIZE=2] c Creates a new archive file named jarfile (if f is specified) or to
standard output (if f and jarfile are omitted). Add to it the
files and directories specified by inputfiles.
u Updates an existing file jarfile (when f is specified) by adding
to it files and directories specified by inputfiles.
x Extracts files and directories from jarfile (if f is specified) or
standard input (if f and jarfile are omitted). If inputfiles is
specified, only those specified files and directories are
extracted. Otherwise, all files and directories are extracted.
t Lists the table of contents from jarfile (if f is specified) or
standard input (if f and jarfile are omitted). If inputfiles is
specified, only those specified files and directories are listed.
Otherwise, all files and directories are listed.
i Generate index information for the specified jarfile and its
dependent jar files.
[/SIZE]
More info is provided here:
The JAR Overview @
http://java.sun.com/javase/6/docs/technotes/guides/jar/jarGuide.html
The JAR File Specification @
http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html
The JARIndex Spec @
http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html
JAR Tutorial @
http://java.sun.com/docs/books/tutorial/jar/
pack200 Reference Page @
http://java.sun.com/javase/6/docs/technotes/tools/share/pack200.html
Another theory is that it may have something to do with what seem to be, that Google have revoked the the use of MODIFY_PHONE_STATE since Android 2.3, and that this influences the Eclipse behavior, when using and modifying older android.jar's. This was mentioned here and here.
Any help would be very much appreciated!
< bump >
Hi, thanks for the info, I made it using linux and worked really nice, these were the lines that I used, hope be useful.
This is my Android folder at home
Code:
Android/
├── eclipse
├── ndk
├── platforms-internals
├── sdk
└── tools
Start an avd running the desired API to modify in this case API-17
Code:
$ emulator -avd avd_api_17 -no-window &
Get the framework
Code:
$ cd ~/Android/
$ mkdir _framework
$ cd _framework
$ adb -s emulator-5554 pull /system/framework .
Grab BOOTCLASSPATH
Code:
$ adb -s emulator-5554 shell cat init.rc | grep BOOTCLASSPATH > bootclasspath
I didn't have the tools used in this tutorial, so I included the steps for getting them
Decompile with baskmali
Code:
$ cd ~/Android/tools
$ wget https://smali.googlecode.com/files/baksmali-1.4.2.jar
$ cd ~/Android/_framework
$ java -Xmx1024m -jar ../tools/baksmali-1.4.2.jar -a 17 -c core.jar:core-junit.jar:bouncycastle.jar:ext.jar:framework.jar:telephony-common.jar:mms-common.jar:android.policy.jar:services.jar:apache-xml.jar -x framework.odex
The parameter -a for baksmali refers to the API we are working with.
Generate out.dex with smali
Code:
$ cd ~/Android/tools
$ wget https://smali.googlecode.com/files/smali-1.4.2.jar
$ cd ~/Android/_framework
$ java -jar ../tools/smali-1.4.2.jar out
Get internal and hidden classes using dex2jar
Code:
$ cd ~/Android
$ wget https://dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip
$ unzip dex2jar-0.0.9.15.zip
$ rm dex2jar-0.0.9.15.zip
$ cd _framework/
$ ../tools/dex2jar-0.0.9.15/d2j-dex2jar.sh out.dex
$ unzip out-dex2jar.jar -d framework-classes
Add these classes to plataform's default android.jar
Code:
$ cd ~/Android
$ unzip sdk/platforms/android-17/android.jar -d custom-android
$ cp -r _framework/framework-classes/* custom-android/
$ rm -r _framework
$ cd custom-android
$ zip -r ../custom-android.jar *
$ cd ..
$ rm -r custom-android
Create new extended platform
Code:
$ cd ~/Android
$ cp -r sdk/platforms/android-17 platforms-internals/android-17-internals
$ mv custom-android.jar platforms-internals/android-17-internals/android.jar
$ vi platforms-internals/android-17-internals/build.prop
ro.build.version.release=4.2.2
ro.build.version.release=4.2.2.internal
$ ln -s ~/Android/platforms-internals/android-17-internals ~/Android/sdk/platforms/android-17-internals
I use a symlink for keep it a little organized
Hack ADT
Code:
$ cd ~/Android
$ unzip eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar -d hacked_adt
Go to right folder
Code:
$ cd hacked_adt/com/android/ide/eclipse/adt/internal/project/
Find file where is our desired string
Code:
$ strings -f -a -t x * | grep "android\/internal"
Edit with an hex editor
Code:
$ bless AndroidClasspathContainerInitializer.class &
Here we change the l for the x.
Replace original file making a backup
Code:
$ cd ~/Android
$ cp eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar.original
$ cd hacked_adt/
$ zip -r ../eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar *
$ cd ..
$ rm -r hacked_adt
This worked for me... thanks E:V:A
I got just one error related to a dropbox class, but i think this is not important... hope that
lenieto3 said:
...Start an avd running the desired API to modify in this case API-17... This worked for me... I got just one error related to a dropbox class, but i think this is not important...
Click to expand...
Click to collapse
Thanks and sorry for late reply. I'm very happy to hear these instructions still works with API-17! Could you also upload your hacked JAR somewhere so that people can save some time when experimenting?
I was just here to check-in and try to bump this thread to see if it is still useful to anyone.
E:V:A said:
Thanks and sorry for late reply. I'm very happy to hear these instructions still works with API-17! Could you also upload your hacked JAR somewhere so that people can save some time when experimenting?
I was just here to check-in and try to bump this thread to see if it is still useful to anyone.
Click to expand...
Click to collapse
ive got access to ActivityManager's hidden methods.
I want to use the removeTask method, but it keeps saying that I dont have the REMOVE_TASKS permissions even though I added it to the manifest (and turned off lint).
Permission Denial: removeTask() from pid=9963, uid=10179 requires android.permission.REMOVE_TASKS
Does someone know if there are any automated tools to do/performs steps 1-9?
I'd like to see a tool to automatically pull (from phone), extract and create a compatible android.jar.
@Mohammad_Adib: Sorry, this is the wrong thread for those type of questions.
see this link stackoverflow.com|questions|30656933|android-system-framework-jar-files

[Scripts support init.d] Developer something that should take into account

Introduction:
Developers and users in general hope that this information will be of help and understand how important this issue is
to develop the script according assume smartphone
Warning:
In most of the roms we can find a folder called init.d where you will find a variety of script that are optimizations for
the system itself there is only a detail as you may have noticed the scripts that are used are the same as those used in xperia x8,
type, among other variety as well as file modifications Encourage build.prop only device in this topic we will learn to notice how
the scripts are built and running as smoothly as possible.
Because I say that the scripts are the same? Simple is good since I have seen your code and have compared with the terminals I have
the code and some things should not be there just do not serve the terminal to Encourage and lagee much scripts are not universal
trust me
Some things like values?? optimzacion kernel (sysctl) such xperia L example of code are these:
-kernel= kernel.auto_msgmni = 0
-vm= vm.block_dump = 0
-fs= fs.aio-max-nr = 65536
-net= net.core.dev_weight = 64
note: and so on the more codes each terminal should be just the scripts for each terminal
some files should not come optimizations init.d support
host file this file is normally used to block advertising terminal does support that file in init.d wonder?
comes as other files named sqlite3 that is used to access the root does support init.d ???? now the other file called zipalign that is
normally used to sign apk applications to sign in support init.d ???
the init.d support should be different for each terminal is different because each firmware should not be general
important: it is my view and my knower that is also something constructive
1.-Basic commands and file handling
command: #!/bin/bash
information: +-/home: User home directories
command cd
command to change directory.
command ls
list contents of directories
command cp
copy files/directories
command rm
delete files/directories
command mkdir
create directories
command sysctl
Configure kernel parameters at runtime
2.-All configurations such scripts
/system/etc
3.- startup file
path of the file to be started: /system/etc/init.d/init
example code:
Seed file support init.d: /system/etc/udev/rules.d/init.rules
KERNEL=="mount*", ACTION=="change", SUBSYSTEM=="platform", RUN+="/system/etc/init.d/init"
5.- permissions script
chmod 755
6.- Example of script
#!/bin/bash
# This is a comment
echo "Hello world"
7.- Conditional if o if-else
example
code if:
if value
then
values
fi
code if-else:
if value
then
values
else
8.- Code if o if-else
example:
-a file: True if the file exists
-e file: The same
-b file: True if file exists and has a special block
-c file: True if file exists and is of type character
-d file: True if file exists and is a directory
-f file: True if file exists and is a common file
-g file: True if file exists and its set bit in September GroupID
-h file: True if file exists and is a symbolic link
-k file: True if file exists and its sticky bit set
-p file: True if file exists and is a named pipe
-r file: True if file exists and is readable
-s file: True if file exists and is greater than 0
-u file: True if file exists and has the setuid bit set
-w file: True if file exists and is writable
-x file: True if file exists and has execute permissions
-O file: True if file exists and is nuetro EUID user
-G file: True if file exists and is in our group EGID
-L file: True if file exists and is a symbolic link
-S file: True if file exists and is a socket
-N file: True if file exists and has changed since the last reading
9.- Checks
example:
if [ ! -e $1 ]
then
echo “No”
fi
10.- the main thing you have to develop the script support init.d
Note: In the coming days I will do a better, more detailed documentation :fingers-crossed:
reserved

Execute Dex File From Shell

(Not sure if it goes here or in another forum)
I've figured out a way to execute a dex file without manually running the dalvikvm command. It works similarly to self-extracting tar archives, by taking the dex file, and adding a little script at the top to run dalvikvm on it. This could probably allow for architecture-independent executables that have more abilities than shell scripts, they would just need to be written in Java and then dexed. Just add this to the start of a dex file (obviously put in the actual main class):
Code:
#!/system/bin/sh
tmpdex="$0".tmp;
sed -e '1,/^exit;$/d' "$0" > "$tmpdex";
dalvikvm -cp "$tmpdex" name.of.main.Class "[email protected]";
rm "$tmpdex";
exit;
(Make sure there are no characters after "exit;" [besides a single new line character], or it will break the dex!)
As an example, I have dexed and added this to topjohnwu's zipsigner utility and attached it.

Categories

Resources