Tuesday, May 21, 2013

Gradle - New Android Build System

As I saw keynote on Android Studio (new IDE for Android development which based on IntelliJ IDEA community edition), I start to download the IDE. I'm so enthusiast about what it can help Android developer. However, after testing it awhile, I am not really impressed by it. Actually I feel kind of dumb as I can't build the project which using 3rd party open source library such as ActionBarSherlock.

However, today, after seeing one of the keynote on New Android Build System, I just realized that whatever you add into Android Studio, not directly reflecting the configuration of Gradle build system (but that may change in the future). One of the example is if you adding ActionBarSherlock into AS (Android Studio), and if you build it, there will be Symbol not found errors all over the places (as if ActionBarSherlock is not included in the project. But not according to the IDE as you can still code complete the classes).

So, what can you do to build it successfully? Please consider my project structure:


The red color is where I put the libraries project. The blue color is where my main android project. You may ask, why I put the libraries inside my project directory, it is because easier for gradle to handle it (or more precisely, I don't know yet how to add the external directory to gradle outside main directory).

First thing first, update your settings.gradle:

include ':MahaBarata'
include ':libraries:actionbarsherlock'

As you can see the : symbol will be changed into / (or \ if you are on windows). This to indicate the build path like on eclipse.

The second things is to update your build.gradle inside your main directory (in this case is /Mahabarata dir) to add dependencies on ActionBarSherlock

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':libraries:actionbarsherlock')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
    }
} 

You may confused that there is a / and : symbol mixed within the file, but please bear with it as it is standard coming from gradle.

The third thing to do is to create build.gradle inside your ActionBarSherlock directory

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/android-support-v13.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }

} 

It is kind of similar with the main project build.gradle file above, only adding *.jar dependencies libraries to it.

After the last file is added, just open your terminal to run

gradlew build
or if you are on windows
gradlew.bat build
in the project directory or just build it right away inside AS. And from my case, it build successfully.

Note: not really without any glitches tho. I ran into out of space error within gradle on merging resources task, which suddenly will spawns many aapt thread all over the memory which I killed them and rerun it with gradlew build --info

Update 1: with the newest gradle plugin (on the time I update this blog), version 0.4.2, out of space error is not going to happen anymore.

Update 2: with the newest gradle plugin keep rolling out. You can simply update your gradle.build file as follow:

Update 3: newest Android Studio (v0.2.0) is out and google dev update the google plugin to 0.5

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }

Wednesday, August 15, 2012

Setting coordinate/positioning using Javascript

If you ever set positioning in javascript using syntax similar to below and didn't work (in browser standard mode):

var some_div = document.getElementById("some_div");
some_div.style.top  = "30";
some_div.style.left = "30";

What missing is only the unit of measurement when setting top and left position.

var some_div = document.getElementById("some_div");
some_div.style.top  = "30" + "px";
some_div.style.left = "30" + "px";

Monday, July 23, 2012

Reverse Indentation CSS

As you all may already know that text-indent css property can make indented paragraph on the first line, but how about if you want to indent from the second line and so on while the first line is normal?

You can try out this snippet below:

margin-left: 10px;
text-indent: -10px;

Wednesday, April 25, 2012

Git Tips

For you who use github or git, these are several tips for you.
Getting your remote url
git remote -v
Will show up remote name, url, and fetch/push status
Changing remote url
git remote set-url [remote_name] [new_url]

Getting your ssh default key (via git bash)
ssh-keygen -y
Entering your passphrase, then you can get your ssh key which you can copy to your github or other git compatible versioning control
Pushing to your newly created git
git push [git_name] [branch]
eg:
git push my_first_git master

Thursday, April 19, 2012

Git/SVN

Recently I need to keep track my project which files is updated, changed or added even deleted. The project is maintained not just by me, but other person as well which we do different things, but may involve in modifying a single file in a project, thus I do a research in internet for finding out tools to do this and, taa daa... I find out github (or git) and TortoiseSVN or mostly just called that Source Version Control (SVN).

However, pushing my project, which not an open source project and for internal corporate used only, I can't simply use github (actually I can, but I need to pay, and I don't think my company would like to pay for that), so I do searching again if there's other tools whatever it is do the same, but I want to do it locally (whether I pushed it into my own computer or even company's server later) and I found out GitStack.

I used it daily now, and I must quite impressive by it capabilities. Very easy to setup and use on my Win7 machine (git is quite hard to learn, however). I also using TortoiseSVN though for cloning other projects, but the principal behind it just similar to git. Once you know the basic, it is applicable to other SVN as well. But, still I like git more. How about you?

Monday, April 9, 2012

Several vim tips

To convert tab into space tab (if you set so)
:retab
Open split at 50:50 proportion of windows height
:sp
Set mark in column xx
:set cc=xx
I will add more and more later. Ciao...

Friday, March 2, 2012

Bug when combining Twitter Bootstrap Modal and Modernizr using Opera 11.61

I suffer from a bug when using Twitter Bootstrap (TB) Modal and using Modernizr.mq (media queries) functionality, in my case is the animation is not firing using Opera 11.61. The code that I write is:
$(document).ready(function () {
  // firing up notice (modal) after loading
  $('#announcementModal').modal('show');
  
  // I have other purpose on this and just try to do 
  //  console logging to see if that work or not
  if (Modernizr.mq('(min-width: 980px)')) {
    console.log('screwed');
  }  
});
and BAM! The modal animation isn't firing at all. After googling and searching for the bug's solution and found out none, I have a thought on doing this: switching the code like this
$(document).ready(function () {
  // I have other purpose on this and just try to do 
  //  console logging to see if that work or not
  if (Modernizr.mq('(min-width: 980px)')) {
    console.log('screwed');
  }

  // firing up notice (modal) after loading
  $('#announcementModal').modal('show');    
});
And the result? Everything just worked! Can't believe this. I think there's a mechanism in Modernizr to stop all things to make detection more precise and (maybe) they forget to return all the things that they stop. Just my $0.02 though, I kind of busy to deep down the code yet. :(