Set doNotStrip packagingOptions to a specific buildType
Asked Answered
N

3

11

My project has 3 different buildTypes and I need only one of them to keep the debug info of its native libraries. I'm using com.android.tools.build:gradle:3.1.4.

I tried the following

buildTypes {

   debug {
   ...
   }

   release {
   ...
   }

   unstripped {
       ...
       packagingOptions {
           doNotStrip '**/*.so'
       } 
   }
}

but that makes all buildTypes to keep unstripped versions of the libraries.

I also tried changing the doNotStrip pattern to something like '**/unstripped/*.so' but that does not seem to help me, since the docs say that the path is related to the APK's root dir.

Nowhither answered 24/10, 2018 at 15:8 Comment(4)
Did you ever figure out why this doesn't work?Cockadoodledoo
Poked at it some more, and it seems like a gradle bug or atleast an issue with how packagingOptions is implemented. The settings seem to be global and not scoped to particular variants.Cockadoodledoo
I opened a bug report here: issuetracker.google.com/issues/155215248Cockadoodledoo
Hi, @tmm1. No, I haven't been working with Android for a while now, so I ended up not looking further into it. Thanks for opening the report! :)Nowhither
N
3

Since Android Gradle Plugin (AGP) version 7.0.0 the use case is supported with a new syntax. Example:

androidComponents {
    onVariants(selector().withBuildType("debug")) {
        packaging.jniLibs.keepDebugSymbols.add("**/debugDoNotStrip.so")
    }
}
Nowhither answered 20/12, 2022 at 17:9 Comment(1)
Where in the build.gradle file should the androidComponents be specified?Indefeasible
G
13

Here is a workaround for you:

Feeding a command line option "doNotStrip"

  1. Add below to your app/build.gradle

    android {
        ...
        if (project.hasProperty("doNotStrip")) {
            packagingOptions {
                doNotStrip '**/*.so'
            }
        }
        ...
    }
    
  2. Use below command to generate the unstripped apk by feeding option doNotStrip to the command.

    ./gradlew assembleUnstripped -PdoNotStrip
    
  3. For other build types, you can still use the normal build commands. e.g.

    ./gradlew assembleDebug
    

    or

    ./gradlew assembleRelease
    

    but don't feed option doNotStrip to the command.

Groundsheet answered 25/10, 2018 at 1:47 Comment(1)
Thanks a lot! That's almost exactly what I ended up doing, actually... But, is it possible to do something as simple as I tried to initially? If it's impossible, could you explain me why? I'm new to Gradle and it would be nice to learn more about it.Nowhither
N
3

Since Android Gradle Plugin (AGP) version 7.0.0 the use case is supported with a new syntax. Example:

androidComponents {
    onVariants(selector().withBuildType("debug")) {
        packaging.jniLibs.keepDebugSymbols.add("**/debugDoNotStrip.so")
    }
}
Nowhither answered 20/12, 2022 at 17:9 Comment(1)
Where in the build.gradle file should the androidComponents be specified?Indefeasible
M
0

doNotStrip function is deprecated, please use keepDebugSymbols instead.

build.gradle.kts:

android {
  buildTypes {
        debug {
            packaging.jniLibs.keepDebugSymbols.add("**/*.so")
        }
    }
}

It works for me.

Malicious answered 2/2 at 7:43 Comment(1)
Could not get unknown property 'packaging' Getting this error :/Ellinger

© 2022 - 2024 — McMap. All rights reserved.