android gradle build - 在assembleRelease或assembleLive之前运行任务

时间:2017-11-22 10:36:44

标签: android android-studio gradle command-line android-gradle

以下是app的build.gradle文件。

的build.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    google()
}



android {
    signingConfigs {
        liveSigning {
            keyAlias 'xxxxxxx'
            keyPassword 'xxxxxx'
            storeFile file('D:\\xxxxx\\xxxxxxx\\xxxxx\\xxxx.jks')
            storePassword 'xxxxxxx'
        }

    }

    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    useLibrary 'org.apache.http.legacy'
    sourceSets.main {

        jniLibs.srcDir 'src/main/libs'
        jni.srcDirs = [];
    }
    defaultConfig {
        applicationId rootProject.ext.applicationId
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.compileSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        multiDexEnabled true
        resConfigs "en"
        vectorDrawables.useSupportLibrary  true
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.liveSigning
        }

        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
    task ndkBuild(type: Exec) {
        commandLine rootProject.ext.ndkPath, '-C', file('src/main').absolutePath
    }
    tasks.withType(JavaCompile) {

        compileTask -> compileTask.dependsOn ndkBuild

    }

    lintOptions {
        abortOnError false
    }
    productFlavors {
        live {
            minSdkVersion rootProject.ext.minSdkVersion
            applicationId rootProject.ext.applicationId
            signingConfig signingConfigs.liveSigning
            targetSdkVersion rootProject.ext.compileSdkVersion
            versionCode rootProject.ext.versionCode
            versionName rootProject.ext.versionName
            resConfigs "en"
        }
    }
    flavorDimensions "default"
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //compile files('libs/gson-2.2.4.jar')
    compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    compile "com.android.support:cardview-v7:${rootProject.ext.supportLibVersion}"
    compile "com.android.support:design:${rootProject.ext.supportLibVersion}"
    compile files('libs/cordova-5.jar')
    compile files('libs/org.apache.commons.io.jar')
    compile files('libs/httpmime-4.2.3.jar')
    compile files('libs/jsoup-1.7.3.jar')
    compile files('libs/activation.jar')
    compile project(':libraries:EventBus')
    compile project(':libraries:PDFViewCtrlTools')
    compile project(':libraries:imageannotations')
    compile project(':libraries:bottom-bar')
    compile project(':libraries:tooltip')
    compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
        transitive = true;
    }
    compile files('libs/glide-3.6.1.jar')
    //compile 'com.roughike:bottom-bar:2.2.0'
    compile project(':libraries:k4l-video-trimmer')
    compile 'com.android.support:multidex:1.0.1'
    compile 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.0.1'
    compile 'com.google.firebase:firebase-messaging:10.2.0'
    compile 'me.relex:circleindicator:1.2.2'
    compile 'com.daimajia.swipelayout:library:1.2.0@aar'
    compile 'org.apache.commons:commons-collections4:4.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

apply plugin: 'com.google.gms.google-services'

在我为Google Play商店生成已签名的APK上传之前,我想执行命令行代码。通过使用该命令行程序,我想做一些预发布活动,如禁用NDK日志,更改URL到实时网站等等。那么如何在assembleLive或assembleRelease之前执行它?我不想在“assembleDebug”之前执行该命令行代码。我想在“assembleLive或assembleRelease”之前执行它。

1 个答案:

答案 0 :(得分:1)

我将以下代码添加到我的gradle文件中,它就像一个魅力。

task prelivetask (type: Exec) {
        commandLine 'cmd','/C', 'start', 'change.cmd'
    }
    tasks.whenTaskAdded { task ->
        if (task.name == 'assembleRelease' || task.name == 'assembleLive') {
            task.dependsOn prelivetask 
        }
    }