gpt4 book ai didi

android - 如何使用 Gradle 动态地从 aar 中排除文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:10 27 4
gpt4 key购买 nike

我有 android 库 (aar),其中包含(并在构建模块时构建) native 代码:

apply plugin: 'com.android.library'

android {
compileSdkVersion 16
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 11
targetSdkVersion 16
versionCode 1
versionName "1.0"

ndk {
abiFilters 'armeabi-v7a'
}
}

externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
jni.srcDirs = ['jni']
}
}

packagingOptions {
exclude '/lib/armeabi-v7a/libv8.cr.so'
exclude '/lib/armeabi-v7a/libv8_libbase.cr.so'
// TODO : generate dynamically from `SHARED_V8_LIB_FILENAMES`
}
}

我已经修改了 Android.mk 文件,使其具有依赖于 SHARED_V8_LIB_FILENAMES 环境变量的不同配置:

...
# Report configuration
ifeq ($(SHARED_V8_LIB_FILENAMES),)
# static
$(info [Configuration] Linking statically with built-in v8)
else
# dynamic

define info_define
$(info [Configuration] Linking dynamically with shared v8 library ./libfoo-binaries/android_$(TARGET_ARCH_ABI)/$1)
endef
$(foreach item,$(SHARED_V8_LIB_FILENAMES),$(eval $(call info_define,$(item))))
endif
...

我如何修改我的 build.gradle 以生成依赖于环境变量 SHARED_V8_LIB_FILENAMESpackageOptions 部分?我需要检查变量是否已设置,用空格分隔符解析它并添加要从最终 aar 文件中排除的文件列表。

最佳答案

其实我自己也能做到:

apply plugin: 'com.android.library'

android {
compileSdkVersion 16
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 11
targetSdkVersion 16
versionCode 1
versionName "1.0"

ndk {
abiFilters 'armeabi-v7a'
}
}

externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
jni.srcDirs = ['jni']
}
}

def sharedV8LibFilesSet = []
def sharedV8LibFiles = System.getenv('SHARED_V8_LIB_FILENAMES')
if (sharedV8LibFiles != null) {
def sharedV8LibFilesArray = sharedV8LibFiles.split(' ')
sharedV8LibFilesArray.each { eachFileName ->
sharedV8LibFilesSet.add("**/" + eachFileName)
println "[Configuration] Excluding shared v8 library " + eachFileName + " from AAR"
}
} else {
println "[Configuration] No shared v8 libraries excluded from AAR"
}

packagingOptions {
excludes = sharedV8LibFilesSet
}
}

感谢任何代码风格的更改/改进。

关于android - 如何使用 Gradle 动态地从 aar 中排除文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543271/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com