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

相关推荐
Java猿_1 小时前
Spring Boot 集成 Sa-Token 实现登录认证与 RBAC 权限控制(实战)
android·spring boot·后端
Jerry4 小时前
Jetpack Compose Navigation
android
介一安全4 小时前
【Frida Android】实战篇17:Frida检测与绕过——基于inline hook的攻防实战
android·网络安全·逆向·安全性测试·frida
龙之叶6 小时前
Android如何通过adb命令push文件后在媒体库中显示
android·adb
Just_Paranoid6 小时前
【Settings】Android 设备信息相关参数的获取
android·5g·wifi·ip·sn·network
StarShip7 小时前
SystemServer类 与 system_server进程
android
画个太阳作晴天8 小时前
Android App 跟随系统自动切换白天/黑夜模式:车机项目实战经验分享
android·android studo
成都大菠萝8 小时前
2-2-2 快速掌握Kotlin-语言的接口默认实现
android
代码s贝多芬的音符8 小时前
android webview 打开相机 相册 图片上传。
android·webview·webview打开相机相册
游戏开发爱好者88 小时前
抓包工具有哪些?代理抓包、数据流抓包、拦截转发工具
android·ios·小程序·https·uni-app·iphone·webview