3

没有IDE,该如何创建Android项目并运行

 3 years ago
source link: http://www.androidchina.net/11314.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

回到起初,我们在编写Java Hello World的时候,可以不借助任何IDE完成编译并且运行,但是到了学习Android的时候,却很少看到有文章不使用Android Studio创建Hello World项目的,如今依然记得通过Eclipse创建Android项目的时候,这个坑爹玩意儿差点没劝退,所以有了本篇文章,在下面的操作中,我们不会借助任何IDE,纯手创建各种文件并且运行到真机上。这将会加深对Android构建系统的了解。

下载Gradle

首先要做的事是下载Gradle,Android项目的主要复杂性就是构建系统,学习好Gradle对后续学习Android有很大的帮助,可以到https://gradle.org/releases/ 进行下载,但如果你已经使用Android Studio创建并运行过Android项目,就不需要下载了,可以到/home/hxl/.gradle/wrapper/dists/路径下找到,这个路径是对于Linux下,Windows下同样在用户文件夹,只不过.gradle文件是隐藏的,需要设置一下。

ia_100000005

然后把下面路径加入到系统环境变量,Linux下可以加入到/etc/profile中。

ia_100000006

初始化项目

这里我使用的是6.6.1版本,进入到一个空文件夹后,命令行执行gradle init,后续需要连续输入几个选项,如项目类型、构建脚本、项目名。

hxl@hxl-PC:/home/HouXinLin/projects/android/AndroidNoIde$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 1

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Project name (default: AndroidNoIde): Demo

> Task :init
Get more help with your project: https://guides.gradle.org/creating-new-gradle-builds

BUILD SUCCESSFUL in 1m 42s
2 actionable tasks: 2 executed
hxl@hxl-PC:/home/HouXinLin/projects/android/AndroidNoIde$ 

成功后会生成下面几个文件,怎么样?是不是很熟悉。

ia_100000007
  1. 首先打开build.gradle,将以下内容放进去。

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}
  1. 创建app文件夹,在将以下内容放入到app/build.gradle文件中。
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    defaultConfig {
        applicationId "com.test"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'com.android.support:appcompat-v7:25.3.1'
}
  1. 更改settings.gradle,将以下内容放入进去。
include ':app'

4.依次创建app/src/main文件夹,并在main中创建AndroidManifest.xml文件,其内容如下。

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

    <application
        android:label="Demo App"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

由于第4步引入了一个样式,还需要在app/src/main/res/values下创建一个style.xml文件,其内容如下。

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    </style>

</resources>
  1. 编写Java代码

依次创建app/src/main/java/com/test文件夹,在test文件夹下创建MainActivity.java,其内容如下。

package com.test;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }
}
  1. 最后一步,创建布局文件

由于第6步中引入了一个布局文件,还需要在app/src/main/res/layout下创建activity_main.xml文件,其内容如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="Hello World"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TextView>
</RelativeLayout>
  1. 配置SDK

最后一步需要配置SDK,如果没下载过,后面也无法继续,下载后在根文件夹创建local.properties文件,其内容如下,sdk.dir要指向sdk目录。

sdk.dir=/home/HouXinLin/apps/androidstudio/android-sdk

Android应用都有两个构建类型,一个是调试类型,用于调试我们的应用,另一个是发布类型,用于向用户发布我们的应用,但是必须先签名,然后才能将应用安装到手机(调试类型会使用SDK工具提供的调试密钥自动签名)。

如果我们需要调试应用,可以在根目录执行 gradlew assembleDebug 命令,成功后在app/build/outputs/apk/debug路径下会生成一个apk,可以通过adb install安装,也可以使用./gradlew installDebug来安装。

hxl@hxl-PC:/home/HouXinLin/projects/android/AndroidNoIde$ ./gradlew assembleDebug

> Task :app:lint
Ran lint on variant debug: 7 issues found
Ran lint on variant release: 7 issues found
Wrote HTML report to file:///home/HouXinLin/projects/android/AndroidNoIde/app/build/reports/lint-results.html
Wrote XML report to file:///home/HouXinLin/projects/android/AndroidNoIde/app/build/reports/lint-results.xml

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 18s
54 actionable tasks: 54 executed
hxl@hxl-PC:/home/HouXinLin/projects/android/AndroidNoIde$ 

在输入./gradlew installDebug命令后,手机中就会出现一个Demo App的应用

hxl@hxl-PC:/home/HouXinLin/projects/android/AndroidNoIde$ ./gradlew installDebug

> Task :app:installDebug
Installing APK 'app-debug.apk' on 'MI 8 - 10' for app:debug
Installed on 1 device.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
27 actionable tasks: 1 executed, 26 up-to-date
hxl@hxl-PC:/home/HouXinLin/projects/android/AndroidNoIde$ 
ia_100000008

嘿嘿,我们刚刚制作了一个Android应用程序,虽然他仅带了一个文本,但我们并没有使用IDE。

安装SDK

最后还是要说一下SDK的下载,如果没有使用过Android Studio,SDK是不存在的,需要下载,否则后面无法编译,在上面并没有明确说明如何下载,所以下面会做演示。

首先到https://developer.android.google.cn/studio ,下载命令行工具,我以Linux为例。

ia_100000009

下载后在bin目录下有个sdkmanager,这是关键,既然是命令行工具,那就不提供UI界面,记得以前是有个UI管理器。

ia_100000010

然后执行下面命令,后面的参数代表sdk下载的路径,下载完成后修改local.properties文件即可,但是由于网络原因,可能下载速度非常慢。

./sdkmanager  "platform-tools" "platforms;android-25" "build-tools;27.0.3" --sdk_root=/home/HXinLin/apps/android/sdk
复制代码

下面是下载完成后的目录结构。

ia_100000011

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK