Skip to content

Instantly share code, notes, and snippets.

@kaiinui
Last active March 3, 2017 03:54
Show Gist options
  • Save kaiinui/5eb54f34ade5ee5f5492 to your computer and use it in GitHub Desktop.
Save kaiinui/5eb54f34ade5ee5f5492 to your computer and use it in GitHub Desktop.
Android開発いろいろ

EventBus

EventBus は必ず onStart()onStop() で呼ぶ。バックグラウンドで色々動かれて困る。 また、onStop() では super.onStop() よりも 先に 呼ぶ。

@Override
protected void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
protected void onStop() {
    EventBus.getDefault().unregister(this);
}

proguard の設定

忘れがちですが、設定しないと最適化オプション付けたときに死ぬ

-keepclassmembers class ** {
    public void onEvent*(**);
}

# Only required if you use AsyncExecutor
-keepclassmembers class * extends de.greenrobot.event.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

メインスレッドで呼んでほしい。

onEvent だと Post されたスレッドで呼び出されます。onEventMainThread だと必ずメインで呼ばれる。

参考

Tablet 判定用のフラグを Resource で定義する

res/values/bools.xml

<bool name="isTablet">false</bool>

res/values-sw600dp/bools.xml

<bool name="isTablet">true</bool>
public class SizeUtil {
    public static boolean isTablet(Context context) {
        return context.getResources().getBoolean(R.bool.isTablet);
    }
}

SecurePreferences

https://github.com/scottyab/secure-preferences

SharedPreferences はどこからでも読めるので、せめて get/set 時に暗号化を介してくれる SecurePreferences を使う。

あと、Android の暗号化関連はバグ多いので Facebook 製の Conceal 等のライブラリを使った方が良い。

https://github.com/facebook/conceal

Fragment の引数渡し

必ず Fragment.setArguments() を使って引数をやりとりする。 Fragment はいつでも Android に殺されたり再利用されたりする。その際、このメソッドで渡した Bundle のみがリストアされる。

一番使いやすいのは、newInstance() という名の static メソッドを定義して、その中で setArguments() する。

public class FooFragment extends Fragment {
    public static String EXTRA_ARG1 = "arg1";
    public static String EXTRA_ARG2 = "arg2";

    public static FooFragment newInstance(String arg1, int arg2) {
        final FooFragment fragment = new FooFragment();
        
        final Bundle args = new Bundle();
        args.putString(EXTRA_ARG1, arg1);
        args.putString(EXTRA_ARG2, arg2);
        fragment.setArguments(args);
        
        return fragment;
    }
}

Activity の Intent もこんな感じで、呼び出しのための Intent を返す static メソッドを作っておくと便利。

Google Play Services の分割導入

死ぬほど多いメソッドを緩和。

compile 'com.google.android.gms:play-services-base:6.5.87'

ホーム画面のカメラで起動出来るようにする

普通の Intent で動いてて、Action はこれ。

android.media.action.STILL_IMAGE_CAMERA
<intent-filter>
    <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

URL スキームと Intent

HTTP のスキームを使うのと、独自スキームを使うのと二種類ある。

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="hogehoge"
        android:host="hoge"
        android:pathPrefix="/" />
</intent-filter>

以下の URL か、これより下層の URL を開くと、アプリが起動する。

hogehoge://hoge/

これを HTTP にすると、

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http"
        android:host="huga.com"
        android:pathPrefix="/hoge" />
</intent-filter>

以下の URL を開く際に、どこアプリで開くか?のアプリ選択ダイアログが出る。(たいていブラウザかこのアプリ)

http://huga.com/hoge

また、URL の指定方法にも色々あり、

intent://hoge/#Intent;scheme=hogehoge;package=com.kaiinui.hogehoge;end

と、細かく指定出来る記法がある。(https://developer.chrome.com/multidevice/android/intents)

location.href に突っ込んでも行けるが、iframe.src に突っ込んでも何も起きない。(上記参考 URL 参照)

汎用てきなやつ

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

compile 'com.orhanobut:logger:1.3'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
compile('de.keyboardsurfer.android.widget:crouton:1.8.5@aar') {
  exclude group: 'com.google.android', module: 'support-v4'
}
compile 'com.orhanobut:hawk:1.6'
compile 'com.malinskiy:superrecyclerview:1.1.0'
compile('com.joanzapata.android.asyncservice:android-asyncservice:0.0.5@aar') { transitive = true }

// Supports
compile "com.android.support:appcompat-v7:21.0.0"
compile "com.android.support:support-v4:21.0.0"
compile 'com.android.support:recyclerview-v7:+'

// Material Design
compile 'com.jpardogo.materialtabstrip:library:1.0.9'
compile 'com.cocosw:bottomsheet:0.+@aar'
compile 'com.github.navasmdc:MaterialDesign:1.+@aar'
compile 'com.nispok:snackbar:2.10.5'

他便利なさーびす

  • Crashlytics
  • Mixpanel
  • Yozio
  • AppSee Analytics
  • FB SDK (便利...?)

外部のやつ

  • Searchman ASO
  • AppAnnie

MaterialDesignLibrary にも Snackbar はありますがなぜか秒数指定が出来ないので、'com.nispok:snackbar:2.10.5' おすすめ

ぷろがーど

-keepclassmembers class ** {
    public void onEvent*(**);
}

# Only required if you use AsyncExecutor
-keepclassmembers class * extends de.greenrobot.event.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

Espresso + Roboletric

classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'

apply plugin: 'android-unit-test'
// @see http://qiita.com/Nkzn/items/d5f30bfe31bdf329860b
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0')
androidTestCompile('com.android.support.test:testing-support-lib:0.1')
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-core:1.1'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile 'org.hamcrest:hamcrest-integration:1.1'

testCompile('org.robolectric:robolectric:2.4') {
    exclude module: 'classworlds'
    exclude module: 'commons-logging'
    exclude module: 'httpclient'
    exclude module: 'maven-artifact'
    exclude module: 'maven-artifact-manager'
    exclude module: 'maven-error-diagnostics'
    exclude module: 'maven-model'
    exclude module: 'maven-project'
    exclude module: 'maven-settings'
    exclude module: 'plexus-container-default'
    exclude module: 'plexus-interpolation'
    exclude module: 'plexus-utils'
    exclude module: 'wagon-file'
    exclude module: 'wagon-http-lightweight'
    exclude module: 'wagon-provider-api'
}

Play Services

compile 'com.google.android.gms:play-services-base:7.0.0'
compile 'com.google.android.gms:play-services-gcm:7.0.0'
compile 'com.google.android.gms:play-services-analytics:7.0.0'
compile 'com.google.android.gms:play-services-appindexing:7.0.0'

ぷろがーど

// EventBus

-keepclassmembers class ** {
    public void onEvent*(**);
}

# Only required if you use AsyncExecutor
-keepclassmembers class * extends de.greenrobot.event.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

// Google Play

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

// asyncserivce

-dontwarn com.joanzapata.android.asyncservice.**
-keep @com.joanzapata.android.asyncservice.api.annotation.AsyncService class *
-keep class **Injector
-keepnames class **Injector
-keepnames class * {
    @com.joanzapata.android.asyncservice.api.annotation.InjectService *;
}
-keepnames class * {
    @com.joanzapata.android.asyncservice.api.annotation.OnMessage *;
}

ライブラリつくるとき

classpath 'com.github.dcendents:android-maven-plugin:1.2'

apply plugin: 'android-maven'

com.android.application -> com.android.library にかえる。

apply plugin: 'com.android.library'

// applicationId "com.hogehoge.hoge" // 消す

https://jitpack.io/ 使うのが一番はやい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment