Tag Archives: Android Studio

New Android Code Samples

Posted by Rich Hyndman, Developer Advocate

A new set of Android code samples, covering Android Wear, Android for Work, NFC and Screen capturing, have been committed to our Google Samples repository on GitHub. Here’s a summary of the new code samples:

XYZTouristAttractions

This sample mimics a real world mobile and Android Wear app. It has a more refined design and also provides a practical example of how a mobile app would interact and communicate with its Wear counterpart.

The app itself is modeled after a hypothetical tourist attractions experience that notifies the user when they are in close proximity to notable points of interest. In parallel,the Wear component shows tourist attraction images and summary information, and provides quick actions for nearby tourist attractions in a GridViewPager UI component.

DeviceOwner - A Device Owner is a specialized type of device administrator that can control device security and configuration. This sample uses the DevicePolicyManager to demonstrate how to use device owner features, including configuring global settings (e.g.automatic time and time-zone) and setting the default launcher.

NfcProvisioning - This sample demonstrates how to use NFC to provision a device with a device owner. This sample sets up the peer device with the DeviceOwner sample by default. You can rewrite the configuration to use any other device owner.

NFC BeamLargeFiles - A demonstration of how to transfer large files via Android Beam on Android 4.1 and above. After the initial handshake over NFC, file transfer will take place over a secondary high-speed communication channel such as Bluetooth or WiFi Direct.

ScreenCapture - The MediaProjection API was added in Android Lollipop and allows you to easily capture screen contents and/or record system audio. The ScreenCapture sample demonstrates how to use the API to capture device screen in real time and show it on a SurfaceView.

As an additional bonus, the Santa Tracker Android app, including three games, two watch-faces and other goodies, was also recently open sourced and is now available on GitHub.

As with all the Android samples, you can also easily access these new additions in Android Studio using the built in Import Samples feature and they’re also available through our Samples Browser.

Check out a sample today to help you with your development!

New course: Take Android app performance to the next level

Posted by Jocelyn Becker, Developer Advocate

Building the next great Android app isn't enough. You can have the most amazing social integration, best API coverage, and coolest photo filters, but none of that matters if your app is slow and frustrating to use.

That's why we've launched our new online training course at Udacity, focusing entirely on improving Android performance. This course complements the Android Performance Patterns video series, focused on giving you the resources to help make fast, smooth, and awesome experiences for users.

Created by Android Performance guru Colt McAnlis, this course reviews the main pillars of performance (rendering, compute, and battery). You'll work through tutorials on how to use the tools in Android Studio to find and fix performance problems.

By the end of the course, you'll understand how common performance problems arise from your hardware, OS, and application code. Using profiling tools to gather data, you'll learn to identify and fix performance bottlenecks so users can have that smooth 60 FPS experience that will keep them coming back for more.

Take the course: https://www.udacity.com/course/ud825. Join the conversation and follow along on social at #PERFMATTERS.

Build Mobile App Services with Google Cloud Tools for Android Studio v1.0

Posted by Chris Sells, Product Manager, Cloud Tools for Android Studio

Cloud Tools for Android Studio allows you to simultaneously build the service- and client-side of your mobile app. Earlier this month, we announced the release of Android Studio 1.0 that showed just how much raw functionality there is available for Android app developers. However, the client isn’t the whole picture, as most mobile apps also need one or more web services. It was for this reason that the Cloud Tools for Android Studio were created.

Cloud Tools put the power of Google App Engine in the same IDE alongside of your mobile client, giving you all the same Java language tools for both sides of your app, as well as making it far easier for you to keep them in sync as each of them changes.

Getting Started

To get started with Cloud Tools for Android Studio, add a New Module to your Android Studio project, choose Google Cloud Module and you’ll have three choices:

You can add three Google Cloud module types to your Android Studio project

The Java Servlet Module gives you a plain servlet class for you to implement as you see fit. If you’d like help building your REST endpoints with declarative routing and HTTP verbs and automatic Java object serialization to and from JSON, then you’ll want the Java Endpoints Module. If you want the power of endpoints, along with the ability to send notifications from your server to your clients, then choose Backend with Google Cloud Messaging.

Once you’re done, you’ll have your service code right next to your client code:

You can build your mobile app’s client and service code together in a single project

Not only does this make it very convenient to build and test your entire end-to-end, but we also dropped a little extra something into your app’s build.gradle file:

The android-endpoints configuration build step in your build.gradle file creates a client-side library for your server-side endpoint

The updated Gradle file will now create a library for use in your app’s client code that changes when your service API changes. This library lets you call into your service from your client and provides full code completion as you do:

The client-side endpoint library provides code completion and documentation

Instead of writing the code to create HTTP requests by hand, you can make calls via the library in a typesafe manner and the marshalling from JSON to Java will be handled for you, just like on the server-side (but in reverse, of course).

Endpoints Error Detection

Meanwhile, back on the server-side, as you make changes to your endpoints, we’re watching to make sure that they’re in good working order even before you compile by checking the attributes as you type:

Cloud Tools will detect errors in your endpoint attributes

Here, Cloud Tools have found a duplicate name in the ApiMethod attribute, which is easy to do if you’re creating a new method from an existing method.

Creating an Endpoint from an Objectify Entity

If, as part of your endpoint implementation, you decide to take advantage of the popular Objectify library, you’ll find that Cloud Tools provides special support for you. When you right-click (or control-click on the Mac) on a file containing an Objectify entity class, you’ll get the Generate Cloud Endpoint from Java class option:

The generate Cloud Endpoint from Java class option will create a CRUD endpoint for you

If you’re running this option on a Java class that isn’t built with Objectify, then you’re going to get an endpoint with empty methods for get and insert operations that you can implement as appropriate. However, if you do this with an Objectify entity, you’ll get a fully implemented endpoint:

Cloud Tools has built-in support for generating Objectify-based cloud endpoint implementations

Using your Cloud Endpoint

As an Android developer, you’re used to deploying your client first in the emulator and then into a local device. Likewise, with the service, you’ll want to test first to your local machine and then, when you’re ready, deploy into a Google App Engine project. You can run your service app locally by simply choosing it from the Configurations menu dropdown on the toolbar and pressing the Run button:

The Configurations menu in the toolbar lets you launch your service for testing

This will build and execute your service on http://localhost:8080/ (by default) so that you can test against it with your Android app running in the emulator. Once you’re ready to deploy to Google Cloud Platform, you can do so by selecting the Deploy Module to App Engine option from the Build menu, where you’ll be able to choose the source module you want to deploy, log into your Google account and pick the target project to which you’d like to deploy:

The Deploy to App Engine dialog will use your Google credentials to enumerate your projects for you

Cloud Tools beta required some extra copying and pasting to get the Google login to work, but all of that’s gone now in this release.

What’s Next?

We’re excited to get this release into your hands, so if you’ve haven’t downloaded it yet, then go download Android Studio 1.0 right now! To take advantage of Cloud Tools for Android Studio, you’ll want to sign up for a free Google Cloud Platform trial. Nothing is stopping you from building great Android apps from front to back. If you’ve got suggestions, drop us a line so that we can keep improving. We’re just getting started putting Google Cloud Platform tools in your hands. We can’t wait to see what you’ll build.

Android Studio 1.0

By Jamal Eason, Product Manager, Android

Today we are excited to introduce Android Studio 1.0. Android Studio is the official Integrated Development Environment (IDE) from the Android team. It is built on the popular IntelliJ IDEA (Community Edition) Java IDE.

We first released a preview of Android Studio at I/O last year. We value the on-going feedback from you, thanks! We are making Android Studio 1.0 available for download as a stable release on the Android Developer site.

Download Android Studio

If you are currently developing for Android or thinking about getting started, now is the time to download Android Studio 1.0 (or upgrade if you are using an earlier version). Similar to the Chrome release channels, Android Studio will continue to receive updates on four different release channels: Stable, Beta, Dev, and Canary. Canary builds are at the bleeding edge of development, while the stable release is fully tested. With this range of release channels you can choose how quickly you want to get the latest features for Android Studio.

Android Studio features

With the release of Android Studio, you have access to a new set of features to enable your development workflow. Some of the key features of Android Studio are listed below, but make sure to check out the Android Studio page for a full feature overview.

Startup experience

  • First-run setup wizard — The getting started experience now installs the right Android SDK, sets up your development environment settings, and creates an optimized emulator for testing your app. Plus, we include a set of code templates to help you get started.
  • Sample Importing & templates — Android Studio includes wizards that enable you to start with new project templates or import Google code samples.

Code and resource editing, user interface design

  • Code Editing — Android Studio takes advantage of all the intelligent code editing capabilities of IntelliJ IDEA such as advanced code completion, refactoring, and code analysis.
  • Internationalization string editing — Manage string translations of your app in Android Studio.
  • User interface design — Edit and preview your Android Layouts across multiple screen sizes, languages, and even API versions.

Performance analysis

  • Memory monitor — View the memory usage of your app over time to help find ways to improve the performance of your app.

Unified build system

  • Android Studio uses a Gradle-based build system that provides a lot of flexibility and extensibility, as well as the ability to build from within and outside of the IDE. This unified build system decouples the build from Studio itself, meaning that Studio updates never impact the output of your build.
  • Some of the key features of the build systems are: build variant support to better handle different build types (debug vs. release), or different versions of the same app (paid vs. free), multi-apks handling through splits, multi-dex support, and dependency management for 3rd party libraries.

Instant access to Google Cloud Services

  • Android Studio even enables an easy way to add Google Cloud Backends & Endpoints to your app, as well as Google Cloud Messaging (find out more).

Time to migrate & update

If you are an Eclipse user, check out our migration steps or you can just import your projects right into Android Studio with the import wizard, shown below:

If you were using one of the early versions of Android Studio, you should also upgrade to version 1.0 since we have added a host of new features and have addressed many bugs.

We have also released version 1.0 of the Gradle plugin with a file format that is now stable. The communication between Android Studio and the Gradle plugin is now stable, so updating one will not require updating the other. Check the technical release notes for additional tips for updating your previous Android Studio projects.

Give us your feedback

We are always seeking to bring you the best Android development experience. We already have plans to add features ranging from improved testing and better support for game development, but we want to know how you work and what capabilities you'd like to have for your Android development.

Please take a moment to complete a short survey (less than 5 minutes). Your responses will help shape the next set of features offered in Android Studio.

Questions?

We develop Android Studio and the corresponding tools in open source, so you can also file bugs via the public Android bug tracker and we will do our best to address your issues or questions. If you have specific questions or need help in your migration, feel free to connect with the Android developer tools team on our Google+ community page.

Android L Developer Preview and Android Studio Beta

By Jamal Eason, Product Manager, Android

At the Google I/O keynote yesterday we announced the L Developer Preview — a development version of an upcoming Android release. The Developer Preview lets you explore features and capabilities of the L release and get started developing and testing on the new platform. You can take a look at the developer features and APIs in the API Overview page.

Starting today, the L Developer Preview is available for download from the L Developer Preview site. We're also announcing that Android Studio is now in beta, and making great progress toward a full release.

Let’s take a deeper dive into what’s included in the preview and what it means for you as a developer as you prepare your apps for the next Android release.

What’s in the L Developer Preview

The L Developer Preview includes updated SDK tools, system images for testing on an emulator, and system images for testing on a Nexus 5 or Nexus 7 device.

You can download these components through the Android SDK Manager:

  • L Developer Preview SDK Tools
  • L Developer Preview Emulator System Image - 32-bit (64-bit experimental emulator image coming soon)
  • L Developer Preview Emulator System Image for Android TV (32-bit)

(Note: the full release of Android Wear is a part of Android KitKat, API Level 20. Read more about Android Wear development here.)

Today, we are also providing system image downloads for these Nexus devices to help with your testing as well:

  • Nexus 5 (GSM/LTE) “hammerhead” Device System Image
  • Nexus 7 [2013] - (Wifi) “razor” Device System Image

You can download both of these system images from the L Developer Preview site.

With the SDK Tools, and Nexus device images, you can get a head start on testing out your app on the latest Android platform months before the official launch. You can use the extra lead time to take advantage of all the new app features and APIs in your apps. The Nexus device images can help you with testing, but keep in mind that they are meant for development purposes only and should not be used on a production device.

Notes on APIs and publishing

The L Developer Preview is a development release and does not have a standard API level. The APIs are not final, and you can expect minor API changes over time.

To ensure a great user experience and broad compatibility, you can not publish versions of your app to Google Play that are compiled against L Developer Preview. Apps built for L Developer Preview will have to wait until the full official launch to publish on Google Play.

Android Studio Beta

To help you develop your apps for the upcoming Android version and for new Android device types, we’re also happy to announce Android Studio Beta. Android Studio Beta helps you develop apps by enabling you to:

  • Incorporate the new material design and interaction elements of the L Developer Preview SDK
  • Quickly create and build apps with a new app wizard and layout editor support for Android Wear and Android TV

Building on top of the build variants and flavors features we introduced last year, the Android Studio build system now supports creating multiple apks, such as for devices like Android Wear. You can try out all the new features with the L Developer Preview by downloading the Android Studio Beta today.

How to get started

To get started with the L Developer Preview and prepare your apps for the full release, just follow these steps:

  1. Try out Android Studio Beta
  2. Visit the L Developer Preview site
  3. Explore the new APIs
  4. Enable the material theme and try out material design on your apps
  5. Get the emulator system images through the SDK Manager or download the Nexus device system images.
  6. Test your app on the new Android Runtime (ART) with your device or emulator
  7. Give us feedback

As you use the new developer features and APIs in the L Developer Preview, we encourage you to give us your feedback using the L Developer Preview Issue Tracker. During the developer preview period, we aim to incorporate your feedback into our new APIs and adjust features as best as we can.

You can get all the latest downloads, documentation, and tools information from the L Developer Preview site on developer.android.com. You can also check our Android Developer Preview Google+ page for updates and information.

We hope you try the L Developer Preview as you start building the next generation of amazing Android user experiences.

Get it on Google Play

New ways to connect your app to the Cloud using Android Studio and Google Cloud Platform

Posted by Manfred Zabarauskas, Product Manager on Google Cloud Platform

Many Android developers like Snapchat or Pulse build and host their app backends on the Google Cloud Platform, and enjoy automatic management, with simple expansion to support millions of users.

To quickly add a Google Cloud Platform backend to your Android app, you can now use a number of built-in features in Android Studio 0.6.1+.

Google App Engine backend module templates

Google App Engine enables you to run your backend applications on Google's infrastructure, without ever requiring you to maintain any servers.

To simplify the process of adding an App Engine backend to your app, Android Studio now provides three App Engine backend module templates which you can add to your app. You can find them under "New → Module" menu:

  1. App Engine Java Servlet Module provides a simple App Engine Java backend servlet with minimal boilerplate code,
  2. App Engine Java Endpoints Module template leverages Google Cloud Endpoints for your backend, and includes automated object marshalling/unmarshalling, generation of strongly-typed Java client libraries and so on,
  3. App Engine Backend with Google Cloud Messaging includes both Google Cloud Endpoints and Google Cloud Messaging integration, which enables additional features like push notifications.

When you choose one of these template types, a new Gradle module with your specified module/package name will be added to your project containing your new App Engine backend. All of the required dependencies/permissions will be automatically set up for you.

You can then run it locally (on http://localhost:8080) by selecting the run configuration with your backend's module name, as shown in the image below.

For more information about these backend templates, including their deployment live to App Engine and code examples which show how to connect your Android app to these backends, see their documentation on GitHub. (Also, the code for these templates lives in the same GitHub repository, so do not hesitate to submit a pull request if you have any suggestions!)

Built-in rich editing support for Google Cloud Endpoints

Once you have added the backend module to your Android application, you can use Google Cloud Endpoints to streamline the communication between your backend and your Android app. Cloud Endpoints automatically generate strongly-typed client libraries from simple Java server-side API annotations, automate Java object marshalling to and from JSON, provide built-in OAuth 2.0 support and so on.

As a concrete example, "App Engine Java Endpoints Module" contains a simple annotated Endpoints API at <backend-name>/src/main/java/<package-name>/MyEndpoint.java file (shown below):

import javax.inject.Named;

@Api(name = "myApi",
     version = "v1",
     namespace = @ApiNamespace(ownerDomain = "<package-name>",
                               ownerName = "<package-name>",
                               packagePath=""))
public class MyEndpoint {
    @ApiMethod(name = "sayHi")
    public MyBean sayHi(@Named("name") String name) {
        MyBean response = new MyBean();
        response.setData("Hi, " + name);
        return response;
    }
}

On deployment, this annotated Endpoints API definition class generates a RESTful API. You can explore this generated API (and even make calls to it) by navigating to Endpoints API explorer as shown in the image below:

Google APIs explorer is available at http://localhost:8080/_ah/api/explorer once you deploy your app. Color overlays in this screenshot match the colors in the API snippet above (MyEndpoint.java).

To simplify calling this generated API from your Android app, Android Studio will automatically set up your project to automatically include all compile dependencies and permissions required to consume Cloud Endpoints, and will re-generate strongly-typed client libraries if your backend changes. This means that you can start calling the client libraries from your Android app immediately after defining the server-side Endpoints API:

Code completion for automatically generated, strongly-typed client libraries. Color overlays match the colors in the API snipped above (MyEndpoint.java)

As server-side Endpoints API definitions have to conform to a number of syntactic rules, Android Studio also includes a number of Endpoints-specific inspections and quick-fixes, which help you to avoid mistakes when writing Endpoints APIs.

For example, "@Named" annotation is required for all non-entity type parameters passed to server-side methods. If you forget to add this annotation when modifying sayHi method in MyEndpoint.java file, Android Studio will underline the problematic statement as-you-type:

Furthermore, to help you easily fix the problems with Cloud Endpoints, Android Studio will provide quick-fixes for the most common Cloud Endpoints development mistakes. To see these quick-fix suggestions, press Alt + Enter if you're running on Linux/Windows or ⌥ + Enter if you're running on Mac:

As expected, choosing the first quick-fix ("Add @Named") will automatically add "@Named" annotation to method's parameter, solving this problem in two key presses.

The underlying work-horses: Gradle, and Gradle plug-in for App Engine

Under the hood, Gradle is used to build both your app and your App Engine backend. In fact, when you add an App Engine backend to your Android app, an open-source App Engine plug-in for Gradle is automatically downloaded by Android Studio, and common App Engine tasks become available as Gradle targets. This allows you to use the same build system across your IDE, command-line or continuous integration environments.

For example, to deploy your backend to App Engine from Android Studio, you can launch the "appengineUpdate" task from the "Gradle tasks" tool window:

Similarly, if you want to integrate your backend's deployment into your command-line scripts, simply launch "./gradlew backend:appengineUpdate" command from your project's root directory.

Try out a codelab, or see these features live at Google I/O 2014!

If you want to give these features a spin in a more guided environment, try out our Cloud Endpoints codelab for Android. We will also be demonstrating some of these features live at Less Code, More Services, Better Android Apps session in Google I/O 2014 (as well as some of the new and even more exciting stuff), so don't forget to tune in!

We look forward to your questions or feedback, and learning about the amazing applications you have built using Android Studio and Google Cloud Platform. You can find us lurking on StackOverflow's App Engine and Cloud Endpoints forums!