You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo Nitro Modules集成.AAR SDK与Dagger Hilt问题求助

在Expo Nitro Modules中集成带Dagger Hilt的AAR SDK解决方案

针对你遇到的三个核心问题及清单合并错误,整理如下实操方案:


1. Manifest配置:接收器的正确放置位置

接收器应该放在Nitro Module的AndroidManifest.xml中,但绝对不能在模块的<application>标签中设置android:name属性——这是导致你遇到清单合并错误的直接原因。主应用的MainApplication已经占据了这个属性,多个Application定义会触发合并冲突。

修改后的Nitro模块Manifest示例:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"> <!-- 必须添加tools命名空间 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application> <!-- 移除android:name属性 -->
        <receiver
            android:name="br.com.sdk.BootReceiver"
            android:enabled="true"
            android:exported="true"
            tools:ignore="ExportedReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

2. Dagger Hilt依赖注入配置

Nitro模块不能独立定义Hilt的Application类,必须依附于主应用的Hilt容器,步骤如下:

  • 主应用端配置:
    在你的Expo主应用的MainApplication类上添加@HiltAndroidApp注解,确保主应用的build.gradle包含Hilt插件和依赖:
    plugins {
        id 'com.android.application'
        id 'com.google.dagger.hilt.android'
        // 其他插件...
    }
    
    dependencies {
        implementation 'com.google.dagger:hilt-android:2.48'
        kapt 'com.google.dagger:hilt-android-compiler:2.48'
        // 其他依赖...
    }
    
  • Nitro模块端配置:
    模块的build.gradle只需要添加Hilt依赖,不需要添加Hilt插件:
    dependencies {
        implementation 'com.google.dagger:hilt-android:2.48'
        kapt 'com.google.dagger:hilt-android-compiler:2.48'
        // 你的AAR依赖...
    }
    
    在模块中创建Hilt模块提供SDK实例,指定安装到主应用的单例组件:
    @Module
    @InstallIn(SingletonComponent::class)
    class SdkModule {
        @Provides
        fun provideSdkInstance(): YourSdkClass {
            return YourSdkClass()
        }
    }
    
  • 接收器中获取Hilt实例:
    如果BootReceiver需要使用SDK实例,通过Hilt EntryPoint避免强转主应用:
    @EntryPoint
    @InstallIn(SingletonComponent::class)
    interface SdkEntryPoint {
        fun getSdk(): YourSdkClass
    }
    
    class BootReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            context?.let {
                val entryPoint = EntryPointAccessors.fromApplication(
                    it,
                    SdkEntryPoint::class.java
                )
                val sdk = entryPoint.getSdk()
                sdk.initialize()
            }
        }
    }
    

3. AAR导入的正确方式

无需手动复制到node_modules,通过Nitro模块的build.gradle配置本地仓库和依赖即可:

  1. 将AAR文件放在Nitro模块的android/libs/目录下;
  2. 在模块的android/build.gradle中添加本地仓库声明和依赖:
    repositories {
        flatDir {
            dirs 'libs'
        }
        // 其他仓库...
    }
    
    dependencies {
        // 方式1:直接引用文件
        implementation files('libs/AndroidSDK.aar')
        // 方式2:如果AAR有依赖,使用name+ext形式
        // implementation(name: 'AndroidSDK', ext: 'aar')
        // 其他依赖...
    }
    
  3. 确保Nitro模块的package.json中配置正确的main和android字段,Expo预构建时会自动识别并打包该模块的依赖。

解决清单合并错误

你遇到的错误根源是Nitro模块定义了自己的Application类,和主应用的MainApplication冲突。按照上述步骤移除模块Manifest中的android:name属性后,冲突会自动解决,不需要添加tools:replace="android:name"(除非主应用和其他模块还有其他Manifest属性冲突)。

内容的提问来源于stack exchange,提问作者Vitor Rafael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 16:06:08