Tuesday, 24 September 2013

Android apk generation through command line (HTML to APK)

Android APK generation using command line
In order to generate, we need to follow the following steps
1.      1.  Folder
Create these folders inside your parent folder named AndroidTest. Folders you need to create inside manually are

·         Assets
This folder contains all html folder.

·         Res
o   Folders you need to create manually inside this folder
o   Inside res/drawable copy app icon image, rename the image as mylogo.png
o   Inside res/values create a xml file named as string.xml
Paste the following code

  

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="myApplicationName">My App Name</string>
</resources>


Where myApplicationName sets the name of your app.
·         Src
o   This folder contains the package including java source
Ex: com.tt.rnd (i.e. com\tt\rnd)
 

o   Create a file named MyActivity.java inside your package
Ex : com\tt\rnd\MyActivity.java Paste the following code inside MyActivity.java 
package com.tt.rnd;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       WebView   webview = new WebView(this);
        webview.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/index.html");
        setContentView(webview);
    }
}

 2.       AndroidManifest.xml You need create this file inside your parent folder. Copy the below code 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mycompany.package1"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-sdk android:minSdkVersion="8"  android:targetSdkVersion="15"/>
    <application android:icon="@drawable/mylogo"
                 android:label="@string/myApplicationName">
        <activity android:name="com.tt.rnd.MyActivity"
                  android:label="@string/myApplicationName">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>




3. Download Java
The Java Standard Edition (Java SE) is available from Oracle at http://www.oracle.com/technetwork/java/javase/downloads.
Default installation location will vary between Java versions and OS platforms, so create a pointer to its location:
JAVA_HOME = C:/Program Files/Java/jdk1.6.0_26

5. Download Android
The Android Software Development Kit (Android SDK) is available from Google at http://developer.android.com/sdk.
Default installation location will vary between Android versions and OS platforms, so create a pointer to its location:
ANDROID_HOME = C:/Program Files/Android/android-sdk

6. Choose project
Locate the path to your project named- AndroidTest
DEV_HOME = C:/Users/johnd/dev/AndroidTest

7. Create keystore
A keystore is a database of private keys and their associated X.509 certificate chains authenticating the corresponding public keys. An Android program must be signed in order to execute on a device, and the program is signed by using a key from a keystore.
Use the keytool program to create a keystore:
JAVA_HOME/bin/keytool
                -genkeypair
                -validity 10000
                -dname "CN=company name,
                        OU=organisational unit,
                        O=organisation,
                        L=location,
                        S=state,
                        C=country code"
                -keystore DEV_HOME/AndroidTest.keystore
                -storepass password
                -keypass password
                -alias AndroidTestKey
                -keyalg RSA
                -v

Replace the distinguished name (dname) information with your own. Make sure to specify passwords as indicated. The key is accessed through the specified alias which can be any string.
The output of the command will be the DEV_HOME/AndroidTest.keystore file which contains one key identified by the supplied alias.
8. Create R.java
In order for the application source code to be able to access the resources within the res/ directory, a class called R.java (for Resources) is created.
Use the Android Asset Packaging Tool (aapt) to create the R.java file:
ANDROID_HOME/platform-tools/aapt
                        package
                        -v
                        -f
                        -m
                        -S DEV_HOME/res
                        -A DEV_HOME/assets
                        -J DEV_HOME/src
                        -M DEV_HOME/AndroidManifest.xml
                        -I ANDROID_HOME/platforms/android-15/android.jar
The destination location of R.java within the src/ tree is determined by the package attribute of the manifest file.


9. Compile code
Use the javac tool to compile java source code in a package:
JAVA_HOME/bin/javac
                -verbose
                -d DEV_HOME/obj
                -classpath ANDROID_HOME/platforms/android-15/android.jar;DEV_HOME/obj
                -sourcepath DEV_HOME/src
                DEV_HOME/src/com/tt/rnd/0*.java
The command must be applied for each existing package. 3rd-party .jar files from the lib/ directory must be listed in the -classpath entry. Note that on UNIX-like operating systems the classpath entry delimiter should be a colon (":").
The output of the command is .class files in the obj/ tree.
Non-java files within the src/ tree must be copied to the associated location in the obj/ tree.

10. Create DEX file
DEX ("Dalvik Executable") is the specific bytecode format understood by the Dalvik virtual machine (VM) present in all Android devices.
Use the dx tool to bundle the content of the obj/ directory as well as 3rd-party .jar files from the lib/ directory into a single .dex file:
ANDROID_HOME/platform-tools/dx
                --dex
                --verbose
                --output=DEV_HOME/bin/classes.dex
                DEV_HOME/obj
                DEV_HOME/lib
This will create the classes.dex file in the bin/ directory. The content of a .dex file can be inspected using the ANDROID_HOME/platform-tools/dexdump tool.
11. Create APK file
The Android package format (APK) is the .jar equivalent for Android. The package contains the manifest file, the resources and the classes.dex file.
Use the aapt tool to create the .apk file:
ANDROID_HOME/platform-tools/aapt
                         package
                         -v
                         -f
                         -A DEV_HOME/assets
                         -M DEV_HOME/AndroidManifest.xml
                         -S DEV_HOME/res
                         -I ANDROID_HOME/platforms/android-15/android.jar
                         -F DEV_HOME/bin/AndroidTest.unsigned.apk
                         DEV_HOME/bin
This will create the AndroidTest.unsigned.apk file in the bin/ directory. Note that APK is an ordinary archive format that can be inspected by tools like WinZip or unzip -l.
12. Sign APK file
In order to execute on an Android device, the Android package needs to be signed.
Use the jarsigner tool and the key from the keystore created above to create a signed version of the package:
JAVA_HOME/bin/jarsigner
                -verbose
                -keystore DEV_HOME/AndroidTest.keystore
                -storepass password
                -keypass password
                -signedjar DEV_HOME/bin/AndroidTest.signed.apk
                DEV_HOME/bin/AndroidTest.unsigned.apk
                AndroidTestKey
The signing process adds the META-INF/ directory to the APK archive including the signature (.SF) file and the associated PKSC file (.RSA).
The signed APK is stored as AndroidTest.signed.apk file in the bin/ directory.
13. Zip-align APK file
zipalign is an archive alignment tool that provides important optimization to Android packages. This step is optional, but recommended:
ANDROID_HOME/tools/zipalign
                -v
                -f
                4
                DEV_HOME/bin/AndroidTest.signed.apk
                DEV_HOME/bin/AndroidTest.apk

This will create the AndroidTest.apk which is the final product delivered in one self-contained unit.
Guys give your views about the article. 

20 comments:

  1. Why did you write:
    webview.load--"file:///android_asset/index.html");

    What does this mean: "webview.load--"
    and did you forget there also an "(" ?

    ReplyDelete
  2. Thomas,
    Thats copy past error, I will change it, thanks for notifying

    ReplyDelete
  3. Thanks,
    It's Really help me but I have problem how to sign a APK file by generated key because when i open APK file it's show a message for signing

    ReplyDelete
  4. You are gooooood!
    Pls try and come up with something very simple to eliminate these long protocols/procedures. I believe you can do it.
    Many thanks.

    ReplyDelete
    Replies
    1. HI Fairness,
      I will update blog on that soon. Thanks

      Delete
  5. It seems great but I have little problem now (they should be more) at step 7, can you make me example for dname? thanks much

    ..btw sorry for my english :)

    ReplyDelete
    Replies
    1. HI Chiff re,

      Sure I will upload a video, which makes you easier to refer.

      Delete
  6. at step 9. you have an error

    JAVA_HOME/bin/javac
    -verbose
    -d DEV_HOME/obj
    -classpath ANDROID_HOME/platforms/android-15/android.jar;DEV_HOME/obj
    -sourcepath DEV_HOME/src
    DEV_HOME/src/com/tt/rnd/0*.java

    i just wrote
    JAVA_HOME/bin/javac
    -verbose
    -d DEV_HOME/obj
    -classpath ANDROID_HOME/platforms/android-15/android.jar
    -sourcepath DEV_HOME/src/com/tt/rnd/*.java

    ..and it works :) i deleted at classpath ";DEV_HOME/obj" after at sourcepath "DEV_HOME/src" and "/rnd/*.java" <--deleted 0 ("/rnd/0*.java")

    ReplyDelete
  7. done :) but it's force closes..any suggestions for fix it please? it froce closes immediately after start..please help

    ReplyDelete
    Replies
    1. Thats cool, Good to know.
      Can you please check the logcat & let me know?

      Delete
  8. Hi, I am a newbie...
    Could not understand/proceed from Point No.3 onwards...
    Also please where I can get the video?

    ReplyDelete
    Replies
    1. HI Ritesh,
      There is no video as of now, please gimme sometime, I will promise that I will make a video and attach to it.

      Delete
    2. Thanks. Will be waiting for it.

      Delete
  9. Hi Ritesh,

    Thanks for your post. When i am trying to generate R.java it is giving following error.

    **Crunching PNG Files in source dir: E:\AppBuilder\res
    To destination dir: (null)**

    Please help me how to solve this.

    ReplyDelete
    Replies
    1. here is the FULL tutorial on this...

      http://geosoft.no/development/android.html

      Delete
  10. will this code perform the same functions as www.apkbuilder.com

    ReplyDelete
  11. Great Article it its really informative and innovative keep here: android app development sydney with new updates. Its was really valuable. Thanks a lot.
    android app development perth

    ReplyDelete