Tag: IntelliJ IDEA

Programming Stuff

Lambda Expressions in Android Studio

I’m often asked by students from my Android courses as to how I’m able to use Lambda expressions like the following in my Android programs.

Lambda Code Folded

The short answer is: I’m not able to use them. 🙂 What I typed was the following.

Lambda Expanded

The Lambda expressions appear in the editor due to a feature of Android Studio called “Code Folding”.

Android Studio does this sort’a thing in a number of scenarios. You can tell the Lambda expression is a result of code-folding by the highlight that appears over the folded portion of code.

You can also easily identify that the Lambda expression is a result of code folding because the editor will display a plus-sign to the left of the statement. You can view the full, original statement by clicking on the plus sign.

Lamba expression substitution is controlled by the following value in the Android Studio Settings dialog.

Code Folding Settings

As this is my first post of 2014, let me take this opportunity to wish everyone … Happy New Year!!

Programming Stuff

How to Clean an Android Studio Project

You’ve probably noticed that Android Studio doesn’t have a “clean” menu option. But we know that, like any build environment, there are intermediate files created that we may want to occasionally remove.

It turns out the solution is the command line. Simply open a command line in your project’s root folder (Usually named something like MyAppProject) and run the following command

gradlew clean

You’ll see something like this…

gradlew_clean

Notice that you get a bit of an unexpected message, “BUILD SUCCESSFUL”. This simply indicates that it has gone through and successfully cleaned out the intermediate files. I’ve found that when dealing with larger projects its not uncommon to reduce the amount of disk space used by the project by 70% or more.

So a bit of an unexpected solution (especially for those who’ve been in a Windows environment for a long time) but an easy one.

To learn more about Android programming, checkout Jim’s Android courses on Pluralsight.

Jim's Android courses on Pluralsight

Jim’s latest book, “Creating Dynamic UI with Android Fragments” is now available on amazon.

Creating Dynamic UI with Fragments

Programming Stuff

Android Studio 0.1.9 Brings the New Resource Directory Dialog Back

If you’ve been working with the last few builds of Android Studio you know that the ability to create new resource directories was broken. The only work-around was a hacky process of going into some of the settings dialogs and creating the folder from there.

Well the 0.1.9 patch just came out and the New Resource Directory dialog is back (actually it came back in the short-lived 0.1.7 release from a few days ago).

NewResourceFolderDlg

I love the way that this is implemented because it takes care of all of that subtle folder naming that goes into resource specialization.

Android’s support for resource specialization is one of the real powers of the platform in my opinion but manually building those names can be easy to mess up. With this dialog you simply select the resource type, the qualifiers and then enter or select (depending on the qualifier) the value to qualify by.

This is a great feature and goes a long way to making one’s life simpler.

For more information on working with Android Studio, check out Jim’s latest course:

Apps with Android Studio

Programming Stuff

Android Studio Bug Workaround V2 – Project breaks when copied or moved

As you’ll recall, I posted yesterday about working around the issue in Android Studio where copying a project to a new folder creates problems because the Android Studio project keeps references to the original folder. After my post yesterday, I figured out an easier way to work around the problem.

You still have to modify just as many files but the steps are easier to follow and perform than my suggestion from yesterday.

So the new workaround…

We’ll use the same assumptions as yesterday.

  • The project name is:
    AndroidExample
  • Original project folder:
    C:\Demo\before\AndroidExampleProject
  • Destination project folder:
    C:\Demo\AFTER\AndroidExampleProject

The cool thing about this workaround is that after copying the project to the folder, you always make the same (relatively simple) changes. There’s nothing specific about the destination folder in the changes we make.

Note: I’m including line numbers just as estimates. Your line numbers will almost definitely be different.

Be sure that Android Studio is completely closed.

In both of the following files, change the value for PROJECT_ABSOLUTE_PATH to be blank

  • C:\Demo\AFTER\AndroidExampleProject\AndroidExampleProject.iml (approx. line 7)
  • C:\Demo\After\AndroidExampleProject\AndroidExample\AndroidExample.iml (approx. line 15)

Change:

<option name="PROJECT_ABSOLUTE_PATH" value="C:\Demo\before\AndroidExampleProject" />

To:

<option name="PROJECT_ABSOLUTE_PATH" value="" />

In both of the following files remove the full path before build.gradle (including the slash ” \ “) and replace it with $PROJECT_DIR$/

  • C:\Demo\AFTER\AndroidExampleProject\.idea\gradle.xml (approx. line 6)

Change:

<option name="externalProjectPath" value="C:\Demo\before\AndroidExampleProject\build.gradle" />

To:

<option name="externalProjectPath" value="$PROJECT_DIR$/build.gradle" />
  • C:\Demo\AFTER\AndroidExampleProject\.idea\workspace.xml (approx. line 207)
    • The “option” element you’re changing will be a child of the “ExternalProjectPojo” element

Change:

<option name="path" value="C:\Demo\before\AndroidExampleProject\build.gradle" />

To:

<option name="path" value="$PROJECT_DIR$/build.gradle" />

And with that, you’re ready to open the destination project in Android Studio.

When you save the project, Android Studio will automatically save the new folder as the PROJECT_ABSOLUTE_PATH value.

I’m finding these changes much easier to manage than the ones I walked through yesterday. But as I mentioned yesterday, the best news of all is that I suspect that this will be a short lived bug and we won’t have to deal with this workaround for long.

For information on getting started developing Android applications or working with Android Studio, checkout Jim’s Android programming courses on Pluralsight including his latest course.

Android for .Net Developers Series - Getting Started

Programming Stuff

Android Studio Bug Workaround – Project breaks when copied or moved

Since I write training courses about Android development, I frequently have to copy projects from one folder to another. I commonly create a project in a “before” folder, copy the project to an “after” folder, and then do the work being discussed in the course in the project in the “after” folder. When students view the course they can download “before” and “after” copies of the project.

Well one of the pains I’ve been dealing with in the preview release of Android Studio is the fact that when a project is copied from one folder to another, the project keeps pointers to the original folder. One ends up with a mess where some things in the new project point to the new folder and somethings point to the old. Deleting the original folder makes things even worse.

I’ve reported the issue as an Android Studio bug. It appears to have been accepted as an issue but as of this writing the version of Android Studio currently available still has the problem.

The version available as of this writing…

AndroidStudioAbout_AI-130.700763

I’m certain this is an issue that will be addressed so if you’re reading this post substantially beyond the posting date, verify that the problem still exists before doing the steps I list here.

OK so the workaround…

UPDATE: The day after I made this post, I figured out a simpler work around. Please see this post for the new workaround.

The solution I’ve found is to modify the path information contained in 4 of the project files in the destination folder.

Before doing anything, be sure Android Studio is completely closed.

Let’s make the following Assumptions in copying our project to a new folder.

  • The project name is:
    AndroidExample
  • Original project folder:
    C:\Demo\before\AndroidExampleProject
  • Destination project folder:
    C:\Demo\AFTER\AndroidExampleProject

Note: I’m including line numbers just as estimates. Your line numbers will almost definitely be different.

1. File: C:\Demo\AFTER\AndroidExampleProject\AndroidExampleProject.iml (approx. line 7)

Change:

<option name="PROJECT_ABSOLUTE_PATH" value="C:\Demo\before\AndroidExampleProject" />

To:

<option name="PROJECT_ABSOLUTE_PATH" value="C:\Demo\AFTER\AndroidExampleProject" />

2. File: C:\Demo\AFTER\AndroidExampleProject\.idea\gradle.xml (approx. line 6)

Change:

<option name="externalProjectPath" value="C:\Demo\before\AndroidExampleProject\build.gradle" />

To:

<option name="externalProjectPath" value="C:\Demo\AFTER\AndroidExampleProject\build.gradle" />

3. File: C:\Demo\AFTER\AndroidExampleProject\.idea\workspace.xml(approx. line 207)

Note: The “option” element you’re changing will be a child of the “ExternalProjectPojo” element

Change:

<option name="path" value="C:\Demo\before\AndroidExampleProject\build.gradle" />

To:

<option name="path" value="C:\Demo\AFTER\AndroidExampleProject\build.gradle" />

4. File: C:\Demo\After\AndroidExampleProject\AndroidExample\AndroidExample.iml(approx. line 15)

Change:

<option name="PROJECT_ABSOLUTE_PATH" value="C:\Demo\before\AndroidExampleProject" />

To:

<option name="PROJECT_ABSOLUTE_PATH" value="C:\Demo\AFTER\AndroidExampleProject" />

5.
Now open the project in Android Studio

You may be prompted with a dialog indicating that it’s removing a non-existent project. Let it do that. It’s just cleaning up some internal data, it doesn’t appear to have any impact on the original project.

Voila! We now have a copy of our project.

The best news of all … I’m pretty sure that the bug that’s causing us to have to go through all of these steps (rather than simply copying the project folder) is a bug that will soon be fixed.

For information on getting started developing Android applications or working with Android Studio, checkout Jim’s Android programming courses on Pluralsight including his latest course.

Android for .Net Developers Series - Getting Started

Programming Stuff

Android Studio Power Save Mode

Have you noticed a shortening of your laptop’s battery life when working with Android Studio?

Android Studio is a great tool and some of the great things it does require fairly substantial processing to occur in the background. Much of this processing is constantly firing  performing what are called Inspections.

To get a sense of the kind of background work that’s going on, choose Project \ Settings from the Android Studio menu and then highlight Inspections in the Project Settings pane.

Android Studio Inspection Settings

To save power we could go through and turn off some of these inspections so that less background processing occurs. These inspections are good stuff, though. I prefer to have these checks available. Fortunately, Android Studio doesn’t require us to make this choice.

Android Studio gives us the option of stopping the automatic checks and instead running them on demand. You can prevent these inspections from automatically running by selecting Project \ Power Save Mode in the Android Studio menu.

You can then run the checks manually by choosing Analyze \ Inspect Code… from the Android Studio menu.

With this, we get the benefit of Android Studio code inspection while still maximizing our laptop battery life.

For information on getting started developing Android applications or working with Android Studio, checkout Jim’s Android programming courses on Pluralsight including his latest course.

Android for .Net Developers Series - Getting Started

Programming Stuff

Android Studio 0.1.2 Is WAY more stable

The stability of the  initial preview release of Android Studio (version 0.1) was pretty impressive. Being built on a long-standing product like JetBrains Intellij IDEA gave it a great start.

That said, it did at times feel like a version 0 product … unexpected message boxes popping up, strange behavior in the feature that displays the string value where string resources are used (sometimes didn’t work .. sometimes showed the string value on a different line than where the resource appeared), those sort’a things.

About a week or so after the initial release Android Studio automatically updated to Android Studio 0.1.1 and there was some improvement but not a huge amount.

Well then yesterday, I received another update, Android Studio 0.1.2 (Build #AI-130.692269) … I’ve only been using it for less than 24 hours but so far this build feels MUCH MUCH better. It’s feeling much more stable.

AndroidStudioAbout_0.1.2

If you haven’t already done so, I encourage you to download Android Studio and start checking it out. I’m sure there will still be some issues but it’s improving quickly and IMHO is already so much better than using Eclipse that any minor annoyances are a small price to pay to have such a superior dev tool.

For information on getting started developing Android applications or working with Android Studio, checkout Jim’s Android programming courses on Pluralsight including his latest course

AndroidDotNetSeries_GettingStarted_Logo

Programming Stuff

Android Studio Dark Theme (Darcula)

After seeing those cool demos of Android Studio showing the neat-looking dark theme, the default theme that Android Studio downloads with can be a little disappointing.

If you haven’t seen it, here’s Android Studio set to the Darcula theme.

Android Studio Darcula Theme

To get the Darcula theme, first open the Android Studio Settings dialog

  1. Select File on the Android Studio menu
  2. Then select Settings

Once on the Settings screen just…

  1. Highlight Appearence
  2. Click on the Theme dropdown and select Darcula
  3. Click OK

Setting Darcula Theme

You’ll be prompted to restart Android Studio. Allow the restart and there you have it. The great look of Android Studio in Darcula.

Adapted from Jim’s Pluralsight course Android for .NET Developers: Getting Started

AndroidDotNetSeries_GettingStarted_Logo

Programming Stuff

Android ADB reports device is “offline”

Device upgrades are so often a good-news-bad-news situation … and so was the case of my device’s upgrade to Android 4.2.2 over the weekend.

I love this latest version of Android – in addition to just generally being a good update, it also has a sweet new camera and, some cool new clock/stopwatch/timer features.

The new camera features are especially cool for me as I’m right in the midst of writing a new Android developer course Android Photo and Video Programming for Pluralsight.

But like so many exciting weekend events, Monday often comes with regret … and so it was today.

I fire up my favorite Android IDE (currently JetBrains IntelliJ IDEA), connect my device, then launch the debugger … and it’s then that I see “waiting for device” … I keep waiting, waiting, waiting.

To check things out, I do an ‘adb kill-server’ followed by an ‘adb devices’ and I see…

List of devices attached
99999C99999D999         offline

That’s something I don’t see too often so I do the standard stuff … reboot my desktop computer, reboot the device. Always the same result … the device reports being offline

The Answer….

I resolved the issue by upgrading to the latest version of the Android SDK Platform-tools – for me, that’s version 16.0.2:

AndroidSDKManager-PlatformTools16.0.2

Just something to remember … you may have to run the SDK Manager more than once to get all of the latest updates.

In my case, I had to…

  • Run the SDK Manager
  • Execute ‘Install packages…’
  • Exit the SDK Manager
  • Restart the SDK Manager
  • Execute ‘Install packages…’

Only then did I get the necessary updates to resolve the offline issue so that I now see the much more familiar (and welcome) device message in ADB.

List of devices attached
99999C99999D999         device