29 Jul 2017

Generate android app apk on shell or command prompt

Generation of Android app apk at command prompt of Windows will be as follows:

1. Install Java in form of jdk and jre in folders c:\jdk and c:\jre. Only install version 6 standard edition as it is small size compatible with Android.
Next install Android in folder.   c:\android.
Run Android package manager and put Android API level 23 platform tools for version 6 of Android lollipop in directory
c:\android\platform\android-23
.This will have a file Android.jar containing all class files as a library jar for making a Dex file run and execute as app on Android lollipop mobile.
Set path environment variable in  advance system settings of Windows. Include c:/jdk;c:/jre;c:/Android/tools etc in path variable.

2. Lets use shortcut mypro for my project. Mypkg means my package. Make directory folder  c:\mypro\com\mypkg.
Note that com.pkg is a package where dot or period separates com and pkg sub directories.
In case your package doesn't have this dot or period, then during compilation no error but when you install in mobile, it will say invalid apk.
Put myapp.java containing your code in this directory.
Put needed image icon files in  c:\mypro\res\drawable.
Put AndroidManifest.XML file in c:\mypro.

3. Now we generate R class for res directory. R.drawable class for subdir drawable and R.attr class for attributes to gather information from AndroidManifest.XML file and other layout XML style XML string xml files.
For this we use aapt command.

aapt p -S res -J ./com/pkg -I android.jar
Where
    aapt=Android app pkging tool
    S= Sources in res directory
    J= Java generated files
    I = Include dir or jar file
          For target Android mobile
          Say lollipop.
This  generates R.Java, having R.attr.class,etc classes in it.

4.  Now goto dir c:\mypro\com\mypkg. Give command
             javac    *.java
to get myapp.class
,R.class, R.attr.class, etc in same directory.

5. Now we make a Dex file from these classes.

dx -dex --output=classes.dex .

Notice the dot above represents current directory.
So all binaries including .class and .jar and .apk all will be compiled to Dex file.
Therefore remove all other jar and apk files. delete them prior executing above command. Also don't keep Android.jar here.

6.Now make apk out of Dex and resources.
First delete all class files as we have incorporated already in dex. Also delete any previous jar or apk. Goto directory
c:/mypro.
aapt p -I android.jar -S res -F myapp.apk  classes.dex

Where F=file to be created

No comments:

Post a Comment