Page 1

An Ultimate Guide to Android App Development with Java If you are new to Android App Development, this is where you should start. Here is a comprehensive guide that gives you a bird-eye view to develop custom Android app development with Java. The Android app framework works around the Java programming language and specifically starting with Java 7, Android uses a customized version of open JDK, an open source implementation of Java from Oracle. Prior to Android 7, Android used a customized version of Java that was descended from another tool kit but the way you program with Java and Android has not changed significantly. In Android, all Java 5 and 6 syntax is supported and most Java 7 syntak works as well. If you choose, you can configure a project to use what's known as the Jack compiler, which is part of a tool chain named Jack and Jill. If you turn on the Jack compiler, you'll then be able to use a lot of Java 8 syntax including Lambda expressions and method references but that doesn't mean everything in Java 7 and Java 8 is available to you. For example, not all Java APIs are implemented in the Android SDK. To find out whether a particular API is available, look at the Android documentation. Here's a listing of some Java syntax that works and doesn't work. The diamond operator allows you to reduce code with generic typing. So that for example, with a collection, you need to declare the type of the items that are being managed by the collection in the declaration but you don't need to re-declare it in the assignment. Another Java 7 improvement that does work with Android is the switch statement with string based values. You can also use multicatch clauses and underscores in numeric literals but there are some things in Java syntax that don't work across the board. The try-with-resources syntax, which allows you to automatically close objects that implement an interface named Closeable can only be used in an app if the minimum SDK is set to API 19 or greater. If you want to target older versions of Android, you just can't use that syntax and there's annotation named @SafeVarargs in Java 7 that's not a part of Android because the required dependent class just isn't implemented. Many of Java's standard APIs are implemented in the Android SDK. For example, everything in java.lang is there and most everything in java.io and nio, java.math, java.net and so on, are all a part of the Android SDK. But again, this doesn't mean everything is


available. You just need to look at the docs to find out whether something is available in Android. The Android SDK also includes many custom APIs. These are features that are in custom packages that typically start with Android and end with a description of what that package does. So for example, android.app, android.database, android.net and so on, include classes and interfaces that aren't available in general Java but are available for Android. To learn what's available, look at the API reference at this URL. When you build an app in Android studio or from the command line, there are two possible build process that you can use. The default build process, as of October 2016, is called the Legacy process, and here's how it works. You start off with a set of java files, either source code files or files that have been pre-compiled to bytecode code in .class files. These are processed through the standard javac compiler which turns these files into class files, Java bytecode. From there, optionally, you can transform those classes using a tool called ProGuard. The ProGuard tool minimizes and obfuscates the bytecodeso that it's harder to decompile and it's smaller. After ProGuard does it's thing, you still have .class files but they'll be smaller than the original files. Then these files are taken through a tool called DEX which turns the files into DEX based bytecode. DEX bytecode is universal bytecode for Android and this is what's actually packaged in the application when it's deployed to devices through the app store. Now if you configure your project to use the Jack compiler to allow you to use for example, Java 8 based syntax, the process of building the app is a little bit different. You once again start off with your Java files, either source code files or pre-compiled class files. These are processed through the Jack compiler which goes directly to the DEX bytecode. The Jack compiler does all of the tasks that javac and ProGuard were used for in the Legacy process. It does the compilation and optionally can minimize and obfuscate the code. When you use the Jack compiler, you'll see that your compilation process is a little bit faster and you'll be able to use a lot more Java syntax than before. There's one downside to using the Jack compiler that I know of at this point and that's that in Android Studio there's a great tool called Instant Run which can deploy parts of an application to a testing device. Projects that are built with the Jack compiler don't support Instant Run at this point. So especially when working with larger applications, they'll take a lot longer to build and deploy during the coding and testing process. If you're willing to put up with that, it's worth going over to the Jack compiler to


give yourself more flexibility in coding style. Once the DEX code is delivered to the device, what happens then depends on which version of Android is being run. The DEX bytecode has to be translated to machine code on the device. Android can run on a variety of CPU's from ARMSs to Intel chips and so on, and so the machine code that's going to be really fast at runtime has to be built by tools that are on that device. What changes from one Android version to another, is when that DEX code is created. On the Dalvik runtime in Android 4.4 and prior, DEX bytecode is built as the application is running each time a feature is used for the first time. This process is called just in time or JIT compilation. Starting with the Android runtime in Android 5, the process moved to ahead of time compilation. And in Android 5 and 6, this process happened when the app was installed or upgraded or when the operating system was upgraded. In Android 7, Nougat, this process has been changed yet again. Now each application will be compiled ahead of time the first time it's started up and that means when you upgrade your device, going to a more recent version of Android, it reduces the amount of time it takes. All of the applications don't all have to be re-compiled, significantly delaying the amount of time before you can actually use the device. So that's how Java is used in custom Android app development. Again which aspect of Java you'll be able to use depends on which version of Android you're using and which version of Android Studio you're using and whether you've configured your project to work with the new Jack and Jill tool chain and the Jack compiler. BTC Mobile App Development Partner for your Enterprise If you want to leverage the power of enterprise mobility to grow your business to next level, BTC can help you to do so. We have clients from Fortune 500 companies to big enterprises. Our services range from Android app development, iOS mobile app development and Enterprise Mobile App Development.

An ultimate guide to android app development with java  
An ultimate guide to android app development with java  
Advertisement