Android: Can you add activities to the Instant App

2019-01-29 06:47发布

问题:

I'm trying to create an Instant App, but wanted to know if I can add an activity to my Instant App module.

When creating an Instant App module, the android studio wizard doesn't give you the option to add an activity either and was wondering if it is just supposed to be empty. I've tried adding an activity manually, but ran into some build issues.

Before I give up I just wanted to ask if an empty Instant App module is how it's designed to be and only supposed to rely on the base feature/feature modules as a dependency.

EDIT: When I try to add an activity to the com.android.instantapp module, it cant find the R resource file. Also getting a build error :

Error:Execution failed for task :my-instantapp:packageDebugInstantAppBundle'. > 
Expected configuration ':my-instantapp:debugCompileClasspath' to contain exactly 
one file, however, it contains no files.

回答1:

You can't add code to a module using the com.android.instantapp plugin.

From the documentation:

When you build your instant app, this module takes all of the features and creates Instant App APKs. It does not hold any code or resources; it contains only a build.gradle file and has the com.android.instantapp plugin applied to it.

Given that all code can be shared through com.android.feature modules with it's not necessary to have code in the instantapp module.

For reusability it is recommended to have little to no code in modules that apply the com.android.application plugin, but to add dependencies to the projects like this:

dependencies {
    implementation project(':features:hello')
    implementation project(':features:bye')
    implementation project(':features:base')
}

And host code within modules that rely on the com.android.feature plugin.

These will be compiled like this:

|    depending module     |     compiled to     |
|-----------------------------------------------|
| com.android.application | application apk     |
| com.android.instantapp  | apk for each module | <- then packed into a zip file