Tuesday, May 26, 2015

Android: One Configuration To Build Them All

In the past, finding an Android build configuration that works on Android Studio, IntelliJ as well as command line has proven to be extremely difficult. On each new SDK-, IDE- or Gradle version, things seemed to be broken again. It wasn't a stable working environment at all, and I'm glad that Google decided to work on that (source: http://tools.android.com/tech-docs/new-build-system/version-compatibility).

Now, I'm using a build configuration that seems to work on all platforms, and I'd like to explain to you what I've done to make it work (and hopefully help you save a lot of time).


1. Install the Android SDK


First, I've downloaded android-sdk_r24.0.2-macosx.zip (Android SDK 24.0.2) from the Android website, and unzipped it (I've used directory /Users/Stefan/android-sdk).  I'm building on Mac, but you can also find the Windows and Linux releases on the website.

After unzipping, you create an environment variable called ANDROID_HOME, that points to the directory of the SDK that you've just unzipped:
export ANDROID_HOME="/Users/Stefan/android-sdk"
(on Mac or Linux, just add it to your ~/.profile file so that it will automatically be set each time you open your terminal)

Next, start the Android SDK manager, using:
$ANDROID_HOME/tools/android
Here, you have to install the following packages (this will take some time):

  • Android SDK Tools v24.0.2
  • Android 5.0.1 (API 21)
  • Android SDK Build-tools v21.1.2

Your SDK is now ready.


2. Install the IDE


Install the IDE of your choosing (Android Studio or IntelliJ).

For testing, I've downloaded both:
  • android-studio-ide-135.1740770-mac.dmg (Android Studio 1.1.0 build 135.1740770)
  • ideaIC-14.0.3.dmg (IntelliJ 14.0.3)


3. Create a new project (Hello World)


Now we're ready to create a new project.

Execute in your terminal:

$ANDROID_HOME/tools/android create project  
-t "Google Inc.:Google APIs:21" 
-p testproject  
-a TestActivity  
-k be.ad.testproject 
-g  
-v 1.1.0

This will create a directory called testproject, in which a Hello World application is generated (under package name be.ad.testproject).  The application will just have one activity, called TestActivity.  It will use Google APIs version 21 and Gradle build tools version 1.1.0.

Next, open testproject/gradle/wrapper/gradle-wrapper.properties and
  • change the Gradle version to gradle-2.2.1-all.zip.

Open testproject/build.gradle and
  • remove runProguard false line
  • replace the Android plugin with "com.android.application"


4. Test the build configuration


Ready to test it?

Execute in your terminal (in the testproject directory):

./gradlew clean assembleDebug

It should compile without any problems.

Next, try to open the build.gradle file using IntelliJ and/or Android Studio (be sure to select Use Default Gradle Wrapper when opening the project.  And when asked, choose to use the Project SDK instead of the IDE SDK).

It should open and build on both IDEs, without ANY problems.

And if you deploy the application on your device or emulator, you will see a nice Hello World application...

I prefer creating my projects command line, not by IDE.  When creating a project using the IDE, a lot of additional files will be generated that you might not need in your project.


5. Links


No comments: