How to avoid stripping native library packaged in an AAR for debug build type only?

Issue

I have a C++ library built for Android, which I package as an Android archive (.aar), along with some Java classes and a Manifest.

Everything is done outside of Android Studio. To package the .aar, I have a directory that contains:

  • app/src/main/jniLibs/arm64-v8a/: directory with library
  • app/src/main/java: directory with Java classes

The directory also contains the manifest, and Gradle files.

Here is the build.gradle in the app directory:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.2"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 27
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:27.0.2'
}

apply plugin: 'maven-publish'

publishing {
    publications {
        fooapp(MavenPublication) {
            groupId 'com.example'
            artifactId 'fooapp'
            version "develop"
            artifact('build/outputs/aar/app-release.aar')
        }
    }
    repositories {
        maven {
            mavenLocal()
        }
    }
}

publishToMavenLocal.dependsOn assemble

By default, all the AAR files contain a stripped version of the library. I do not want the library to be stripped when assembling the Debug version of the AAR.

I found out that I can use packagingOptions to disable stripping, so I tried to add packagingOptions { doNotStrip "**/*/*.so" } under the debug {...} scope of the configuration file.

My problem is that both the debug and release versions of the AAR are then stripped. The two versions of the AAR do seem different, since they don’t have the same size (hence have different md5sum).

Where/how can I place my packagingOptions in the Gradle configuration file so that only the release version is stripped? Thanks

Solution

The packaging options are specified under buildTypes and then the configuration like Debug or Release etc. Example:

buildTypes {
    debug {
        packagingOptions {
            doNotStrip '**/*.so'
        }
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        packagingOptions {
            doNotStrip '**/*.so'
        }
    }
}

Note: you added an extra directory /*/ in your specified path.

Answered By – AndrewBloom

Leave a Comment