Build.Gradle and Project Libs

build.gradle and project libs

I think you are specifying the path to your library project incorrectly. If I'm interpreting your project layout correctly the line should be the following:

compile project(':MyApp:libraries:projectLib')

When you start a project path with ':' you are making an absolute path from the root project and then basically just writing a path with ':' instead of '/'. In this case your projectLib module is in the directory MyProject/MyApp/libraries/projectLib, and MyProject is where your settings.gradle is, making it your root project. So swapping in colons for slashes gets you the line I wrote above.

You'll need to modify your settings.gradle to include the full path as well:

include 'MyApp:libraries:projectLib'

Finally, if you want to save some typing for stuff like your repository configuration you can put it in an allproject block in your root project.

allprojects {
repositories {
mavenCentral()
}
}

A lot of nice little tips like that covered in the multi-module docs.

How to add a library to Gradle build in Android Studio project?

You have to edit your module-level build.gradle (the one you've posted is project-level). It's typically in the "app" folder of your project. Find there dependencies block and add this line:

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
...
}

Then create a folder "libs" near this gradle file and put your .jar library there. Sync the project with Gradle.

Difference between build.gradle (Project) and build.gradle (Module)

build.gradle (Project:My-app)

Top-level build file where you can add configuration options common to
all sub-projects/modules.

Each project contains a top-level Gradle file. It usually contains common configurations for all modules. Whatever is included in this top-level Gradle gile, it will affect all modules.

Example:

// 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:2.0.0-alpha3'

//Maven plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle (Module:app)

Build file of your specific module (where you add your dependencies, signing configurations, build types, flavors, etc.)

All modules have a specific Gradle file. Whatever is included in this gradle file, it will only affect the module that is included on.

Example:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.hrskrs.gesturefun"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':gesture-fun')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
}

Android Gradle adding external library and nested external libraries to a project

In your top level settings.gradle (App1/settings.gradle) file do something like this for each library

include ':library1'   
include ':library2'
include ':library3'
include ':library4'

project(':library1').projectDir = new File(rootProject.projectDir, '../libraries/library1')
project(':library2').projectDir = new File(rootProject.projectDir, '../libraries/library2')
project(':library3').projectDir = new File(rootProject.projectDir, '../libraries/library3')
project(':library4').projectDir = new File(rootProject.projectDir, '../libraries/library4')

Remove the other settings.gradle files, you don't need them

then in each build script you only need to use

compile project (':library1')
compile project (':library2')
etc....

as stated above just use a single settings.gradle file in the root project (App1).

Then from your App1 folder run gradlew clean :library1:build to validate that library1 is building correctly.

As for the issue about App1 complaining about missing libraries 3 & 4, are you sure you have no code in the app directly referencing these libraries, either that or the libraries are not being found when compiling library1. Build each library individually to validate they all build ok.

Gradle build for set of Android projects (including libraries)

You can use your build.gradle in root, or you can define some values in gradle.properties in root folder to achieve your scope.

For example:

root/build.gradle:

ext {
compileSdkVersion = 19
buildToolsVersion = "19.0.3"
}

module/build.gradle:

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}

OR using properties

root/gradle.properties:

VERSION_NAME=1.0.1
VERSION_CODE=11

module/build.gradle:

android {

defaultConfig {
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)

}

How is libs created and maintained in a gradle android project?

With Android Studio AND Gradle, there is no need to use libs folder (except for old .jar library).

In fact you can develop Android app whitout Android Studio as in your build.gradle there is already a apply plugin: 'com.android.application'

Gralde is using Maven or jCenter via gradle dependencies to import libraries. Gradle and Android Gradle plugin will automaticly download the libs as you sayed in a build/ folder. It is not static and can be clean with the Clean projet on Android Studio. Also, Android Studio will add a warning when a new library version is available automaticly in your build.gradle.

Dont miss the old libs folder used to import .jar library

How to change libs directory in Gradle?

Actually, @Michael's answer is correct, it is also obsolete. Now, using gradle all you need to do is to add the lines below in the build.gradle file:

android {
...
sourceSets {
main.jniLibs.srcDirs = ['libs']
test.jniLibs.srcDirs = ['libs']
}
}

or directly put your .so libraries into:

src/main/jniLibs

This way, when you build your application or library, the jni libraries are being copied into destination .jar/.aar file.

Include independent local library to my android app with gradle

Ok, found the problem. Need to add a group 'com.example' on the my-external-lib\build.gradle and then I can add the dependency like using:

implementation "com.example:my-external-lib"

See commit with the fix.



Related Topics



Leave a reply



Submit