apply plugin: 'com.android.library' import groovy.io.FileType import java.util.regex.Matcher import java.util.regex.Pattern android { compileSdkVersion 27 buildToolsVersion '27.0.3' buildDir = rootDir.absolutePath + "/../../../target/gradle/servoview" defaultConfig { minSdkVersion 18 targetSdkVersion 27 versionCode 1 versionName "1.0" } compileOptions { incremental false sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } flavorDimensions "default" productFlavors { main { } googlevr { minSdkVersion 21 } oculusvr { minSdkVersion 21 } } splits { density { enable false } abi { enable false } } buildTypes { // Default debug and release build types are used as templates debug { jniDebuggable true } release { signingConfig signingConfigs.debug // Change this to sign with a production key minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } // Custom build types armDebug { initWith(debug) ndk { abiFilters getNDKAbi('arm') } } armRelease { initWith(release) ndk { abiFilters getNDKAbi('arm') } } armv7Debug { initWith(debug) ndk { abiFilters getNDKAbi('armv7') } } armv7Release { initWith(release) ndk { abiFilters getNDKAbi('armv7') } } arm64Debug { initWith(debug) ndk { abiFilters getNDKAbi('arm64') } } arm64Release { initWith(release) ndk { abiFilters getNDKAbi('arm64') } } x86Debug { initWith(debug) ndk { abiFilters getNDKAbi('x86') } } x86Release { initWith(release) ndk { abiFilters getNDKAbi('x86') } } } sourceSets { main { assets.srcDirs = ['../../../../resources'] } armDebug { jniLibs.srcDirs = [getJniLibsPath(true, 'arm')] } armRelease { jniLibs.srcDirs = [getJniLibsPath(false, 'arm')] } armv7Debug { jniLibs.srcDirs = [getJniLibsPath(true, 'armv7')] } armv7Release { jniLibs.srcDirs = [getJniLibsPath(false, 'armv7')] } arm64Debug { jniLibs.srcDirs = [getJniLibsPath(true, 'arm64')] } arm64Release { jniLibs.srcDirs = [getJniLibsPath(false, 'arm64')] } x86Debug { jniLibs.srcDirs = [getJniLibsPath(true, 'x86')] } x86Release { jniLibs.srcDirs = [getJniLibsPath(false, 'x86')] } } // Ignore default 'debug' and 'release' build types variantFilter { variant -> if(variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) { variant.setIgnore(true); } } // Call our custom NDK Build task using flavor parameters tasks.all { compileTask -> Pattern pattern = Pattern.compile(/^compile[A-Z][\w\d]+([A-Z][\w\d]+)(Debug|Release)Ndk/) Matcher matcher = pattern.matcher(compileTask.name) if (!matcher.find()) { return } def taskName = "ndkbuild" + compileTask.name tasks.create(name: taskName, type: Exec) { def debug = compileTask.name.contains("Debug") def arch = matcher.group(1) commandLine getNdkDir(), 'APP_BUILD_SCRIPT=../jni/Android.mk', 'NDK_APPLICATION_MK=../jni/Application.mk', 'NDK_LIBS_OUT=' + getJniLibsPath(debug, arch), 'NDK_OUT=' + getTargetDir(debug, arch) + '/apk/obj', 'NDK_DEBUG=' + (debug ? '1' : '0'), 'APP_ABI=' + getNDKAbi(arch), 'SERVO_TARGET_DIR=' + getTargetDir(debug, arch) } compileTask.dependsOn taskName } project.afterEvaluate { android.libraryVariants.all { variant -> Pattern pattern = Pattern.compile(/^[\w\d]+([A-Z][\w\d]+)(Debug|Release)/) Matcher matcher = pattern.matcher(variant.name) if (!matcher.find()) { throw new GradleException("Invalid variant name for output: " + variant.name) } def arch = matcher.group(1) def debug = variant.name.contains("Debug") def finalFolder = getTargetDir(debug, arch) def finalFile = new File(finalFolder, "servoview.aar") variant.outputs.all { output -> Task copyAndRenameAARTask = project.task("copyAndRename${variant.name.capitalize()}AAR", type: Copy) { from output.outputFile.getParent() into finalFolder include output.outputFileName rename(output.outputFileName, finalFile.getName()) } variant.assemble.finalizedBy(copyAndRenameAARTask) } } } } dependencies { //Dependency list def deps = [ new ServoDependency("blurdroid.jar", "blurdroid") ] // Iterate all build types and dependencies // For each dependency call the proper implementation command and set the correct dependency path def list = ['arm', 'armv7', 'arm64', 'x86'] for (arch in list) { for (debug in [true, false]) { String basePath = getTargetDir(debug, arch) + "/build" String cmd = arch + (debug ? "Debug" : "Release") + "Implementation" for (ServoDependency dep : deps) { String path = findDependencyPath(basePath, dep.fileName, dep.folderFilter) if (path) { "${cmd}" files(path) } } } } googlevrImplementation 'com.google.vr:sdk-base:1.140.0' googlevrImplementation(name: 'GVRService', ext: 'aar') oculusvrImplementation(name: 'OVRService', ext: 'aar') implementation 'com.android.support.constraint:constraint-layout:1.1.2' } // folderFilter can be used to improve search performance static String findDependencyPath(String basePath, String filename, String folderFilter) { File path = new File(basePath); if (!path.exists()) { return '' } if (folderFilter) { path.eachDir { if (it.name.contains(folderFilter)) { path = new File(it.absolutePath) } } } def result = '' path.eachFileRecurse(FileType.FILES) { if(it.name.equals(filename)) { result = it.absolutePath } } return result } class ServoDependency { ServoDependency(String fileName, String folderFilter = null) { this.fileName = fileName; this.folderFilter = folderFilter; } public String fileName; public String folderFilter; } apply plugin: 'maven' import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact uploadArchives { doFirst { for ( arch in ["arm", "armv7", "arm64", "x86"] ) { def target = getTargetDir(false, arch) def aar = new File(target, "servoview.aar") if (aar.exists()) { def art = new DefaultPublishArtifact("servoview-" + arch, "aar", "aar", null, new Date(), aar); project.artifacts.add('archives', art) } } } repositories.mavenDeployer { repository(url: "file://localhost/${buildDir}/maven") def cmd = "git rev-parse --short HEAD" def proc = cmd.execute() def commit = proc.text.trim() def version = "0.0.1." + new Date().format('yyyyMMdd') + "." + commit for ( arch_ in ["arm", "armv7", "arm64", "x86"] ) { def arch = arch_ addFilter(arch) {artifact, file -> artifact.name == "servoview-" + arch} pom(arch).artifactId = "servoview-" + arch pom(arch).groupId = 'org.mozilla.servoview' pom(arch).version = version pom(arch).project { licenses { license { name 'The Mozilla Public License, v. 2.0' url 'https://mozilla.org/MPL/2.0/' distribution 'repo' } } } } } }