从gradle运行HelloAndroid?

时间:2017-11-06 01:58:40

标签: 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$ 

2 个答案:

答案 0 :(得分:1)

gradle run不用于Android项目,而是尝试汇编[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并且正在运行仿真器或连接的设备,则输出应为" Success"。如果您已经安装了相同的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中更改它,你也需要包名。

答案 1 :(得分:0)

你可以看到

gradle installDebug

将应用程序安装到设备上。您仍然需要按照@moxi的说明运行它:$ 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如果您进行了一些测试,也可以使用

gradle connectedAndroidTest

启动应用程序以运行测试。