android 从Groovy迁移到科特林DSL Gradle

代码示例
超详细的实例教程,先记录日后整理。

搜索

报名

登录

从Groovy迁移到科特林DSL Gradle(第1部分)
Nicos Nicolaou
尼科斯·尼古拉

·
遵循

4分钟读取
·
二〇二三年十二月四日
10

在本文中,我们将看到从Groovy迁移到新的科特林DSL Gradle文件。我决定写这篇文章,因为当我尝试迁移我的项目Gradle文件时,我花了很多时间从我的个人项目中找到所有正确的代码,从不同的来源,从官方Android开发者网站,教程,论坛等。本文将分为两部分。

注意:在本文中,我将展示Gradle文件的一些基础知识和常见代码,开发人员通常在他们的Gradle文件中。

迁移
首先,将Gradle文件从build.gradle重命名为build.gradle.kts。我们可以开始迁移了。

Groovy

plugins {
id ‘com.android.application’
id ‘kotlin-android’
}
科特林

plugins {
id(“com.android.application”)
id(“kotlin-android”)
}
Groovy

android {
signingConfigs {
release {
storeFile file(“…/app/keystoreFileName.keystore or keystoreFileName.jks”)
storePassword “storePassword”
keyAlias “keyAlias”
keyPassword “keyPassword”
}
}
//…
}
科特林

android {
signingConfigs {
create(“config”) {
keyAlias = “keyAlias”
keyPassword = “keyPassword”
storeFile = file(“…/app/keystoreFileName.keystore or keystoreFileName.jks”“”)
storePassword = “storePassword”
}
}
//…
}
Groovy

android {
//…
buildToolsVersion ‘34.0.0’
compileSdkVersion 34
namespace ‘com.myproject.myproject’
//…
}
科特林

android {
//…
buildToolsVersion = “34.0.0”
compileSdk = 34
namespace = “com.myproject.myproject”
//…
}
Groovy

defaultConfig {
applicationId “com.myproject.myproject”
minSdkVersion 27
targetSdkVersion 34
versionCode 1
versionName “1.0.0”
testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner”
multiDexEnabled true

    buildFeatures {
        dataBinding true
        viewBinding true
        compose true
    }

    javaCompileOptions {
        annotationProcessorOptions {
            arguments += [
                    "room.incremental":"true"]
        }
    }

    vectorDrawables {
        useSupportLibrary true
    }

    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    ndk {
        abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
    }
    externalNativeBuild {
        cmake {
            cppFlags "-std=c++11 -fexceptions"
        }
    }
}

科特林

defaultConfig {
applicationId = “com.myproject.myproject”
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = “1.0.0”
testInstrumentationRunner = “androidx.test.runner.AndroidJUnitRunner”
multiDexEnabled = true

    buildFeatures {
        dataBinding = true
        viewBinding = true
        compose = true
    }

    javaCompileOptions {
        annotationProcessorOptions {
            arguments += mapOf(
                "room.incremental" to "true"
            )
        }
    }

    vectorDrawables {
        useSupportLibrary = true
    }

    buildTypes {
        release {
            isShrinkResources = true
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
        debug {
            isShrinkResources = true
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }

    ndk {
        abiFilters.add("armeabi-v7a")
        abiFilters.add("arm64-v8a")
        abiFilters.add("x86")
        abiFilters.add("x86_64")
    }
    externalNativeBuild {
        cmake {
            cppFlags("-std=c++11 -fexceptions")
        }
    }
}

Groovy

externalNativeBuild {
cmake {
path “src/main/cpp/CMakeLists.txt”
version “3.22.1”
}
}
ndkVersion ‘26.1.10909125’
科特林

externalNativeBuild {
cmake {
path = file(“src/main/cpp/CMakeLists.txt”)
version = “3.22.1”
}
}
ndkVersion = “26.1.10909125”
Groovy

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
科特林

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
Groovy

composeOptions {
kotlinCompilerExtensionVersion = ‘1.5.4’
}

packagingOptions {
resources {
excludes += ‘/META-INF/{AL2.0,LGPL2.1}’ //for compose
}
}
科特林

composeOptions {
kotlinCompilerExtensionVersion = “1.5.4”
}

packaging {
resources {
excludes += “/META-INF/{AL2.0,LGPL2.1}” //for compose
}
}
Groovy

flavorDimensions “version”
productFlavors {
dev{
applicationId “com.myproject.myproject.dev”
minSdkVersion 27
targetSdkVersion 37
versionCode 1
versionName “1.0.0”
flavorDimensions “version”
resValue “string”, “app_name”, “App Name dev”
buildConfigField “String”, “url”, ““https://testServer.com””
}
pro{
applicationId “com.myproject.myproject”
minSdkVersion 27
targetSdkVersion 34
versionCode 1
versionName “1.0.0”
flavorDimensions “version”
resValue “string”, “app_name”, “App Name”
buildConfigField “String”, “url”, ““https://liveServer.com””
signingConfig signingConfigs.release
}
}
科特林

flavorDimensions += “version”
productFlavors {
create(“dev”) {
applicationId = “com.myproject.myproject.dev”
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = “1.0.0”
dimension = “version”
resValue(“string”, “app_name”, “App Name dev”)
buildConfigField(“String”, “url”, ““https://testServer.com””)
}
create(“pro”) {
applicationId = “com.myproject.myproject”
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = “1.0.0”
dimension = “version”
resValue(“string”, “app_name”, “App Name”)
buildConfigField(“String”, “url”, ““https://liveServer.com””)
signingConfig = signingConfigs.getByName(“config”)
}
}
Groovy

ext {
activityVersion = ‘1.8.1’
}

dependencies {
implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
implementation “androidx.activity:activity-ktx:$activityVersion”
}
科特林

val activityVersion by extra(“1.8.1”)

dependencies {
implementation(fileTree(mapOf(“dir” to “libs”, “include” to listOf(“*.jar”))))
implementation(“androidx.activity:activity-ktx:$activityVersion”)
}
希望你觉得这篇文章有用。我希望我能帮助你在一个地方找到Gradle的基本代码,而不是花时间搜索它们。让我知道你的意见下的评论,随时报告任何问题。

接下来,您可以检查本文的第2部分,它显示了其他Gradle文件。

从Groovy迁移到科特林DSL Gradle(第2部分)
本文是从Groovy迁移到新的科特林DSL Gradle文件的第2部分。我们将看到两个…
medium.com

参考文献:

将构建配置从Groovy迁移到科特林|Android Studio| Android开发者
将Gradle配置文件从Groovy迁移到科特林。
developer.android.com

科特林DSL现在是新Gradle构建的默认值
今天,我们很高兴地宣布,我们将把构建脚本的默认语言切换到科特林!我们最近
android-developers.googleblog.com

Gradle科特林DSL入门
Gradle的科特林DSL为传统的Groovy DSL提供了一种替代语法,并在.
docs.gradle.org

科特林DSL现在是新Gradle构建的默认值
用于Gradle的科特林DSL于2016年8月在Gradle构建工具的3.0版本中引入,并于2016年12月作为1.0版本发布。
blog.gradle.org

Android开发
Android应用程序开发
Android开发
Gradle
Gradle科特林DSL
10

Nicos Nicolaou
Written by Nicos Nicolaou
16 Followers
BSc Computer Science, MSc Mobile Systems, Android and Flutter Developer, Google Play Developer, Programming, Guitar. 👇🔗 https://github.com/NicosNicolaou16

Follow

More from Nicos Nicolaou
Foreground Service in Android
Nicos Nicolaou
Nicos Nicolaou

Foreground Service in Android
For this article we will see the setup for the foreground services in Android and specific we will see the setup for the foreground…
4 min read
·
Dec 4, 2023
14

1

NDK Setup on Android/Flutter Android Project
Nicos Nicolaou
Nicos Nicolaou

NDK Setup on Android/Flutter Android Project
For this article, we will see the setup for the NDK on Android and Android Flutter project, otherwise, how we call…
5 min read
·
Jan 6
1

Migration from Groovy to Kotlin DSL Gradle (Part 2)
Nicos Nicolaou
Nicos Nicolaou

Migration from Groovy to Kotlin DSL Gradle (Part 2)
This article is the part 2 for migration from the Groovy to the new Kotlin DSL Gradle file. We will see the two remaining Gradle files.
2 min read
·
Dec 4, 2023
11

Android TV Application (Jetpack Compose and Flutter)
Nicos Nicolaou
Nicos Nicolaou

Android TV Application (Jetpack Compose and Flutter)
For this article we will see the setup for an Android TV Application with Android Jetpack Compose and Flutter, tested on Android.
2 min read
·
Nov 22, 2023
6

1

See all from Nicos Nicolaou
Recommended from Medium
Build and Publish multi-module Android library to Maven Central
Jayesh Seth
Jayesh Seth

Build and Publish multi-module Android library to Maven Central
Learn to build and publish your single or multi-module Android library to Maven Central!
14 min read
·
Aug 31, 2023
3

Android Permissions Unveiled: A Developer’s Insight
Raghav Aggarwal
Raghav Aggarwal

in

ProAndroidDev

Android Permissions Unveiled: A Developer’s Insight
I believe that writing code is not hard, but understanding why we write it is.
10 min read
·
Jan 9
69

Lists
Photo of graphic image face

Medium’s Huge List: Publications Accepting Story Submissions
226 stories
·
1507 saves
Getting Payment Card Information with NFC
ibrahimertanylmz
ibrahimertanylmz

in

Huawei Developers

Getting Payment Card Information with NFC
Hi everyone! 👋 This article is based on Getting Payment Card Information using NFC for an Android Application in Kotlin.
4 min read
·
Oct 25, 2023
315

Kotlin’s Enum vs Sealed: Choosing the Right Type for Your Data
Tippu Fisal Sheriff
Tippu Fisal Sheriff

Kotlin’s Enum vs Sealed: Choosing the Right Type for Your Data
A Practical Guide to Enum Classes and Sealed Classes in Kotlin
3 min read
·
Jan 8
107

Why Migrate Your Build Configurations from Groovy to Kotlin DSL(KTS)?
Aritra Das
Aritra Das

in

Stackademic

Why Migrate Your Build Configurations from Groovy to Kotlin DSL(KTS)?
Hey, Android Devs 👋. I hope that you are recently seeing posts & videos regarding the migration of build.gradlefrom Groovy to Kotlin…
5 min read
·
Aug 21, 2023
19

GITHUB ACTIONS CANDY CRUSH CI/CD
Ajay Kumar Yegireddi
Ajay Kumar Yegireddi

GITHUB ACTIONS CANDY CRUSH CI/CD
In today’s fast-paced world of software development, automation is the name of the game. GitHub Actions is the ace up the sleeve of modern…
1 min read
·
Jan 8
60

See more recommendations
Help

Status

About

Careers

Blog

Privacy

Terms

Text to speech

Teams

腾讯翻译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值