适配 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

相关推荐
TeleostNaCl11 分钟前
使用 Android Jetpack 中的 Startup 组件快速实现组件初始化逻辑与主模块解耦
android·经验分享·android jetpack·androidx·android runtime·jetpack android
恋猫de小郭23 分钟前
让 AI 用 Flutter 实现了猗窝座的破坏杀·罗针动画,这个过程如何驯服 AI
android·前端·flutter
2501_9371931411 小时前
技术加持!PLB-TV:HDR10+UDP 传输
android·源码·源代码管理·机顶盒
霸王大陆13 小时前
《零基础学 PHP:从入门到实战》模块十:从应用到精通——掌握PHP进阶技术与现代化开发实战-2
android·开发语言·php
低调小一16 小时前
在 Android 上获取视频流逐帧时间戳并与 GPS/IMU 对齐(CameraX 实践)
android
Android_Trot17 小时前
Flutter android 多渠道配置,多包名、icon、等配置。
android·flutter
zhangphil18 小时前
Android性能:trace上的锁竞争monitor contention with owner at
android
砖厂小工18 小时前
Now In Android 精讲 9 - Benchmark 与 Baseline Profile
android
愤怒的代码19 小时前
深入解析 SystemUI 依赖注入:Dagger2 与 Hilt 核心机制重温
android·dagger