Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Gradle 升级 kotlin 踩坑 #543

Open
ythy opened this issue Oct 17, 2024 · 0 comments
Open

Android Gradle 升级 kotlin 踩坑 #543

ythy opened this issue Oct 17, 2024 · 0 comments

Comments

@ythy
Copy link
Owner

ythy commented Oct 17, 2024

其实最大的坑是各种仓库网络链接错误

Studio 版本

先升级了IDE,版本信息如下

Android Studio Ladybug | 2024.2.1 Canary 8
Build #AI-242.20224.300.2421.12279409, built on August 27, 2024
Runtime version: 21.0.3+-12099254-b509.4 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.awt.windows.WToolkit
Windows 10.0
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 2048M

Gradle 版本

其实试了好几个版本,最终用的是7.5. 这里的url可以直接上镜像。
这里有一个坑 之前用的URL都是bin结尾的,下载没问题编译报错!改成了all 正常。配置如下:

#Tue Mar 19 09:55:12 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.5-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

几个概念

plugin设置方法

plugins {
`«plugin id»` // 对于核心 Gradle 插件
id(«plugin id») // 对于构建脚本已经可用的核心 Gradle 插件或插件
id(«plugin id») version «plugin version» [apply «false»]
//对于需要解析的二进制 Gradle 插件.
//带有 boolean 的 apply 语句可用于禁用立即应用插件的默认行为(例如,您只想在 subprojects 中应用它)。
}

整体结构

settings.gradle.kts
include(":hello-a")

build.gradle.kts
plugins {
    id("com.example.hello") version "1.0.0" apply false
    id("com.example.goodbye") version "1.0.0" apply false
}

hello-a/build.gradle.kts
plugins {
    id("com.example.hello")
}

repositories官方说明

When your dependency is something other than a local library or file tree, Gradle looks for the files in whichever online repositories are specified in the dependencyResolutionManagement { repositories {...} } block of your settings.gradle file. The order in which you list each repository determines the order in which Gradle searches the repositories for each project dependency. For example, if a dependency is available from both repository A and B, and you list A first, Gradle downloads the dependency from repository A.

By default, new Android Studio projects specify Google's Maven repository, and the Maven central repository as repository locations in the project's settings.gradle file, as shown below:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

正片开始

settings.gradle.kts 职责加强

pluginManagement 设置插件相关的资源路径。这里的resolutionStrategy 因为有的插件格式不标准,需要重新定义一下名字。
dependencyResolutionManagement 设置依赖相关的资源路径, 这里设置过的话,就不能在build.gradle.kts里设置了

pluginManagement {
    repositories {
        maven("https://maven.aliyun.com/repository/public/")
        maven("https://maven.aliyun.com/repository/central")
        maven("https://maven.aliyun.com/repository/gradle-plugin")
        mavenLocal()
        mavenCentral()
        google()
        gradlePluginPortal()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "io.objectbox") {
                useModule("io.objectbox:objectbox-gradle-plugin:${requested.version}")
            }
        }
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven("https://maven.aliyun.com/repository/public/")
        maven("https://maven.aliyun.com/repository/central")
        maven("https://maven.aliyun.com/repository/gradle-plugin")
        mavenLocal()
        mavenCentral()
        google()
        gradlePluginPortal()
    }
}


rootProject.name = "XXXXX"
include(":app")

Project : build.gradle.kts

这里去掉了以前的 buildScript classpath相关的写法,只保留了plugins
并且在gradle\libs.versions.toml 里做常量管理

plugins {
    //`kotlin-dsl` apply false
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.kotlin.kapt) apply false
    alias(libs.plugins.objectbox) apply false
}
[versions]
kotlin =  "1.9.0"
agp = "7.4.2"
objectBox = "4.0.2"
jGit = "5.8.0.202006091008-r" #5.8.0.202006091008-r 5.3.0.201903130848-r "3.7.0.201502260915-r"//6.8.0.202311291450-r
material = "1.9.0" #1.2.0-alpha05
appcompat = "1.6.1"
constraintlayout = "2.0.4"
navigation = "2.3.5"
junit = "4.12"
dagger = "2.8"
jsr = "1.0"
inject = "1"
pinyin4j = "2.5.0"
gson = "2.9.0"
okhttp = "4.12.0"
zip4j = "2.11.5"
jnanoid = "2.0.0"


[libraries]
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
androidx-navigation-fragment-ktx = { module = "androidx.navigation:navigation-fragment-ktx", version.ref = "navigation" }
androidx-navigation-ui-ktx = { module = "androidx.navigation:navigation-ui-ktx", version.ref = "navigation" }
junit = { module = "junit:junit", version.ref = "junit" }
dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "dagger" }
dagger = { module = "com.google.dagger:dagger", version.ref = "dagger" }
jsr250 = { module = "javax.annotation:jsr250-api", version.ref = "jsr" }
inject = { module = "javax.inject:javax.inject", version.ref = "inject" }
pinyin4j = { module = "com.belerweb:pinyin4j", version.ref = "pinyin4j" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
material = { module = "com.google.android.material:material", version.ref = "material" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
jgit= { module = "org.eclipse.jgit:org.eclipse.jgit", version.ref = "jGit" }
jgit-ssh-jsch = { module = "org.eclipse.jgit:org.eclipse.jgit.ssh.jsch", version.ref = "jGit" }
zip4j = { module = "net.lingala.zip4j:zip4j", version.ref = "zip4j" }
jnanoid = { module = "com.aventrix.jnanoid:jnanoid", version.ref = "jnanoid" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
objectbox = { id = "io.objectbox", version.ref = "objectBox" }

app: build.gradle.kts

  1. 这里的plugins 用一个获取ID的方法,不用直接写
  2. 常量用object管理,本来应该在buildSrc里写,但是网络问题,依赖下载不下来,先写在这里。
  3. android下面一系列的变更,对比之前主要要写等号,双引号。
  4. dependencies 下面的依赖也改成了用 toml管理
plugins {
    id(libs.plugins.android.application.get().pluginId)
    id(libs.plugins.kotlin.android.get().pluginId) // Only for Kotlin projects
    id(libs.plugins.kotlin.kapt.get().pluginId)// Only for Kotlin projects
    id(libs.plugins.objectbox.get().pluginId) // Apply last
}

object Configuration {
    const val compileSdk = 34
    const val targetSdk = 34
    const val minSdk = 31
    const val minSdkDemo = 31
    const val majorVersion = 1
    const val minorVersion = 0
    const val patchVersion = 12
    const val versionName = "$majorVersion.$minorVersion.$patchVersion"
    const val versionCode = 13
    const val snapshotVersionName = "$majorVersion.$minorVersion.${patchVersion + 1}-SNAPSHOT"
    const val artifactGroup = "com.mx.xxx"
}

@Suppress("UnstableApiUsage")
android {
    namespace = "com.mx.xxx"
    compileSdk = Configuration.compileSdk

    defaultConfig {
        applicationId = "com.mx.emperor"
        minSdk = Configuration.minSdk
        targetSdk = Configuration.targetSdk
        versionCode = Configuration.versionCode
        versionName = Configuration.versionName
        ndk {
            abiFilters.addAll(listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64"))
            // Filter for architectures supported by Flutter
        }
    }

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

    signingConfigs {
        getByName("debug") {
            storeFile = rootProject.file("sign/debug.keystore")
            keyAlias = "androiddebugkey"
            keyPassword = "android"
            storePassword = "android"
        }
        create("release") {
            storeFile = rootProject.file("sign/debug.keystore")
            keyAlias = "androiddebugkey"
            keyPassword = "android"
            storePassword = "android"
        }
    }

    lint {
        checkReleaseBuilds = false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError = false
    }

    buildFeatures {
        viewBinding = true
        //compose = true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.navigation.fragment.ktx)
    implementation(libs.androidx.navigation.ui.ktx)

    testImplementation(libs.junit)
    kapt(libs.dagger.compiler)
    implementation(libs.dagger)
    compileOnly(libs.jsr250)
    implementation(libs.inject)
    implementation(libs.pinyin4j)
    implementation(libs.kotlin.stdlib)
    implementation(libs.gson)
    implementation(libs.material)
    implementation(libs.okhttp)
    implementation(libs.jgit)
    implementation(libs.jgit.ssh.jsch)
    implementation(libs.zip4j)
    implementation(libs.jnanoid)


    //implementation(project(":flutter"))
//    implementation("com.github.skydoves:orchestra-spinner:1.2.0")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant