PackageManager.getInstalledPackages() vs PackageManger.getInstalledApplications()

PackageManager.getInstalledPackages()
Lists all the packages installed on the device including services, providers etc.

 PackageManger.getInstalledApplications()
Lists only applications installed on the device. Excludes services, providers etc.

Setting up Unit Testing in AndroidStudio - 1.5 with Robolectric

AndroidStudio Version: 1.5 - Preview2
Robolectric: robolectric:3.0-rc2


  • Choose "Unit Tests" in "Build Variants" as "Test Artifact". 
  • Create a Unit Test under "src/test" folder.
  • Run the test !


Configuration details:

build.gradle additions related with unit tests:

dependencies {
    
    testCompile 'junit:junit:4.12'    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'        exclude group: 'org.apache.httpcomponents', module: 'httpclient'    }
}

Sample Test class:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21)
public class MyTest {

    @Before    public void setUp() throws Exception {
        // setup    }

    @Test    public void testSomething() throws Exception {
        // test        assert(true);
    }
}




Pro-guard in Android


  • Pro-guard can only be enabled in release builds, which is a good thing. Otherwise you would have to debug through obfuscated code.
  • Pro-guard can shrink, optimize and obfuscate code.
  • Obfuscated stack traces could be decoded using tool "retrace".
  • Enabling pro-guard in gradle based builds: set "minifyEnabled" to true.
  • Default pro-guard settings are available in <sdk>/tools/proguard/
    proguard-android.txt
  • Optimization can be enabled by using <sdk>/tools/proguard/proguard-android-optimize.txt file. But testing have to be thorough because these optimization rules may not be applicable to all versions of dalvik.
  •  Custom pro-guard rules can be added in proguard-rules.pro
  • While building with gradle build system, particular pro-guard rules can be applied to a targeted flavor

Running gradle as daemon (Mac)

How to know if gradle daemon is running or not ? Run "gradle --stop" on commandline. It should spit out that gradle is not running as daemon.
$ gradle --stop
No Gradle daemons are running.

Follow these steps to make gradle run as daemon:

Navigate to /Users/<your user name>/.gradle
cd /Users/<your user name>/.gradle
Create a file name gradle.properties
vi gradle.properties
Add a line in "gradle.properties"
org.gradle.daemon=true

Done !

Note: Execute any gradle task to kick start daemon.

Verify
$ gradle --stop
Stopping daemon(s).
Gradle daemon stopped.


 
 

Adding JAVA_HOME env variable to bash (Mac)

On your terminal, execute this command all together:
echo "# Java environment variables
export JAVA_HOME=$(/usr/libexec/java_home)
export JDK_HOME=$(/usr/libexec/java_home)
" >> ~/.bash_profile&&\
source ~/.bash_profile

Installing gradle using commandline (Mac)

Simple ! Just execute this command on your terminal :

brew install gradle

Pre-req: you should have Homebrew for Mac already installed. 

Installing gradle on Mac (using gradle wrapper)

The simplest way to install gradle from commandline is to simply execute this script that comes with gradle wrapper.

All you have to do is to execute this command on terminal:
./gradlew
Output:

Downloading https://services.gradle.org/distributions/gradle-2.3-bin.zip
...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Unzipping /Users/PTyagi/.gradle/wrapper/dists/gradle-2.3-bin/a48v6zq5mdp1uyn9rwlj56945/gradle-2.3-bin.zip to /Users/PTyagi/.gradle/wrapper/dists/gradle-2.3-bin/a48v6zq5mdp1uyn9rwlj56945
Set executable permissions for: /Users/PTyagi/.gradle/wrapper/dists/gradle-2.3-bin/a48v6zq5mdp1uyn9rwlj56945/gradle-2.3/bin/gradle 

Keep trying out ideas !

This is not a technical post, but worth giving a though into !

The best way to invent something Great in life, is to keep trying out Silly ideas :)

"And yet the best way to have a good idea is to have as many bad ones as you can. The best way to write good poetry is to write a whole lot of stuff, and find the diamonds in the rough. The best way to set goals is to set 45 of them and see which ones stick around that are really the most interesting to us. And the best way to keep loving people we love is to keep the air clear with honest feedback." ~ David Allen

Android Dev Tools

Here's casual list for Android developer tools that could be really useful when developing complex applications:

  • StrictMode: A tool for identifying long running operations on Main UI Thread.
  • @UiThread/@WorkerThread : Useful support annotations for static analysis for catching Threading bugs.

Android Fragments using RxJava and Dagger

This is really cool introduction on Android Fragments using RxJava and Dagger dependency injection framework.

Scheduling Repeating Local Notifications using Alarm Manager

Learn about Scheduling Repeating Local Notifications using Alarm Manager in this post .