Skip to content

Instantly share code, notes, and snippets.

@mattak
Last active December 20, 2015 01:39
Show Gist options
  • Save mattak/6050982 to your computer and use it in GitHub Desktop.
Save mattak/6050982 to your computer and use it in GitHub Desktop.
gradle android library project startup flavor
#!/bin/sh
#
# gradle android plugin
#
function usage() {
cat <<__USAGE__
usage:
gradroid [command] [args]?
command:
create [projectname] [args]+
-k [package_name] (required)
the project package name
-a [main activity] (required)
the main activity.
create-lib [project_name] [options]+
-p [package_name] (required)
the project packagename
-g [group_id] (required)
maven group id
-a [artifact_id] (required)
maven artifact id
__USAGE__
exit 1
}
#
# create project build.gradle
#
#
# create build.gradle
#
function create_lib_build_gradle() {
BUILD_FILE="build.gradle"
GROUP_ID=$1
ARTIFACT_ID=$2
cat <<__BUILD_SCRIPT__ > $BUILD_FILE
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
apply plugin: 'maven'
def code = 1
def version = '0.0.1'
def group = '$GROUP_ID'
def name = '$ARTIFACT_ID'
def home = System.getProperty("user.home")
def repos = "file://\$home/project/android-Repository/"
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
versionCode code
versionName version
}
}
uploadArchives {
repositories {
mavenDeployer {
repository url: repos
pom.version = version
pom.groupId = group
pom.artifactId = name
}
}
}
__BUILD_SCRIPT__
}
function create_src() {
PKG=$1
SRC_DIR=`echo "src/main/java/$1" | sed 's|\.|/|g'`
mkdir -p "${SRC_DIR}"
SRC_FILE="${SRC_DIR}/Sample.java"
cat <<... > "${SRC_FILE}"
package $PKG;
import android.content.Context;
import ${PKG}.R;
public class Sample {
private Context mContext;
public Sample(Context context) {
mContext = context;
}
public String getHello() {
return mContext.getString(R.string.hello);
}
}
...
}
function create_manifest() {
PACKAGE=$1
cat <<... > "src/main/AndroidManifest.xml"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="$PACKAGE"
android:versionCode="1"
android:versionName="0.0.1">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
</manifest>
...
}
function create_test() {
PKG=$1
PKG_TEST="${1}.test"
SRC_DIR=`echo "src/main/instrumentTest/$PKG_TEST" | sed 's|\.|/|g'`
mkdir -p "${SRC_DIR}"
SRC_FILE="${SRC_DIR}/SampleTest.java"
cat <<... > "${SRC_FILE}"
package $PKG_TEST;
import ${PKG}.Sample;
public class SampleTest extends AndroidTestCase {
private Context mContext;
public void setUp() throws Exception {
super.setUp();
mContext = getContext();
}
public void tearDown() throws Exception {
super.tearDown();
}
public void test001CheckHello() {
Sample sample = new Sample(mContext);
assertEquals("hello world", sample.getHello());
}
public void test002Failure() {
assertTrue(false);
}
}
...
}
function create_res() {
SRC_DIR="src/main/res/values/"
mkdir -p "${SRC_DIR}"
SRC_FILE="${SRC_DIR}/strings.xml"
cat <<... > "${SRC_FILE}"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">hello world</string>
</resources>
...
}
function create_lib_readme() {
SRC_DIR="."
mkdir -p "${SRC_DIR}"
SRC_FILE="${SRC_DIR}/README.md"
cat <<... > $SRC_FILE
# your project name
your project description.
# setup
$ curl -s get.gvmtool.net | bash
$ gvm install gradle 1.6
# build
$ gradle clean build
# test
$ gradle clean connectedInstrumentTest
# publish to repository
$ gradle clean uploadArchives
# release histroy
- `date '+%y-%m-%d'` v0.0.1 repository created.
...
}
function create_gitignore() {
SRC_DIR="."
mkdir -p "${SRC_DIR}"
SRC_FILE="${SRC_DIR}/.gitignore"
cat <<... > $SRC_FILE
# tmp
*.swp
*~
# android
build/
gen/
bin/
# android studio
*.iml
.idea/
# gradle
.gradle/
...
}
function create_git() {
SRC_DIR="."
mkdir -p "${SRC_DIR}"
cd $SRC_DIR
git init
cd -
}
#
# main
#
if [ $# -eq 0 ]; then
usage
fi
if [ $1 = "create-lib" ]; then
shift
PROJECT=$1; shift
[ $# -eq 0 ] && usage
TEMP=$(getopt p:g:a: "$@")
#TEMP=$(getopt -o p:g:a: -l package:,group:,artifact: "$@")
[ $? != 0 ] && usage
eval set -- "$TEMP"
while true
do
case "$1" in
-p | --package)
PACKAGE="$2"
shift 2;
continue
;;
-a | --artifact)
ARTIFACT_ID="$2"
shift 2;
continue
;;
-g | --group)
GROUP_ID="$2"
shift 2;
continue
;;
--)
# no more argument to parse
shift;
break;
;;
*)
printf "Unknown options %s\n" "$1"
exit 1
;;
esac
shift
done
echo "PRJ: $PROJECT"
echo "PKG: $PACKAGE"
echo "ART: $ARTIFACT_ID"
echo "GRP: $GROUP_ID"
if [ "$PACKAGE" = "" -o "$ARTIFACT_ID" = "" -o "$GROUP_ID" = "" ]; then
echo "required option missing."
exit 0
fi
mkdir -p $PROJECT
CURRENT_DIR=`pwd`
cd $PROJECT
create_lib_build_gradle $GROUP_ID $ARTIFACT_ID
create_src $PACKAGE
create_manifest $PACKAGE
create_res
create_test $PACKAGE
create_lib_readme
create_gitignore
cd $CURRENT_DIR
else
usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment