How to Create Android Project with Gradle from Command Line

How to create android project with gradle from command line?

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.

UPDATE:

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
}
}

Sample Image

http://marketplace.eclipse.org/content/gradle

read at http://www.nodeclipse.org/projects/gradle/

build android apps through command-line using gradle

I noticed that Android Studio creates a temporary file each time I debug an app and pass that file as an argument to gradle command-line:

2022-04-03 10:37:18,658 [ thread 18] INFO - oject.common.GradleInitScripts - init script file sync.local.repo contents "allprojects {\n buildscript {\n repositories {\n maven { url 'C:\\\\Program Files\\\\Android\\\\Android Studio\\\\gradle\\\\m2repository'}\n }\n }\n repositories {\n maven { url 'C:\\\\Program Files\\\\Android\\\\Android Studio\\\\gradle\\\\m2repository'}\n }\n}\n"
the content in a normal perspective:


allprojects {
buildscript {
repositories {
maven { url 'C:\\Program Files\\Android\\Android Studio\\gradle\\m2repository'}
}
}
repositories {
maven { url 'C:\\Program Files\\Android\\Android Studio\\gradle\\m2repository'}
}
}

Passing to gradle command-line with --init-script option:


gradle --init-script path/to/file --offline assembleDebug

--init-script option prabably could be defined in gradle.properties file, I'v not tested!

Build Android Studio app via command line

Android Studio automatically creates a Gradle wrapper in the root of your project, which is how it invokes Gradle. The wrapper is basically a script that calls through to the actual Gradle binary and allows you to keep Gradle up to date, which makes using version control easier. To run a Gradle command, you can simply use the gradlew script found in the root of your project (or gradlew.bat on Windows) followed by the name of the task you want to run. For instance, to build a debug version of your Android application, you can run ./gradlew assembleDebug from the root of your repository. In a default project setup, the resulting apk can then be found in app/build/outputs/apk/app-debug.apk. On a *nix machine, you can also just run find . -name '*.apk' to find it, if it's not there.

create android project with gradle from command line deprecated?

As instructed here:

http://eqdn.tech/android-development-on-the-command-line/

You can still use "android create project", but will need to adjust the results manually a little bit afterwards. Essentially changing in gradle/wrapper/gradle-wrapper.properties this:

distributionUrl=http://services.gradle.org/distributions/gradle-1.12-all.zip

To this:

distributionUrl=http://services.gradle.org/distributions/gradle-2.2.1-all.zip

And changing "runProguard false" to "minifyEnabled true" in build.gradle.

How to Launch Android Gradle App using command line on emulator and physical device. Not just build

Build Using Command-Line ->

For building our app using the command line you can use -

gradlew assembleDebug (For debug app)

For install apk on the connected device -

gradlew installDebug

For more info, you can check -

https://developer.android.com/studio/build/building-cmdline

How to create a minimal Android gradle project?

You can try out this:

Use Gradle 6.1.1 and Android SDK 29.

settings.gradle

empty

build.gradle (example from the official docs):

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.application'
android { compileSdkVersion 29 }
repositories { jcenter() }

src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
</manifest>

Plenty of android boilerplates are also available on Github

Android project and Gradle: assemble a single module

gradlew.bat assembleDebug -a -b path/to/module/build.gradle

-a only builds the component and does not rebuild its dependencies

Use -b to specify another Gradle build file. In this case, the module's instead of the top-level build.gradle.

If you weren't using the Gradle wrapper, you could alternatively just cd to the module directory and run gradle assembleDebug -a there.

How to run gradle from the command line for Android Studio when versions mismatch?

When you run gradle from the command line, it invokes gradle from your system path, which apparently is gradle 2.10

When you add a wrapper section to your gradle.build file, as is the case with Android Studio projects, you will see a gradlew.bat and a gradlew shell script in the project root folder. These scripts will invoke (and download if required) the version of gradle defined in your wrapper section.

TL;DR: Run ./gradlew instead of gradle

How to build android project on MacOS from terminal?

Your problem is incorrectly cloned and converted project - see here:
env: bash\r: No such file or directory



Related Topics



Leave a reply



Submit