Gradle script for copying apk to external path according to build type

Here is a gradle script for copying apk according to build type:

def publish = project.tasks.create("archive")
    applicationVariants.all { variant ->
        if ( !project.hasProperty("type") ) {
            return
        }
        if (variant.buildType.name == project.property("type")) {
            println "=> Called archiveCache"
            variant.outputs.each { output ->
                if (output.outputFileName.endsWith('.apk')) {
                    def task = project.tasks.create("archive${variant.name.capitalize()}Apk", Copy)
                    def srcDir = variant.packageApplicationProvider.get().outputDirectory.getAsFile().get()
                    println "=> srcDir=" + srcDir
                    def srcPath = new File(srcDir, output.outputFileName)
                    println "=> srcPath=" + srcPath

                    task.from(srcPath)

                    def outputFolder = localProperties.getProperty("archiveFolderPath", "") + parent.name + "/" + new Date().format('yyyy-MM-dd-HH_mm_ss') + "/"
                    println "=> outputFolder=" + outputFolder

                    task.into(outputFolder)
                    task.dependsOn variant.assembleProvider.get()
                    publish.dependsOn task
                }
            }
        }
    }

Console usage:

./gradlew archive -Ptype=debug (or release or any other build type)

Output path “archiveFolderPath” was declared in: local.properties file as below:

archiveFolderPath=/path/folder/destination/

Connect Huawei device over wifi with adb

We need:

  1. Device’s name and ip
    1. My target’s name: EHT0220B19001804
    2. My target’s ip: 192.168.0.249
  2. Enabled USB Debugging and checked Allow ADB debugging in charge only mode.

Then we call:

> adb devices   

List of devices attached
EHT0220B19001804	device << TARGET
192.168.0.38:5555	device

> adb -s EHT0220B19001804 tcpip 5555

> adb connect 192.168.0.249:5555

> adb -s 192.168.0.249:5555 shell

Now you can safely disconnect the cable and try write down the command:

adb -s 192.168.0.249:5555 shell

If you will see something like this below it means it works!

HWVOG:/ $

ObjectAnimator change text color with repeat and reverse

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ...

        TextView notPaidText = findViewById(R.id.not_paid);
        ObjectAnimator colorAnim = ObjectAnimator.ofInt(
                notPaidText,
                "textColor",
                Color.parseColor("#50ff0000"),
                Color.parseColor("#00ffffff")
        );
        colorAnim.setEvaluator(new ArgbEvaluator());
        colorAnim.setDuration(10000);
        TimeInterpolator baseInterpolator = colorAnim.getInterpolator();
        colorAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (animation.getInterpolator() instanceof AccelerateDecelerateInterpolator) {
                    animation.setInterpolator(new ReverseInterpolator());
                } else {
                    animation.setInterpolator(baseInterpolator);
                }
                animation.start();
            }
        });
        colorAnim.start();
    }

    public class ReverseInterpolator implements Interpolator {

        private final Interpolator delegate;

        public ReverseInterpolator(Interpolator delegate) {
            this.delegate = delegate;
        }

        public ReverseInterpolator() {
            this(new LinearInterpolator());
        }

        @Override
        public float getInterpolation(float input) {
            return 1 - delegate.getInterpolation(input);
        }
    }
}

Get current Activity name by adb shell

Here is the command:

adb shell "dumpsys window windows | grep -E 'mCurrentFocus'"

The output would be:

mCurrentFocus=Window{b6a99d1d0 u0 com.sec.android.app.launcher/com.sec.android.app.launcher.activities.LauncherActivity}

My activity name is: LauncherActivity

create RainbowSpan effect

public class RainbowSpan extends CharacterStyle implements UpdateAppearance {
    private final int[] colors;

    public RainbowSpan(Context context) {
        colors = new int[]{
                Color.parseColor("#FF0033"),
                Color.parseColor("#FF8000"),
                Color.parseColor("#FFE600"),
                Color.parseColor("#1AB34D"),
                Color.parseColor("#1A66FF"),
                Color.parseColor("#801AB3")
        };
    }

    @Override
    public void updateDrawState(TextPaint paint) {
        paint.setStyle(Paint.Style.FILL);
        Shader shader = new LinearGradient(
                0, 0, 0,
                paint.getTextSize() * colors.length,
                colors, null,
                Shader.TileMode.MIRROR);
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        shader.setLocalMatrix(matrix);
        paint.setShader(shader);
    }
}