标签: dynamic-feature-module

带有 DataBinding 和 R8 的 Android 动态特性

我在动态功能模块中使用 DataBinding 有问题。

当我无法生成与我的片段相关的绑定对象时 isMinifyEnabled = true

通常,我尝试使用此代码来执行此操作: val viewDataBinding: FragmentFeature1Binding = DataBindingUtil.inflate(inflater, layoutId, container, false) 但返回的inflate()值始终为 null,没有任何附加消息。我试图FragmentFeature1Binding直接使用膨胀布局,但我得到了相同的结果。

当我将片段移动到app模块中时,一切正常。

我的应用程序build.gradle.kts

android {
    compileSdkVersion(AndroidVersions.compileSdk)
    buildToolsVersion(AndroidVersions.buildTools)

    dataBinding.isEnabled = true

    defaultConfig {
        applicationId = ApplicationConfig.id
        minSdkVersion(AndroidVersions.minSdk)
        targetSdkVersion(AndroidVersions.targetSdk)
        versionCode = ReleaseVersions.versionCode
        versionName = ReleaseVersions.versionName
    }
    buildTypes {
        getByName(BuildTypes.debug) {
            isMinifyEnabled = true
            isShrinkResources = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            buildConfigField("Long", "API_TIMEOUT_IN_SECONDS", "30l")
        }
    }

    compileOptions {
        targetCompatibility = JavaVersion.VERSION_1_8
        sourceCompatibility = JavaVersion.VERSION_1_8
    }

    dynamicFeatures …
Run Code Online (Sandbox Code Playgroud)

android android-databinding android-r8 dynamic-feature-module

8
推荐指数
1
解决办法
1013
查看次数

当 minSdkVersion >= 26 时,为动态功能模块生成的绑定类出现 NullPointerException

我正在尝试将 MinSdkVersion 从 25 更新到 26,但在访问生成的绑定类的属性时收到 NullPointerExceptions。

以下代码片段显示哪行代码导致 NPE:

class DynamicActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding: ActivityDynamicBinding = DataBindingUtil.setContentView(
            this, R.layout.activity_dynamic)

        // Access a random binding to do a random thing
        binding.observableFieldsActivityButton.setOnClickListener { // This is the line it will crash
            Log.i("Tag", "I did a random thing, it works")
        }
        binding.viewmodelActivityButton.setOnClickListener {
            Log.i("Tag", "I did another random thing, it works")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

binding.observableFieldsActivityButton空。

这是 XML 文件:

class DynamicActivity : AppCompatActivity() { …
Run Code Online (Sandbox Code Playgroud)

android android-databinding dynamic-feature-module

7
推荐指数
1
解决办法
1067
查看次数

fragmentScenario Espresso 测试动态功能模块中的 Fragment

有没有人想出如何使用 fragmentScenario Espresso 测试(应用程序的 Activity 位于基本模块中)单独测试动态功能模块中的片段。

我已经克服了几个问题,例如关于样式不是 Theme.Appcompat 和 Android Studio 没有运行的withId抱怨,但现在在运行时抱怨它R.id在片段的布局中找不到。

android android-fragments android-studio android-espresso dynamic-feature-module

6
推荐指数
1
解决办法
152
查看次数

使用内部应用共享来测试按需动态功能模块不起作用

我正在尝试使用内部应用程序共享来测试我是否可以正确请求和下载按需动态功能模块。

当我尝试这个时,我可以看到它首先开始正确下载新功能,但它永远不会安装。我看到这些com.example is installed but certificate mismatch日志。这可能是导致问题的原因吗?我知道内部应用共享上传是“使用内部应用共享密钥自动重新签名的,该密钥是由 Google 自动为您的应用创建的”。来源。内部应用程序共享是否可能未正确签署按需 apk?

下载有问题的代码:

private const val ONDEMAND_FEATURE_MODULE_NAME: String = "ondemandfeature" // must match gradle module name

fun downloadOnDemandFeature(context: Context) {
    val request = SplitInstallRequest
            .newBuilder()
            .addModule(ONDEMAND_FEATURE_MODULE_NAME)
            .build()

    val splitInstallManager = create(context)

    // Initializes a variable to later track the session ID for a given request.
    var mySessionId = 0
    val listener = SplitInstallStateUpdatedListener { state ->
        if (state.sessionId() == mySessionId) {
            when (state.status()) {
                SplitInstallSessionStatus.DOWNLOADING -> {
                    val …
Run Code Online (Sandbox Code Playgroud)

android google-play-core dynamic-feature-module internal-app-sharing

6
推荐指数
1
解决办法
417
查看次数

跨动态功能的 Android ViewBindings

碰巧我有一个主应用程序模块

构建.gradle

    dynamicFeatures = [":myFeature"]

    viewBinding {
        enabled = true
    }
Run Code Online (Sandbox Code Playgroud)

AdroidManifest.xml

package="com.mydomain.testproject"
Run Code Online (Sandbox Code Playgroud)

使用我在整个应用程序 eq 中使用的一些常见布局: app/res/error_view.xml

我有动态特征文件

构建.gradle

    viewBinding {
        enabled = true
    }
Run Code Online (Sandbox Code Playgroud)

AdroidManifest.xml

package="com.mydomain.testproject.myFeature"
Run Code Online (Sandbox Code Playgroud)

myfeature_fragment.xml

    <include
        android:id="@+id/error_view"
        layout="@layout/error_view"
        android:visibility="gone" />
Run Code Online (Sandbox Code Playgroud)

我的特征片段.kt

binding = MyFeatureFragmentBinding.bind(view)
Run Code Online (Sandbox Code Playgroud)

问题来了。当我尝试访问binding.errorViewAS 时显示错误Cannot access class 'com.mydomain.testproject.myFeature.databinding.ErrorViewBinding'. Check your module classpath for missing or conflicting dependencies

奇怪的是,当我检查生成的MyFeatureFragmentBinding类时,它包含以下内容

  @NonNull
  public final View errorView;
Run Code Online (Sandbox Code Playgroud)

所以我想 AS 知道的比它显示给我的要多。我还发现在主模块的生成类中,原始ErrorViewBinding和 OFC 从那里可以完美地工作。

有人设法从另一个功能模块引用通用布局?
或者如何强制生成的公共视图绑定的类型?

android android-viewbinding dynamic-feature-module

6
推荐指数
1
解决办法
872
查看次数

在多模块项目中访问绑定适配器

我有一个多模块项目,其中应用程序模块包含我的绑定适配器,而我的功能模块取决于我的应用程序模块,因为它是动态功能模块。

应用程序(包含绑定适配器)---->动态功能模块(存在布局的地方)

我在所有模块中启用了数据绑定和 kapt。

我无法成功构建应用程序。Android Studio 给我以下错误,

error: cannot find symbol
import com.app.module1.databinding.FeatureItemBindingImpl;
Run Code Online (Sandbox Code Playgroud)

动态功能模块中的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="item"
            type="com.app.module1.views.FeatureItem" />

        <variable
            name="clickListener"
            type="com.app.module1.views.FeatureRecyclerViewAdapter.FeatureItemClickListener" />
    </data>

    <com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        app:cardElevation="3dp"
        android:id="@+id/card"
        android:onClick="@{(view) -> clickListener.onClickFeature(view, item)}">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/feature_image"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="7"
                android:scaleType="fitXY"
                app:set_image="@{item.featureImage}"
                tools:src="@color/green_200" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/feature_name"
                style="@style/TextAppearance.MyTheme.Body2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@{item.featureName.featureTitle}"
                tools:text="Order Pills" />
        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>
</layout>
Run Code Online (Sandbox Code Playgroud)

App模块中绑定Adapter:

@BindingAdapter("set_image")
fun AppCompatImageView.setImageToImageView(@DrawableRes drawable: Int) {
    this.setImageResource(drawable)
}
Run Code Online (Sandbox Code Playgroud)

java android kotlin android-databinding dynamic-feature-module

6
推荐指数
1
解决办法
860
查看次数

Android:在 featureModule 与库模块上导入资源

应用程序结构

featureModule = 应用插件:'com.android.dynamic-feature'

libraryModule = 应用插件:'com.android.library'

问题- 在功能模块中使用任何库资源都需要完整的包名称限定符,这最终会给功能模块中的数据绑定生成类带来问题。

描述- 我有上面的应用程序结构。当我尝试在任何 featureModule 中使用任何库资源时,它需要指定具有完整包名称限定符的资源,因为默认情况下我将功能资源 R 作为主要导入。

因此,对于任何 kotlin 类,假设活动代码如下所示

功能 1/TestActivity.kt

internal class TestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityTestBinding = DataBindingUtil.setContentView(this, R.layout.activity_test)
        binding.testName.text = getText(R.string.abc_test) // this resource is present in core-lib module
    }
}
Run Code Online (Sandbox Code Playgroud)

以上将导致未解决的引用错误,您可以通过使用全名而不是R.string.abc_test使用来解决该错误com.blah.core-lib.R.string.abc_test

R.layout.activity_test

<layout xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:id="@+id/test_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/spacing_medium"
        android:text="@{@string/abc_test}" />
</layout>

Run Code Online (Sandbox Code Playgroud)

但是对于 XML 数据绑定,它会产生更大的问题。由于R.layout.abc_test在功能模块中,因此无法访问库资源。我可以通过使用实用程序类并让实用程序类返回带有完整包名称限定符的字符串来解决这个问题,但是这个问题限制了数据绑定的许多功能,我不能直接在 XML 本身中使用资源。

PS:如果我将我的功能模块转换为库模块,这一切都很好。即使在代码级别,如果它是一个库模块,我也不会使用完整的包名限定符。

有没有人有更好的建议来解决功能模块的这个问题?

java android android-databinding dynamic-feature-module

6
推荐指数
0
解决办法
285
查看次数

带有 appbundle 和 PROGUARD 的动态功能不起作用

概述:
由于 proguard,我在从基本模块访问按需动态功能模块的活动时遇到问题。(很可能我猜)

描述:
我已经实现了一个带有应用程序包的按需动态功能模块并上传到 Play 商店。
使用自定义规则实现了 proguard。
从 Play 商店下载应用程序并在运行时访问该模块后,该模块将被下载。刚下载后,我就收到了从我的基本模块到该动态模块访问活动的调用。
我收到如下错误

...
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{xxx.yyyyyy.zzzzzz.stage/xxx.yyyyyy.zzzzzz.apphub.appview.view.AppHubActivity}:
java.lang.ClassNotFoundException: Didn't find class "xxx.yyyyyy.zzzzzz.apphub.appview.view.AppHubActivity"
on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file 
...
...
Run Code Online (Sandbox Code Playgroud)

仅供参考:
xxx.yyyyyy.zzzzzz 是我为了隐私而更改的包名称。

讽刺:
这整个代码在调试中运行良好,同时从本地的应用程序包访问它而没有 proguard

我已经尝试了下面的所有链接来克服这个问题,但不能。
1)https://issuetracker.google.com/issues/120517460
2)https://github.com/android/plaid/issues/764
3)java.lang.NoClassDefFoundError:解析失败:Lorg/apache/http/协议版本
4) https://issuetracker.google.com/issues/79478779
5) https://github.com/android/app-bundle-samples/issues/17

我也尝试了我们可以使用的所有类型的 proguard 文件,但仍然无能为力。
还保留了 proguard 中的两个类:基本和动态模块活动类,但没有成功。
希望在这里寻找解决方案。

更新:
不适用于 android OS 8,9,但适用于 android 10 的文件。

android runtime-error proguard android-app-bundle dynamic-feature-module

5
推荐指数
1
解决办法
794
查看次数

如何将动态功能模块与 BottomNavigationView 一起使用?

使用如下所示的导航图添加动态功能,并且可以与 viewPager2 或其他片段配合使用,但不能与BottomNavigationView.

布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/nav_host_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:menu="@menu/menu_bottom_nav" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</layout>
Run Code Online (Sandbox Code Playgroud)

BottomNavigationView应导航到动态功能模块的选项卡的导航图

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph_dashboard"
    app:startDestination="@id/dashboardFragment1">

    ...

    <!-- photos dynamic feature module-->
    <include-dynamic
        android:id="@+id/nav_graph_photos"
        android:name="com.abc.photos"
        app:graphResName="nav_graph_photos"
        app:moduleName="photos">
        <argument
            android:name="count"
            android:defaultValue="0"
            app:argType="integer" />
    </include-dynamic>

</navigation>
Run Code Online (Sandbox Code Playgroud)

由于BottomNavigationView其选项卡片段没有单独的后退堆栈,因此该类用于后退导航。

/**
 * Manages the various graphs needed for a [BottomNavigationView].
 *
 * This sample is …
Run Code Online (Sandbox Code Playgroud)

android bottomnavigationview android-architecture-navigation dynamic-feature dynamic-feature-module

5
推荐指数
1
解决办法
2663
查看次数

Android:PreviewView 导致动态功能模块崩溃

我正在尝试使用 androidx.camera 包创建一个按需功能模块。当活动膨胀包含 PreviewView 的布局时,我遇到崩溃。请注意,如果我只使用像 TextView 这样简单的东西,模块会正确加载并显示正确的布局,所以我不认为(希望无论如何)这是我的动态功能设置/下载的问题。

另外,如果我在基本应用程序模块中包含相机依赖项,​​那么这一切都可以正常工作,但是似乎我应该能够在动态功能模块中做到这一点。我不确定我错过了什么..以前有人遇到过这个吗?

功能模块的build.gradle:

apply plugin: 'com.android.dynamic-feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':app')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.71"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.core:core-ktx:1.3.1'

    def constraintLayoutVersion = "2.0.0-beta3"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"

    // CameraX core library
    def camerax_version = "1.0.0-beta06"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation …
Run Code Online (Sandbox Code Playgroud)

android dynamic-feature-module

5
推荐指数
2
解决办法
1839
查看次数