Author Archives: Ben Lam

Android Robolectric Support Fragment FragmentTestUtil

To use Android Support Fragment v4 in Robolectric 3.0 you must add gradle dependency in /app/build.gradle

testCompile 'org.robolectric:shadows-support-v4:3.0'

Then import into your Robolectric testing java class

import org.robolectric.shadows.support.v4.SupportFragmentTestUtil;

then you can create/start android support v4 fragments within your Robolectric testing java class for unit testing:

SupportFragmentTestUtil.startVisibleFragment(fragment);

Cross posted to StackOverflow Android Robolectric Support Fragment.

Android Studio generate foreach loop

Begin typing foreach until autocomplete pops up.  Select “Create a for each loop”.

You’ll then get an empty for each loop that you can start filling in, first supplying the Type of each object, a variable name of your choice for that object, then hit Enter to move to the name of the variable (a Collection), then hit Enter, then supply the statements you wish to run against each item within the collection.

android-studio-foreach-generate

android-studio-foreach-generate-example

Pause genymotion Android Emulator on Mac

To Pause genymotion Android Emulator on Mac:

  1. Press the power button of emulator (bottom right corner)
    1. This is equivalent to executing “avd stop” within adb shell on the emulator
  2. Find the process ID number of VBoxHeadless using Activity Monitor
  3. In Terminal: kill -STOP <ID number from step 2>
    1. Your emulator is now effectively paused and uses no CPU

To Restart / Unpause Android Emulator

  1. In Terminal: kill -CONT <ID number from step 2 above>
  2. Press the power button on emulator (bottom right corner of window)
    1. Your emulator is now restarted & should operate normally

 

Android Studio androidTest sourceSets gradle not recognized

Android Studio (app) build.gradle sourceSets androidTest java.srcDir property may not be recognized when using the array format of this source directory property setting.

BAD

sourceSets {
    androidTest {
        java.srcDirs = ['androidTest/java']
    }
}

GOOD

sourceSets {
    androidTest {
        java.srcDir file('androidTest/java')
    }
}

For setting up Robotium UI / Integration testing, it’s important that your androidTest source directory is recognized.

Android Fix Failed to Find Build Tools Revision

Failed to Find Build Tools Revision 2X.X.X

Android Build tools get updated often and whenever an Android project you import that is using a different Build Tools Revision than the one you’re using, you’ll see this error in Android Studio.

To fix “Failed to Find Build Tools Revision” you have 2 options:

  1. Install the missing Build Tools revision
  2. Change the Build Tools version in your ../app/build.gradle

Install missing Android Build Tools revision

Install a missing Android Build Tools revision via Tools -> Android -> SDK Manager -> SDK Tools (tab) -> Check Show Package Details (bottom right check box) -> mark the checkbox of the requested Android Build Tools version

android-studio-failed-build-tools

 

Change the Android Build Tools Version in Build.Gradle

  1. Open app/build.gradle
  2. Find buildToolsVersion
  3. Change the version number to one you have installed (see above SDK Tools image on how to find which versions you have installed, which are marked as “Installed” or “Not Installed”)
  4. Sync Gradle.

 

 

build-gradle-build-tools-version

Eclipse – Add Java Gradle Project to existing Eclipse Project

How to add a java Gradle Project into an existing Eclipse project

Start by importing your required Java Gradle projects into Eclipse’s Project Explorer.

Highlight your main project in Project Explorer.

eclipse-project-explorer

This is the project you want to add a Gradle java project to (so you can use import statements on packages found in the other Gradle project).

On the Main toolbar menu go to

Project > Properties > Java Build Path > Projects (tab) > Add (button)

A list of projects in the current Project Explorer will show up in a window.

Select the java project that you wish to use within your main project, that is not already on the Build Path.

Click OK.

Eclipse should refresh / rebuild automatically and check for import statements for references to the newly added project from your main project.

eclipse-project-build-path-add

Kill Tomcat on Mac

How To Kill Tomcat on Mac when IntelliJ IDEA or Eclipse has crashed or you forgot to stop Tomcat before closing Eclipse.

Step 1: find which java process is Tomcat

  • Start Activity Monitor (Applications > Utilities), then order list by Process name
  • Scroll to find java processes
  • Note the PID #’s (process ID’s)

Step 2: Pause java processes while checking Tomcat address

  • From a browser load your normal Tomcat address, e.g. http://localhost:8080/
  • You should get some sort of response, perhaps a Whitelabel Error page if using Spring
  • repeat below until Tomcat stops responding:
    • pause java PID using: kill -STOP <PID>
      • replace <PID> with the actual process ID # you found from Activity Monitor
      • example: kill -STOP 25532
    • reload Tomcat web address
    • if web page no longer responds (just continues to load), you’ve found Tomcat
      • otherwise (wasn’t Tomcat), unpause process: kill -CONT <PID>
      • example: kill -CONT 25532
    • move to next process ID number in Activity Monitor with name “java” and repeat steps

Step 3: Kill Tomcat on Mac

  • kill the Tomcat java process in Terminal using command: kill -9 <PID>
    • example: kill -9 25532
  • Check: Reload Tomcat webpage in browser.  Should show “This webpage is not available”

Activity Monitor - find java processes

Pause / Continue java process in Terminal

Tomcat page when process is paused

Android Security Fix MMS Flaw

Android security fix MMS flaw – Turn Off MMS Message Auto retrieve.

Below is only a workaround for Android’s MMS Security Flaw (aka Stagefright)

Please update your Android phone ASAP.  There are several other flaws related to Stagefright that this workaround will not help!

Android Security Fix Steps

Step 1 – Hangouts or Messaging

If you’re using Google Hangouts for SMS/MMS, open Hangouts app. (For Messaging app?)

android-hangouts

Step 2 – Settings

Click on Settings

android-hangouts-settings-0

Step 3 – SMS

Click on SMS

android-hangouts-sms

Step 4 – Multimedia (MMS) Messages > Auto retrieve MMS = OFF

(Click to) remove the checkbox from Auto retrieve MMS.  You’re done.

android-hangouts-mms-auto-retrieve

 

Stagefright flaw in Android exploits how Android’s MMS (Multimedia Messages) system automatically downloads MMS content (e.g. video) to be read / cached locally, without any user interaction.  This automatic reading of the MMS content allows attackers to embed their virus / trojan payload into a video which will automatically be run by Android’s Content Provider / Stagefright system.

This Google Android Vulnerability MMS Flaw was found by Joshua Drake (twitter) of Zimperium.

More info links on Android MMS Flaw & Stagefright

http://blog.zimperium.com/experts-found-a-unicorn-in-the-heart-of-android/

http://www.forbes.com/sites/thomasbrewster/2015/07/27/android-text-attacks/

Android Media / Stagefright

Android Messaging MMS Security Flaw Fix

If you’re using Android Messaging app, instead of Google Android Hangouts, follow these steps…

Step 1 – Open Messaging app & Messaging Settings

android-messaging-settings

Step 2 – Multimedia (MMS) Messages – Auto-retrieve OFF

Scroll down to Multimedia (MMS) Messages.

Uncheck Auto-retrieve.  You’re done.

android-messaging-mms-auto-retrieve-off

Android Studio – Failed to Sync Gradle project – Fix

Matching SDK versions identified in build.gradle (Module: app) with the SDK versions you have installed via SDK Manager, then “Sync Now”, will fix most Android Studio build errors.

Common Android Studio build errors:

  • Failed to Sync Gradle project
  • Failed to find target android-21
  • Install missing platform(s) and sync project

If you’re building / running someone else’s project that you’ve cloned from Github likely you’ll run into build errors in Android Studio.

Most Android Studio project build errors can be fixed in one file: /project/app/build.gradle

There are multiple Gradle build files, frustratingly named build.gradle.  You want the build.gradle (Module: app) Gradle file.

android-studio-build-gradle

Here’s a typical Gradle build.gradle file

android {
 compileSdkVersion 21
 buildToolsVersion "21.1.2"

 defaultConfig {
 applicationId "com.domain.appname"
 minSdkVersion 18
 targetSdkVersion 21
 versionCode 1
 versionName "1.0"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:21.0.3'
}

Starting from the top of build.gradle file and moving down,

  • compileSdkVersion
    • This number should not be higher than the highest SDK version you have installed via SDK Manager.  You can scroll through SDK Manager looking for SDK Platform entries, checking the API # (18 in the screenshot) and the Status – Installed:
    • android-sdk-version-installed
  • buildToolsVersion
    • This line in your build.gradle should be set to the latest Android SDK build-tools version you have installed, not a revision/version that matches your target SDK.  Again, the build tools version # is not related to your SDK version #.  In general you want to install and use the latest build tools version.
    • Visible in SDK Manager here:
    • android-sdk-build-tools-version
  • targetSdkVersion
    • This should generally be the same as your compileSdkVersion
  • dependencies
    • Here is where support libraries are identified / referenced.
    • Note: If you don’t specifically need libraries from the support library, such as Support FragmentActivity, remove the reference! Comment out (double forward slash //) these libraries or delete the line entirely.
    • Example:
      compile 'com.android.support:appcompat-v7:21.0.3'
    • A version # suffix is used when identifying the support library, which comes after the colon (21.0.3 in above example).  This specifies the version of the support library, not your SDK version.  Revisions of the support library are listed here.  Generally you’ll want to use the latest unless you specifically need to use previous versions of methods/functions in older support library revisions.
    • In SDK Manager, you want to ensure you have Support Repository installed.
    • Android Studio uses the Support Repository, not the Support Library (which I assume is for Eclipse projects).  Make sure you have the Support Repository installed.  Generally you’ll want this library updated to the latest version, so update it within SDK Manager when you see an update is available.
  • After correcting version numbers in build.gradle, Android Studio will request you Sync with the following message:
    • Gradle files have changed since last project sync.  A project sync may be necessary for the IDE to work properly.  Sync Now.
    • Click the blue Sync Now link to sync your project.
    • gradle-sync-now
  • After sync try building your project using Cmd + F9 (Control + F9 for Windows).  Hopefully the Android Studio project builds without errors.
  • If all goes well, you see a tiny completion message, bottom left corner saying: Gradle build finished in 2 sec.
  • If things go wrong, the Messages window will pop-up showing the error
  • gradle-sync-error
  • Language Level Changed – Answer Yes
  • language-levels-change