常用Gradle配置

1、添加aar依赖

在allprojects->repositories节点下添加

flatDir {
    dirs 'folder with your aar' //dirs '../libs/'
}

在dependencies节点下添加

compile(name: 'name with your aar', ext: 'aar') // compile(name: 'framework-release', ext: 'aar')

2、Native编译依赖

在android->defaultConfig节点下添加平台选项及编译选项

externalNativeBuild {
    cmake { //ndkBuild{
        cppFlags "-std=c++11"
        abiFilters "armeabi-v7a"
    }
}

在Android节点下添加编译文件的路径(cmake/Android.mk)

externalNativeBuild {
    cmake { //ndkBuild {
        path "src/main/cpp/CMakeLists.txt"
    }
}

3、编译结束重命名、拷贝文件、Clear清除文件

在android的同级添加下面代码

def pathOut = project.rootDir.absolutePath + "/outLibs/"  // 需要拷贝到的目录
def pathIn = project.rootDir.absolutePath + "/app/build/outputs/apk/debug/"
def srcName = "AndroidInterface-release.aar"              // 编译生成文件的名字
def destName = "svr_plugin_android_interface.aar"         // 需要自定义的名字

// 清除需要拷贝目录下的文件
clean {}.doLast {
    if (file(pathOut + destName).exists()) {
        project.delete(pathOut + destName)
    }
}
task copyJarToOut(type: Copy) {
    outputs.upToDateWhen { false }
    from pathIn //tasks.matching { it.name.startsWith('bundle') }//apk/aar原始生成目录过滤(拷贝的路径)
    into pathOut
    include(srcName)
    rename(srcName, destName)
}
tasks.whenTaskAdded { task ->
    if (task.name == 'assembleDebug') {
        task.finalizedBy(copyJarToOut)
    } else if (task.name == 'assembleRelease') {
        task.finalizedBy(copyJarToOut)
    }
}

你可能感兴趣的:(常用Gradle配置)