Skip to content

Instantly share code, notes, and snippets.

@grimrose
Last active August 29, 2015 14:03
Show Gist options
  • Save grimrose/498ec21570c9191738a7 to your computer and use it in GitHub Desktop.
Save grimrose/498ec21570c9191738a7 to your computer and use it in GitHub Desktop.
#yokohamagroovy #25

Yokohama.groovy #25

http://connpass.com/event/6979/

やること

Groovy on Android

https://speakerdeck.com/melix/groovy-on-android

https://speakerdeck.com/melix/developing-android-applications-in-groovy

GroovyでAndroidのアプリケーションを作るためのGradle plugin

melix/groovy-android-gradle-plugin

サンプルアプリケーション

melix/gr8confagenda

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}
  • ${projectDir}/app/build.gradleを編集する
buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.2'
    }
}

apply plugin: 'android'

repositories {
    jcenter()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        // workaround for http://stackoverflow.com/questions/20673625/android-gradle-plugin-0-7-0-duplicate-files-during-packaging-of-apk
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/groovy-release-info.properties'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile 'com.android.support:appcompat-v7:19.+'

    compile 'org.codehaus.groovy:groovy:2.4.0-beta-1:grooid'
    // the following dependency is necessary if you want JSON support
    compile ('org.codehaus.groovy:groovy-json:2.4.0-beta-1') { transitive = false }
}

apply plugin: 'groovy-android'
  • emulatorで動かそうとしたところ、以下のExceptionに遭遇。
com.android.build.gradle.internal.model.ApiVersionImpl cannot be cast to java.lang.Integer
java.lang.ClassCastException: com.android.build.gradle.internal.model.ApiVersionImpl cannot be cast to java.lang.Integer

以下のURLによると、IntelliJ IDEA 13.1.3では、サポートして無い模様。

https://code.google.com/p/android/issues/detail?id=70926

ここで、Android Studioへ移行。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.grimrose.android.groovy.app" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • app/src/main/res/layout/activity_main.xmlを修正。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/hello_groovy_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
  • org.grimrose.android.groovy.app.activityパッケージを作成。
  • MainActivity.groovyを作成。
package org.grimrose.android.groovy.app.activity

import android.app.Activity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import org.grimrose.android.groovy.app.R

class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)

        setContentView R.layout.activity_main

        def textView = findViewById(R.id.hello_groovy_text_view) as TextView
        textView.text = "Hello Groooovy!"
    }

    @Override
    boolean onCreateOptionsMenu(Menu menu) {
        menuInflater.inflate(R.menu.main, menu)
        true
    }

    @Override
    boolean onOptionsItemSelected(MenuItem item) {
        def id = item.itemId
        (id == R.id.action_settings) ?: super.onOptionsItemSelected(item)
    }

}
  • エミュレータで実行して確認。

今後の予定

  • データの登録、表示、修正、削除が出来る
  • Genymotionを使って見る。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment