2018年5月13日日曜日

[Android Things][RPi3] User-Space Driverの作成と使い方

Android Things version 1.0が公開されました
Android Things 1.0 Features and APIs

かなり盛り上がってきているAndroid Thingsを今更ながらAndroidもほとんど使ったことない私が触ってみました

チュートリアルを一通り行って、最後のページにあるUser-Space Driverというのが気になりました
Googleが用意してくれたDriverの使い方は書かれているが、
どうやってDriverを作るのか疑問に思ったため、GoogleのチュートリアルにあるButton Driverを再度作ることをしてみました

以下、文字だけのため分かりづらいと思いますが。。

1. Android Thingsのプロジェクトを作成する

Libraryだけを作れないようなので普通に空のActivityを作成します




2. Android Libraryの作成

File -> New ModuleからAndroid Libraryを作成します
3. コードの編集

build.gradle(Module: button-driver)に以下のようにcompileOnlyとしてAndroid Thingsを付け加えます

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compileOnly 'com.google.android.things:androidthings:+'
}
そのままbutton_driverフォルダ以下にコードを加えていきます
コードの内容ですが、Googleのリポジトリを確認してください
私のgithubアカウントにも同等のコードを置いてあります
以下は完成したUser-Space Driverの使い方です
4. Libraryと等価のプロジェクトにあるアプリで使うには
同プロジェクトにある空のActivityにサンプルアプリを作ります
その時に、implementation projectを加えることで作成したlibraryの使用が出来ます
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compileOnly 'com.google.android.things:androidthings:+'
    implementation project(':button_driver')
}
5. Libraryを作ったプロジェクトと別のプロジェクトで使うには
こっちがメインだと思います
A. Import Module
File -> New -> Import Moduleで上記で作成したフォルダを指してModuleをコピーして使う
この際に、settings.gradle (Project Settings)に以下を記述する必要があります
include ':app', ':button-driver'
B. githubなどに置きGradleに記載
出来上がったコードはネットワーク上のどこかに置くと思います (ex:github, 社内リポジトリなど)
toolを使い、aarファイルを作成します
まずbuild.gradle(Module:button_driver)にuploadArchiveを定義します
apply plugin: 'maven'

def repo = new File(rootDir, "repository")

uploadArchives {
    repositories {
        mavenDeployer {
            repository url: "file://${repo.absolutePath}"
            pom.version = '1.0.0'
            pom.groupId = 'me.yoshio'
            pom.artifactId = 'button-driver'
        }
    }
}
プロジェクトフォルダー以下で、./gradlewを実行します
出来上がったrepositoryフォルダ以下をgithubなど特定の箇所に配置します
次にそのDriverを使いたいプロジェクトを開きます
build.gradle(Module: app)を開き、以下を記載 urlは人により、今回は上記のリポジトリを指しています
repositories {
    maven { url 'https://github.com/yoshio916/ThingsButtonSample/raw/master/repository' }
}
implementationは、上記のuploadArchivesで記載した内容になります
version:groupId:version
上記のような書き方になります
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compileOnly 'com.google.android.things:androidthings:+'
    implementation 'me.yoshio:button-driver:1.0.0'
}
次に、settings.gradleにbutton-driverを書き加えて終わりです
include ':app', ':button_driver'

以上です

今回のことを記載したリポジトリは以下です
https://github.com/yedama/ThingsButtonSample