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

1、versionName & versionCode & packageName

  • versionName
  • versionCode
  • packageName

AGP7+

groovy 复制代码
android = mProject.extensions.findByName("android")

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

AGP8+

kt 复制代码
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+

groovy 复制代码
android.defaultConfig.ndk.abiFilters = abiList

AGP8+

kt 复制代码
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+

groovy 复制代码
Map<String, Object> placeInMap = new HashMap<>()

android.defaultConfig.manifestPlaceholders = placeInMap

AGP8+

kt 复制代码
private var mManifestPlaceMap = mutableMapOf<String, String>()

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

		//manifest place holder
		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
c 复制代码
//一些编译配置
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

kt 复制代码
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

相关推荐
STCNXPARM6 分钟前
Android camera子系统概述
android·图像处理·摄像头·车载
牛马11116 分钟前
flutter Riverpod 中的 overrideWith
android·java·flutter
2501_9371931429 分钟前
TV 电视影视大全:全终端流畅观影技术解析
android·源码·源代码管理·机顶盒
catchadmin38 分钟前
PHP 现在可以零成本构建原生 iOS 和 Android 应用 NativePHP for Mobile v3 发布
android·ios·php
独自破碎E1 小时前
【回溯】二叉树的所有路径
android·java
安卓机器1 小时前
安卓玩机工具推荐------免root权限使用的 支持安卓4至安卓16系统的系统开源应用 推荐
android·反编译apk
summerkissyou19871 小时前
android-蓝牙-广播启动-startAdvertising和startAdvertisingSet区别
android·蓝牙
雪球Snowball1 小时前
【Android关键流程】Configuration变更时更新应用程序配置
android
h7ml1 小时前
于 CompletableFuture 的异步编排优化企业微信通知发送性能
android·windows·企业微信
子木鑫1 小时前
[SUCTF 2019] CheckIn1 — 利用 .user.ini 与图片马构造 PHP 后门并绕过上传检测
android·开发语言·安全·php