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

时间:2017-11-17 16:49:37

标签: android android-studio gradle android-gradle dex

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

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

我想创建一个gradle任务:

  1. 构建classes.jar和 dex 它(类似于dex --dx ...);
  2. 构建本机库;
  3. 将所有内容移动到我可以选择的文件夹中。
  4. 不幸的是,我只找到了构建本机库的方法(使用嵌入式gradle任务 externalNativeBuildRelease )。

    有人可以帮我解决这个时髦的问题吗?

1 个答案:

答案 0 :(得分:1)

这是一个build.gradle文件,如果不是您想要从this中提取的所有内容,则会执行大部分内容。
我在this回答中使用了它。
另请参阅The Gradle build system- TutorialWriting Custom Tasks

以下是已定义的任务:

  • task copyClasses
  • 任务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)