Android Studio安装与App开发步骤详解

环境准备

使用的软硬件具体如下:

  • Windows 7
  • Android Studio安装与App开发步骤详解_第1张图片
  • Android Studio 3.3.2【简称:AS,从官网可以下载,或者使用低版本升级:help->check for update】
  • Android Studio安装与App开发步骤详解_第2张图片
  • JDK1.8.0_131
  • Android Studio安装与App开发步骤详解_第3张图片
  • HuaWei Pad/SamSung Pad

JDK/SDK/SDK_Manager更新与配置

在D盘分别建立【android_sdk】目录,用于存放sdk,建立【android_studio】目录,用于安装AS。

JDK安装在【C:\Program Files\Java\jdk1.8.0_131】目录

JDK、SDK都需要配置环境变量,其中:

在Path环境变量加入:D:\android_sdk\ndk-bundle;D:\android_sdk\platform-tools;

创建【java_home】环境变量,值为:C:\Program Files\Java\jdk1.8.0_131

对于首次运行AS的,会出现如下页面(由于我的首次运行时,没有截图,只好从其它网页拷贝一下,所以版本不是3.3.2):

点击下面的【Configure】按钮,进入AS的配置页面如下:

Android Studio安装与App开发步骤详解_第4张图片

 

Android Studio安装与App开发步骤详解_第5张图片

或者在已打开的AS主界面的工具栏或者【Tools】菜单栏,打开SDK Manager,根据开发的需要选择【SDK Platforms】、【SDK tools】,例如:需要NDK开发则需要在SDK Tools里勾选对应的NDK;如果需要支持C/C++的,则需要勾选CMake;如果真实设备在设备管理器里显示为无法识别(问号)时,需要勾选Google USB Driver

注意:更新SDK需要连接到外网,大家也可以参考【http://www.cnblogs.com/smyhvae/p/4390905.html】,作为补充。

对于有些安装无法识别,需要通过手动安装设备的驱动,具体如下:

Android Studio安装与App开发步骤详解_第6张图片

Android Studio安装与App开发步骤详解_第7张图片

Android Studio安装与App开发步骤详解_第8张图片

后面直接更新即可,如果还不行(例如我的经历是能够更新HuaWei设备,但是三星的后来通过360驱动进行更新的),正常更新后设备列表里有ADB设备,例如:

Android Studio安装与App开发步骤详解_第9张图片

注意,有时候调试时原来是发现到设备的,但是突然有时没有发现到设备了,出现Error:No target Device错误。此时可以在控制台输入如下指令:

Android Studio安装与App开发步骤详解_第10张图片

说明:上面两条命令的意思是重启下adb服务,其中adb是AS通过端口5037监听真实设备的服务,重启服务后在插拔下真实设备即可。

创建工程和简单调试

选择【File->New->Project】,弹出各种工程模板,根据具体开发需求进行选择;创建完成后,【Project】页面会自动生成1个App和1个Module,分别有build.gradle。App只能有一个,module可以有多个。

其中App【项目】的build.gradle具体内容如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.google.com' }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
        maven { url 'https://maven.google.com' }
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块的build.gradle,具体内容如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.example.dvcs_manage"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

说明:其中CMakeLists.txt是描述编译C/C++库的文件,具体如下:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

打包与发布

打包分为无签名和有签名方式,具体参考【https://www.jianshu.com/p/b28a5be05029】。注意打包中间会出现如下情况:

  • Error:error: failed to read PNG signature: file does not start with PNG signature。原因是应该图片格式不正确,打开编辑后另存为覆盖即可(参考:https://blog.csdn.net/niuzaiwenjie/article/details/81587475)。
  • Timeout To connect:原因是需要连接公网
  • Read timed out:在项目的build.gradle中 repositories和allprojects添加
    ...
    mavenCentral()
    maven { url 'https://maven.google.com' }
    ...
    并将jcenter()放到这两个的下面,具体参考App的build.gradle

注意:只有有签名方式下,才会有Release模式;无签名方式下,是没有Release模式的。

签名并打包成Release版本成功如下:

 

Android Studio安装与App开发步骤详解_第11张图片

总结

经过近一周的AS安装到调试App,先从2.2的版本安装,后来发现gradle构建工程出现很多错误;特别是NDK的问题,例如:

No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android

后来发现只有将AS更新到3.1以上(参考:https://blog.csdn.net/vocanicy/article/details/83004626),后来索性更新到最新版本。

其实NDK的问题也是很麻烦的,好在有C源代码;我显示创建一个C++library的Project,之后拷贝CMakeLists.txt文件到我的App,并仿照Demo工程进行改造,这是一个好的办法;特别是对付各种配置和库的版本不兼容的问题,是行之有效的

你可能感兴趣的:(开发问题)