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

相关推荐
柯南二号4 小时前
Android Studio根目录下创建多个可运行的模块
android·ide·android studio
恋猫de小郭7 小时前
Compose Multiplatform iOS 稳定版发布:可用于生产环境,并支持 hotload
android·flutter·macos·ios·kotlin·cocoa
音视频牛哥9 小时前
把Android设备变成“国标摄像头”:GB28181移动终端实战接入指南
android·音视频·大牛直播sdk·gb28181安卓端·gb28181对接·gb28181平台对接·gb28181监控
tangweiguo0305198710 小时前
Jetpack Compose 响应式布局实战:BoxWithConstraints 完全指南
android
難釋懷10 小时前
Android开发-视图基础
android
Anthony_sun13 小时前
UniAppx 跳转Android 系统通讯录
android·uniapp
温柔的小猪竹17 小时前
android中的背压问题及解决方案
android
小妖66617 小时前
uni-app 引入vconsole web端正常,安卓端报错 Cannot read property ‘sendBeacon‘ of undefined
android·前端·uni-app
努力学习的小廉17 小时前
深入了解linux系统—— 进程控制
android·linux·服务器
帅次20 小时前
Flutter TabBar / TabBarView 详解
android·flutter·ios·小程序·iphone·taro·reactnative