Gradle 命令执行

1. 自动执行命令

执行自定义 task

android {
    compileSdkVersion 19
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "xxxxxxxxxxxxxx"
        minSdkVersion 8
        targetSdkVersion 19
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets.main {
        jniLibs.srcDir 'libs'
    }

    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
    //打包运行app时会自动调用ndkBuild task
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn 'ndkBuild'
    }

之后

task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
    println('executing ndkBuild')
    def ndkBuildingDir = 'C:\\Android\\sdk\\ndk-bundle'
    def ndkBuildPath = ndkBuildingDir
    ndkBuildPath = ndkBuildingDir + '/ndk-build.cmd'
    commandLine ndkBuildPath, '-j8', '-C', file('src/main').absolutePath
}

2. 生成自定义 jar 包

一键生成 jar 包

def SDK_BASENAME = "raw";
def SDK_VERSION = "";
def sdkDestinationPath = "build";//生成jar存放目录

//打包release目录下的classes.jar(开启混淆后这个jar就是混淆的源码)----注意由于as版本原因有的在default目录
def releaseJar = file('build/intermediates/intermediate-jars/release/classes.jar')

//打包jar前先删除原先的jar
task deleteBuild(type: Delete) {
    delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
    delete 'build/proGuard_du.jar'
}

task makeJar(type: Jar) {
    from zipTree(releaseJar)//导出混淆的jar
    //from fileTree(dir: 'src/main', includes: ['assets/**'])//将assets目录打入jar包
    //exclude('**/BuildConfig.class')//排除不必要的class文件
    baseName = SDK_BASENAME + SDK_VERSION
    destinationDir = file(sdkDestinationPath)
}

makeJar.dependsOn(deleteBuild, build)

task makeProguardJar(type: proguard.gradle.ProGuardTask, dependsOn: ['makeJar']) {
    // 未混淆的jar路径
    injars 'build/raw_du.jar'
    // 混淆后的jar输出路径
    outjars 'build/proGuard_du.jar'
    // 混淆协议
    configuration 'proguard-rules.pro'
}
    原文作者:QQ204805481
    原文地址: https://www.jianshu.com/p/e187316a9b6d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞