Android Gradle常用配置

0 Gradle 依赖总结

1 Gradle 编译,lintb报错
配置: android-lintOptions - 屏蔽
android {
lintOptions {
abortOnError false
// if true, only report errors.
ignoreWarnings true
}
}

2 Gradle 添加aar
配置: android-repositories- 添加libs
android {
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
compile( name : 'com.netposa.ffmpeg' , ext : 'aar' )
}

3 Gradle JNI配置
默认放入jniLibs 中
如果 libs/aar 文件中有 libs/包含so
需要: 指定jni 路径
android {
sourceSets {
main {
jniLibs.srcDirs = [ 'libs' ]
}
}

4 Gradle 修改数据表结构
需要:增加Version 版本号
android {
greendao {
//版本号,升级时可配置
schemaVersion 2
}
}

5 Gradle 模块间的jar 引用
在主模块中,通过api可以复用子模块中的jar 包引用
api project( ':jpush' )
dependencies {
api files( 'libs/jcore-android-1.1.9.jar' )
api files( 'libs/jpush-android-3.1.1.jar' )
}

6 Gradle rootProject
需要:
通过在根目录下,设置ext 对象
ext {
// Sdk and tools
compileSdkVersion = 26
}
子模块中可以通过
compileSdkVersion rootProject.ext.compileSdkVersion
minSdkVersion rootProject.ext.minSdkVersion

7 Gradle中配置Multidex  
android {    
    defaultConfig {         // Enabling multidex support.        
        multiDexEnabled true    
    }
}
dependencies {   compile 'com.android.support:multidex:1.0.1' }
Myapplication要继承 android.support.multidex. MultiDexApplication

8 Gradle Version name Hash值配置
1 新建 common.gradle 配置文件
gradle.allprojects {
ext.getGitHeadRefsSuffix = {
if (ext.releaseVer){
return "" ;
}
try {
def headFile = new File( '.git/HEAD' )
if (headFile.exists()) {
String[] strings = headFile.getText( 'UTF-8' ).split( " " );
if (strings.size() > 1 ) {
String refFilePath = '.git/' + strings[ 1 ];
def refFile = new File(refFilePath.replace( " \n " , "" ));
return "_" + refFile.getText( 'UTF-8' ).substring( 0 , 8 )
}
}
} catch (Exception e) {
e.printStackTrace()
}
return ""
}
}
2 在VersionName的gradle处引用
apply from : 'common.gradle'
3 使用
versionName = '1.0.2' +getGitHeadRefsSuffix()

9 Gradle 报错

Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

原因是:Android Studio3.0与butterknife的冲突,github上面已经有了这个问题issue,暂时的解决方法是降至8.4的版本。


10  BuildConfig 直接配置全局常量

可以在build.gradle 中配置新增
android {  
    buildTypes {  
        debug {  
            buildConfigField "String", "URL",   "\"http://www.baidu.com/\""  
            buildConfigField "int",    "MONEY",  "1000"  
        }  
        release {  
            minifyEnabled false  
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'  
        }  
    }  


猜你喜欢

转载自blog.csdn.net/qq_42022061/article/details/80762224