android studio之build.gradle写法简介

本文介绍如何在Android Studio中手动编辑build.gradle文件来添加JAR依赖、配置网络依赖及处理本地代码编译等问题。具体包括批量添加libs目录下的JAR包、引入网络上的依赖库、设置NDK编译选项等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多时候,studio的IDE并不能满足需求,在以下场景下,我们需要手动修改build.gradle文件来改变配置


为工程添加libs目录下所有JAR依赖

在module settings中为module增加dependence时,有如下选项:


第一项可以添加网络依赖的support jar包,第二项可以增加单个jar的依赖,第三个可以增加module依赖。

libs目录下有成吨的jar,每次添加都要修改配置,万一忘记就不好了。

可以在dependencies节点添加如下内容:

    compile fileTree(dir: 'src/main/libs', include: ['*.jar'])
用IDE添加单个JAR依赖后,build.gradle文件增加的对应的内容是“compile file”

增加网络JAR依赖

support-v4等包比较神烦,为了统一管理,可以直接引用。

不过好像直接用IDE也能修改,在dependencies节点下面增加:

    compile 'com.android.support:support-v4:19.1.0'

这段也是在depenencies下面,可以和上一段进行合并。。


修改了libs目录

修改了libs目录,也要修改配置,在android节点下增加如下内容:
sourceSets.main {
    jniLibs.srcDirs 'src/main/libs'
}

在NDK中引用了一些库

我的jni层使用的是C++代码,C++和C的JNI函数还是有区别的。

我的工程中使用了android的log以及C++的STL库。

之前是eclipse的工程,在C++代码中包含了:

#include <android/log.h>
而在jni的Application.mk文件中添加了STL库支持:
APP_STL := stlport_static
直接在命令行中NDK-BUILD没有错误,导出到studio之后编译找不到log以及STL。

现在在build.gradle文件的android.defaultConfig节点下加入如下内容:

ndk {
    moduleName "aa"
    stl "stlport_static"
    ldLibs "log"
}
其中aa是我的so库的名字。


最后,我的配置文件粘贴如下供参考:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.sample"
        minSdkVersion 14
        targetSdkVersion 19

        ndk {
            moduleName "aa"
            stl "stlport_static"
            ldLibs "log"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets.main {
        jniLibs.srcDirs 'src/main/libs'
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile fileTree(dir: 'src/main/libs', include: ['*.jar'])
}


在android stduio编译时不重新编译native代码

这点很关键。每次编译的时候都编译一遍native层的代码,你是不是已经够了,这也是很多人说studio慢的另一个原因(第一个原因参考Gradle Build速度加快终极方法)。

编译时禁用native编译,在sourceSets.main下面加入:

jni.srcDirs = [] //disable automatic ndk-build call


<think>我们正在将项目的构建脚本从Groovy(build.gradle)转换为KotlinDSL(build.gradle.kts)。以下是依赖项的转换:原Groovy依赖配置:dependencies{//Retrofitimplementation'com.squareup.retrofit2:retrofit:2.9.0'implementation'com.squareup.retrofit2:converter-gson:2.9.0'//MaterialDesignimplementation'com.google.android.material:material:1.9.0'//RecyclerViewimplementation'androidx.recyclerview:recyclerview:1.3.1'//Lifecyclecomponentsimplementation'androidx.lifecycle:lifecycle-extensions:2.2.0'}转换为KotlinDSL(build.gradle.kts)的格式如下:注意:在KotlinDSL中,我们使用双引号,并且依赖项配置使用字符串。同时,在KotlinDSL中,我们使用`implementation`等作为函数调用。</think> 以下是将 `build.gradle` (Groovy) 转换为 `build.gradle.kts` (Kotlin DSL) 的完整解决方案: ### Gradle Kotlin DSL 转换后的代码 ```kotlin plugins { id("com.android.application") // 如果你使用的是 library,使用 id("com.android.library") kotlin("android") version "1.9.0" // 确保与你的项目匹配 } android { compileSdk = 34 // 其他 Android 配置... } dependencies { // Retrofit implementation("com.squareup.retrofit2:retrofit:2.9.0") implementation("com.squareup.retrofit2:converter-gson:2.9.0") // Material Design implementation("com.google.android.material:material:1.9.0") // RecyclerView implementation("androidx.recyclerview:recyclerview:1.3.1") // Lifecycle components implementation("androidx.lifecycle:lifecycle-extensions:2.2.0") // 更多声明方式见下方说明 } // 其他配置... ``` ### 关键转换要点解释 1. **语法差异**: - Groovy 使用单引号 `' '`,Kotlin DSL 使用双引号 `" "` - Groovy 用 `=` 赋值,Kotlin DSL 中直接调用方法 - Groovy 的 `compileSdkVersion 34` → Kotlin DSL 的 `compileSdk = 34` 2. **依赖声明**: ```kotlin // Groovy 写法: // implementation 'com.squareup.retrofit2:retrofit:2.9.0' // Kotlin DSL 写法: implementation("com.squareup.retrofit2:retrofit:2.9.0") ``` 3. **变量处理**: ```kotlin // Groovy: def retrofitVersion = "2.9.0" // Kotlin: val retrofitVersion = "2.9.0" implementation("com.squareup.retrofit2:retrofit:$retrofitVersion") ``` 4. **插件声明**: ```kotlin plugins { id("com.android.application") version "8.0.2" // 版本可选 kotlin("android") version "1.9.0" } ``` 5. **设置存储库**: ```kotlin repositories { google() mavenCentral() } ``` ### 额外配置示例 #### 1. 多模块设置 ```kotlin // settings.gradle.kts pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "MyApp" include(":app") ``` #### 2. 自定义任务 ```kotlin tasks.register("hello") { doLast { println("Hello from Kotlin DSL!") } } ``` #### 3. Build Types & Flavor 配置 ```kotlin android { buildTypes { getByName("debug") { isMinifyEnabled = false } create("staging") { initWith(getByName("debug")) matchingFallbacks += "debug" } } productFlavors { create("free") { dimension = "pricing" } create("pro") { dimension = "pricing" } } } ``` ### 转换完整示例 **原 build.gradle (Groovy)**: ```groovy apply plugin: 'com.android.application' android { compileSdkVersion 34 defaultConfig { applicationId "com.example.myapp" minSdkVersion 21 targetSdkVersion 34 versionCode 1 versionName "1.0" } } dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' } ``` **转换后 build.gradle.kts**: ```kotlin plugins { id("com.android.application") } android { compileSdk = 34 defaultConfig { applicationId = "com.example.myapp" minSdk = 21 targetSdk = 34 versionCode = 1 versionName = "1.0" } } dependencies { implementation("com.squareup.retrofit2:retrofit:2.9.0") } ``` ### 转换工具建议 1. **官方迁移工具**: - 在 Android Studio 中尝试 "Convert to Kotlin DSL" - 通过 Gradle 命令:`./gradlew kotlinDslAccessorsReport` 2. **辅助资源**: - [官方迁移文档](https://docs.gradle.org/current/userguide/kotlin_dsl.html) - [Android Kotlin DSL 示例](https://github.com/android/gradle-recipes) ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值