2014-09-10 22:57:43 +08:00
/ *
Licensed to the Apache Software Foundation ( ASF ) under one
or more contributor license agreements . See the NOTICE file
distributed with this work for additional information
regarding copyright ownership . The ASF licenses this file
to you under the Apache License , Version 2.0 ( the
"License" ) ; you may not use this file except in compliance
with the License . You may obtain a copy of the License at
http: //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing ,
software distributed under the License is distributed on an
"AS IS" BASIS , WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND , either express or implied . See the License for the
specific language governing permissions and limitations
under the License .
* /
2014-09-10 23:39:29 +08:00
import java.util.regex.Pattern
2021-07-03 12:04:13 +08:00
import io.github.g00fy2.versioncompare.Version
2015-01-07 04:56:09 +08:00
2015-01-21 00:33:55 +08:00
String doEnsureValueExists ( filePath , props , key ) {
if ( props . get ( key ) = = null ) {
throw new GradleException ( filePath + ': Missing key required "' + key + '"' )
}
return props . get ( key )
}
2014-09-10 22:57:43 +08:00
2015-01-21 00:33:55 +08:00
String doGetProjectTarget ( ) {
def props = new Properties ( )
2017-06-14 01:36:00 +08:00
def propertiesFile = 'project.properties' ;
if ( ! ( file ( propertiesFile ) . exists ( ) ) ) {
propertiesFile = '../project.properties' ;
}
file ( propertiesFile ) . withReader { reader - >
2015-01-21 00:33:55 +08:00
props . load ( reader )
2014-09-10 22:57:43 +08:00
}
2015-01-21 00:33:55 +08:00
return doEnsureValueExists ( 'project.properties' , props , 'target' )
2014-09-10 22:57:43 +08:00
}
2020-01-16 07:40:54 +08:00
Boolean isVersionValid ( version ) {
return ! ( new Version ( version ) ) . isEqual ( '0.0.0' )
}
2020-01-15 06:28:54 +08:00
String doFindLatestInstalledBuildTools ( String minBuildToolsVersionString ) {
2020-01-17 20:43:25 +08:00
def buildToolsDirContents
2014-09-10 22:57:43 +08:00
try {
2020-01-17 20:43:25 +08:00
def buildToolsDir = new File ( getAndroidSdkDir ( ) , "build-tools" )
buildToolsDirContents = buildToolsDir . list ( )
2014-09-10 22:57:43 +08:00
} catch ( e ) {
println "An exception occurred while trying to find the Android build tools."
throw e
}
2020-01-17 20:43:25 +08:00
2021-07-27 00:17:25 +08:00
def minBuildToolsVersion = new Version ( minBuildToolsVersionString )
def maxVersion = new Version ( ( minBuildToolsVersion . getMajor ( ) + 1 ) + ".0.0" )
2020-01-17 20:43:25 +08:00
def highestBuildToolsVersion = buildToolsDirContents
. collect { new Version ( it ) }
// Invalid inputs will be handled as 0.0.0
2021-07-27 00:17:25 +08:00
. findAll { it . isHigherThan ( '0.0.0' ) & & it . isLowerThan ( maxVersion ) }
2020-01-17 20:43:25 +08:00
. max ( )
if ( highestBuildToolsVersion = = null ) {
throw new RuntimeException ( "" "
2022-02-22 22:14:22 +08:00
No installed build tools found . Please install the Android build tools
version $ { minBuildToolsVersionString } .
2020-01-17 20:43:25 +08:00
"" " . replaceAll ( /\s+/ , ' ' ) . trim ( ) )
2014-09-10 22:57:43 +08:00
}
2020-01-17 20:43:25 +08:00
if ( highestBuildToolsVersion . isLowerThan ( minBuildToolsVersionString ) ) {
throw new RuntimeException ( "" "
2021-07-27 00:17:25 +08:00
No usable Android build tools found . Highest $ { minBuildToolsVersion . getMajor ( ) } . x installed version is
2022-02-22 22:14:22 +08:00
$ { highestBuildToolsVersion . getOriginalString ( ) } ; Recommended version
is $ { minBuildToolsVersionString } .
2020-01-17 20:43:25 +08:00
"" " . replaceAll ( /\s+/ , ' ' ) . trim ( ) )
}
highestBuildToolsVersion . getOriginalString ( )
2014-09-10 22:57:43 +08:00
}
String getAndroidSdkDir ( ) {
def rootDir = project . rootDir
2014-09-16 00:15:32 +08:00
def androidSdkDir = null
2020-04-16 10:43:17 +08:00
String envVar = System . getenv ( "ANDROID_SDK_ROOT" )
if ( envVar = = null ) {
envVar = System . getenv ( "ANDROID_HOME" )
}
2020-06-10 21:32:43 +08:00
2014-09-10 22:57:43 +08:00
def localProperties = new File ( rootDir , 'local.properties' )
2014-09-16 00:15:32 +08:00
String systemProperty = System . getProperty ( "android.home" )
if ( envVar ! = null ) {
androidSdkDir = envVar
} else if ( localProperties . exists ( ) ) {
2014-09-10 22:57:43 +08:00
Properties properties = new Properties ( )
localProperties . withInputStream { instr - >
properties . load ( instr )
}
def sdkDirProp = properties . getProperty ( 'sdk.dir' )
if ( sdkDirProp ! = null ) {
androidSdkDir = sdkDirProp
} else {
sdkDirProp = properties . getProperty ( 'android.dir' )
if ( sdkDirProp ! = null ) {
androidSdkDir = ( new File ( rootDir , sdkDirProp ) ) . getAbsolutePath ( )
}
}
}
2014-09-16 00:15:32 +08:00
if ( androidSdkDir = = null & & systemProperty ! = null ) {
androidSdkDir = systemProperty
}
if ( androidSdkDir = = null ) {
throw new RuntimeException (
"Unable to determine Android SDK directory." )
}
2014-09-10 22:57:43 +08:00
androidSdkDir
}
2015-01-21 00:33:55 +08:00
def doExtractIntFromManifest ( name ) {
2015-01-07 04:56:09 +08:00
def manifestFile = file ( android . sourceSets . main . manifest . srcFile )
def pattern = Pattern . compile ( name + "=\"(\\d+)\"" )
def matcher = pattern . matcher ( manifestFile . getText ( ) )
matcher . find ( )
2016-04-23 01:10:49 +08:00
return new BigInteger ( matcher . group ( 1 ) )
2015-01-07 04:56:09 +08:00
}
2015-12-22 06:53:49 +08:00
def doExtractStringFromManifest ( name ) {
def manifestFile = file ( android . sourceSets . main . manifest . srcFile )
def pattern = Pattern . compile ( name + "=\"(\\S+)\"" )
def matcher = pattern . matcher ( manifestFile . getText ( ) )
matcher . find ( )
return matcher . group ( 1 )
}
2015-04-22 21:44:28 +08:00
def doGetConfigXml ( ) {
2017-12-30 08:07:45 +08:00
def xml = file ( "src/main/res/xml/config.xml" ) . getText ( )
2015-04-22 21:44:28 +08:00
// Disable namespace awareness since Cordova doesn't use them properly
return new XmlParser ( false , false ) . parseText ( xml )
}
def doGetConfigPreference ( name , defaultValue ) {
name = name . toLowerCase ( )
def root = doGetConfigXml ( )
def ret = defaultValue
root . preference . each { it - >
def attrName = it . attribute ( "name" )
if ( attrName & & attrName . toLowerCase ( ) = = name ) {
ret = it . attribute ( "value" )
}
}
return ret
}
2021-07-06 14:38:28 +08:00
def doApplyCordovaConfigCustomization ( ) {
// Apply user overide properties that comes from the "--gradleArg=-P" parameters
if ( project . hasProperty ( 'cdvMinSdkVersion' ) ) {
cordovaConfig . MIN_SDK_VERSION = Integer . parseInt ( '' + cdvMinSdkVersion )
}
if ( project . hasProperty ( 'cdvSdkVersion' ) ) {
cordovaConfig . SDK_VERSION = Integer . parseInt ( '' + cdvSdkVersion )
}
2022-05-18 22:18:33 +08:00
if ( project . hasProperty ( 'cdvCompileSdkVersion' ) ) {
cordovaConfig . COMPILE_SDK_VERSION = Integer . parseInt ( '' + cdvCompileSdkVersion )
}
2021-07-06 14:38:28 +08:00
if ( project . hasProperty ( 'cdvMaxSdkVersion' ) ) {
cordovaConfig . MAX_SDK_VERSION = Integer . parseInt ( '' + cdvMaxSdkVersion )
}
if ( project . hasProperty ( 'cdvBuildToolsVersion' ) ) {
cordovaConfig . BUILD_TOOLS_VERSION = cdvBuildToolsVersion
}
if ( project . hasProperty ( 'cdvAndroidXAppCompatVersion' ) ) {
cordovaConfig . ANDROIDX_APP_COMPAT_VERSION = cdvAndroidXAppCompatVersion
}
2021-07-06 22:39:12 +08:00
if ( project . hasProperty ( 'cdvAndroidXWebKitVersion' ) ) {
cordovaConfig . ANDROIDX_WEBKIT_VERSION = cdvAndroidXWebKitVersion
}
2021-07-06 14:38:28 +08:00
2021-07-27 00:26:42 +08:00
if ( ! cordovaConfig . BUILD_TOOLS_VERSION ) {
cordovaConfig . BUILD_TOOLS_VERSION = doFindLatestInstalledBuildTools (
cordovaConfig . MIN_BUILD_TOOLS_VERSION
)
}
// Ensure the configured build tools version is at least our declared minimum
def buildToolsVersion = new Version ( cordovaConfig . BUILD_TOOLS_VERSION )
if ( buildToolsVersion . isLowerThan ( cordovaConfig . MIN_BUILD_TOOLS_VERSION ) ) {
throw new RuntimeException ( "" "
Expected Android Build Tools version > = $ { cordovaConfig . MIN_BUILD_TOOLS_VERSION } ,
but got Android Build Tools version $ { cordovaConfig . BUILD_TOOLS_VERSION } . Please use version $ { cordovaConfig . MIN_BUILD_TOOLS_VERSION } or later .
"" " . replaceAll ( /\s+/ , ' ' ) . trim ( ) )
}
2021-07-06 14:38:28 +08:00
}
2022-05-18 22:18:33 +08:00
def doVerifyCordovaConfigForBuild ( ) {
if ( cordovaConfig . COMPILE_SDK_VERSION < cordovaConfig . SDK_VERSION ) {
println "The \"compileSdkVersion\" (${cordovaConfig.COMPILE_SDK_VERSION}) should be greater than or equal to the the \"targetSdkVersion\" (${cordovaConfig.SDK_VERSION})."
}
}
2015-01-07 04:56:09 +08:00
// Properties exported here are visible to all plugins.
ext {
2021-07-06 14:38:28 +08:00
def defaultsFilePath = './cdv-gradle-config-defaults.json'
def projectConfigFilePath = "$rootDir/cdv-gradle-config.json"
def targetConfigFilePath = null
/ * *
* Check if the project config file path exists . This file will exist if coming from CLI project .
* If this file does not exist , falls back onto the default file .
* This scenario can occur if building the framework ' s AAR package for publishing .
* /
if ( file ( projectConfigFilePath ) . exists ( ) ) {
targetConfigFilePath = projectConfigFilePath
} else {
targetConfigFilePath = defaultsFilePath
}
def jsonFile = new File ( targetConfigFilePath )
cordovaConfig = new groovy . json . JsonSlurper ( ) . parseText ( jsonFile . text )
2022-05-18 22:18:33 +08:00
if ( cordovaConfig . COMPILE_SDK_VERSION = = null ) {
cordovaConfig . COMPILE_SDK_VERSION = cordovaConfig . SDK_VERSION
}
2021-07-06 14:38:28 +08:00
// Apply Gradle Properties
doApplyCordovaConfigCustomization ( )
2015-01-07 04:56:09 +08:00
// These helpers are shared, but are not guaranteed to be stable / unchanged.
privateHelpers = { }
2015-01-21 00:33:55 +08:00
privateHelpers . getProjectTarget = { doGetProjectTarget ( ) }
2021-07-06 14:38:28 +08:00
privateHelpers . applyCordovaConfigCustomization = { doApplyCordovaConfigCustomization ( ) }
2015-01-21 00:33:55 +08:00
privateHelpers . extractIntFromManifest = { name - > doExtractIntFromManifest ( name ) }
2015-12-22 06:53:49 +08:00
privateHelpers . extractStringFromManifest = { name - > doExtractStringFromManifest ( name ) }
2015-01-21 00:33:55 +08:00
privateHelpers . ensureValueExists = { filePath , props , key - > doEnsureValueExists ( filePath , props , key ) }
2015-04-22 21:44:28 +08:00
// These helpers can be used by plugins / projects and will not change.
cdvHelpers = { }
2015-04-22 21:53:13 +08:00
// Returns a XmlParser for the config.xml. Added in 4.1.0.
2015-04-22 21:44:28 +08:00
cdvHelpers . getConfigXml = { doGetConfigXml ( ) }
2015-04-22 21:53:13 +08:00
// Returns the value for the desired <preference>. Added in 4.1.0.
2015-04-22 21:44:28 +08:00
cdvHelpers . getConfigPreference = { name , defaultValue - > doGetConfigPreference ( name , defaultValue ) }
2022-05-18 22:18:33 +08:00
// Display warnings if any cordova config is not proper for build.
cdvHelpers . verifyCordovaConfigForBuild = { doVerifyCordovaConfigForBuild ( ) }
2015-01-07 04:56:09 +08:00
}
2014-09-10 22:57:43 +08:00
2020-01-15 06:28:54 +08:00
buildscript {
repositories {
2021-07-03 12:04:13 +08:00
google ( )
mavenCentral ( )
2020-01-15 06:28:54 +08:00
}
dependencies {
2021-07-03 12:04:13 +08:00
classpath 'io.github.g00fy2:versioncompare:1.4.1@jar'
2020-01-15 06:28:54 +08:00
}
}