I have an android project that I want to convert into an .aar library. Inside this project, I am using some google libraries as well as my own SDK library as an aar. Here is the build.gradle below.
I want the Google library to be included in the final aar, but exclude my own SDK library, so I choose implementation and compileOnly keywords for them respectively. When I compile, the code compiles successfully, but when I generate the .aar file and when I include it in another project, it doesn't have the Google library included for some reasons and throws this error:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/....
So I have to manually add it again in the new project via implementation <google library>.
Is there anything I should do to generate a correct .aar library that includes the Google library but excludes my SDK library? My intention is to create a separate .aar file for this project and include it as a plugin alongside with my SDK.
plugins {
//id 'com.android.application'
id 'com.android.library'
}
android {
namespace 'com.example.sample'
compileSdk 34
defaultConfig {
//applicationId "com.example.sample" //remove for library
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
// Google library
compileOnly 'com.google.somelibrary:1.0.0' //should be included in aar
compileOnly files('libs/mysdk.aar') //should NOT be included in aar
}