söndag 12 oktober 2014

Android : Importing (older) Android gradle projects in IntelliJ IDEA

This is a simple tutorial, i'll start with an Android sample, download 'SlidingTabsBasic' from http://developer.android.com/downloads/samples/SlidingTabsBasic.zip.

Projects built with an older android gradle plugin and build-tools needs to be updated.


First off, start IntelliJ IDEA and select 'Import Project', select the directory where you've unzipped the project.

IDEA auto-selects it as a 'Gradle' project as in the following screenshot.


We'll go with the default settings, and click 'Finish'. Depending on what packages you've installed from the Android SDK you've might get a first error.




The build tools version specified in SlidingTabsBasicSample\build.gradle isn't installed on my computer, i'm currently using 19.1, but it depends, open up your Android SDK manager and see which one you've installed.

File build.gradle updated with a newer build tools version, click open the 'Gradle' tab and hit refresh. Next error comes along, these samples were created with an older Android plugin for Gradle.

Error: 'The project is using an unsupported version of the Android Gradle plug-in (0.9.2)'

In the same file update the line classpath 'com.android.tools.build:gradle:0.9.+' to a newer plugin version, as of this writing i'll use 'com.android.tools.build:gradle:0.12.2'. Hit refresh again.

Finally the project builds sucessfully, Now we'll create the 'Run/Debug Configurations'.
Select Run -> Edit Configurations


IDEA should pick up and select the default settings, select a emulator image, and run the project!
Hopefully this will help out when trying out samples made with an older version of the build-tools and the android gradle plugin.




lördag 23 augusti 2014

Android : IntelliJ, Gradle & Signing

IntelliJ IDEA now offers good IDE support for Gradle. We can just go through their 'New Project', select an 'Gradle: Android Module' and project is up and running.



Signing applications need a bit more work though, IDEA points us to Gradle Plugin User Guide.

Default gradle build script in IDEA:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 20
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

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

We need to add the container signingConfigs { }.
Example build.gradle file:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 20
    buildToolsVersion "19.0.0"

    signingConfigs {
        debug {
             // Debug - uses default debug keystore
        }

        release {
                storeFile file("C:\\Android\\Key\\keystore")
                storePassword "myPassword"
                keyAlias "key0"
                keyPassword "testPassword"
            }

    }

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguardandroid.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
            zipAlign true
        }
    }
}

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

As you can see we've added a 'signingConfig' flag in the release build type.
You can also add a debug {} under buildTypes if you want to add or change something in the debug build.
Example:
 buildTypes {
        debug {
            println "Building DEBUG release."
            println "Run ProGuard == TRUE"
            runProguard true
        }
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
            zipAlign true
        }
    }

As you can see we've enabled Proguard in the debug release.

Run 'PROJECT_HOME\gradlew.bat tasks' for a complete list of different tasks to do or build or open the Gradle tab in IntelliJ IDEA.


I've marked signingReport as it'll display your key status. You can run it directly from IntelliJ IDEA or from your favorite shell.

As you can see we get a complete report of the key store. Wrong password or missing file etc.
When everything is working as it should. Just hit 'gradlew.bat install<yourTask>' to install the signed version of your app on the emulator or a physical device.

I'll update this post as i play more with Gradle!

torsdag 12 juni 2014

Android : Importing Google Play Services API in IntelliJ IDEA


I'll be using an gradle built Android module. First create an empty Android project.

Now we need to setup the dependecies, http://developer.android.com/google/play-services/setup.html.
This first way is the easy way, edit the 'build.gradle' in the DriveTest directory and add:
dependencies {
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.google.android.gms:play-services:4.4.52'
}

Lastly open the AndroidManifest.xml for the Android module and add the following as a child under the <application> tag:
<meta-data android:name="com.google.android.gms.version"
                   android:value="@integer/google_play_services_version" />

Afterwards you should be able to auto-complete the dependencies in the IDE.

Another way is to copy the file from 'sdk\extras\google\m2repository\com\google\android\gms\play-services\4.4.52\play-services-4.4.52.aar' to your android modules lib folder then update your build.gradle scripts with the following :
repositories {
    flatDir()
            {
                dirs 'libs'
            }
}
dependencies {
    compile ':play-services:4.4.52@aar'
}



Troubleshooting :
Make sure you have the following installed in the Android SDK.

Be sure to check min-sdk version in your AndroidManifest.xml and build.gradle files. 9 at least.

Remember that Google Play Services work inside the emulator these days, just make an AVD that runs the Google APIs with version 4.2.2 and above. Just be sure to sign the application, i use the command line when installing a signed package as IDEA has no auto-installation when building. (what i've found)

Quickest route for me, uninstalling my debug installation first. Then installing the signed release.