【Gradle】Library库工程的release及debug

背景:

目前工程中是通过手动在代码中修改debug及release变量,这十分低效并且极易遗忘而造成低级错误。

需求是什么?

1.genkins上打aar包时候,直接根据genkins参数设置来决定打debug还是release包 
2.本地工程依赖,需要能区分依赖debug还是release版本

库工程打包所遇到问题:

Library projects only publish their release variants for consumption by other projects or modules. 
We’re working at fixing this but this is non trivial and requires a significant amount of work.

目前因为gradle的限制 android库工程只能发布release包 .见 https://code.google.com/p/android/issues/detail?id=52962 中的issue讨论

根据【参考1】的内容,借助publishNonDefault true 可改变这一现状。

本地工程依赖

库工程build.gradle中加上编译参数

android {
    buildTypes {
        release {
                buildConfigField "String", "ENV", "\"release\""
        }

        debug {
                buildConfigField "String", "ENV", "\"debug\""
        }
    }
    publishNonDefault true //需要加上该参数否则会提示debug not found
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

使用debug编译类型

App工程build.gradle加上工程依赖 debugCompile project(path: ‘:train’, configuration: ‘debug’)

使用release编译类型时

compile project(path: ‘:train’) 
此时默认是为release,如果在Build Variants中强制选择为debug,就会报冲突,如【图1】

genkins上打aar包

genkins 执行assembleDebug 可以得到 XXX-debug.aar.

执行assembleRelease 可以得到 XXX-release.aar.

资料

android官方资料 
https://guides.codepath.com/android/Building-your-own-Android-library#add-the-gradle-dependency

【图1】 
这里写图片描述

【参考1】 
来自 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Referencing-a-Library

Library Publication

缺省情况下,无论你选用什么Build variant ,库工程都只会打出release包,这是由于Gralde语言的限制造成的,但如果想强制使用debug,可以如下指定:

android {
    defaultPublishConfig "debug"
}
  • 1
  • 2
  • 3

defaultPublishConfig右边的Build variant量需要是variant全名,出现 Release and debug 的情况只有在没有设置flavor的前提下。如果有flavor就得如下:

android {
    defaultPublishConfig "flavor1Debug"
}
  • 1
  • 2
  • 3

也可以设置打所有Build variant的包,在project-to-project依赖(工程依赖)的时候该功能显的很有用(google正在做,但是因为gradle的限制,有可能不work),该功能默认不开启的。

android {
    publishNonDefault true
}
  • 1
  • 2
  • 3
dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}
  • 1
  • 2
  • 3
  • 4

Important: Note that the published configuration is a full variant, including the build type, and needs to be referenced as such. 
Important: When enabling publishing of non default, the Maven publishing plugin will publish these additional variants as extra packages (with classifier). This means that this is not really compatible with publishing to a maven repository. You should either publish a single variant to a repository OR enable all config publishing for inter-project dependencies.

来自:http://blog.csdn.net/xude1985/article/details/51548245

背景:

目前工程中是通过手动在代码中修改debug及release变量,这十分低效并且极易遗忘而造成低级错误。

需求是什么?

1.genkins上打aar包时候,直接根据genkins参数设置来决定打debug还是release包 
2.本地工程依赖,需要能区分依赖debug还是release版本

库工程打包所遇到问题:

Library projects only publish their release variants for consumption by other projects or modules. 
We’re working at fixing this but this is non trivial and requires a significant amount of work.

目前因为gradle的限制 android库工程只能发布release包 .见 https://code.google.com/p/android/issues/detail?id=52962 中的issue讨论

根据【参考1】的内容,借助publishNonDefault true 可改变这一现状。

本地工程依赖

库工程build.gradle中加上编译参数

android {
    buildTypes {
        release {
                buildConfigField "String", "ENV", "\"release\""
        }

        debug {
                buildConfigField "String", "ENV", "\"debug\""
        }
    }
    publishNonDefault true //需要加上该参数否则会提示debug not found
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

使用debug编译类型

App工程build.gradle加上工程依赖 debugCompile project(path: ‘:train’, configuration: ‘debug’)

使用release编译类型时

compile project(path: ‘:train’) 
此时默认是为release,如果在Build Variants中强制选择为debug,就会报冲突,如【图1】

genkins上打aar包

genkins 执行assembleDebug 可以得到 XXX-debug.aar.

执行assembleRelease 可以得到 XXX-release.aar.

资料

android官方资料 
https://guides.codepath.com/android/Building-your-own-Android-library#add-the-gradle-dependency

【图1】 
这里写图片描述

【参考1】 
来自 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Referencing-a-Library

Library Publication

缺省情况下,无论你选用什么Build variant ,库工程都只会打出release包,这是由于Gralde语言的限制造成的,但如果想强制使用debug,可以如下指定:

android {
    defaultPublishConfig "debug"
}
  • 1
  • 2
  • 3

defaultPublishConfig右边的Build variant量需要是variant全名,出现 Release and debug 的情况只有在没有设置flavor的前提下。如果有flavor就得如下:

android {
    defaultPublishConfig "flavor1Debug"
}
  • 1
  • 2
  • 3

也可以设置打所有Build variant的包,在project-to-project依赖(工程依赖)的时候该功能显的很有用(google正在做,但是因为gradle的限制,有可能不work),该功能默认不开启的。

android {
    publishNonDefault true
}
  • 1
  • 2
  • 3
dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}
  • 1
  • 2
  • 3
  • 4

Important: Note that the published configuration is a full variant, including the build type, and needs to be referenced as such. 
Important: When enabling publishing of non default, the Maven publishing plugin will publish these additional variants as extra packages (with classifier). This means that this is not really compatible with publishing to a maven repository. You should either publish a single variant to a repository OR enable all config publishing for inter-project dependencies.

猜你喜欢

转载自blog.csdn.net/gaojinshan/article/details/77998404
今日推荐