Tag Archives: java

My Experience with Xamarin Android

Hi folks, I have been working on Xamarin Android for the last 6 months. I normally don’t go for a third party development tools to make Android or iOS apps(I am not talking about Xamarin Mono but Xamarin Android). But the project requirement was to develop in Xamarin. The reason client said was that we can have the service layer for both Android and iOS as same. Ok that’s cool most of the service calls are handled by the service layer which is developed by our back end developer.

Some people had this mind set when it comes to Xamarin, where they compare it with other hybrid platforms like Phonegap. But the reality is Xamarin is a framework designed specially for the people who knows C#, so that they can write the Android code in C# and not have to worry about Java.  And the out put is not a web based solution, it is a native app that Xamarin out puts so it is faster like all native apps. Many C# developer I know have tried to develop apps using Xamarin and ended up complaining that, ‘Hey man its the same android code that we have to write but in C# so whats the point in it’ . So here what I am trying to say is, even though it looks or give a comfortable feel, for the C# people when they start developing they will come to understand that they need at least some basic knowledge on Android.

I tried to give an overview of what people think and what I think of Xamarin. Lets see how it feels when you try to develop and publish and app. So for C# developer its good if they knew some basic Android. Else you can still follow the Xamarin tutorials and figure out things. For Android developers if they have some basic knowledge on C# language it would be enough.

When you create an android project. It creates the file structure exactly like in Eclipse or Android Studio giving you a good first impression. Then you can add Activities and Layouts as usual. Here the layout xmls come with the extension axml. The xml we create as resources are normal xmls. When you start developing you will start to see the differences.
For examples the edit text component has a method called setText

editText.setText("String to Show")

but in Xamarin android, its not a method but a property in that class, so you simply set the value like

editText.Text =  "String to Show";

These kind of changes, you will see a lot. And when it comes to libraries. You have to Go for the Xamarin Components. Where there is only a handful of components to choose from. Most of the very popular ones are being added by the Xamarin people and popular companies. But still, at some times you have to create a component project by looking into a java library project available for normal android. But you have lots of other NuGets to use  for other simple things you find hard to handle in Android.

And one of the important think on updates are, that Google go on updating it’s v4, v7  libraries and Xamarin is slow on updating their system to support it. So When you are using libraries or components, you have to have a good knowledge on the component and their dependencies to manage all of the features you want from them. So in some cases you have to use old versions of components.

I have no problems in building and running a debug apk, but when I had to release a release build I had to face a lot of problems. One of them is the problem with the dependency versions which I have explained above. On one occasion I have to remove all the Google Play Services components and have to find an alternative way to handle Google play services which I manages to do with some http get/post requests.

So what I will always recommend is, if you are an Android developer unless it is a must don’t go for the Xamarin. Else if you are a C# developer, you will get used to the Xamarin and hopefully Microsoft will acquire it and will make it more wonderful in future.

Advertisements

Android Proguard a General Idea

What is Proguard on android and what does it do?

ProGuard is a tool that comes with the Android SDK. It  shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer. Because ProGuard makes your application harder to reverse engineer, it is important that you use it when your application utilizes features that are sensitive to security.

How to enable it? And is it recommended?

ProGuard is integrated into the Android build system. And to enable it you just have to find the “project.properties” file in your android project and have to uncomment the line which sets the path for the ProGuard. Also Proguard slows the app building sequence.
So you do not have to invoke it manually. ProGuard runs only when you build your application in release mode, so you do not have to wait longer time when debugging the app. Having ProGuard run is completely optional, but highly recommended.

So is that all that we have to do to enable and use ProGuard?

Yes that’s the basic, and it only does the Shrinking, but there are 2 more functions that give more advantages in increasing the security and management of the build.
They are Obfuscation and Repackaging.

So what is Obfuscation and how does it Work

With tools available to extract the contents of APK’s, deodex, and read the class files, it’s important to obfuscate to protect the proprietary aspects of your codebase.

ProGuard generates a mapping file that allows you to map the stack traces of obfuscated code to actual methods. Basically it replaces the methods in meaning full name to something unguessable like a work or letter.

Original Code

public Data(int id, int result, String message) {
       mId = id;
       mResult = result;
       mMessage = message;
    }

Obfuscated Code

public a(int paramInt1, int paramInt2, String paramString)
    {
       this.a = paramInt1;
       this.b = paramInt2;
       this.c = paramString;
   }

 Hope you got an idea on how it works

So what is Repackaging?

Repackaging allows ProGuard to take externals jars and class files and move them to a single container with a common java package location.

For those of you building libraries, repackaging is extremely helpful if you choose to show a simple interface to third party developers while keeping a maintainable and well structured project hierarchy in the source repository. This can also be useful in organizing lower level packages while exposing well defined interfaces!

 Optimizing

There is another function of Proguard which Optimizes the code, where you can specify which part of the code, which type of code needs to be Optimized.  However, there are known incompatibility issues with various Dalvik versions.
But It is good to know this feature, which can be used without issues using some other tools.

Reference : http://www.crashlytics.com/blog/mastering-proguard-for-building-lightweight-android-code/, http://developer.android.com/tools/help/proguard.html

Setting Up JDK for JAVA Development in your Windows PC

Android is based on Java so
First you need JDK(Java Development KIT) installed in your machine and it needs to be configured in the Environment Variables.
To check if JDK is already installed in your machine. Open the command prompt and type “javac”
javac

Then press enter. If you see a screen like this

javacresults

That means you have JDK installed and configured.
else go to
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

And download the JDK suitable for your pc version(The latest version changes with years this Screen shot is taken on 2014 September)
JDK Download

After you have downloaded it and installed it copy the path of the bin folder in side the installation folder. Installation folder path will be normally
C:\Program Files\Java\jdk1.8.0_05\bin
Depending on the version of Java u have installed the folder name and depending on the Windows version the program folder name also changes.

Now Go to Control Panel-> System and click on Advanced System Settings
Advanced system Settings

Step 1
You will see a new Window called “System Properties” Pop up

Step 2
Then in that Windows you will see a Button Saying “Environment Variables”, click on it

Step 3
It opens a new window called “Environment Variables”

Note: Before Going to Step 4, check if the “User Variables for ‘Your PC user name’ “, already got “path” in the list, if it was not there follow Step 4 else skip to Step 5.2

Step 4
In that window under the “User Variables for ‘Your PC user name‘ “Click the New Button

Step 5
Now you will see a new window “New User Variable”, there on the ‘Variable name‘ section type “path”, and on the ‘Variable value‘ section paste the java sdk path you have copied earlier

Step 5.2
If there is already a path variable, select that section and click on the “Edit”, and without doing anything  in the ‘Variable name‘ section, go to the ‘Variable value‘ and add a “;” next to the existing value and paste the java sdk path you have copied earlier.

Step 6
After adding the value, click ‘OK’ , then again ‘OK’ on the “Environment Variables” window and finally click  on the ‘Apply” button on the “System Properties” window.

Settings

Now you will be able to get the result I have got earlier when typing “Javac” on the command prompt.

 

 

 

What comes with the new Google GLASS Development Kit?

The GDK is an Android SDK add-on that contains APIs for Glass-specific features.
sdk-gdk

Unlike the Mirror API, Glassware built with the GDK runs on Glass itself, allowing access to low-level hardware features.

At the time of writing this article Sample GDK has been released out introducing ways to develop native android apps for Google Glass.

gdk-glassware-android

So what does the new GDK brings

1. A new platform for you to develop your GLASS apps so it will have special libraries needed to for the Google GLASS. Not all are available yet, you have to wait for the final version to come.

2.Touch Gestures – Accessing raw data from the Glass touchpad is possible with the Android SDK. However, the GDK provides a gesture detector designed for the Glass touchpad that automatically detects common gestures on Glass, including tapping, swiping, and scrolling. Click Here for detailed info on developing

3.Voice Input – Voice is an integral part in a hands-free experience for users. Glass lets you declare voice triggers to launch your Glassware from the ok glass voice menu. Click Here for detailed info on developing

4.Location and Sensors – You access location and sensor data using the standard Android platform APIs. You have to access the paired device for location and there is another way of gettin location without the help of paired device. It is taken based on the Wifi hotspot, but it wont be accurate as much as the location taken fron the paired device’s gps. Click Here for detailed info on developing

5.Camera – You can use the Glass camera to capture images and video and to also display the camera’s preview stream for a variety of different use cases. Click Here for detailed info on developing

Reference : Site Name – Glass Development KIT, Url – https://developers.google.com/glass/develop/gdk/index, Date 5th December 2013, Time – 12.13pm (GMT +5.30)