android - 添加产品风味以响应本机 android 项目
问题描述
我正在尝试为现有的 react native 项目添加风味,如下所示:
flavorDimensions "default"
productFlavors {
flavourone {
dimension "default"
applicationId "com.example.flavourone"
}
flavourtwo {
dimension "default"
applicationId "com.example.flavourtwo"
}
}
但是当我尝试生成一个签名的 apk 时,似乎我所有的可绘制文件都被复制了,我得到:
Error: Duplicate resources
我尝试了一堆不同的解决方案,清理项目,运行 package.json 中的命令以捆绑到临时目录等。我认为 build.gradle 文件未正确配置为支持添加风味,或者我没有正确执行.
任何帮助将不胜感激,以下是我的 gradle 文件(很少有东西重命名为示例):
project.ext.react = [
entryFile: "index.js",
]
//using custom react gradle here to get around https://stackoverflow.com/questions/52464842/react-native-duplicate-resources
apply from: "../../node_modules/react-native-sentry/sentry.gradle"
apply from: "../react.gradle"
//apply from: "../../node_modules/react-native/react.gradle"
/**
* Set this to true to create two separate APKs instead of one:
* - An APK that only works on ARM devices
* - An APK that only works on x86 devices
* The advantage is the size of the APK is reduced by about 4MB.
* Upload all the APKs to the Play Store and people will download
* the correct one based on the CPU architecture of their device.
*/
def enableSeparateBuildPerCPUArchitecture = false
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false
android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.example"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
missingDimensionStrategy 'react-native-camera', 'general'
multiDexEnabled true
ndk.abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
flavorDimensions "default"
productFlavors {
flavourone {
dimension "default"
applicationId "com.example.flavourone"
}
flavourtwo {
dimension "default"
applicationId "com.example.flavourtwo"
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
debug {
res.srcDirs = ['src/debug/res']
// this uses the debug's res directory which contains a "debug" icon
}
release {
java.srcDirs = ['src/debugRelease/java']
// this avoids copying across code from the debugRelease java directory
}
}
signingConfigs {
release {
(removed)
}
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86-64"
}
}
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
(removed)
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86-64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
}
dependencies {
(removed)
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
react-native bundle --entry-file index.js --platform android --dev false --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
在制作 apk 之前,我使用 package.json ( ) 中的命令。
似乎一旦我运行此命令,我的默认可绘制对象与风味二可绘制对象发生冲突,并以重复资源结束
解决方案
在 android/app/src/main/res drawable-hdpi、drawable-mdpi、drawable-xhdpi、drawable-xxhdpi、drawable-xxxhdpi 和 raw 文件夹中删除它并添加到android/app/build.gradle
文件中
android {
...
aaptOptions {
cruncherEnabled = false
}
然后 ./dradlew clean
,invalid caches/restart
在 android studio 中构建签名的 apk。
推荐阅读
- css - 在简单的 html 页面中“测试”SCSS 的最快解决方案
- javascript - 来自不同数组的相同元素的总和
- javascript - 如果满足条件,是否可以使智能三元运算符返回复数?
- c++ - typedef decltype 函数指针未返回正确的输出 (cout)
- python - Django中的日期字段验证
- html - 如何从 Google 电子表格中的 html 元素获取内容?
- c++ - 解析 PLT 存根地址和名称
- python - 在 Python 中使用带有日期的迭代行
- node.js - Who is productive Model.prototype.save() or Model.create()?
- javascript - PWA 添加到主屏幕弹出按钮不起作用