Jeroen
Jeroen

Reputation: 404

Aar containing string resources aren't visible in a jetpack compose project

I'm having an issue with an aar in a jetpack compose project.

I've created a library which contains all the string resources I'm using in multiple projects. In my old java/kotlin projects there is no issue and I can implement the library and reach the strings. But when I do this in my new jetpack compose project it loads the aar but when in code I do R.string. I don't see the specific strings.

Also when I add some kotlin classes with functions in the library, these functions can be accessed. This way I'm 100% sure the library is loaded.

Is there a way to solve this?

The android manifest for the library looks like:

<manifest package="com.test.library"/>

The build.gradle:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdk 31

    defaultConfig {
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            android.libraryVariants.all { variant ->
                def debug = ""
                if (variant.name == "debug") {
                    debug = "-debug"
                }
                variant.outputs.all {
                    outputFileName = "TestLibrary" + debug + ".aar"
                }
            }
        }
    }
}

dependencies {
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

In my jetpack compose project I implement it with the following call:

implementation files('libs/TestLibrary.aar')

How can I solve this? Is there somebody with the same issue (and a solution)?

Kind regards,

Jeroen

Upvotes: 2

Views: 558

Answers (1)

FalcoVerhagen
FalcoVerhagen

Reputation: 174

I also have been struggling with this for a while and I believe I found what causes this issue. Your jetpack compose project should have a new entry in its gradle.properties file. The entry should look like this:

# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true

If you change this value to false your project should be able to find the string resources declared in your library. As the description shows.

So your new entry will look like this:

# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=false

Hope this helps!

Upvotes: 3

Related Questions