How to create android project with gradle
from command line (without any IDE)?
Before it was with android util like below
Here is the solution PHP Android CLI
Just run:
phpandroid create HelloWorld com.example.helloworld
and your project is scaffolded with latest android-studio parameters, like, compile & target SDK is 29, buildToolsVersion is 29.0.1 & minSdk is 16.
If you want to change, like, want to set minSdk to 14:
phpandroid create PROJECT PACKAGE --minSdk=14
PHP Android CLI can also create Variants and modules (application/library):
phpandroid create PROJECT PACKAGE --modules=common:library,admin --variants=free:type,paid:type,php:backend,firebase:backend
this will generate 2 applications, app
, admin
& a library common
.
App
has 2 dimensions: type
& backend
4 variants: free
& paid
of type
dimension; php
& firebase
variant of backend
dimension.
android create project
was removed in SDK tools 26.0.1
See https://askubuntu.com/questions/906942/android-update-project-path-target-android-25-on-ubuntu-16-04 for more details
Trying to use it fails with:
*************************************************************************
The "android" command is deprecated.
For manual SDK, AVD, and project management, please use Android Studio.
For command-line tools, use tools/bin/sdkmanager and tools/bin/avdmanager
*************************************************************************
Invalid or unsupported command "project create"
Supported commands are:
android list target
android list avd
android list device
android create avd
android move avd
android delete avd
android list sdk
android update sdk
Workaround: Android Studio template + copy helper script
The best workaround I have is to:
The following helper script allows you to do:
./template NewAppName AppTemplateProject
to get a new app NewAppName
from an existing AppTemplateProject
created with Android studio:
#!/usr/bin/env bash
set -ex
new="$1"
shift
if [ $# -gt 0 ]; then
old="$1"
shift
else
old='Min'
fi
old="$(echo "$old" | sed -E 's/\/$//')"
new="$(echo "$new" | sed -E 's/\/$//')"
new_lower="$(echo "$new" | tr 'A-Z' 'a-z')"
old_lower="$(echo "$old" | tr 'A-Z' 'a-z')"
cp -r "$old" "$new"
cd "$new"
find . -type f -print0 | xargs -0 sed -i "s/${old}/${new}/g"
find . -type f -print0 | xargs -0 sed -i "s/${old_lower}/${new_lower}/g"
cd 'app/src/main/java/com/cirosantilli/android_cheat'
GitHub upstream: https://github.com/cirosantilli/android-cheat/blob/0e6b7462705179658b48d0e2e27f0dbce308393c/gradle/template
Yes it is not perfect. But CLI support appears secondary to Google, so what can you do :-(
Similar to How to create Java gradle project
suggest to create Android project with android create project
than add build.gradle
template for classic Android project gh.c/N/n-1/b/m/o.n.e.e.g/docs/android/build.gradle.
(That would allow to develop in any IDE, as old structure is more widely adopted)
Of course there will be some gradle init
options or android create
(from SDK) in the future.
Android SDK 19 has android
CLI -g
option that allows to use gradle template. You might also need to specify android gradle plugin version with CLI -v
option, check android gradle plugin compatibility table. Example command to create the project that uses android gradle plugin (v 0.10) to add gradle support.
android create project \
--gradle \
--gradle-version 0.10 \
--activity Main \
--package com.example.app \
--target android-19 \
--path AppWithGradleTemplate
or for buildTools 19.1+, use a newer version of the Gradle Android plugin via --gradle-version
:
android create project \
--gradle \
--gradle-version 0.11.+ \
--activity Main \
--package com.example.app \
--target android-25 \
--path AppWithGradleTemplate
check android create project -h
for help
However Android Studio 0.6.1 failed to open it correctly (no sources shown), because it took first project folder (that is gradle
) as module folder -> you need to Import, not just open.
In Eclipse it was with a trick of regarding src
folder as root of the project.
.classpath
is
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="java"/><!--ADJUSTED HERE -->
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
And build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android'
android {
//{ for Android Gradle as Eclipse project
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['java']
resources.srcDirs = ['java']
aidl.srcDirs = ['java']
renderscript.srcDirs = ['java']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('../tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
//}
compileSdkVersion 'Google Inc.:Google APIs:10'
buildToolsVersion '19.0.3'
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
lintOptions {
abortOnError false
}
}
http://marketplace.eclipse.org/content/gradle
read at http://www.nodeclipse.org/projects/gradle/