android - 从 gradle 运行 HelloAndroid?

标签 android android-studio gradle ide kotlin

gradle 任务的数量和种类非常多:

thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ gradle clean run

FAILURE: Build failed with an exception.

* What went wrong:
Task 'run' not found in root project 'HelloAndroid'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ gradle tasks --all

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Android tasks
-------------
app:androidDependencies - Displays the Android dependencies of the project.
app:signingReport - Displays the signing info for each variant.
app:sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
app:assemble - Assembles all variants of all applications and secondary packages.
app:assembleAndroidTest - Assembles all the Test applications.
app:assembleDebug - Assembles all Debug builds.
app:assembleRelease - Assembles all Release builds.
app:build - Assembles and tests this project.
app:buildDependents - Assembles and tests this project and all projects that depend on it.
app:buildNeeded - Assembles and tests this project and all projects it depends on.
app:clean - Deletes the build directory.
app:cleanBuildCache - Deletes the build cache directory.
app:compileDebugAndroidTestSources
app:compileDebugSources
app:compileDebugUnitTestSources
app:compileReleaseSources
app:compileReleaseUnitTestSources
app:mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'HelloAndroid'.
app:buildEnvironment - Displays all buildscript dependencies declared in project ':app'.
components - Displays the components produced by root project 'HelloAndroid'. [incubating]
app:components - Displays the components produced by project ':app'. [incubating]
dependencies - Displays all dependencies declared in root project 'HelloAndroid'.
app:dependencies - Displays all dependencies declared in project ':app'.
dependencyInsight - Displays the insight into a specific dependency in root project 'HelloAndroid'.
app:dependencyInsight - Displays the insight into a specific dependency in project ':app'.
dependentComponents - Displays the dependent components of components in root project 'HelloAndroid'. [incubating]
app:dependentComponents - Displays the dependent components of components in project ':app'. [incubating]
help - Displays a help message.
app:help - Displays a help message.
model - Displays the configuration model of root project 'HelloAndroid'. [incubating]
app:model - Displays the configuration model of project ':app'. [incubating]
projects - Displays the sub-projects of root project 'HelloAndroid'.
app:projects - Displays the sub-projects of project ':app'.
properties - Displays the properties of root project 'HelloAndroid'.
app:properties - Displays the properties of project ':app'.
tasks - Displays the tasks runnable from root project 'HelloAndroid' (some of the displayed tasks may belong to subprojects).
app:tasks - Displays the tasks runnable from project ':app'.

Install tasks
-------------
app:installDebug - Installs the Debug build.
app:installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
app:uninstallAll - Uninstall all applications.
app:uninstallDebug - Uninstalls the Debug build.
app:uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
app:uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
app:check - Runs all checks.
app:connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
app:connectedCheck - Runs all device checks on currently connected devices.
app:connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
app:deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
app:deviceCheck - Runs all device checks using Device Providers and Test Servers.
app:lint - Runs lint on all variants.
app:lintDebug - Runs lint on the Debug build.
app:lintRelease - Runs lint on the Release build.
app:lintVitalRelease - Runs lint on just the fatal issues in the release build.
app:test - Run unit tests for all variants.
app:testDebugUnitTest - Run unit tests for the debug build.
app:testReleaseUnitTest - Run unit tests for the release build.

Other tasks
-----------
app:assembleDebugAndroidTest
app:assembleDebugUnitTest
app:assembleReleaseUnitTest
app:bundleAppClassesDebug
app:bundleAppClassesDebugAndroidTest
app:bundleAppClassesDebugUnitTest
app:bundleAppClassesRelease
app:bundleAppClassesReleaseUnitTest
app:checkDebugManifest
app:checkReleaseManifest
clean
app:compileDebugAidl
app:compileDebugAndroidTestAidl
app:compileDebugAndroidTestJavaWithJavac
app:compileDebugAndroidTestKotlin - Compiles the debugAndroidTest kotlin.
app:compileDebugAndroidTestNdk
app:compileDebugAndroidTestRenderscript
app:compileDebugAndroidTestShaders
app:compileDebugJavaWithJavac
app:compileDebugKotlin - Compiles the debug kotlin.
app:compileDebugNdk
app:compileDebugRenderscript
app:compileDebugShaders
app:compileDebugUnitTestJavaWithJavac
app:compileDebugUnitTestKotlin - Compiles the debugUnitTest kotlin.
app:compileLint
app:compileReleaseAidl
app:compileReleaseJavaWithJavac
app:compileReleaseKotlin - Compiles the release kotlin.
app:compileReleaseNdk
app:compileReleaseRenderscript
app:compileReleaseShaders
app:compileReleaseUnitTestJavaWithJavac
app:compileReleaseUnitTestKotlin - Compiles the releaseUnitTest kotlin.
app:consumeConfigAttr
app:createDebugCompatibleScreenManifests
app:createReleaseCompatibleScreenManifests
app:extractProguardFiles
app:generateDebugAndroidTestAssets
app:generateDebugAndroidTestBuildConfig
app:generateDebugAndroidTestResources
app:generateDebugAndroidTestResValues
app:generateDebugAndroidTestSources
app:generateDebugAssets
app:generateDebugBuildConfig
app:generateDebugResources
app:generateDebugResValues
app:generateDebugSources
app:generateReleaseAssets
app:generateReleaseBuildConfig
app:generateReleaseResources
app:generateReleaseResValues
app:generateReleaseSources
app:javaPreCompileDebug
app:javaPreCompileDebugAndroidTest
app:javaPreCompileDebugUnitTest
app:javaPreCompileRelease
app:javaPreCompileReleaseUnitTest
app:mergeDebugAndroidTestAssets
app:mergeDebugAndroidTestJniLibFolders
app:mergeDebugAndroidTestResources
app:mergeDebugAndroidTestShaders
app:mergeDebugAssets
app:mergeDebugJniLibFolders
app:mergeDebugResources
app:mergeDebugShaders
app:mergeReleaseAssets
app:mergeReleaseJniLibFolders
app:mergeReleaseResources
app:mergeReleaseShaders
app:packageDebug
app:packageDebugAndroidTest
app:packageRelease
app:platformAttrExtractor
app:preBuild
app:preDebugAndroidTestBuild
app:preDebugBuild
app:preDebugUnitTestBuild
app:prepareLintJar
app:preReleaseBuild
app:preReleaseUnitTestBuild
app:processDebugAndroidTestJavaRes
app:processDebugAndroidTestManifest
app:processDebugAndroidTestResources
app:processDebugJavaRes
app:processDebugManifest
app:processDebugResources
app:processDebugUnitTestJavaRes
app:processReleaseJavaRes
app:processReleaseManifest
app:processReleaseResources
app:processReleaseUnitTestJavaRes
app:resolveConfigAttr
app:splitsDiscoveryTaskDebug
app:splitsDiscoveryTaskDebugAndroidTest
app:splitsDiscoveryTaskRelease
app:transformClassesWithDexBuilderForDebug
app:transformClassesWithDexBuilderForDebugAndroidTest
app:transformClassesWithPreDexForRelease
app:transformDexArchiveWithDexMergerForDebug
app:transformDexArchiveWithDexMergerForDebugAndroidTest
app:transformDexArchiveWithExternalLibsDexMergerForDebug
app:transformDexArchiveWithExternalLibsDexMergerForDebugAndroidTest
app:transformDexWithDexForRelease
app:transformNativeLibsWithMergeJniLibsForDebug
app:transformNativeLibsWithMergeJniLibsForDebugAndroidTest
app:transformNativeLibsWithMergeJniLibsForRelease
app:transformResourcesWithMergeJavaResForDebug
app:transformResourcesWithMergeJavaResForDebugAndroidTest
app:transformResourcesWithMergeJavaResForDebugUnitTest
app:transformResourcesWithMergeJavaResForRelease
app:transformResourcesWithMergeJavaResForReleaseUnitTest
app:validateSigningDebug
app:validateSigningDebugAndroidTest
app:writeDebugApplicationId
app:writeReleaseApplicationId


BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ gradle clean

BUILD SUCCESSFUL in 0s
2 actionable tasks: 1 executed, 1 up-to-date
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ tree
.
├── app
│   ├── build.gradle
│   ├── libs
│   ├── proguard-rules.pro
│   └── src
│       ├── androidTest
│       │   └── java
│       │       └── android
│       │           └── kotlin
│       │               └── dur
│       │                   └── bounceme
│       │                       └── net
│       │                           └── helloandroid
│       │                               └── ExampleInstrumentedTest.kt
│       ├── main
│       │   ├── AndroidManifest.xml
│       │   ├── java
│       │   │   └── android
│       │   │       └── kotlin
│       │   │           └── dur
│       │   │               └── bounceme
│       │   │                   └── net
│       │   │                       └── helloandroid
│       │   │                           └── MainActivity.kt
│       │   └── res
│       │       ├── drawable
│       │       │   ├── ic_dashboard_black_24dp.xml
│       │       │   ├── ic_home_black_24dp.xml
│       │       │   ├── ic_launcher_background.xml
│       │       │   └── ic_notifications_black_24dp.xml
│       │       ├── drawable-v24
│       │       │   └── ic_launcher_foreground.xml
│       │       ├── layout
│       │       │   └── activity_main.xml
│       │       ├── menu
│       │       │   └── navigation.xml
│       │       ├── mipmap-anydpi-v26
│       │       │   ├── ic_launcher_round.xml
│       │       │   └── ic_launcher.xml
│       │       ├── mipmap-hdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-mdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-xhdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-xxhdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-xxxhdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       └── values
│       │           ├── colors.xml
│       │           ├── dimens.xml
│       │           ├── strings.xml
│       │           └── styles.xml
│       └── test
│           └── java
│               └── android
│                   └── kotlin
│                       └── dur
│                           └── bounceme
│                               └── net
│                                   └── helloandroid
│                                       └── ExampleUnitTest.kt
├── build
│   ├── android-profile
│   │   ├── profile-2017-11-05-17-50-01-533.json
│   │   └── profile-2017-11-05-17-50-01-533.rawproto
│   └── kotlin-build
│       └── version.txt
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── HelloAndroid.iml
├── local.properties
└── settings.gradle

44 directories, 41 files
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 

来自 Android Studio:

run

virtual device?

最简单的“运行”是我所追求的。类似于 runShadow 的东西?

生成的代码和构建文件:

thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ cat build.gradle 
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}
thufir@dur:~/AndroidStudioProjects/HelloAndroid$            
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ cat app/src/main/java/android/kotlin/dur/bounceme/net/helloandroid/MainActivity.kt 
package android.kotlin.dur.bounceme.net.helloandroid

import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                message.setText(R.string.title_home)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                message.setText(R.string.title_dashboard)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                message.setText(R.string.title_notifications)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }
}
thufir@dur:~/AndroidStudioProjects/HelloAndroid$ 

最佳答案

gradle run 未用于 Android 项目,请尝试使用 assemble[Flavor] 任务之一。要将 apk“安装并运行”到模拟器或 Android 设备中,需要 3 个步骤:

1) 创建 apk。 例如,首先你必须运行:

$ ./gradlew clean assembleDebug

这将生成一个 apk 文件,我假设生成的 apk 名为 app-debug.apk。

2) 在您的设备或模拟器中安装生成的 apk:

$ adb install -r app/build/outputs/apk/debug/app-debug.apk 

如果您安装了 adb 并且正在运行模拟器或连接的设备,则输出应该是“成功”。如果您已经安装了相同的 apk,则使用 -r 选项,您可以了解有关其他 adb 安装选项的更多信息。

3) 在您的模拟器或设备中“运行”您的应用:

$ adb shell am start -n "android.kotlin.dur.bounceme.net.helloandroid/android.kotlin.dur.bounceme.net.helloandroid.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER

我假设您的 Launcher 是您的 MainActivity,您可以在 AndroidManifest.xml 中更改它,并且您还需要包名称。

关于android - 从 gradle 运行 HelloAndroid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128696/

相关文章:

java - 内存不足异常+分析hprof文件转储

java - 优化字符串 += 还是连接?

android - 多个设备/模拟器 react native react native 启动错误

android - 支持库版本更改时无法解析符号

java - 如何让媒体播放器自动切换到下一轨道?

java - Android Studio:运行我的应用程序时出现:“Could not create the Java Virtual Machine”错误

java - 导入模块库后 Gradle 错误

java - 无法解决依赖关系conciseclock

android - 如何使用改造从缓存图像发出 MultipartBbody.Part 请求

Android Studio Gradle 错误 : failed to resolve com. google.auto.service :auto-service:1. 0-rc2