buildSrc/src/main/kotlin/AndroidJavaVersion.kt
changeset 0 0a13dcabf7d3
child 22 d0190e5504a6
equal deleted inserted replaced
-1:000000000000 0:0a13dcabf7d3
       
     1 /*
       
     2  * AboutOss is a utility library to retrieve and display
       
     3  * opensource licenses in Android applications.
       
     4  *
       
     5  * Copyright (C) 2023 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of AboutOss.
       
     8  *
       
     9  * AboutOss is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * AboutOss is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.build
       
    23 
       
    24 import com.android.build.api.dsl.CommonExtension
       
    25 import com.android.build.api.dsl.DefaultConfig
       
    26 import com.android.build.gradle.BaseExtension
       
    27 import org.gradle.api.JavaVersion
       
    28 import org.gradle.api.Project
       
    29 import org.gradle.api.plugins.ExtensionAware
       
    30 import org.gradle.kotlin.dsl.dependencies
       
    31 import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
       
    32 
       
    33 /**
       
    34  * Configure java version compile options based on minSdkVersion value
       
    35  */
       
    36 @Suppress("UNCHECKED_CAST")
       
    37 fun BaseExtension.configureJavaVersion() {
       
    38     (this as CommonExtension<*, *, DefaultConfig, *>).configureJavaVersion()
       
    39 }
       
    40 
       
    41 fun CommonExtension<*, *, DefaultConfig, *>.configureJavaVersion() {
       
    42     val api = defaultConfig.minSdk ?: 1
       
    43     val version = when {
       
    44         api >= 30 -> JavaVersion.VERSION_11
       
    45         api >= 24 -> JavaVersion.VERSION_1_8
       
    46         api >= 19 -> JavaVersion.VERSION_1_7
       
    47         else -> JavaVersion.VERSION_1_6
       
    48     }
       
    49     compileOptions {
       
    50         sourceCompatibility = version
       
    51         targetCompatibility = version
       
    52     }
       
    53 
       
    54     (this as ExtensionAware).extensions.findByType(KotlinJvmOptions::class.java)?.apply {
       
    55         if (version >= JavaVersion.VERSION_1_8) {
       
    56             jvmTarget = "1.8"
       
    57         }
       
    58     }
       
    59 }
       
    60 
       
    61 /**
       
    62  * Add missing annotation processor dependencies to build on Java 11
       
    63  */
       
    64 fun Project.configureAnnotationProcessorDeps() {
       
    65     dependencies {
       
    66         configurations.whenObjectAdded {
       
    67             when (name) {
       
    68                 "kapt" -> {
       
    69                     add(name, "javax.xml.bind:jaxb-api:2.3.1")
       
    70                     add(name, "com.sun.xml.bind:jaxb-core:2.3.0.1")
       
    71                     add(name, "com.sun.xml.bind:jaxb-impl:2.3.2")
       
    72                 }
       
    73 
       
    74                 "annotationProcessor" -> add(name, "javax.xml.bind:jaxb-api:2.3.1")
       
    75             }
       
    76         }
       
    77     }
       
    78 }