简体   繁体   English

使用gradle构建Android应用程序时出现问题

[英]Problems when building an android app with gradle

I am trying to understand how to build Android Apps without using and IDE, so basically starting from scratch. 我试图了解如何在不使用和IDE的情况下构建Android应用程序,因此基本上从头开始。 I discovered that a good way to build the Android App is to use Gradle. 我发现构建Android应用程序的一个好方法是使用Gradle。

So from command line, in the root of my project, I type "gradle wrapper" so it creates al the necessary files to use gradle, then i type "gradle init" and it creates the build.gradle text file where i have to put all the setting to build the project (i suppose). 所以从命令行,在我的项目的根目录中,我键入“gradle wrapper”,因此它创建了使用gradle的必要文件,然后我输入“gradle init”,它创建了build.gradle文本文件,我必须放入构建项目的所有设置(我想)。 Here is what I put there searching on the internet. 这是我在互联网上搜索的内容。

// 1
buildscript {
    // 2
    repositories {
        google()
        jcenter()
    }
    // 3
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.51'
    }
}

// 4
allprojects {
    repositories {
        google()
        jcenter()
    }
}

So I write on the command line, "./gradle AssembleDebug" to perform the build but it gives me theese errors. 所以我在命令行上写了“./gradle AssembleDebug”来执行构建,但它给了我这些错误。

FAILURE: Build failed with an exception.

* Where:
Build file '/home/michelangelo/Documents/HelloAndroid/build.gradle' line: 5

* What went wrong:
A problem occurred evaluating root project 'HelloAndroid'.
> Could not find method google() for arguments [] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

I've never done things like this before so i don't know what to do. 我之前从未做过这样的事情所以我不知道该怎么做。

Try ./gradlew AssembleDebug . 试试./gradlew AssembleDebug The Gradle wrapper (gradlew) itself is supposed to pick up your properties and pass it onto the gradle tool to run. Gradle包装器(gradlew)本身应该拾取您的属性并将其传递给gradle工具以运行。

You should use the gradlew tool instead of using gradle directly. 您应该使用gradlew工具而不是直接使用gradle。

See Build your app from the command line , the official Google guideline, states; 请参阅官方Google指南中的命令行构建您的应用程序 ;

You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. 您可以使用Gradle包装器命令行工具执行Android项目可用的所有构建任务。 It's available as a batch file for Windows (gradlew.bat) and a shell script for Linux and Mac (gradlew.sh), and it's accessible from the root of each project you create with Android Studio. 它可以作为Windows的批处理文件(gradlew.bat)和Linux和Mac的shell脚本(gradlew.sh)使用,并且可以从使用Android Studio创建的每个项目的根目录访问它。

To run a task with the wrapper, use one of the following commands from a Terminal window (from Android Studio, select View > Tool Windows > Terminal): 要使用包装器运行任务,请使用“终端”窗口中的以下命令之一(从Android Studio,选择“视图”>“工具窗口”>“终端”):

On Windows: gradlew task-name 在Windows上: gradlew task-name

On Mac or Linux: ./gradlew task-name 在Mac或Linux上: ./gradlew task-name

You can read about Gradle Wrapper in Gradle's official documentation . 您可以在Gradle的官方文档中阅读Gradle Wrapper。 Also read this blog post; 另请阅读此博客文章; Understanding the Gradle Wrapper . 了解Gradle Wrapper

You can also see how I build an Android app from just the project source files, some gradle properties and dependency files, on CircleCI system via command line instructions in this yml file. 您还可以通过 yml文件中的命令行指令,在CircleCI系统上查看如何从项目源文件,一些gradle属性和依赖项文件构建Android应用程序。

Hope this helps you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM