如何从命令行使用 gradle 创建 android 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20801042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:29:19  来源:igfitidea点击:

How to create android project with gradle from command line?

androidgradlecommand-line

提问by Paul Verest

How to create android project with gradlefrom command line (without any IDE)?

如何使用gradle命令行(没有任何 IDE)创建 android 项目?

Before it was with androidutil like below

在使用androidutil之前,如下所示

android create project --target 1 --name MyAndroidApp --path ./MyAndroidAppProject --activity MyAndroidAppActivity --package com.example.myandroid

回答by Paul Verest

Similar to How to create Java gradle projectsuggest to create Android project with android create projectthan add build.gradletemplate 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)

类似于How to create Java gradle project建议创建 Android 项目,而android create project不是build.gradle为经典的 Android 项目gh.c/N/n-1/b/m/oneeg/docs/android/build.gradle添加模板。(这将允许在任何 IDE 中进行开发,因为旧结构被更广泛地采用)

Of course there will be some gradle initoptions or android create(from SDK) in the future.

当然将来会有一些gradle init选项或android create(来自SDK)。

UPDATE:

更新:

Android SDK 19 has androidCLI -goption that allows to use gradle template. You might also need to specify android gradle plugin version with CLI -voption, 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 SDK 19 具有允许使用 gradle 模板的androidCLI-g选项。您可能还需要使用 CLI-v选项指定 android gradle 插件版本,检查android gradle 插件兼容性表。创建使用 android gradle 插件 (v 0.10) 添加 gradle 支持的项目的示例命令。

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:

或者对于 buildTools 19.1+,通过以下方式使用更新版本的 Gradle Android 插件--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 -hfor help

检查android create project -h帮助

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.

然而,Android Studio 0.6.1 未能正确打开它(未显示源),因为它将第一个项目文件夹(即gradle)作为模块文件夹 -> 您需要导入,而不仅仅是打开。

In Eclipse it was with a trick of regarding srcfolder as root of the project.

在 Eclipse 中,它有一个将src文件夹视为项目根目录的技巧。

.classpathis

.classpath

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

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

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

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

阅读http://www.nodeclipse.org/projects/gradle/

回答by Anu Tig3r

Here is the solution PHP Android CLI

这是解决方案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.

并且您的项目使用最新的 android-studio 参数进行搭建,例如 compile & target SDK 为 29,buildToolsVersion 为 29.0.1 & minSdk 为 16。

If you want to change, like, want to set minSdk to 14:

如果要更改,例如,要将 minSdk 设置为 14:

phpandroid create PROJECT PACKAGE --minSdk=14

PHP Android CLI can also create Variants and modules (application/library):

PHP Android CLI 还可以创建变体和模块(应用程序/库):

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. Apphas 2 dimensions: type& backend4 variants: free& paidof typedimension; php& firebasevariant of backenddimension.

这将生成 2 个应用程序appadmin和一个库commonApp有2周尺寸为:typebackend4点的变体:freepaidtype尺寸; php&维度的firebase变体backend