10

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.

  • Did you ever figure out why this doesn't work? – tmm1 Apr 28 '20 at 20:28
  • 1
    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. – tmm1 Apr 28 '20 at 22:07
  • 2
    I opened a bug report here: https://issuetracker.google.com/issues/155215248 – tmm1 Apr 28 '20 at 22:09
  • 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! :) – Bruno Alexandre Rosa Apr 30 '20 at 17:11

2 Answers2

11

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.

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 2
    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. – Bruno Alexandre Rosa Oct 25 '18 at 22:29
2

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")
    }
}