Skip to content

Instantly share code, notes, and snippets.

@daj
Forked from xrigau/AndroidManifest.xml
Last active December 20, 2018 09:44
Show Gist options
  • Save daj/7b48f1b8a92abf960e7b to your computer and use it in GitHub Desktop.
Save daj/7b48f1b8a92abf960e7b to your computer and use it in GitHub Desktop.

Revisions

  1. daj revised this gist Sep 19, 2015. No changes.
  2. daj revised this gist May 5, 2015. 2 changed files with 6 additions and 11 deletions.
    1 change: 1 addition & 0 deletions [debug]AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?xml version="1.0" encoding="utf-8"?>
    <!-- Put this file in the "debug" folder so it only gets merged into debug builds -->
    <manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cookbrite">
    16 changes: 5 additions & 11 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    apply plugin: 'android'

    android {
    // ...
    productFlavors {
    dev {
    // The one for development/testing
    applicationId "com.cookbrite.dev"
    }
    live {
    // The flavour for releasing

    production {
    applicationId "com.cookbrite.android"
    }
    }
    }

    // ...

    // Get the path to ADB. Required when running tests directly from Android Studio.
    @@ -23,10 +23,6 @@ afterEvaluate {
    commandLine "$adb shell pm grant $android.productFlavors.dev.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
    }

    task grantAnimationPermissionStaging(type: Exec, dependsOn: 'installStagingDebug') {
    commandLine "$adb shell pm grant $android.productFlavors.staging.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
    }

    task grantAnimationPermissionProduction(type: Exec, dependsOn: 'installProductionDebug') {
    commandLine "$adb shell pm grant $android.productFlavors.production.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
    }
    @@ -36,8 +32,6 @@ afterEvaluate {
    tasks.each { task ->
    if (task.name.startsWith('assembleDevDebugAndroidTest')) {
    task.dependsOn grantAnimationPermissionDev
    } else if (task.name.startsWith('assembleStagingDebugAndroidTest')) {
    task.dependsOn grantAnimationPermissionStaging
    } else if (task.name.startsWith('assembleProductionDebugAndroidTest')) {
    task.dependsOn grantAnimationPermissionProduction
    }
  3. daj revised this gist May 5, 2015. 6 changed files with 68 additions and 59 deletions.
    11 changes: 0 additions & 11 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    <?xml version="1.0" encoding="utf-8"?>
    <manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.novoda.espresso">

    <!-- For espresso testing purposes, this is removed in live builds, but not in dev builds -->
    <uses-permission android:name="android.permission.SET_ANIMATION_SCALE" />

    <!-- ... -->

    </manifest>
    30 changes: 30 additions & 0 deletions BaseStatelessBlackBoxEspressoTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    public abstract class BaseStatelessBlackBoxEspressoTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {

    private SystemAnimations mSystemAnimations;

    public BaseStatelessBlackBoxEspressoTest(Class clazz) {
    super(clazz);
    }

    @Override
    protected void setUp() throws Exception {
    super.setUp();
    mSystemAnimations = new SystemAnimations(getInstrumentation().getContext());
    mSystemAnimations.disableAll();
    getActivity();
    }

    @Override
    protected void tearDown() throws Exception {
    super.tearDown();

    // If you want to test this entire Gist is working, comment out this line and
    // run a test. Then check these options in Settings -> Developer:
    // -Window animation scale
    // -Transition animation scale
    // -Animator duration scale
    // They should all show "Animation off". Then re-enable this line and run a
    // test again, this time it will show "Animation scale 1x"
    mSystemAnimations.enableAll();
    }
    }
    22 changes: 0 additions & 22 deletions MyInstrumentationTestCase
    Original file line number Diff line number Diff line change
    @@ -1,22 +0,0 @@
    public class MyInstrumentationTestCase extends ActivityInstrumentationTestCase2<MyActivity> {

    private SystemAnimations systemAnimations;

    public MyInstrumentationTestCase() {
    super(MyActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
    super.setUp();
    systemAnimations = new SystemAnimations(getInstrumentation().getContext());
    systemAnimations.disableAll();
    getActivity();
    }

    @Override
    protected void tearDown() throws Exception {
    super.tearDown();
    systemAnimations.enableAll();
    }
    }
    8 changes: 6 additions & 2 deletions SystemAnimations.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    class SystemAnimations {

    /**
    * Disable animations so that they do not interfere with Espresso tests.
    *
    * Source: https://code.google.com/p/android-test-kit/wiki/DisablingAnimations
    */
    public final class SystemAnimations extends AndroidJUnitRunner {
    private static final String ANIMATION_PERMISSION = "android.permission.SET_ANIMATION_SCALE";
    private static final float DISABLED = 0.0f;
    private static final float DEFAULT = 1.0f;
    11 changes: 11 additions & 0 deletions [debug]AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?xml version="1.0" encoding="utf-8"?>
    <manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cookbrite">

    <!-- Disable animations on debug builds so that the animations do not interfere with Espresso
    tests. Adding this permission to the manifest is not sufficient - you must also grant the
    permission over adb! -->
    <uses-permission android:name="android.permission.SET_ANIMATION_SCALE" />

    </manifest>
    45 changes: 21 additions & 24 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -13,36 +13,33 @@ android {
    }
    // ...

    task grantAnimationPermission(type: Exec, dependsOn: 'installDevDebug') { // or install{productFlavour}{buildType}
    commandLine "adb shell pm grant $android.defaultConfig.packageName android.permission.SET_ANIMATION_SCALE".split(' ')
    }
    // Get the path to ADB. Required when running tests directly from Android Studio.
    // Source: http://stackoverflow.com/a/26771087/112705
    def adb = android.getAdbExe().toString()

    tasks.whenTaskAdded { task ->
    if (task.name.startsWith('connectedAndroidTest')) {
    task.dependsOn grantAnimationPermission
    // Source: http://stackoverflow.com/q/29908110/112705
    afterEvaluate {
    task grantAnimationPermissionDev(type: Exec, dependsOn: 'installDevDebug') {
    commandLine "$adb shell pm grant $android.productFlavors.dev.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
    }
    }

    def copyAndReplaceText(source, dest, Closure replaceText) {
    dest.write(replaceText(source.text))
    }
    task grantAnimationPermissionStaging(type: Exec, dependsOn: 'installStagingDebug') {
    commandLine "$adb shell pm grant $android.productFlavors.staging.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
    }

    // Override Data in Manifest - This can be done using different Manifest files for each flavor, this way there's no need to modify the manifest
    android.applicationVariants.all { variant ->
    if (variant.name.startsWith('dev')) { // Where dev is the one you'll use to run Espresso tests
    System.out.println("Not removing the SET_ANIMATION_SCALE permission for $variant.name")
    return
    task grantAnimationPermissionProduction(type: Exec, dependsOn: 'installProductionDebug') {
    commandLine "$adb shell pm grant $android.productFlavors.production.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
    }

    System.out.println("Removing the SET_ANIMATION_SCALE permission for $variant.name")
    variant.processManifest.doLast {
    copyAndReplaceText(manifestOutputFile, manifestOutputFile) {
    def replaced = it.replace('<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>', '');
    if (replaced.contains('SET_ANIMATION_SCALE')) {
    // For security, imagine an extra space is added before closing tag, then the replace would fail - TODO use regex
    throw new RuntimeException("Don't ship with this permission! android.permission.SET_ANIMATION_SCALE")
    }
    replaced
    // When launching individual tests from Android Studio, it seems that only the assemble tasks
    // get called directly, not the install* versions
    tasks.each { task ->
    if (task.name.startsWith('assembleDevDebugAndroidTest')) {
    task.dependsOn grantAnimationPermissionDev
    } else if (task.name.startsWith('assembleStagingDebugAndroidTest')) {
    task.dependsOn grantAnimationPermissionStaging
    } else if (task.name.startsWith('assembleProductionDebugAndroidTest')) {
    task.dependsOn grantAnimationPermissionProduction
    }
    }
    }
  4. @xrigau xrigau revised this gist May 7, 2014. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,6 @@ apply plugin: 'android'

    android {
    // ...
    defaultConfig {
    // ...
    testInstrumentationRunner "com.novoda.espresso.MyInstrumentationTestRunner"
    }
    productFlavors {
    dev {
    // The one for development/testing
    @@ -31,7 +27,7 @@ def copyAndReplaceText(source, dest, Closure replaceText) {
    dest.write(replaceText(source.text))
    }

    // Override Data in Manifest
    // Override Data in Manifest - This can be done using different Manifest files for each flavor, this way there's no need to modify the manifest
    android.applicationVariants.all { variant ->
    if (variant.name.startsWith('dev')) { // Where dev is the one you'll use to run Espresso tests
    System.out.println("Not removing the SET_ANIMATION_SCALE permission for $variant.name")
  5. @xrigau xrigau revised this gist May 7, 2014. 2 changed files with 22 additions and 23 deletions.
    22 changes: 22 additions & 0 deletions MyInstrumentationTestCase
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    public class MyInstrumentationTestCase extends ActivityInstrumentationTestCase2<MyActivity> {

    private SystemAnimations systemAnimations;

    public MyInstrumentationTestCase() {
    super(MyActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
    super.setUp();
    systemAnimations = new SystemAnimations(getInstrumentation().getContext());
    systemAnimations.disableAll();
    getActivity();
    }

    @Override
    protected void tearDown() throws Exception {
    super.tearDown();
    systemAnimations.enableAll();
    }
    }
    23 changes: 0 additions & 23 deletions MyInstrumentationTestRunner.java
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    public class MyInstrumentationTestRunner extends GoogleInstrumentationTestRunner {

    private SystemAnimations systemAnimations;

    @Override
    public void onCreate(Bundle arguments) {
    super.onCreate(arguments);
    systemAnimations = new SystemAnimations(getContext());
    }

    @Override
    public void callActivityOnCreate(Activity activity, Bundle bundle) {
    systemAnimations.disableAll();
    super.callActivityOnCreate(activity, bundle);
    }

    @Override
    public void callActivityOnDestroy(Activity activity) {
    super.callActivityOnDestroy(activity);
    systemAnimations.enableAll();
    }

    }
  6. @xrigau xrigau revised this gist Apr 25, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SystemAnimations.java
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ private void setSystemAnimationsScale(float animationScale) {
    }
    setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales});
    } catch (Exception e) {
    Log.e("Could not change animation scale to " + animationScale + " :'(");
    Log.e("SystemAnimations", "Could not change animation scale to " + animationScale + " :'(");
    }
    }
    }
  7. @xrigau xrigau revised this gist Apr 25, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?xml version="1.0" encoding="utf-8"?>
    <manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.novoda.espresso">

    <!-- For espresso testing purposes, this is removed in live builds, but not in dev builds -->
    <uses-permission android:name="android.permission.SET_ANIMATION_SCALE" />

    <!-- ... -->

    </manifest>
  8. @xrigau xrigau created this gist Apr 25, 2014.
    23 changes: 23 additions & 0 deletions MyInstrumentationTestRunner.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    public class MyInstrumentationTestRunner extends GoogleInstrumentationTestRunner {

    private SystemAnimations systemAnimations;

    @Override
    public void onCreate(Bundle arguments) {
    super.onCreate(arguments);
    systemAnimations = new SystemAnimations(getContext());
    }

    @Override
    public void callActivityOnCreate(Activity activity, Bundle bundle) {
    systemAnimations.disableAll();
    super.callActivityOnCreate(activity, bundle);
    }

    @Override
    public void callActivityOnDestroy(Activity activity) {
    super.callActivityOnDestroy(activity);
    systemAnimations.enableAll();
    }

    }
    48 changes: 48 additions & 0 deletions SystemAnimations.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    class SystemAnimations {

    private static final String ANIMATION_PERMISSION = "android.permission.SET_ANIMATION_SCALE";
    private static final float DISABLED = 0.0f;
    private static final float DEFAULT = 1.0f;

    private final Context context;

    SystemAnimations(Context context) {
    this.context = context;
    }

    void disableAll() {
    int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
    if (permStatus == PackageManager.PERMISSION_GRANTED) {
    setSystemAnimationsScale(DISABLED);
    }
    }

    void enableAll() {
    int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
    if (permStatus == PackageManager.PERMISSION_GRANTED) {
    setSystemAnimationsScale(DEFAULT);
    }
    }

    private void setSystemAnimationsScale(float animationScale) {
    try {
    Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub");
    Method asInterface = windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class);
    Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager");
    Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class);
    Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager");
    Method setAnimationScales = windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class);
    Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales");

    IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window");
    Object windowManagerObj = asInterface.invoke(null, windowManagerBinder);
    float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj);
    for (int i = 0; i < currentScales.length; i++) {
    currentScales[i] = animationScale;
    }
    setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales});
    } catch (Exception e) {
    Log.e("Could not change animation scale to " + animationScale + " :'(");
    }
    }
    }
    52 changes: 52 additions & 0 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    apply plugin: 'android'

    android {
    // ...
    defaultConfig {
    // ...
    testInstrumentationRunner "com.novoda.espresso.MyInstrumentationTestRunner"
    }
    productFlavors {
    dev {
    // The one for development/testing
    }
    live {
    // The flavour for releasing
    }
    }
    }
    // ...

    task grantAnimationPermission(type: Exec, dependsOn: 'installDevDebug') { // or install{productFlavour}{buildType}
    commandLine "adb shell pm grant $android.defaultConfig.packageName android.permission.SET_ANIMATION_SCALE".split(' ')
    }

    tasks.whenTaskAdded { task ->
    if (task.name.startsWith('connectedAndroidTest')) {
    task.dependsOn grantAnimationPermission
    }
    }

    def copyAndReplaceText(source, dest, Closure replaceText) {
    dest.write(replaceText(source.text))
    }

    // Override Data in Manifest
    android.applicationVariants.all { variant ->
    if (variant.name.startsWith('dev')) { // Where dev is the one you'll use to run Espresso tests
    System.out.println("Not removing the SET_ANIMATION_SCALE permission for $variant.name")
    return
    }

    System.out.println("Removing the SET_ANIMATION_SCALE permission for $variant.name")
    variant.processManifest.doLast {
    copyAndReplaceText(manifestOutputFile, manifestOutputFile) {
    def replaced = it.replace('<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>', '');
    if (replaced.contains('SET_ANIMATION_SCALE')) {
    // For security, imagine an extra space is added before closing tag, then the replace would fail - TODO use regex
    throw new RuntimeException("Don't ship with this permission! android.permission.SET_ANIMATION_SCALE")
    }
    replaced
    }
    }
    }