适配 AGP8.5,构建参数 targetSdk 等配置(六)

在这里插入图片描述

1、versionName & versionCode & packageName

  • versionName
  • versionCode
  • packageName

AGP7+

android = mProject.extensions.findByName("android")

android.defaultConfig.applicationId = packageName
android.defaultConfig.versionName = versionName
android.defaultConfig.versionCode = Integer.parseInt(versionCode)

AGP8+

private fun processBuildParams(data: JsonObject, project: Project) {
        val versionName = (data["versionName"] as JsonPrimitive).content
        val versionCode = (data["versionCode"] as JsonPrimitive).content.toInt()
        val packageName = (data["packageName"] as JsonPrimitive).content

        CmdColorPrintUtils.outputGreen(
            "构建参数:\n" +
                    "\tversionName =$versionName\n" +
                    "\tversionCode =$versionCode\n" +
                    "\tpackageName =$packageName\n"
        )

        val androidComponents =
            project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java)
        androidComponents.onVariants { variant ->
            variant.applicationId.set(packageName)
            variant.outputs.forEach { variantOutput ->
                variantOutput.versionCode.set(versionCode)
                variantOutput.versionName.set(versionName)
            }
        }
    }

2、ndk abiFilters

AGP7+

android.defaultConfig.ndk.abiFilters = abiList

AGP8+

project.plugins.withId("com.android.application") {
            val androidDslExt =
                project.extensions.findByType(ApplicationExtension::class.java)
            androidDslExt?.buildTypes?.all { buildType ->

                val abiList = CheckPluginManager.getInstance().abiList
                if (abiList.isNotEmpty()) {
                    CmdColorPrintUtils.outputGreen("${buildType.name} 指定CPU架构:")
                    abiList.forEach {
                        CmdColorPrintUtils.outputGreen("\t $it")
                    }
                    buildType.ndk {
                        abiFilters.addAll(abiList)
                    }
                }
            }
        }

3、manifest Placeholders

AGP7+

Map<String, Object> placeInMap = new HashMap<>()

android.defaultConfig.manifestPlaceholders = placeInMap

AGP8+

推荐方式一

private var mManifestPlaceMap = mutableMapOf<String, String>()

project.plugins.withType(AppPlugin::class.java) {
	val appExt =
		project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java)

		//【方式一】推荐,在添加变体情况下也可以使用
		appExt.finalizeDsl { extension ->
		    extension.defaultConfig {
		        CmdColorPrintUtils.outputGreen("manifest 参数:")
		        mManifestPlaceMap.forEach { (k, v) ->
		            manifestPlaceholders.put(k, v)
		            CmdColorPrintUtils.outputGreen("\t$k  ->  $v")
		        }
		    }
		}
		
	appExt.onVariants { variant ->
		//【方式二】在不添加变体情况下这样是可用的
		CmdColorPrintUtils.outputGreen("${variant.name} manifest 参数:")
		mManifestPlaceMap.forEach { (k, v) ->
			CmdColorPrintUtils.outputGreen("\t$k  ->  $v")
			variant.manifestPlaceholders.put(k, v)
		}
	}

4、targetSdk & minSdk & compileSdk

AGP8+

  • 找到 AndroidComponentsExtension 并设置 beforeVariants
  • 找到 CommonExtension 直接设置 compileSdk
//一些编译配置
val androidExt = project.extensions.getByType(AndroidComponentsExtension::class.java)
val commonExt = project.extensions.getByType(CommonExtension::class.java)

//compileSdk,你需要马上设置
val compileSdk = CheckPluginManager.getInstance().compilerSDK
if (compileSdk > 0) {
    commonExt.compileSdk = compileSdk
    CmdColorPrintUtils.outputGreen("设置包体参数 compileSdk:$compileSdk")
}

androidExt.beforeVariants { variant ->
    val genVariant = variant as GeneratesApkBuilder
    val minSdk = CheckPluginManager.getInstance().minSDK
    val targetSdk = getTargetSdk()
    if (minSdk > 0) {
        variant.minSdk = minSdk
        CmdColorPrintUtils.outputGreen("${variant.name} 设置包体参数 minSdk:$minSdk")
    }
    if (targetSdk > 0) {
		//你也可以这样设置,但是 AS 提示方法过时
		//建议你使用 GeneratesApkBuilder 的接口,使用 variant as GeneratesApkBuilder 转换
        //variant.targetSdk = targetSdk;

        genVariant.targetSdk = targetSdk
        CmdColorPrintUtils.outputGreen("${variant.name} 设置包体参数 targetSdk:$targetSdk")
    }
    CheckPluginManager.getInstance().currentTargetSdkVer = genVariant.targetSdk as Int
    CheckPluginManager.getInstance().currentCompileSdkVer = commonExt.compileSdk!!
}

4.1 compileSdk 相关

设置 compileSdk 怎么找到 CommonExtension 的?

比如我现在使用的是gradle-api-8.7.2

找到这个 jar 使用 JADX 打开搜索设置 compileSdk 相似接口就发现了

在这里插入图片描述

你需要较早设置 compileSdk

设置较晚会报错:It is too late to set compileSdk

A problem occurred configuring project ':app'.
> com.android.build.gradle.internal.dsl.AgpDslLockedException: It is too late to set compileSdk
  It has already been read to configure this project.
  Consider either moving this call to be during evaluation,
  or using the variant API.

同样的,targetSdk、minSdk 你也可以这样寻找

对我们来说 AGP8+ 可能是新事物,这能这样慢慢摸索了,除非官方文档有相应的 demo

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值