Android デベロッパー > ドキュメント > ガイド > 主要トピック > Architecture Components > アプリの起動 が英語だったので翻訳しました。
元ページ:App Startup
アプリの起動(** Android Jetpack**)
The App Startup library provides a straightforward, performant way to initialize components at application startup. Both library developers and app developers can use App Startup to streamline startup sequences and explicitly set the order of initialization.
Instead of defining separate content providers for each component you need to initialize, App Startup allows you to define component initializers that share a single content provider. This can significantly improve app startup time.
App Startup は、アプリの起動時にコンポーネントを初期化(イニシャライズ)する簡単で永続的な方法を提供します。ライブラリ開発者とアプリ開発者の両社とも、起動シーケンスを合理化し、また初期化の順番を明示的に設定するために App Startup を使用することができます。
初期化する必要のあるコンポーネントごとに別々のコンテントプロバイダを定義する代わりに、App Startup を使用すると単一のコンテンツプロバイダを共有するコンポーネントの Initializer を定義することができるようになります。これにより、アプリの起動時間を劇的に改善することができます。
セットアップ
To use Jetpack Startup in your library or app, add the following to your Gradle file:
Jetpack Startup をライブラリやアプリで使用するためには、下記のコードをGradleファイルに追記します:
dependencies {
implementation "androidx.startup:startup-runtime:1.0.0"
}
アプリ起動時に初期化するコンポーネント
Initialize components at app startup
Apps and libraries often rely on having components initialized right away when the app starts up. You can meet this need by using content providers to initialize each dependency, but content providers are expensive to instantiate and can slow down the startup sequence unnecessarily. Additionally, Android initializes content providers in an undetermined order. App Startup provides a more performant way to initialize components at app startup and explicitly define their dependencies.
To use App Startup to initialize components automatically at startup, you must define a component initializer for each component that the app needs to initialize.
アプリやライブラリはアプリが起動すると直ちに初期化されるコンポーネントを持つことに依拠することがよくあります。このニーズに対して各依存性を初期化するためのコンテンツプロバイダを使用することで対処することもできますが、コンテンツプロバイダは初期化コストが高く、起動シーケンスを不必要に遅延させることがあります。それに加えてAndroidはコンテンツプロバイダを順不同で初期化してしまいます。App Startup はアプリ起動時にコンポーネントを初期化し依存性を明示的に定義するためのより効果的な方法になります。
起動時にコンポーネントを自動的に初期化するため App Startup を使用するには、アプリが初期化する必要のある各コンポーネントについてコンポーネントの Initializer を定義する必要があります。
コンポーネント Initializer を実装する
Implement component initializers
You define each component initializer by creating a class that implements the
Initializer<T>
interface. This interface defines two important methods:
- The
create()
method, which contains all of the necessary operations to initialize the component and returns an instance ofT
.- The
dependencies()
method, which returns a list of the otherInitializer<T>
objects that the initializer depends on. You can use this method to control the order in which the app runs the initializers at startup.
コンポーネント Initializer は Initializer インターフェースをimplementするクラスを作成することで定義します。このインターフェースは次の重要な2つのメソッドを定義します:
create()
メソッド:コンポーネントを初期化するためのすべての必要な操作を持ち、T
のインスタンスを返します。dependencies()
:Initializer が依存する他の `Initializer<T>
オブジェクトのリストを返します。起動時にアプリが実行する Initializer の順番を制御するためにこのメソッドを使用することができます。
For example, suppose that your app depends on
WorkManager
and needs to initialize it at startup. Define aWorkManagerInitializer
class that implementsInitializer<WorkManager>
:
例として、アプリが WorkManager に依存していて、アプリの起動時に初期化する必要がある場合を考えてみましょう。この場合、Initializer<WorkManager>
をimplementしている WorkManagerInitializer
クラスを次のように定義します:
The
dependencies()
method returns an empty list becauseWorkManager
does not depend on any other libraries.Suppose that your app also depends on a library called
ExampleLogger
, which in turn depends onWorkManager
. This dependency means that you need to make sure that App Startup initializesWorkManager
first. Define anExampleLoggerInitializer
class that implementsInitializer<ExampleLogger>
:
WorkManager
は他のどのライブラリにも依存しないため、dependencies()
メソッドはからのリストを返します。
アプリが ExampleLogger
と呼ばれる、WorkManager
に依存しているライブラリにも依存性を持つとしましょう。この依存性により、App Startup は必ず最初に WorkManager
を初期化するようにしなければなりません。Initializer<ExampleLogger>
を implement する ExampleLoggerInitializer
クラスを次のように定義します:
Because you include
WorkManagerInitializer
in thedependencies()
method, App Startup initializesWorkManager
beforeExampleLogger
.
WorkManagerInitializer
を dependencies()
メソッドに含めるため、App Startup は ExampleLogger
の前に WorkManager
を初期化します。
Note: If you previously used content providers to initialize components in your app, make sure that you remove those content providers when you use App Startup.
注意:アプリ内でコンポーネントを初期化するためにコンテンツプロバイダを既に使用している場合、App Startup をす要する際には必ずこれらのコンテンツプロバイダを削除してください。
マニフェストの設定
App Startup includes a special content provider called
InitializationProvider
that it uses to discover and call your component initializers. App Startup discovers component initializers by first checking for a<meta-data>
entry under theInitializationProvider
manifest entry. Then, App Startup calls thedependencies()
methods for any initializers that it has already discovered.This means that in order for a component initializer to be discoverable by App Startup, one of the following conditions must be met:
- The component initializer has a corresponding
<meta-data>
entry under theInitializationProvider
manifest entry.- The component initializer is listed in the
dependencies()
method from an initializer that is already discoverable.
App Startup は InitializationProvider
と呼ばれる、あなたの定義したコンポーネント Initializer を発見し呼び出すために使用する特別なコンテンツプロバイダーを含んでいます。App Startup はマニフェストに記載された InitializationProvider
下の <meta-data>
エントリを最初にチェックしてコンポーネント Initializer を見つけ出します。それから App Startup は既に見つけ出した Initializer 毎に dependencies()
メソッドを呼び出します。
このため、App Startup がコンポーネント Initializer を順番に見つけられるよう、次のいずれかの状態になる必要があります:
- コンポーネント Initializer がマニフェストの
InitializationProvider
下にある<metapdata>
エントリと一致する - コンポーネント Initializer が既に見つけられた Initializer から呼ばれた
dependencies()
メソッドのリストに格納されている
Consider again the example with
WorkManagerInitializer
andExampleLoggerInitializer
. To make sure App Startup can discover these initializers, add the following to the manifest file:
もう一度 WorkManagerInitializer
と ExampleLoggerInitializer
の例を考えてみましょう。App Startup がこれらの Initializer を確実に見つけ出せるよう、マニフェストファイルに次の内容を追加します:
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- This entry makes ExampleLoggerInitializer discoverable. -->
<meta-data android:name="com.example.ExampleLoggerInitializer"
android:value="androidx.startup" />
</provider>
You don't need to add a
<meta-data>
entry forWorkManagerInitializer
, becauseWorkManagerInitializer
is a dependency ofExampleLoggerInitializer
. This means that ifExampleLoggerInitializer
is discoverable, then so isWorkManagerInitializer
.The
tools:node="merge"
attribute ensures that the manifest merger tool properly resolves any conflicting entries.
WorkManagerInitializer
は ExampleLoggerInitializer
の依存関係のため、WorkManagerInitializer
については <meta-data>
エントリを追加する必要はありません。つまり、ExampleLoggerInitializer
を見つけることができれば、すなわちWorkManagerInitializer
も見つけられます。
tools:node="merge"
という引数により、 マニフェストマージツール はすべての競合を正しく解決することができるようになります。
lintの実行
The App Startup library includes a set of lint rules that you can use to check whether you've defined your component initializers correctly. You can perform these lint checks by running
./gradlew :app:lintDebug
from the command line.
App Startup ライブラリはコンポーネント Initializer を正しく定義できているかチェックするための、一連の lint ルールを含んでいます。これらの lint はコマンドラインから ./gradlew:app:lintDebug
を実行することで動かすことができます。
コンポーネントを手動で初期化する
Manually initialize components
Ordinarily when you use App Startup, the
InitializationProvider
object uses an entity calledAppInitializer
to automatically discover and run component initializers at application startup. However, you can also useAppInitializer
directly in order to manually initialize components that your app doesn't need at startup. This is called lazy initialization, and it can help minimize startup costs.You must first disable automatic initialization for any components that you want to initialize manually.
App Startup を使用する際は通常、InitializationProvider
オブジェクトはアプリ起動時に自動的にコンポーネント Initializer を発見して実行するため、AppInitializer というエンティティを使用します。しかし、アプリ起動時には必要でないコンポーネントを手動で初期化するために、AppInitializer
を直接使用することも可能です。これは lazy initialization(遅延イニシャラゼーション) と呼ばれ、起動コストを最小化するのに役立ちます。
手動で初期化したいコンポーネントについては、すべて最初に自動での初期化を無効化する必要があります。
自作コンポーネントの自動イニシャライズを無効化する
Disable automatic initialization for an individual component
To disable automatic initialization for a single component, remove the
<meta-data>
entry for that component's initializer from the manifest.For example, adding the following to the manifest file disables automatic initialization for
ExampleLogger
:
単一のコンポーネントに対し自動イニシャライズを無効化するには、マニフェストからそのコンポーネントの Initializer を定義した <meta-data>
エントリを削除します。
例として、ExampleLogger
の自動イニシャライズを無効化する場合、下記をマニフェストに追記します:
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data android:name="com.example.ExampleLoggerInitializer"
tools:node="remove" />
</provider>
You use
tools:node="remove"
in the entry instead of simply removing the entry in order to make sure that the merger tool also removes the entry from all other merged manifest files.
マージツールが他のマージされた全てのマニフェストファイルから同じエントリを確実に削除するよう、単にエントリを削除するのではなく tools:node="remove"
を使用してください。
Note: Disabling automatic initialization for a component also disables automatic initialization for that component's dependencies.
注意: あるコンポーネントの自動イニシャライズを無効化すると、そのコンポーネントと依存関係にある他のコンポーネントも自動イニシャライズが無効化されます。
全コンポーネントの自動イニシャライズを無効化する
Disable automatic initialization for all components
To disable all automatic initialization, remove the entire entry for
InitializationProvider
from the manifest:
自動イニシャライズを全て無効化するには、マニフェストから InitializationProvider
についてのすべてのエントリを削除します。
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
コンポーネント Initializer を手動で呼び出す
Manually call component initializers
If automatic initialization is disabled for a component, you can use
AppInitializer
to manually initialize that component and its dependencies.For example, the following code calls
AppInitializer
and manually initializesExampleLogger
:
あるコンポーネントの自動イニシャライズが無効化されていても、AppInitializer
を使用してそのコンポーネントや依存関係のコンポーネントを初期化することができます。
例えば AppInitializer
を読んで ExampleLogger
を手動で初期化するコードは次のようになります:
As a result, App Startup also initializes
WorkManager
becauseWorkManager
is a dependency ofExampleLogger
.
結果的には、WorkManager
は ExampleLogger
の依存関係のため、App Startup は WorkManager
も初期化します。
依存関係のあたりが怪しいかも…
アプリ → WorkManager
アプリ → ExampleLogger
ExampleLogger → WorkManager
と理解したのですが、違うようでしたらご指摘いただけますと有難いです。