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

I want to add the following library to the Gradle build of my project. The library that I want to add is : signal-protocol-java-2.8.1.jar that I have downloaded. How can I add it in the Gradle build, so that I import the classes included in the .jar it in the java classes that I am developing ?
Here is my build.gradle file :
buildscript {
repositories {
google()
mavenCentral()
jcenter {
content {
includeVersion 'org.jetbrains.trove4j', 'trove4j', '20160824'
includeGroupByRegex "com\\.archinamon.*"
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha09"
}
}
ext {
BUILD_TOOL_VERSION = '30.0.2'
COMPILE_SDK = 30
TARGET_SDK = 30
MINIMUM_SDK = 19
JAVA_VERSION = JavaVersion.VERSION_1_8
}
wrapper {
distributionType = Wrapper.DistributionType.ALL
}
allprojects {
repositories {
google()
jcenter()
}
}
subprojects {
ext.lib_signal_service_version_number = "2.15.3"
ext.lib_signal_service_group_info = "org.whispersystems"
ext.lib_signal_metadata_version = "0.1.2"
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}
}
task qa {
group 'Verification'
description 'Quality Assurance. Run before pushing.'
dependsOn ':Signal-Android:testPlayProdReleaseUnitTest',
':Signal-Android:lintPlayProdRelease',
':libsignal-service:test',
':Signal-Android:assemblePlayProdDebug'
}

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.

Related

Packaging Library with another library aar in Kotlin(build.gradle.kts)

I am creating a library project (lets say B) and I have a dependency for another library (let's say A) which I have in the form of an aar (a.aar) file.
When I build b.aar should be packaged in a way that the a.aar will be bundled inside b.aar
Main Project is not controlled by us, so I do not have control to get a.aar while building the main project and they are looking for a way to bundle a.aar while we build b.aar.
Below is the b build.gradle.kts file.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.library")
kotlin("android")
id("gradle-****-android-module")
id("maven-publish")
}
group = "com.****.******"
version = "0.1-SNAPSHOT"
android {
compileSdk = 33
defaultConfig {
minSdk = 23
targetSdk = 33
consumerProguardFiles("proguard-rules.pro")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.4.1")
androidTestImplementation("androidx.test:runner:1.4.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0-alpha06")
testImplementation("junit:junit:4.12")
implementation("com.google.mlkit:image-labeling:17.0.7")
implementation("com.google.android.gms:play-services-mlkit-text-recognition:18.0.2")
implementation("com.google.android.gms:play-services-mlkit-image-labeling:16.0.8")
implementation("com.example.servicelibrary:servicelibrary:1.0")
}
And in my repositories section, I'm trying to load the a.aar file from local maven.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
val agpVersion: String by project
val repositoryUrl = "file:${rootProject.projectDir.absolutePath}/repository"
repositories {
maven(url = repositoryUrl)
mavenLocal()
google()
mavenCentral()
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
dependencies {
val pmcModuleVersion: String by project
val kotlinVersion: String by project
classpath("com.android.tools.build:gradle:$agpVersion")
classpath(kotlin("gradle-plugin", version = "$kotlinVersion"))
classpath("com.****.gradle.plugins:gradle-****-android-module:$pmcModuleVersion")
}
}
allprojects {
val repositoryUrl = "file:${rootProject.projectDir.absolutePath}/repository"
buildscript {
repositories {
mavenLocal()
google()
mavenCentral()
maven(url = repositoryUrl)
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
}
repositories {
mavenLocal()
google()
mavenCentral()
maven(url = repositoryUrl)
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
}
tasks {
val clean by registering(Delete::class) {
delete(builder)
}
}
Any inputs will be really helpful
In order to bundle LibraryA while building LibraryB, you will need to include LibraryA as a dependency in the build.gradle file of LibraryB.
Follow the steps mentioned below-
Paste ‘LibraryA’ inside the android’s project directory outside the app folder.
In the app level gradle i.e in 'build.gradle(:app)' file of LibraryB, add the following line to the dependencies section:
implementation project(':libraryA')
Make sure that the libraryA module is included in the 'settings.gradle' file of the project
include ':libraryA' //below include ':app'
If their are modules in LibraryA (Eg. Firebase or OneSignalConfig etc) and is mandatory to use then add the module's implementation also in the step 2 -
implementation project(':LibraryA:<module_name>') //add below implementation project(':libraryA')
example - implementation project(':LibraryA:Firebase')
Now try to build and run the app
IN CASE OF ANY DEPENDENCY ERROR WHILE BUILDING/RUNNING THE APP, TRY BELOW STEP
If using latest android structure then Add below code in 'settings.gradle' inside dependencyResolutionManagement{} and inside pluginManagement{}
repositories {
..
maven{url 'https://jitpack.io'} //add this below mavenCentral()
}
If you are using old android structure, then in project level gradle i.e in 'build.gradle(project_name)' add inside allprojects{}-
repositories {
..
maven{url 'https://jitpack.io'} //add below jcenter() and google()
}

Android maven publish plugin is not publishing project dependencies

I have an android project which includes 3 modules ModuleA, ModuleB, ModuleC. ModuleA depends on both ModuleB and ModuleC. I'm trying to publish the ModuleA to a maven repository using android-maven-publish plugin and looks like it is not automatically publishing ModuleB and ModuleC to maven when I run publishToMavenLocal. When I try to use published ModuleA as a dependency in a different project gradle could not resolve ModuleB, ModuleC.
Is there something that I am missing?.
I am using gradle plugin version 3.5.0, gradle version 5.4.1 and mavenPublishVersion 3.6.2.
This is how project's gradle looks like
build.gradle(project)
buildScript {
...
dependencies {
classpath "digital.wup:android-maven-publish:3.6.2"
}
}
allprojects {
apply plugin: "digital.wup.android-maven-publish"
}
and ModuleA's build.gradle
build.gradle(ModuleA)
...
dependencies {
api project(":moduleB")
api project(":moduleC")
}
publishing {
repositories {
...
mavenLocal()
}
publications {
mavenSrcModuleA(MavenPublication) {
from components.android
groupId = "com.example.sdk"
artifactId = "moduleA"
version = "1.0.0"
}
}
}
and ModuleB's build.gradle
build.gradle(ModuleB)
...
publishing {
repositories {
...
mavenLocal()
}
publications {
mavenSrcModuleB(MavenPublication) {
from components.android
groupId = "com.example.sdk"
artifactId = "moduleB"
version = "1.0.0"
}
}
}
and ModuleC's build.gradle
build.gradle(ModuleC)
...
publishing {
repositories {
...
mavenLocal()
}
publications {
mavenSrcModuleC(MavenPublication) {
from components.android
groupId = "com.example.sdk"
artifactId = "moduleC"
version = "1.0.0"
}
}
}

Copy task in Gradle not found

I am developing an app in Unity and need to copy some files before the build.
I have looked around for how to do this and this is what I created (I removed all the "noise"):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
}
}
allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
android {
compileSdkVersion 25
.....
buildTypes {
.......
}
preBuild.dependsOn copyRes
}
task copyRes(type: Copy) {
from file("'../../Assets/Plugins/Android/res")
into file("./src/main/res/values")
}
My build is failing with the following error:
Could not get unknown property 'copyRes' for object of type com.android.build.gradle.AppExtension
This is just an ordering issue. You have tried to use copyRes before it exists.
Option 1 - Use a string instead of the variable
preBuild.dependsOn 'copyRes'
Option 2 - Declare the copyRes task first, before the android {...} block

Intellij Idea 2016.3 complains about Android API level in non-Android module

My project is build by gradle and is structured into android, core, desktop modules. Where android is an Android module, but both core and desktop modules are java modules. The core is dependency of both android and desktop. When I write (in desktop):
Queue<P> qeueue= new ArrayDeque<P>();
The Idea complains:
Cast from ArrayDeque to Queue requires API level 9 (current min is 1)
How can I avoid this complain? Disable the inspection on whole project doesn't seems right. I also do not want to convert the desktop to be an Android module.
Project's build.gradle:
buildscript {
repositories {
// repositories
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
allprojects {
apply plugin: "idea"
version = '1.0'
ext {
appName = 'my-app-name'
}
repositories {
// repositories
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
// some dependencies here
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
// some other dependencies here
}
}
project(":core") {
apply plugin: "java"
dependencies {
// some dependencies here
}
}
Core's build.gradle:
apply plugin: "java"
sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = ["src/", "postprocessing/", "gl/", "math/", "experiment/"]
dependencies {
}
Desktop's build.gradle:
apply plugin: "java"
sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = ["src/", "experiment/", "resources/"]
project.ext.mainClassName = "cz.plajt.wallp.hills.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../android/assets");
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
task dist(type: Jar) {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
from { configurations.compile.collect { zipTree(it) } }
from files(project.assetsDir);
manifest {
attributes 'Main-Class': project.mainClassName
}
}
dist.dependsOn classes
dependencies {
}
This is a known issue for IntelliJ, and I am facing the same issue. I can't even get the project to compile in IntelliJ.
https://youtrack.jetbrains.com/issue/IDEA-122904
There doesn't seem to be a workaround for this.

Remote jar not found in build.gradle

In my application (MyApp), I am using an another project(Appox) as library and in this (Appox) project, I want to include bugsense-jar from their repository. Now in build.gradle of Appox, I am including it in this way
apply plugin: 'android-library'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
repositories {
mavenCentral()
maven { url 'http://www.bugsense.com/gradle/' }
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.bugsense.trace:bugsense:3.6'
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
sourceSets {
main {
//....
}
}
}
But on compiling I am always getting the error Failed to find com.bugsense.trace:bugsense:3.6
Am I including it in the correct way? Is it possible that the file is not even present in their repo?
Just to bring this up to date, Bugsense has been renamed Mint, so the references to the bugsense.com repo are broken. So, update your Gradle file with the following:
apply plugin: 'android'
repositories {
maven { url "https://mint.splunk.com/gradle/" }
}
dependencies {
compile "com.splunk.mint:mint:4.3.0"
}
From: http://docs.splunk.com/Documentation/MintAndroidSDK/latest/DevGuide/Requirementsandinstallation
If you're updating old code, you'll also need to update your imports and rename any instances of BugSenseHandler in your code to Mint. (http://docs.splunk.com/Documentation/MintAndroidSDK/latest/DevGuide/AddSplunkMINTtoyourproject)
Try something like this :
apply plugin: 'android-library'
buildscript {
repositories {
maven { url 'http://www.bugsense.com/gradle/' }
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile 'com.bugsense.trace:bugsense:3.6'
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
sourceSets {
main {
....
}
}
}
Changes made :
All Maven repositories declared at the same time
mavenCentral() is declared in the last position
Sources :
http://blog.bugsense.com/post/58900337206/gradle-repository-is-now-available-for-bugsense
http://gradleproject.wordpress.com/2013/02/14/multiple-maven-repositories-in-gradle/

Categories

Resources