Android API Demos for Studio
When learning to program Android apps nothing beats seeing working example code. Unfortunately rapid changes in the Android Operating System (OS), and the Google preferred tools, means that it is sometimes hard to find good working samples. (Hence the free Android Studio example projects provided here on Tek Eye.) Google does provide plenty of sample projects and code, even though the right piece of code can sometimes be hard to find. This article is about running the old Android API demos sample app in Android Studio.
What Ever Happened to the Old Android Sample Projects
Earlier versions of the Android Software Development Kit (SDK) shipped samples that could be loaded and run in the preferred Integrated Development Environment (IDE) of the time, the Eclipse IDE. Each new release of the SDK would usually introduce new sample projects, mainly to demonstrate a new Application Programming Interface (API). When Google switched to the new Android Studio IDE the Eclipse samples were moved to a legacy folder and new Gradle based samples were added. With the release of Android Nougat (API 23) the samples moved online.
The legacy samples are useful because they cover many of the basic programming functions that new Android developers need to know about. In particular the API Demos legacy sample provides demos and code for many of the fundamental built in Android classes. The API Demos were even installed by default in some Android Virtual Devices (AVDs).
What if you want to run the legacy API Demos app? It can be done, but it is a bit painful. You need to get hold of Android samples from Android Jelly Bean MR1 (API 17). The legacy folder has the API Demos Eclipse Project.
Then you can import the Eclipse project. Sort out the Gradle syncing, rename a file, and begin working through a list of errors reported in Studio. Plus refactor the API Demo namespace if you don't want to remove the existing API Demos app in an AVD.
Alternatively just use the Studio compatible API Demos project that is available here on Tek Eye.
Running the Android API Demos in Studio
The Android API Demos project for Studio will load and run. It is not perfect due to deprecated code and other Android changes over the years. But at least it will compile and run, and most of the demos work. This is great if you want to see some of the fundamental Android classes in action.
Download the API Demos zip file. Extract the code to a directory on the PC (it will remain in that location). Then use the Android Studio Import Project option to load it up. (Selecting the build.gradle file.) Wait for Studio and Gradle to do it's thing. Click OK on the message Gradle settings for this project are not configured yet. (Unless you want to configure Gradle manually). Accept the sync message if it appears. Use the status messages at the bottom of the Studio window to monitor Gradle progress. Once loaded the API Demos should run on a suitable AVD, or an Android device configured for development (otherwise resolve any errors listed).
On some versions of Android, after API 19 (Android KitKat), the API Demos app will exception (error) on loading with java.lang.RuntimeException: Package manager has died. The error reported is !!! FAILED BINDER TRANSACTION !!! when a call is made to queryIntentActivities in the ApiDemos.java file. It appears a change in later versions of Android (APIs 21 to 23, Lollipop and Marshmallow) causes a limit to the amount of Activities that can be defined in AndroidManifest.xml. The manifest file for the API Demos app is over 3000 lines long. If about half the defined activites are removed from AndroidManifest.xml the app will run on APIs 21 to 23. Fortunately, as well as Anndroid KitKat and earlier versions, API Demos will load and run in Android Nougat and Oreo, APIs 24 and later.
A few of the many demos in the API Demos app will also exception, due to chnages to the Android API over the years. The project was a straight conversion to a Studio project from the original Eclipse code. Some messaging (MMS) code examples were commented out due to later SDK incompatibilites, but no other code changes have been made.
At least the project loads, compiles and runs in Studio (on the right API AVD or device), providing access to lots of API demo code.
- See the other Android Studio example projects to learn Android app programming.
- For a full list of the articles on Tek Eye see the full site Index
Author:Daniel S. Fowler Published: