Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

31 Jul 2017

Make android java code

Let's make Main.Java

This Main class file of  android Java must be an extension of Activity class. It's instantiated by Android system when app is run by user.

public class Main extends Activity
{
 protected void onCreate ()
  {
  }
}
Above is a skeleton which says
Let's have a publicly available code resembling inbuilt code of Activity and let's only change a portion of it called onCreate.
    This function is called when an object of this code is instantiated in RAM memory of Android mobile. What is created is an empty screen shown to user with a top title as Activity label defined in manifest.

We can place many more text and buttons etc in this empty screen by putting code in this custom function.

But what code. So see below.
TextView tv = new TextView (this)

Above statement allocates a textview object in RAM memory.

tv.setText("Hi dude");
This puts Hi dude in its data variable for containing text.

setContentView(tv);
This above statement let's the empty screen window object point to the newly allocated tv object so that contents of tv view object are displayed on mobile screen.

30 Jul 2017

Make AndroidManifest.xml

We need one androidManifest.xml file to describe icon and name of app to be displayed in mobile to user. Also this file will tell which .class file to execute first out of all classes stored in Dex file. We can call it as main class file. Let's make it.

AndroidManifest. xml
manifest xmlns:android = "URI" version=1.0 encoding=utf-8

Above statement tells that all elements inside tags in this XML if prefixed by word 'android' then replace Android by URI. URI can be HTTP:/schema/apk... Etc.

application android:icon="@drawable/myicon" android:label="@string/app name".

Above statement tells what icon to use and what label for this app to be displayed to users. Android URI will be prefixed to icon and label elements. When aapt tool makes apk it will only consider those elements which have this prefix.

activity android:name="Main" android:label="My App"

Above statement uses activity element and says that Main.class code must be executed first and title label should be My App.

intent-filter
action android:name="android.intent.action.MAIN"
category android:name = "android. intent. category.LAUNCHER"
intent-filter

Here optional Activity related commands can be placed.
While Activity refers to screen of mobile placed before user.

So things like making it full screen without title bar or allowing it's auto resizing or panning thereby denying scrolling on pop-up of soft keyboard can be done here.

'activity windowSoftInputMode ="adjustSize|adjustPan" /
'activity Orientation="Portrait"/>
Window feature flag full screen.

activity
application
manifest

In above statement two elements <action>,<category> are defined within <intent-filter> element. User actions of clicking touching mobile screen suggest there intentions. When there intention is fired from launching an app by app launcher service in mobile then the category of intention is stored as LAUNCHER.
Now is the user deleting uninstalling or running this app. If running then action is MAIN.

This manifest will be used two time.
First time when generating R.java and R.class etc.
Second time to generate apk by aapt tool.

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

Concept of APK

If you make an Android app called myapp. Then full name will be myapp.apk.
apk means application package.

apk package contains your code as an executable file along with images,sounds,etc. used by your app.

Executable file in Windows is .exe file like myapp.exe.
In Android mobile, executable file is .dex where dex stands for dalvik executable.

Dalvik is a Java virtual machine (JVM) made for Android mobile. JVM can understand .class file which is made by Java compiler javac.

javac can compile a .Java file written in Java language to .class file.

There is a software manager or app manager and launcher program continuously running in background as a service on Android operating system installed over a smartphone mobile.

This program on receiving an apk file of android app will search .dex file for executing its code using other files kept in its packaged apk file which is actually a compressed zip file. The app runs on being launched by a user click on its icon being displayed on mobile screen. This user click or touch signifies a user intent to launch this app from main thread of user interface app manager program of android OS in mobile.