Gradle问题: Android Studio 如何生成jar,生成jar出错如何处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yi_master/article/details/81222193

在升级Android Studio到3.2 Canary 11之后, 项目中无法正常打jar包,报错如下

Execution failed for task ':migutvbox:lint'.
> Could not resolve all files for configuration ':xxxx:lintClassPath'.
   > Could not resolve com.android.tools.lint:lint-gradle:26.1.2.
     Required by:
         project :xxxx
      > Could not resolve com.android.tools.lint:lint-gradle:26.1.2.
         > Could not get resource 'https://jcenter.bintray.com/com/android/tools/lint/lint-gradle/26.1.2/lint-gradle-26.1.2.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/com/android/tools/lint/lint-gradle/26.1.2/lint-gradle-26.1.2.pom'.
               > Read timed out
 1:先来看下  如何使用task来打jar包
  1. 新建一个任务,即在module的build.gradle文件添加task
  2. 指定需要打包的类
  3. 去除不需要打包的类

详细代码如下

   //makeRealeaseJar为task的名称
    task makeRealeaseJar(type: Jar) {
        //从class文件生成jar包
        from file('build/intermediates/classes/release')
        //对jar包命名
        archiveName = '50008.jar'
        //生成jar的位置
        destinationDir = file('build/libs')
        //过滤不需要的class
        exclude "**/**/BuildConfig.class"
        exclude "**/**/BuildConfig\$*.class"
        exclude "**/R.class"
        exclude "**/R\$*.class"

        //指定打包的class
        include "com/migu/tv/**/*.class"
    }
    makeRealeaseJar.dependsOn(build)

最后你会在Gradle project中发现这个task的存在
这里写图片描述

双击这个红色方框,即可完成对指定文件打成jar

 2:再来看下  Could not resolve all files for configuration ':xxxx:lintClassPath如何处理

解决方案

在module的build.gradle文件中的repositories中添加google(),代码如下

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

你还有可能遇到其他问题,保险起见添加上lintOptions

android {

    lintOptions {
        abortOnError false
        //tasks.lint.enabled = false

        lintConfig file("${project.rootDir}/config/quality/lint/lint.xml")
        // if true, generate an HTML report (with issue explanations, sourcecode, etc)

        //htmlReport true
        // optional path to report (default will be lint-results.html in the builddir)
        //htmlOutput file("$project.buildDir/reports/lint/lint.html")

        xmlReport true
        xmlOutput file("$project.buildDir/reports/lint/lint-results.xml")
    }
 }

1:为什么添加了google()选项之后,就能正确编译?

其根本原因还是Gradle插件不匹配导致的

2:jcenter()含义

他指明了gradle从哪里获取jar包,如

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile files('libs/AuthClient_v1.0.3.jar')
    compile 'com.android.support:support-v4:23.1.1'
//    compile 'com.android.support:appcompat-v7:23.1.1'
//    compile 'com.android.support:support-v13:23.0.1'
//    compile 'com.android.support:design:23.0.1'
}

就指明了com.android.support:support-v4:23.1.1这个jar是从jcenter
中获取的

猜你喜欢

转载自blog.csdn.net/yi_master/article/details/81222193