DEV Community

Toshihiro Yagi
Toshihiro Yagi

Posted on

Enable split apk only when release build.

During development most of the time you do not need split apk, but doesn't work settings like bellow.

buildType {
  debug {
    splits {
      abi {
        println ("split settings for debug.")
        enable false
      }
    }
  }

  release { 
    splits {
      abi {
        println ("split settings for release.")
        enable true
        reset()
        include 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        universalApk true
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

This is because the setting process is executed sequentially.

./gradlew assembleDebug
> ...
> split settings for debug.
> split settings for release.
> ...
Enter fullscreen mode Exit fullscreen mode

Solution 1

You can access the gradle task name on gradle scripts. Control the splits by the task name as follows.

android {
  splits {
    abi {
      enable gradle.startParameter.taskNames.contains("assembleRelease")
      reset()
      include 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
      universalApk true
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Solution 2

You can pass parameters to gradle script.

android {
  splits {
    abi {
      enable project.hasProperty("enabledSplitApks")
      reset()
      include 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
      universalApk true
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Control split apk as needed.

./gradlew assembleDebug -PenabledSplitApks
Enter fullscreen mode Exit fullscreen mode

It saves build time by omitting extra processing 😄

Top comments (0)