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

我有一个包含(并在构建模块时构建)本机代码的android库(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']
    }
  }

  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_FILENAMES的packageOptions部分?我需要检查是否设置了变量,使用空格分隔符对其进行了解析,并添加了要从最终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 studio 打包 —— aar 嵌套引用本地 aar 的打包


下一篇:Android Studio Module打包AAR