Thursday, May 26, 2011

how to set error on edittext in android?

        final EditText mNum=(EditText)findViewById(R.id.edittxt);
        final EditText mLines=(EditText)findViewById(R.id.edittxt3);
        final EditText mTitle=(EditText)findViewById(R.id.edittxt4);
        final EditText mPage=(EditText)findViewById(R.id.edittxt5);
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        final String date= Integer.toString(mDay) +"/"+ Integer.toString(mMonth+1) +"/"+        Integer.toString(mYear);
        Button read=(Button)findViewById(R.id.btn1);
        read.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
               
                 final String words=mNum.getText().toString().trim();
                 final String lines=mLines.getText().toString().trim();
                 final String page=mPage.getText().toString().trim();
                 final String title= mTitle.getText().toString();
               
                if((mNum.length() == 0) || (words.equals("0")))
                {
                    mNum.setError("Error");
                }   
                else if((mLines.length()==0) || (lines.equals("0")))
                {
                    mLines.setError("Error");
                }
                else if(mTitle.length()==0)
                {
                    mTitle.setError("Error");
                }
                else if((mPage.length()==0) || (page.equals("0")))
                {
                    mPage.setError("Error");
                       }

Monday, May 16, 2011

How to build Android application package (.apk) from the command line using the SDK tools + continuously integrated using CruiseControl.

Hello all android developers, I just want to share my experience building android apk manually using sdk tools without using Eclipse.  My original goal is motivated firstly by the desire to incorporate continuous integration aspect to Android development process and secondly to ditch the ADT eclipse plugin since the plugin auto-build process blocks Eclipse UI if you have large resources, assets in your Android project,  and a slow computer like mine.  I am using CruiseControl as my continuous integration tool.

Below is one of the many apk build processes:








You can find ant scripts in: <SDK_HOME>\platforms\android-1.5\templates\android-rules.xml
Step 1: Generate Resource java code and packaged Resources
aapt  package -f -M ${manifest.file} -F ${packaged.resource.file} -I ${path.to.android-jar.library} -S ${android-resource-directory} [-m -J ${folder.to.output.the.R.java}]
Step 2: Compile java source codes + R.java
use javac

Step 3: Convert classes to Dalvik bytecodes
use dx.bat
dx.bat  –dex  –output=${output.dex.file}  ${compiled.classes.directory}  ${jar files..}
Step 4: Create unsigned APK
use apkbuilder

apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}

or

apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}  -rf  ${source.dir}  -rj  ${libraries.dir}
-rf = resources required for compiled source files?
-rj = resources required for jar files
Step 6: Generate a key
use keytool

Step 7: Sign APK
use jarsigner

jarsigner  -keystore ${keystore} -storepass  ${keystore.password} -keypass ${keypass} -signedjar ${signed.apkfile} ${unsigned.apkfile} ${keyalias}
Step 8: Publish
use adb
adb -d install -r ${signed.apk}
Inspecting your APK file:
aapt list -v latest.apk
Open questions:
1. Can you include more than one dex file in the apk?
2. Can you have dex file named other than classes.dex in the apk?
3. Does an apk have to have a packaged resource?