apply from: rootProject.file('cc-settings.gradle')
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'android-aspectjx'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]

    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
    // 避免 lint 检测出错时停止构建
    lintOptions {
        abortOnError false
    }
    dataBinding {
        enabled true
    }
    defaultConfig {
        if (project.ext.runAsApp) {
            applicationId "com.gingersoft.supply_chain"
        }
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]
        multiDexEnabled true
        //配置注解处理器
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath true
            }
        }
    }
//    viewBinding {
//        enabled = true
//    }
    resourcePrefix "supply_chain"

    buildTypes {
        debug {
            buildConfigField "boolean", "LOG_DEBUG", rootProject.ext.buildType["debugLogDebug"]
            buildConfigField "boolean", "USE_CANARY", rootProject.ext.buildType["debugUseCanary"]
            minifyEnabled false
            proguardFiles 'proguard.cfg'
        }
        release {
            buildConfigField "boolean", "LOG_DEBUG", rootProject.ext.buildType["releaseLogDebug"]
            buildConfigField "boolean", "USE_CANARY", rootProject.ext.buildType["releaseUseCanary"]
            minifyEnabled false
            shrinkResources false
            zipAlignEnabled false
            proguardFiles 'proguard.cfg'
            //修改生成的apk名字
            if (project.ext.runAsApp) {
                applicationVariants.all { variant ->
                    variant.outputs.all {
                        def fileName
                        def date = new Date()
                        def formattedDate = date.format('yyyy-MM-dd')
                        if (variant.buildType.name == 'release') {
                            fileName = "採購_${variant.mergedFlavor.versionName}_${formattedDate}.apk"
                        } else if (variant.buildType.name == 'debug') {
                            fileName = "採購_${variant.mergedFlavor.versionName}_${formattedDate}.apk"
                        }
                        outputFileName = fileName
                    }
                }
            }
        }
    }

}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    testImplementation rootProject.ext.dependencies["junit"]

    annotationProcessor rootProject.ext.dependencies["dagger2-compiler"]
    annotationProcessor rootProject.ext.dependencies["butterknife-compiler"]
    implementation rootProject.ext.dependencies["BaseRecyclerViewAdapter"]

    debugImplementation rootProject.ext.dependencies["canary-debug"]
    releaseImplementation rootProject.ext.dependencies["canary-release"]
    testImplementation rootProject.ext.dependencies["canary-release"]

    implementation rootProject.ext.dependencies["glide"]
    annotationProcessor rootProject.ext.dependencies["glide-compiler"]
    implementation 'com.github.yalantis:ucrop:2.2.6'
    if (project.ext.runAsApp) {
        addComponent 'component-scan'
    }
    aspectjx {
        enabled true
        exclude 'androidx', 'org.jetbrains.kotlin', "module-info", 'com.google', 'com.squareup', 'org.apache', 'versions.9', 'com.squareup.okio', 'okio', 'leakcanary', 'com.google.firebase', 'com.google.android'
    }
}

import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

//if (project.ext.runAsApp) {
//    final def log = project.logger
//    final def variants = project.android.applicationVariants
//
//    variants.all { variant ->
//        if (!variant.buildType.isDebuggable()) {
//            log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
//            return;
//        }
//
//        JavaCompile javaCompile = variant.javaCompile
//        javaCompile.doLast {
//            String[] args = ["-showWeaveInfo",
//                             "-1.8",
//                             "-inpath", javaCompile.destinationDir.toString(),
//                             "-aspectpath", javaCompile.classpath.asPath,
//                             "-d", javaCompile.destinationDir.toString(),
//                             "-classpath", javaCompile.classpath.asPath,
//                             "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
//            log.debug "ajc args: " + Arrays.toString(args)
//
//            MessageHandler handler = new MessageHandler(true);
//            new Main().run(args, handler);
//            for (IMessage message : handler.getMessages(null, true)) {
//                switch (message.getKind()) {
//                    case IMessage.ABORT:
//                    case IMessage.ERROR:
//                    case IMessage.FAIL:
//                        log.error message.message, message.thrown
//                        break;
//                    case IMessage.WARNING:
//                        log.warn message.message, message.thrown
//                        break;
//                    case IMessage.INFO:
//                        log.info message.message, message.thrown
//                        break;
//                    case IMessage.DEBUG:
//                        log.debug message.message, message.thrown
//                        break;
//                }
//            }
//        }
//    }
//}