為さねば成らぬ

retia.verno@gmail.com

Nicoscape0: プライバシーポリシー

情報収集モジュール

当アプリでは、広告配信を目的として以下の広告配信事業者がご利用者の情報を自動取得する場合がございます。取得する情報、利用目的、第三者への提供等につきましては、以下の広告配信事業者のアプリケーション・プライバシーポリシーのリンクよりご確認ください。

Nend https://nend.net/privacy/sdkpolicy


Information gathering module

In this application, the following advertisement distribution businesses may automatically acquire user's information for the purpose of advertisement distribution. Regarding information to be acquired, purposes of use, provision to third parties, etc., please confirm from the application privacy policy link of the following advertisement distribution company.

Nend https://nend.net/privacy/sdkpolicy

Junit5を導入する

GitHub - mannodermaus/android-junit5: Testing with JUnit 5 for Android.

build.gradleに入れるだけ。

// build.gradle
buildscript {
  dependencies {
    classpath "de.mannodermaus.gradle.plugins:android-junit5:1.2.0.0"
  }
}
// app/build.gradle
apply plugin: "de.mannodermaus.android-junit5"

dependencies {
  // (Required) Writing and executing Unit Tests on the JUnit Platform
  testImplementation "org.junit.jupiter:junit-jupiter-api:5.2.0"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.2.0"

  // (Optional) If you need "Parameterized Tests"
  testImplementation "org.junit.jupiter:junit-jupiter-params:5.2.0"

  // (Optional) If you also have JUnit 4-based tests
  testImplementation "junit:junit:4.12"
  testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.2.0"
}

実際のテストコードはこちら。

class Test{
  @BeforeEach
  fun init(){
  }

  @Test
  fun test1(){
  }

  @Nested
  inner class HogeTest{
    @BeforeEach
    fun init(){
    }
  
    @Test
    fun test2(){
    }
  }
}

Junit4からJunit5へのmigrationで気をつけるのが

@Test

Junit4にもTestアノテーションがあるが、Junit5だとpackageが別。(org.junit.jupiter.api.Test) 間違っているとテストとして認識されない。

@BeforeEach

@Beforeは@BeforeEachへ変わっている。

JUnit5で一番良い(というかこれのために使っている)のは@Nested。 @Nestedをつけたinner classに含まれる@Testもテストが走るようになり、複数階層の入れ子が可能。 構造的にテストが書けるのでだいぶわかりやすくなる。 t-wadaさんのTDDワークショップ受けた時にテストを構造的に書くことの重要性を知ったので、@Nestedが入ってほんとよかった。

Viewのアスペクト比を動的に設定する

参考: stackoverflow.com

ImageViewを表示する時あらかじめアス比を指定したい。 この場合はImageViewをConstraintLayout直下に入れてdimentionRatioを設定する。

 <android.support.constraint.ConstraintLayout
            ...
            >

            <ImageView
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="h,1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

表示する画像のアス比がバラバラで、アス比はAPIで取得できる。constraintDimentionRatioを動的に設定したい。 その場合はConstraintSetを用いればよい。

val set = ConstraintSet()
set.clone(constraintLayout)
set.setDimensionRatio(R.id.image, "h,${dimensionRatio}:1")
set.applyTo(constraintLayout)

ConstraintLayout.LayoutParamsにパラメータがあったけど、いじっても何も起きませんでした。

なんとなしで使ってたDataBinding

なんとなく使ってた結果最近はまったDataBinding.

onClick

developer.android.com

  android:onClick="@{(v) -> v.isVisible() ? doSomething() : void}"

公式にもあるが、onClickを設定する場合はこの形にする。(v) でvには()をつける。 ()つけない

  android:onClick="@{v -> v.isVisible() ? doSomething() : void}"

だと、Clickイベントがちゃんととれたりとれなかったりした。謎なので公式通りにしようという知見を得た。

Unused Resources RemoverでdryRun実行する

github.com

Unused Resources Remove for Android めっちゃ便利ですよね 。 今までの類似ツールと比べて誤判定少なくてありがたいです。

dryRunする場合にはbuild.gradleにオプションを指定します。

unusedResourcesRemover {
  dryRun = true
}

ただこれだとdryRun<->Runを切り替えるためにbuild.gradleの書き換えが必要です。 毎回書き換えなくていいように以下のタスクを追加しました。

project.task("checkUnusedResources").doLast {
    unusedResourcesRemover.dryRun = true
    removeUnusedResources.execute()
}
./gradlew checkUnusedResources

以上です。

prerenderを使うとエラーが出る

最近ちょっとReact Rails触ってる。 まずはServer Side Renderingやろうとprerenderをonにしてみたのだけど、エラーが出る。

Encountered error "#<ExecJS::ProgramError: TypeError: Cannot read property 'toLowerCase' of undefined>"

指定していたcomponent名が間違っていました。わかんねぇ・・・