确保共享模块已经正确安装并在工作区中注册。
在 Android 项目的 build.gradle 文件中添加以下代码:
android {
...
buildTypes {
...
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.all {
// Change this to the APK name of your shared module
def sharedModuleName = "shared-module-release"
// Change these to match the package names of your app and shared module
def applicationPackageName = "com.example.myapp"
def sharedModulePackageName = "com.example.sharedmodule"
if (outputFileName.contains(applicationPackageName)) {
outputFileName = outputFileName.replace(applicationPackageName, sharedModulePackageName)
}
// Check if the shared module can be found
def newFile = new File("${projectDir}/../node_modules/${sharedModulePackageName}/${sharedModuleName}.apk")
if (newFile.exists()) {
// If the shared module is found, add it to the APK
println("Adding shared module ${sharedModulePackageName}:${sharedModuleName} to ${outputFileName}")
doLast {
copy {
from(newFile) {
into("${outputFile.parent}/assets/${sharedModuleName}")
rename { file -> 'app-release.apk' }
}
}
}
} else {
// If the shared module cannot be found, throw an error message
throw new FileNotFoundException("Could not find shared module ${sharedModulePackageName}:${sharedModuleName}")
}
}
}
}
}
}
将示例中的“shared-module-release”替换为你的共享模块名称,将“com.example.myapp”和“com.example.sharedmodule”替换为你的应用程序和共享模块的包名称。
在终端中进入 Android 项目的根目录,运行以下命令:
./gradlew clean assembleRelease