Android Studio キャッシュの種類とそれぞれのクリーンの操作

ビルド速度が遅いので、


org.gradle.caching=true

としたら、署名付きビルドが更新されない。

「キャッシュ」のせいなのは分かるが、

いろいろ種類がありそうだよな、

Android Studio の「キャッシュ」って。

 

🤔 キャッシュの種類

ChatGPT に聞くと、3つあるそうです。

もちろん信用はしてないです。

IDE キャッシュ
コード補完、シンタックスハイライト、プロジェクトインデックス、ビルド設定など

Gradle キャッシュ
依存ライブラリ、ビルドキャッシュ(Gradleがダウンロードしたファイルなど)

ビルドキャッシュ
build/ フォルダ内の成果物やコンパイル済みファイルなど

そして、今回の調べるきっかけとなった


org.gradle.caching=true

は「ビルドキャッシュ」のようです。

 

🤔 まとめ

いきなり、分かったことをまとめます。


org.gradle.caching=true

は、ビルドキャッシュ。

キャッシュの範囲は、


ビルドキャッシュ < Gradle キャッシュ < IDE キャッシュ

のようなので、

キャッシュを消したいときは、


[Build] - [Clean Project]


[Build] - [Rebuild Project]


./gradlew clean


rm -rf ~/.gradle/caches


[File] → [Invalidate Caches / Restart]


rm -rf ~/.gradle

を上から順番に試していくのがいいと思います。

もちろん信用はしてません。

公式読むと良いです。

👉 Build Cache - docs.gradle.org

 

🧑🏻‍💻 おまけ

今回のような場合、

Gradle タスクに追加しておく方法もあるらしい。


tasks.withType(Sign).configureEach {
    outputs.cacheIf { false }
}

記述場所としては、

基本的に、android {} の外、末尾に記述する。

マルチモジュールなら subprojects {} 内に。


この操作を完了する権限がありません [OR-CAC-07]

これなんなのか。

聞いてみました。


スルーかな。

仕方ないので関連ポストを探す。


【Kotlin】Android アプリ 課金実装時の ProductDetails や Purchase の内容

公式ドキュメントの解読には骨が折れる。

詳細どんなものが含まれているのか。

Timber ですが、純正 Log でもいけるので置き換えてどうぞ。

👉 JakeWharton/timber: A logger with a small, extensible API which provides utility on top of Android's normal Log class.


Jetpack Compose で Activity を取得する

なんとなくまだ古くさい感じはありますが。


val context = LocalContext.current


fun Context.getActivityOrNull(): Activity? {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }    
    return null
}


fun Context.findActivity(): Activity {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    throw IllegalStateException("Permissions should be called in the context of an Activity")
}

👉 android - How to get activity in compose - Stack Overflow


android.intent.action.MY_PACKAGE_REPLACED をBorodcastReceiver で反応させるテスト

Debug でなく Run ボタンでいけてたような気がするが
セキュリティの絡みか、今では反応がない。

なので、

コマンドラインで adb から。


❯ adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED
Broadcasting: Intent { act=android.intent.action.MY_PACKAGE_REPLACED flg=0x400000 }

Exception occurred while executing 'broadcast':
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MY_PACKAGE_REPLACED from pid=6153, uid=2000

ダメですか。

またセキュリティですか。

 

🤔 解決法

一度、ビルドして、 apk をインストールしたらいけた。

Go to Build -> Build APK, and note the location that the .apk is stored.

Then, run in terminal:

adb install -r debugapp.apk

This will trigger the MY_PACKAGE_REPLACED intent, since newer Android SDKs only allow the system to broadcast it.

👉 Android: Broadcast ACTION_MY_PACKAGE_REPLACED never received - Stack Overflow

-r」いるんかね ? いらなくね ?

 

🤔 まとめ

今後以下でやろう。


❯ ./gradlew assembleDebug  
                         
> Configure project :app
BUILD SUCCESSFUL in 59s
42 actionable tasks: 21 executed, 21 up-to-date

❯ find . -name "*.apk" | fzf | xargs adb install

Performing Streamed Install
Success

❯ adb shell am start -a android.intent.action.MAIN -n YOUR_PACKAGE/.TARGET_ACTIVITY 

Starting: Intent { act=android.intent.action.MAIN cmp=YOUR_PACKAGE/.TARGET_ACTIVITY }

当然、バージョン番号は上げておかなければなりません。

👉 junegunn/fzf: :cherry_blossom: A command-line fuzzy finder