SigningConfig for Gradle - Android

Add following code snippet in your build.gradle:

signingConfigs {
   debug {
      storeFile file('<full-path-to-keystore>/.android/debug.keystore')
      storePassword 'android'      keyAlias 'androiddebugkey'      keyPassword 'android'   } 
}
Add More variants for each flavor like release.

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. 

Scheduling Repeating Local Notifications using Alarm Manager

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