android - 如何设置 gradle 任务来提取 dex jar 和 c 库?

标签 android android-studio gradle android-gradle-plugin dex

我有一个带有单个 Android 库模块的 Android 项目,但我不想以传统方式使用它。

我现在所做的是组装库(使用assemble gradle 任务)并对生成的 *.aar 文件进行后处理。

我想创建一个 gradle 任务:

  1. 构建classes.jar并dex它(类似于dex --dx ...);
  2. 构建 native 库;
  3. 将所有内容移动到我可以选择的文件夹中。

不幸的是,我只找到了一种构建原生库的方法(使用嵌入式 gradle 任务externalNativeBuildRelease)。

任何人都可以帮我解决这个 groovy gradle 问题吗?

最佳答案

这是一个 build.gradle 文件,它可以完成您想要从 this 中提取的大部分(如果不是全部)内容。 。
我在this中使用过它答案。
另请参阅 The Gradle build system- TutorialWriting Custom Tasks .

以下是定义的任务:

  • 任务副本类
  • 任务 assembleExternalJar
  • 任务 copyJarToOutputs

它应该会给你灵感去做你想做的事:

import org.apache.tools.ant.taskdefs.condition.Os

//jrg
apply plugin: 'com.android.library'
//apply plugin: 'com.android.application'

android {
    compileSdkVersion Integer.parseInt(COMPILE_SDK)
    buildToolsVersion BUILD_TOOLS_VERSION

    defaultConfig {
        targetSdkVersion Integer.parseInt(TARGET_SDK)
        minSdkVersion Integer.parseInt(MIN_SDK)
    }
}
// Add the main project as a dependency for our library
dependencies {
    compile project(':app')
}
// Define some tasks which are used in the build process
task copyClasses(type: Copy) { // Copy the assembled *.class files for only the current namespace into a new directory
    // get directory for current namespace
    def namespacePath = PLUGIN_NAMESPACE.replaceAll("\\.","/")
    // set source and destination directories
    from "build/intermediates/classes/release/${namespacePath}/"
    into "build/intermediates/dex/${namespacePath}/"

    // exclude classes which don't have a corresponding entry in the source directory
    def remExt = { name -> name.lastIndexOf('.').with {it != -1 ? name[0..<it] : name} }
    eachFile {details ->
        def thisFile = new File("${projectDir}/src/main/java/${namespacePath}/", remExt(details.name)+".java")
        if (!(thisFile.exists())) {
            details.exclude()
        }
    }
}

task assembleExternalJar << {
    // Get the location of the Android SDK
    ext.androidSdkDir = System.env.ANDROID_HOME
    if(androidSdkDir == null) {
        Properties localProps = new Properties()
        localProps.load(new FileInputStream(file('local.properties')))
        ext.androidSdkDir = localProps['sdk.dir']
    }
    // Make sure no existing jar file exists as this will cause dx to fail
    new File("${buildDir}/intermediates/dex/${PLUGIN_NAMESPACE}.jar").delete();
    // Use command line dx utility to convert *.class files into classes.dex inside jar archive
    String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : ''
    exec {
        commandLine "${androidSdkDir}/build-tools/${BUILD_TOOLS_VERSION}/dx${cmdExt}", '--dex',
                    "--output=${buildDir}/intermediates/dex/${PLUGIN_NAMESPACE}.jar",
                    "${buildDir}/intermediates/dex/"
    }
    copyJarToOutputs.execute()
}

task copyJarToOutputs(type: Copy) {
    // Copy the built jar archive to the outputs folder
    from 'build/intermediates/dex/'
    into 'build/outputs/'
    include '*.jar'
}


// Set the dependencies of the build tasks so that assembleExternalJar does a complete build
copyClasses.dependsOn(assemble)
assembleExternalJar.dependsOn(copyClasses)

关于android - 如何设置 gradle 任务来提取 dex jar 和 c 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47355578/

相关文章:

android - 推送配置 API(TapAndPay 库)返回调用包未验证

android - FaceDetector.Builder().build(getApplicationContext()) 给我错误

android - 无法将可绘制对象转换为可绘制对象

gradle - 如何在Gradle构建脚本中获取android输出文件夹(绝对路径)

android - 无法导入 ActivityChooserModel

android - 三星手机中的安全异常 WRITE_USE_APP_FEATURE_SURVEY

java - MainActivity 应在启动画面期间加载

android - 将 AS 更新到 1.0 后,项目中出现 "method ID not in [0, 0xffff]: 65536"错误

android - 定制阵列适配器协助

android - Unity Gradle build 找不到 Build Tools 修订版 28.0.0