Android multi module with flavors to maven repository - android

I've been trying to upload a project to a maven repository unsuccessfully. Here is my project structure :
_root
|_ lib1 (aar)
|_ lib2 (aar)
|_ javalib (jar)
lib1 depends on lib2
lib2 depends on javalib
lib1 and lib2 have two flavors (intg and prod)
Problem is when I launch uploadArchives task I have no pom.xml created.
It seems to be due to my flavors (when I remove flavors it works fine), and I can't figure out how to fix this problem.
I really need to work with flavors ... any help is welcome ;)
here are my build.gradle files :
root :
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
ext.versionName = "$System.properties.version"
configure(subprojects) {
apply plugin: 'maven'
group = 'com.test.build'
version = project.versionName
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://localhost:8081/content/repositories/test/") {
authentication(userName: "admin", password: "admin123")
}
}
}
}
}
allprojects {
repositories {
jcenter()
}
}
lib1 :
apply plugin: 'com.android.library'
android {
publishNonDefault true
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName project.versionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
prod{}
intg{}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
intgCompile project(path: ':lib2', configuration: 'intgRelease')
prodCompile project(path: ':lib2', configuration: 'prodRelease')
}
lib2 :
apply plugin: 'com.android.library'
android {
publishNonDefault true
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName project.versionName
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
prod{}
intg{}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(path: ':javalib')
}
javalib :
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

I finally found the answer to my issue :
it is NOT possible, but it might be possible to do it one day :
see documentation about that :
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication
I went through this problem with this solution :
// default publication is production which will be published without env name
publishNonDefault true
defaultPublishConfig "productionRelease"
if (android.productFlavors.size() > 0) {
android.libraryVariants.all { variant ->
// Publish a main artifact, otherwise the maven pom is not generated
if (android.publishNonDefault && variant.name == android.defaultPublishConfig) {
def bundleTask = tasks["bundle${name.capitalize()}"]
artifacts {
archives(bundleTask.archivePath) {
classifier null
builtBy bundleTask
}
}
}
}
}
I am not satisfied with it but it will do the job for now ...

I spend a lot time to discover why my Android project din't upload my modules to maven. The problem is because I didn't set the defaultPublishConfig inside the android {} brackets in the file build.gradle. The snippet below is how I solved my problem. You need set what flavor you wanna publish to Maven (access build variants tab in the Android Studio to see all your flavors name):
android {
...
defaultConfig {
...
defaultPublishConfig "myFlavorDebug"
}
}

Related

Gradle - classes of a library is not includded inside the aar

I have library that is called sensorcore and I have another library (org.eclipse.paho.android.service-1.1.1.aar) that I would like to include it's classes inside the sensorcore aar that is built.(in sensorcore/build/output/arr/).
The library structure
-libraryFolder
--sensorcore
---build.gradle
--app
---build.gradle
--libs
---build.gradle
---paho.android.service.aar
-build.gradle
-settings.gradle
sensorcore/build.gradle
apply plugin: 'com.android.library'
def LIBRARY_VERSION = "1.2.8"
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 14
targetSdkVersion 31
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
groupId = "***"
artifactId = "***"
version = "${LIBRARY_VERSION}"
from components.release
}
}
repositories {
maven {
url "mavlink"
name "Git"
credentials(HttpHeaderCredentials) {
name = 'Job-Token'
value = System.getenv("CI_JOB_TOKEN")
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.work:work-runtime:2.7.1'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
implementation 'com.google.android.gms:play-services-location:17.1.0'
implementation project(":libs")
}
app/build.gradle
apply plugin: 'com.android.application'
android {
signingConfigs {
releasekeystore {
keyAlias '***'
keyPassword '***'
storeFile rootProject.file('secrets/key.jks')
storePassword '***'
}
}
compileSdkVersion 31
defaultConfig {
applicationId "******"
minSdkVersion 14
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releasekeystore
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
implementation project(":sensorcore")
}
libs/build.gradle
configurations.maybeCreate("default")
artifacts.add("default", file('org.eclipse.paho.android.service-1.1.1.aar'))
libraryFolder build.gradle
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id 'maven-publish'
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
libraryFolder settings.gradle
include ':app', ':sensorcore', ':libs'
rootProject.name='Sensor'
when I'm trying just to build it using the gradle command "gradle clean build" it gives me an error
What went wrong:
Execution failed for task ':app:validateSigningRelease'.
> Keystore file 'libraryFolder\secrets\key.jks' not found for signing config 'releasekeystore'.
Is it possbile to "build" without the releasekey? maybe there is any command that can do it.
I didn't find any infomation in the gradle documentation, maybe I have missed it.
Anybody has any idea how to resolve it? thank you.

Cannot find local aar built through Flutter Module

I am trying to add the flutter module as aar dependency on my Android Project. Here is the guide
https://flutter.dev/docs/development/add-to-app/android/project-setup#add-the-flutter-module-as-a-dependency
I am able to generate the local AAR and I can see these steps to be done:
1. Open <host>/app/build.gradle
2. Ensure you have the repositories configured, otherwise add them:
repositories {
maven {
url '/Users/asharma/Documents/Flutter/animation_module/build/host/outputs/repo'
}
maven {
url 'http://download.flutter.io'
}
}
3. Make the host app depend on the Flutter module:
dependencies {
debugImplementation 'com.example.animation_module:flutter_debug:1.0
profileImplementation 'com.example.animation_module:flutter_profile:1.0
releaseImplementation 'com.example.animation_module:flutter_release:1.0
}
4. Add the `profile` build type:
android {
buildTypes {
profile {
initWith debug
}
}
}
In my Android project, I have app module and library module. I want to include this aar in my library module, here is my library module build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
profile {
initWith debug
}
}
}
dependencies {
debugImplementation 'com.example.animation_module:flutter_debug:1.0'
profileImplementation 'com.example.animation_module:flutter_profile:1.0'
releaseImplementation 'com.example.animation_module:flutter_release:1.0'
}
repositories {
maven {
url '/Users/asharma/Documents/Flutter/animation_module/build/host/outputs/repo'
}
maven {
url 'http://download.flutter.io'
}
}
I have also added mavenLocal() at my project's build.gradle in repository. (Tried with and without it) But dependencies are not resolved.
I get an error:
Could not find com.example.animation_module:flutter_debug:1.0.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/example/animation_module/flutter_debug/1.0/flutter_debug-1.0.pom
- https://dl.google.com/dl/android/maven2/com/example/animation_module/flutter_debug/1.0/flutter_debug-1.0.jar
- https://jcenter.bintray.com/com/example/animation_module/flutter_debug/1.0/flutter_debug-1.0.pom
- https://jcenter.bintray.com/com/example/animation_module/flutter_debug/1.0/flutter_debug-1.0.jar
Required by:
project :app > project :animation_flutter_sdk
I know dependency is not present on the remote at https://dl.google.com but it is present in my local and gradle should pick it up from my local. Please help me how to build this project.
I have a project with several library modules and an app module. I faced the same issue as I wanted to start the flutter module from one of my library modules.
I solved it by adding
repositories {
maven {
url '../path/to_your/flutter_repo'
}
maven {
url 'https://storage.googleapis.com/download.flutter.io'
}
in both app module and library module build.gradle
I didn't need mavenLocal().
I also needed to add
profile {
initWith debug
}
in each modules build.gradle.
This is now how my app module and library module build.gradle looks like:
App module build.gradle:
plugins {
id 'com.android.application'
}
repositories {
maven {
url '../core/webshop/flutter_repo'
}
maven {
url 'https://storage.googleapis.com/download.flutter.io'
}
}
android {
compileSdkVersion toolVersions.compileSdk
defaultConfig {
...
}
signingConfigs {
release {
...
}
}
buildTypes {
release {
...
}
profile {
initWith debug
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Project modules
implementation project('your_lib_module1')
implementation project('your_lib_module2')
...
}
Library module build.gradle, where you want to add flutter module:
apply plugin: 'com.android.library'
repositories {
maven {
url './flutter_repo'
}
maven {
url 'https://storage.googleapis.com/download.flutter.io'
}
}
android {
compileSdkVersion toolVersions.compileSdk
defaultConfig {
...
}
buildTypes {
release {
...
}
profile {
initWith debug
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Project modules
implementation project('your_lib_module3')
// flutter
debugImplementation 'com.example.animation_module:flutter_debug:1.0'
profileImplementation 'com.example.animation_module:flutter_profile:1.0'
releaseImplementation 'com.example.animation_module:flutter_release:1.0'
...
}
I have the same issue and solve it adding in repositories of settings.gradle inside of dependencyResolutionManagement
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
maven {
url '/yourpath/your_flutter_module/build/host/outputs/repo'
}
maven {
url 'https://storage.googleapis.com/download.flutter.io'
}
}
}
I have the same problem, it is solved by setting the configurations
android {
buildTypes {
release {
....
}
profile {
....
}
debug {
....
}
}
}
configurations {
// Initializes a placeholder for the profileImplementation dependency configuration
profileImplementation {}
}
dependencies {
debugImplementation xxx
// Then it can work
profileImplementation xxx
releaseImplementation xxx
}
Picking a specific build type in a dependency

can't access the class from ActionBar-PullToRefresh library

I'm on a android project and I need import the ActionBar-PullToRefresh library, so I import the library with gradle like following:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.android.support:appcompat-v7:20.0.0') {
force = true
}
compile('com.android.support:support-v4:20.0.0') {
force = true
}
repositories {
mavenCentral()
}
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Then I Sync Project with Gradle Files, so I can use the PullToRefreshLayout in my layout xml file.But when I use the library's class in my Fragment code,I can't get nothing code trigger. When I spell the whole class name PullToRefreshLayout the Android studio will not import the class automatically. So what's the problem.
The next two file is my build.gradle file in the root directory and my app directory:
build.gradle file in the root directory
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
build.gradle file in the app directory
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.ifeve.ifeveforandroid"
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.android.support:appcompat-v7:20.0.0') {
force = true
}
compile('com.android.support:support-v4:20.0.0') {
force = true
}
repositories {
mavenCentral()
}
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Try importing library this way, add in dependencies section this line:
compile 'com.github.chrisbanes.actionbarpulltorefresh:library:0.9.9'

Robospock and gradle build variants?

I'm using Robospock to perform unit testing and mocking with gradle. This worked great until I added gradle build variants to the mix.
My android build.gradle file:
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
versionCode 1
versionName "0.0.1"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
uat {
packageName "com.acme.dev"
}
stage {
packageName "com.acme.staging"
}
prod {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.mcxiaoke.volley:library:1.0.4'
}
My robospock build.gradle file:
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
classpath 'org.robospock:robospock-plugin:0.4.0'
}
}
repositories {
mavenCentral()
}
apply plugin: 'groovy'
dependencies {
compile "org.codehaus.groovy:groovy-all:1.8.6"
compile 'org.robospock:robospock:0.4.4'
compile 'cglib:cglib-nodep:2.2'
compile 'org.objenesis:objenesis:1.3'
}
project.ext {
robospock = ":Mothership" // project to test
}
apply plugin: 'robospock'
The Android gradle plug-in offers me build variant tasks such as assembleProd, assembleProdDebug, assembleStageDebugTest. Can I pass something to Robospocl in my build.gradle so that it can participate in the build variants?
Currently when I execute ./gradlew robospok, it cannot find the classes defined in com.acme.dev or com.acme.staging

How to define different dependencies for different product flavors

I am converting one of my apps to Gradle and would like to use the new build flavor features to have a paid and a free ad based flavor.
I want only the ad based version to depend on the admob SDK.
My build file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
productFlavors {
Pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
Free {
dependencies {
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile fileTree(dir: 'libs', include: '*.jar')
}
Is there a way to configure the dependency in the free product flavor to have its own libs folder that is merged with the main libs folder that contains general libraries for both flavors?
If this is possible how would I define this folder?
To define a flavor specific dependency you can use proCompile instead of compile in your dependency section. When you run gradle properties you get an overview of automatic created configurations.
The correct build file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 22
}
productFlavors {
pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
free { }
}
}
dependencies {
compile 'com.android.support:support-v4:22.2.0'
freeCompile 'com.google.android.gms:play-services-ads:7.5.0'
}
Fast forward to mid-2018. You will need to add flavorDimensions.
android {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "dimensionName"
productFlavors {
pro {
dimension "dimensionName"
}
free {
dimension "dimensionName"
}
}
}
dependencies {
implementation 'com.android.support:support-v4:22.2.0'
freeImplementation 'com.google.android.gms:play-services-ads:15.0.1'
}
Also, take note:
Configuration 'compile' is obsolete and has been replaced with
'implementation' and 'api'. It will be removed at the end of 2018. For
more information see:
http://d.android.com/r/tools/update-dependency-configurations.html
Simple:
dependencies {
....
....
gradle.startParameter.getTaskNames().each { task ->
if(task.contains("free")) {
implementation 'com.google.android.gms:play-services-ads:17.2.0'
}
}
....
....
}
or just:
FreeImplementation 'com.google.android.gms:play-services-ads:17.2.0'
You need to manually add configuration for each flavor. Example
configurations {
proCompile
freeCompile
}
dependencies {
compile 'com.parse.bolts:bolts-tasks:1.3.0'
proCompile 'com.android.support:design:23.1.1'
freeCompile 'com.parse:parse-android:1.12.0'
}
Edit: I recommend using one of the other techniques!
An alternative to the accepted answer is this:
ext {
flavorType = ""
}
gradle.startParameter.getTaskNames().each { task ->
if(task.contains("flavor1")){
flavorType = "flavor1"
} else if (task.contains("flavor2")){
flavorType = "flavor2"
} else {
flavorType = "flavor3"
}
}
if(flavorType == 'flavor1' || flavorType == 'flavor2') {
compile 'com.android.support:support-v4:18.0.+'
}
If you use Gradle with Kotlin, dependencies for custom flavors can be added as follows:
dependencies {
"freeImplementation"("com.google.android.gms:play-services-ads:7.5.0")
}

Categories

Resources