Git pre-commit hook to run lint check? - android

how to Write a git pre-commit hook to run lint check for change/newly added file,
currently, I'm running "./gradlew lint" command before commit, It would be much easier if there is a way to run the lint only for add/change files before committing to git.

# Get custom info
dirToLint=$(git config hooks.lintTargetDirectory)
lintArgs=$(git config hooks.lintArgs)
# If user has not defined a preferred directory to lint against, make it .
if [ -z "$dirToLint"]
then
dirToLint="."
fi
# Perform lint check
echo "Performing pre-commit lint check of ""$dirToLint"
lint $lintArgs "--exitcode" $dirToLint
lintStatus=$?
if [ $lintStatus -ne 0 ]
then
echo "Lint failure."
exit 1
fi
exit $lintStatus
put this in your .git/hooks/ folder as pre-commit.sh

Related

AOSP Build error: EXECUTABLES/iw_intermediates/version.c

I've made a AOSP build 7.1.1 couple of days ago and it works well. Today I decided to make a clean build (make clean) and start working on it. After that when I'm trying to compile by make otapackage after some time it gives me below error.
[ 44% 7552/17136] build out/target/product/hikari/gen/EXECUTABLES/iw_intermediates/version.c
FAILED: /bin/bash -c "external/iw/version.sh out/target/product/hikari/gen/EXECUTABLES/iw_intermediates/version.c"
fatal: No names found, cannot describe anything.
ninja: build stopped: subcommand failed.
build/core/ninja.mk:148: recipe for target 'ninja_wrapper' failed
make: *** [ninja_wrapper] Error 1
#### make failed to build some targets (01:39:34 (hh:mm:ss)) ####
Everytime, its the same. Any solution ?
Its failing when it is trying to check the git version number.
Inside external/iw/version.sh, it is failing when it gets to:
descr=$(git describe --match=v*):
if [ -d .git ] && head=`git rev-parse --verify HEAD 2>/dev/null`; then
git update-index --refresh --unmerged > /dev/null
descr=$(git describe --match=v*)
# on git builds check that the version number above
# is correct...
[ "${descr%%-*}" = "v$VERSION" ] || exit 2
v="${descr#v}"
if git diff-index --name-only HEAD | read dummy ; then
v="$v"-dirty
fi
else
v="$VERSION"
fi
echo '#include "iw.h"' > "$OUT"
echo "const char iw_version[] = \"$v\";" >> "$OUT"
I had the same problem and there is two ways to go about it:
You can comment out everything except v="$VERSION" and the last two
echo lines.But this is a temporary fix.
Another way to solve this is by doing a repo sync before you execute make. I fixed it by simply doing a repo sync and that updated and
fixed everything for me.

Travis.yml ./gradlew : Permission denied

Using Travis CI for an existing Android project calling
$ ./gradlew build connectedCheck
I get this error:
/home/travis/build.sh: line 45: ./gradlew: Permission denied
The command "./gradlew build connectedCheck" failed and exited with 126 during .
It depends by the exec-permission to your unix gradlew script.
It can be fixed using the command:
git update-index --chmod=+x gradlew
A little desciption to understand the problem.
First of all you can check your permissions using:
git ls-tree HEAD
You will see:
100644 blob xxxxxxxxxxx gradlew
As you can see the file has 644 permission.
Fix it by setting the executable flag on your gradlew file changing it to 755:
git update-index --chmod=+x gradlew
Just commit and push the changes:
git commit -m "permission access for travis"
[master e80ab1b] gradlew permission access for travis
1 file changed, 0 insertions(+), 0 deletions(-)
mode change 100644 => 100755 gradlew
A last check running git ls-tree again to see the change:
git ls-tree HEAD
You can see:
100755 blob xxxxxxxxxxxxx gradlew
Another way to solve this issue is to use:
before_install:
- chmod +x gradlew
This kind of solution doesn't change the permission in your git repo, but just changes the permission runtime in the execution.
script:
- chmod +x ./gradlew build connectedCheck
Thanks all.
This code is available.
The key focus is on chmod +x

After upgrading Gradle to v2.8 Travis doesn't pass Findbugs

I just updated my gradle from version 2.2 to latest one 2.8. I didn't have any issue with findbugs on version 2.2.
I'm working on an Android project that contains two modules. In order to use find bugs in both modules I have following configuration on main build.gradle file of root directory.
configure(allprojects) {
apply plugin: 'findbugs'
task findbugs(type: FindBugs) {
ignoreFailures = false
effort = "max"
classes = fileTree('build/intermediates/classes/')
source = fileTree('src/main/java')
classpath = files()
excludeFilter = file("exclude.xml")
reportLevel = "high"
reports {
xml.enabled = false
html.enabled = true
}
}
}
When I run ./gradlew findbugs on my local machine everything is fine and build is successful however when I push my PR to Github and Trivis tries to build then I get error:
:findbugs UP-TO-DATE
:passenger-app:findbugs
:passenger-sdk:findbugs FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':passenger-sdk:findbugs'.
> FindBugs rule violations were found. See the report at: file:///home/travis/build/project-name/passenger-android/passenger-sdk/build/reports/findbugs/findbugs.html
I'm really confused why I have no problem on my local machine while Travis shows error! I tried to print out the contents findbugs.html on Travis but I got permission denied :(
I'm using java 1.8 while Travis is using 1.7. Does problem relates to this? Thanks
Update:
In order to print the contents of findbugs.html on Trivis I created a print_findbugs.sh and this is its contents.
#!/usr/bin/env bash
echo '**********************'
echo '*** Print Findbugs ***'
echo '**********************'
echo file:///home/travis/build/company/passenger-android/passenger-sdk/build/reports/findbugs/findbugs.html
Then I set sudo true in my .travis.yml file. What I have in this file.
sudo: true
language: android
android:
components:
- build-tools-23.0.1
- android-23
- extra-android-support
- extra-google-google_play_services
- extra-google-m2repository
- extra-android-m2repository
env:
global:
// some settings are there
before_cache:
# Delete the gradle lock file which forces creation of a new build cache
- rm ~/.gradle/caches/modules-2/modules-2.lock
cache:
directories:
- ~/.gradle
before_script:
# Overwrite the keystore if it is a pull request
- ./before_script.sh
script:
# Override Travis default script to not run connectedCheck until this bug is fixed:
# https://code.google.com/p/android/issues/detail?id=59592
- ./gradlew clean build findbugs -PdisablePreDex
- ./print_findbugs.sh
before_deploy:
# Clean up the output folder
# Link up the new builds into individual html files
- ./before_deploy.sh
after_deploy:
# Upload to...
and finally my travis prints:
:findbugs UP-TO-DATE
:passenger-app:findbugs
:passenger-sdk:findbugs
FindBugs rule violations were found. See the report at: file:///home/travis/build/company/passenger-android/passenger-sdk/build/reports/findbugs/findbugs.html
BUILD SUCCESSFUL
Total time: 8 mins 9.966 secs
The command "./gradlew clean build findbugs -PdisablePreDex" exited with 0.
$ ./print_findbugs.sh
/home/travis/build.sh: line 41: ./print_findbugs.sh: Permission denied
The command "./print_findbugs.sh" exited with 126.
before_cache
$ rm ~/.gradle/caches/modules-2/modules-2.lock
cache.2
Done. Your build exited with 1.
I don't use it and I need more information about the permission denied.
Html reports
In the past, I printed html reports using Travis-ci, point 5 here. I downloaded lynx using apt-get (it's not possible now using container-infrastructure and sudo: false) and converted and printed the reports.
before_script:
# - echo 'LOGCAT'
# Check logcat debug output: http://developer.android.com/tools/help/logcat.html
# Check debugging log: http://developer.android.com/tools/debugging/debugging-log.html
# Comment the lines belows to debug output and redirect it to a file. Custom tags for your app.
- adb -e logcat *:W | tee logcat.log > /dev/null 2>&1 &
after_failure:
# - echo 'FAILURE'
# Check apt configuration: http://docs.travis-ci.com/user/ci-environment/#apt-configuration
# Comment out the lines below to show log about tests with app name customized on exports section.
- sudo apt-get install -qq lynx
- export MOD_NAME=yourappmodulename
- export LOG_DIR=${TRAVIS_BUILD_DIR}/${MOD_NAME}/build/outputs/reports/androidTests/connected/
- lynx --dump ${LOG_DIR}com.android.builder.testing.ConnectedDevice.html > myConnectedDevice.log
- lynx --dump ${LOG_DIR}com.android.builder.testing.html > myTesting.log
- for file in *.log; do echo "$file"; echo "====================="; cat "$file"; done || true
Xml reports
Can be used in both, legacy and container based infrastructure.
I read you can enable xml reports like this:
reports {
xml.enabled = true
html.enabled = true
}
You can easily print xml reports on Travis-ci using cat like here:
- cat ${TRAVIS_BUILD_DIR}/ui/espresso/*/app/build/outputs/androidTest-results/connected/* # logs
Adding * will include all the subfolders at this point.
You need to first find locally the folder for the xml reports, in my case is not the same folder than html, and add to your travis.yml file something like this:
after_failure:
- cat /home/travis/build/*/passenger-android/passenger-sdk/build/reports/findbugs/*
This doesn't solve your issue but perhaps helps to find reason.
Update:
I suggest you to try to use cat and the xml version without the script first.
A good explanation about the permission issue here and how to solve it making the file executable:
before_script:
- chmod +x yourscript
Update 2:
A better approach to fix the permission denied issue explained here.
Use this and commit the changes:
git update-index --chmod=+x yourscript
In my case, ignoreFailures was true, but nevertheless the findbugs task was failing after upgrading gradle because build.gradle was depending on findbugs:annotations:3.0.0 instead of findbugs:findbugs-annotations:3.0.1. (Note: annotations vs. findbugs-annotations, which is a new artifact.
Finally I was able to get everything passed.
I first used following command rather than simple $ ./gradlew findbugs.
./gradlew clean aGD findbugs -PdisablePreDex
aGD is abbreviation of my task something like assembleDebug.
I could see a lot of complains by gradle. Probably Travis was showing this (afterDebug) but since it prints html page in console is not human readable. So I added filters into Findbug's exclude file (ref) in order to pass Findbugs check.
I was successful! Once I pushed my code, Travis failed again.
But since I printed out Findbug's output on Travis and there was only an issue I could find it in my code and Fix it. After that Travis didn't failed and passed my PR.
So the problem is still exist unfortunately. I can get Findbugs passed on my local project while Travis sometimes finds more issues. I'm suspicious that Travis is able to load recent versions of Findbugs but mine isn't able, due to cache things. But yes, this is my assumption!

Enabling Loadable Modules Android

I'm trying to compile a kernel (Goldfish) to later insert some modules. I know that I need to enable module support, module loading, and unloading. I'm using OSX Mavericks.
So far I have checked out goldfish from git, and have done the following:
make ARCH=arm goldfish_armv7_defconfig
No problems there. Now when I do:
make ARCH=arm CROSS_COMPILE=/Volumes/untitled/WORKING_DIRECTORY/prebuilts/gcc/darwin-x86/arm/arm-eabi-4.6/bin/arm-eabi- menuconfig
I get the following:
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: * [scripts/kconfig/mconf] Error 1
make: * [menuconfig] Error 2
Is there a way that I can enable modules another way? Maybe go into a file and do it manually?
If I just run (which I'm supposed to run after to get a zImage):
make ARCH=arm SUBARCH=arm CROSS_COMPILE=/Volumes/untitled/WORKING_DIRECTORY/prebuilts/gcc/darwin-x86/arm/arm-eabi-4.6/bin/arm-eabi -j16
The kernel compiles and I can run it via an emulator, but obviously I can't upload kernel modules.
module loading is disabled by default in android kernel.
Enable it using CONFIG_MODULES=y in your config file.
Open scripts/kconfig/lxdialog/check-lxdialog.sh
Replace ldflags() with this
ldflags()
{
for ext in so a dylib ; do
for lib in ncursesw ncurses curses ; do
#$cc -print-file-name=lib${lib}.${ext} | grep -q /
#if [ $? -eq 0 ]; then
if [ -f /usr/lib/lib${lib}.${ext} ]; then
echo "-l${lib}"
exit
fi
done
done
exit 1
}
Now make menuconfig should work. The problem seems to be the result of a bug with clang on OS X. The -print-file-name option doesn't work correctly.
Alternatively, you could open .config file in a text editor and set
CONFIG_MODULES=y

Android ant and bash scripting

I've had 0 exposure to BASH scripting and this is something I would love to learn. I can't figure out how to run a conditional statement based on the output of ant debug on an Android build.
I would like to essentially say something like
if(`ant debug` == SUCCESS) {
// EXECUTE THESE COMMANDS
} else {
// EXECUTE THESE COMMANDS
}
How can I determine if the ant debug has passed or failed in shell script?
SOLUTION
Okay here is what I have:
ant clean
if ant debug; then
echo "success"
else
echo "failure"
fi
I'll give a quick summary for you.
In Bash, conditionals are based around the exit codes of programs. An exit code of 0 is accepted as true, while everything else is accepted as false.
For example, the true program always exits with an exit code of 0, which means that something like this is possible:
if true;
echo "It is true"
fi
Most commands honor this system, but not every program does. The first thing to check is what exit code ant returns on success and failure. You can check the exit code of the previous command with $?.
Here is an example:
$ true
$ echo $?
0
$ false
$ echo $?
1
If ant does honor the exit code system properly, then something like the following should be possible:
if ant debug; then
echo success
else
echo failure
fi
I know nothing about Ant debugging, but there are two approaches to doing what you want to do in Bash. The first is to test output like you've shown:
if test $(ant debug) == 'SUCCESS'; then
# do stuff
else
# do other stuff
fi
You can make your shell script portable to other variants on the Bourne shell by using backticks instead of $(.....) like you wrote in your question, but that starts to become a hassle if your commands later involve nested quotes.
The second way, which is a little more robust, is to test the exit value of the commands instead of their output. This depends on Ant (or whatever) having exit codes that are documented and stable, but it means that if the output of the commands changes, your scripts will continue to work. For example, the POSIX standard says that if a programs succeeds in doing whatever it's supposed to do, it should exit() with a value of zero:
ant debug > /dev/null
ant_exit_code=$?
# other commands can safely go here now that $? is captured
if test $ant_exit_code -eq 0; then
# do stuff
else
# do other stuff
fi
And yes, Bourne shell really does end an if block with "fi". :-)
A quick man ant shows that ant debug invokes Ant with the debug task. Ant's tasks are kinda of user-defined XML scripts, and by default Ant searches a build.xml file in the current directory. You can generate the file with the android tools, however a template is kept in android-sdk/tools/ant and you can view it online (line 1005 defines the debug target).
So ant debug isn't really a command, and should not be put in a script toolchain. However, if you find your output to be stable and feel brave, you can always compare string. This is the definitve guide.
if [ `ant debug` = $SOMETHING ]; then
echo Success
else
echo Failure
fi

Categories

Resources