http://connpass.com/event/6979/
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
サンプルアプリケーション
-
Android SDKがインストールされていなかったので、インストール
- http://developer.android.com/sdk/index.html
- GET THE SDK FOR AN EXISTING IDEからダウンロード
- 解凍後、~/android-sdkに移動。
-
IntelliJ IDEAにてプロジェクトを作成
- Create New Project
- Android -> Gradle: Android Module
- 以降のversionに合わせるため19にして進める。
- appcompact-v7にてエラーになったので、以下のURLを参考に解決
- How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA
- http://stackoverflow.com/questions/18025942/how-do-i-add-a-library-android-support-v7-appcompat-in-intellij-idea
-
${projectDir}のbuild.gradleを編集する。
// 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へ移行。
-
Android Studioのインストール
-
Import Projectで上記で作ったProjectをインポート。
-
AndroidManifest.xmlを修正。
<?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を使って見る。