Gradle

Posted by ooftf on April 12, 2021

Gradle官方文档

强制使用指定版本

configurations.all {

1
2
3
4
5
6
7
8
9
10
resolutionStrategy {
    force 'com.android.support:appcompat-v7:26.1.0'
    force "com.android.support:support-v4:${supportVersion}"
    force "com.android.support:appcompat-v7:${supportVersion}"
    force "com.android.support:design:${supportVersion}"
    force "com.android.support:recyclerview-v7:${supportVersion}"
    force "com.android.support:cardview-v7:${supportVersion}"
    force "com.android.support:gridlayout-v7:${supportVersion}"
    force "com.android.support:support-annotations:${supportVersion}"
} }

app module的build.gradle下添加以下代码 configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == ‘com.squareup.okhttp3’ && details.requested.name == ‘okhttp’) { details.useVersion “3.12.1” } } }

configurations.all {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
resolutionStrategy {
    force 'com.google.code.gson:gson:2.8.5'
    force 'com.github.bumptech.glide:glide:4.11.0'
    force "androidx.appcompat:appcompat:1.2.0"
} } ## exclude * exclude 在pom文件中不会起作用,只是在本项目中起作用,如果打包成maven依赖,exclude不起作用 #### 排除指定引用 dependencies {
implementation('some-library') {
    exclude group: 'com.example.imgtools', module: 'native'
}
implementation('com.a.b:cc-dd:1.0.0') {
        exclude group: 'com.abc.def', module: 'yyy'
} } #### 在所有库中排除指定引用   (待验证) 在app 的build.gradle 跟节点使用 ```groovy configurations.all {
exclude group: 'com.ooftf', module: 'http-ui-mapping' } ``` #### 排除测试Variants指定库 在app 的build.gradle 跟节点使用 ```groovy android.testVariants.all { variant ->
variant.getCompileConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
variant.getRuntimeConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp' } ```

transitive = false

publishDebugPublicationToMavenLocal,publishToMavenLocal即不用debugImplementation也不用releaseImplementation

查看更多错误信息

1
gradlew build --info --stacktrace --debug --scan

## kotlinOptions { jvmTarget = ‘1.8’ }

Didn’t find class “androidx.core.app.CoreComponentFactory”

1
2
3
4
5
6
7
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}

dataBinding

1
2
3
buildFeatures {
       dataBinding true
   }

关闭Javadoc

1
2
3
4
5
6
在project build.gradle中添加
allprojects {
    tasks.withType(Javadoc).all {
        enabled = false
    }
}

不使用缓存版本

1
2
3
4
5
6
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
implementation ('xxxx') {
        changing = true
}

AS上运行Java文件的main()方法时报错

在.idea/gradle.xml文件中添加如下代码,然后重新运行就可以了

1
2
3
   <GradleProjectSettings>
        <option name="delegatedBuild" value="false" />
   </GradleProjectSettings>

maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apply plugin: 'maven-publish'
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId = 'com.github.ooftf'
                artifactId = project.name
                version = '1.0.0-SNAPSHOT'
                artifact generateSourcesJar
            }
        }
        repositories {
            maven {
                // release  https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/
                name = 'sonatypeRepository'
                url = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
                credentials {
                    username = "ooftf"
                    password = "*****"
                }
            }
        }
    }
}
task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier 'sources'
}


maven {
            url 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
    }

Received status code 400 from server: Bad Request

snapshot仓库没有加SNAPSHOT

javadoc 乱码问题

module gradle 添加

1
2
3
4
5
6
tasks.withType(Javadoc) {
    enabled = false
    options.addStringOption('Xdoclint:none', '-quiet')
    options.addStringOption('encoding', 'UTF-8')
    options.addStringOption('charSet', 'UTF-8')
}

使用 Java 8

android { … // Configure only for each module that uses Java 8 // language features (either in its source code or // through dependencies). compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // For Kotlin projects kotlinOptions { jvmTarget = “1.8” } }

修改JDK版本

  1. File
  2. Project Structure
  3. SDK Location
  4. JDK Location