Adding and Modifying an Android Library in Android Studio

Mashruf Zaman
3 min readJan 13, 2024

--

While trying to use the Paho MQTT library for Android for recent version of Android (targetSdkVersion 34) it gave the following error:

FATAL EXCEPTION: MQTT Rec:
Process: com.example.project, PID: 15856
java.lang.IllegalArgumentException: com.example.project: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

In this case you need to modify the library to use FLAG_IMMUTABLE. So adding the library dependency from the Gradle repository in the build.gradle file will not work.

You can add the library as standalone dependency in your Android project. First download the Paho Library and unzip in a location.

In your project in Android Studio go to File > Project Structure

lkasjdf

Select Dependencies from the left panel. Click + Modules from where you should be able to locate the android service project folder. Android Studio will detect is as a Module.

After that you need to modify the build.gradle file for this module. After modification which looks something like this:

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android {

namespace 'com.example.paho'

compileSdkVersion 24

defaultConfig {
minSdkVersion 16
targetSdkVersion 24

testApplicationId "org.eclipse.paho.android.service.test"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

lintOptions {
abortOnError false
}
}

repositories {
//maven { url rootProject.ext.mavenUrl }
}

configurations {
//androidTestCompile.exclude module: 'org.eclipse.paho.client.mqttv3'
}

dependencies {
implementation "org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0"
implementation "com.android.support:support-v4:24.2.1"
implementation fileTree(include: ['*.jar'], dir: 'libs')
}

android.libraryVariants.all { variant ->
task("generate${variant.name}Javadoc", type: Javadoc) {
title = "$name $version API"
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar =
"${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
doFirst { classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar) }
options.links("http://docs.oracle.com/javase/7/docs/api/");
options.links("http://d.android.com/reference/");
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}

// Generate Sources Jar
task("generateSourcesJar", type: Jar) {
from android.sourceSets.main.java.srcDirs
archiveClassifier = 'sources'
}

// Copy the Paho Java client into the libs directory to bundle it with the AAR
task("copyLibs", type: Copy) {
println "Copying MQTT Jar into libs directory"
from configurations.implementation
into 'libs'
include 'org.eclipse.paho*'
}

// Renames the Release AAR file to a maven convention
task("renameReleaseAAR", type: Copy) {
from("$buildDir/outputs/aar")
into("$buildDir/outputs/aar/")
include('org.eclipse.paho.android.service-debug.aar')
rename('org.eclipse.paho.android.service-debug.aar', 'org.eclipse.paho.android.service' + '-' + '1.1.1' + '.aar')
}

// Generate Plain Jar Version of library (good for legacy users)
task("generateLibraryJar", type: Jar) {
from("$buildDir/intermediates/classes/release")
}

// Generate Javadoc
task("generateJavaDocJar", type: Jar, dependsOn: ('1.1.1'.endsWith('-SNAPSHOT') ? 'generatedebugJavadoc' : 'generatereleaseJavadoc')){
from("$buildDir/docs/javadoc")
archiveClassifier = 'javadoc'
}

publishing {
publications {
paho(MavenPublication) {
groupId 'org.eclipse.paho'
artifact generateSourcesJar
artifact generateLibraryJar
artifact generateJavaDocJar
artifact "$buildDir/outputs/aar/" + 'org.eclipse.paho.android.service' + "-" + '1.1.1' + ".aar"
}
}
repositories {
if (project.hasProperty('eclipseRepoUsername')) {
maven {
name 'eclipse'
url 'https://repo.eclipse.org/content/repositories/paho-' + ('1.1.1'.endsWith('-SNAPSHOT') ? 'snapshots/' : 'releases/')
credentials {
username eclipseRepoUsername
password eclipseRepoPassword
}
}
} else {
maven {
url "$buildDir/repo"
}
}
}
}


task debug {
doLast {
configurations.compile.each { println it }
}
}
// Required to bundle Java library Jar in AAR
clean.doLast{
copyLibs.execute()
}
assemble.doLast {
renameReleaseAAR.execute()
}

clean.doFirst {
delete 'libs'
}

Once all the gradle discrepencies are set the module should be visible in Android Studio

Now in the AlarmPingSender class modify the line to use the library successfully in your project :

pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
action), PendingIntent.FLAG_IMMUTABLE);

--

--