android开发环境搭建linux.docx
《android开发环境搭建linux.docx》由会员分享,可在线阅读,更多相关《android开发环境搭建linux.docx(18页珍藏版)》请在冰豆网上搜索。
![android开发环境搭建linux.docx](https://file1.bdocx.com/fileroot1/2022-12/30/fd14e9b6-9c26-48bc-a94d-f1aa434e111b/fd14e9b6-9c26-48bc-a94d-f1aa434e111b1.gif)
android开发环境搭建linux
一、linux下android开发环境搭建
1.jdk的安装
1下载安装包jdk-6u43-linux-x64.bin(64位linux操作系统)
安装方法:
进入软件包存放路径我的是放在/home/software下
命令:
cd/home/software
./jdk-6u43-linux-x64.bin
相当于解压
把解压出的文件夹jdk1.6.0_43拷贝到/usr/java中
命令:
cd/usr
mkdirjava
cp-r/home/software/jdk1.6.0_43/usr/java
2配置环境变量
命令:
vi/etc/profile
在该文件后追加如下内容:
JAVA_HOME=/usr/java/jdk1.6.0_43
PATH=$PATH:
/usr/java/jdk1.6.0_43/bin
CLASSPATH=.:
/usr/javajdk1.6.0_43/jre/lib/rt.jar
exportJAVA_HOMEPATHCLASSPATH
2.eclipse的安装
1下载安装包eclipse-SDK-4.2.1-linux-gtk-x86_64.tar.gz(linux64位操作系统)
安装方法:
进入软件包存放路径我的是放在/home/software下
命令:
cd/home/software
tar–xzvfeclipse-SDK-4.2.1-linux-gtk-x86_64.tar.gz
解压出eclipse文件夹
cp–r/home/software/eclipse/usr/local
cd/usr/local/eclipse
./eclipse
运行eclipse,到此eclipse安装成功。
如果想在任何目录下输入eclipse命令启动eclipse需要如下配置:
命令:
cdroot(修改root用户)
ls–al|more
显示内容如下:
修改.bash_profile文件
命令:
vi.bash_profile
在PATH后追加:
/usr/local/eclipse如图:
可以在任何的目录下启动eclipse了。
3.android-sdk-linux的安装
1下载安装包android-sdk_r15-linux.tgz
安装方法:
进入软件包存放路径我的是放在/home/software下
命令:
cd/home/software
tar–xzvfandroid-sdk_r15-linux.tgz
解压出android-sdk-linux文件夹
命令:
cp-randroid-sdk-linux/usr/local
cd/usr/local/android-sdk-linux/tools
./android
显示如下:
勾选然后安装(linux下下载速度还是很快的),下载完成,安装完成。
4.在eclipse中在安装android开发插件
命令:
eclipse(或进入eclipse安装目录启动eclipse)
启动eclipse
选择installNewSoftware…,弹出如下图所示窗口,点击add按钮,添加name和location
分别为:
name:
Android(可自定义),location:
https:
//dl-
然后点击next,finish等待。
。
。
下载、安装完成。
5.添加sdk
6.点击图中的按钮,新建一个虚拟机,
选择DeviceDefinitions。
选择一个机型,点击createAVD
选择好后点击ok。
7.新建一个AndroidApplicationProject,点击Next,输入工程名,选择api,一路next,直到finish,运行程序输出helloworld!
8.ubuntu中配置真机测试环境
第一步:
插上你的手机打开终端,输入lsusb,查看你的手机的情况。
譬如我的是BUS002Device004:
ID19d2:
1382ZTETechnologiesMSM
第二步:
打开/etc/udev/rules.d在这个文件夹下新建文件,命名为:
XX-android.rules其中XX的值要大于当前值的最大值。
第三步:
打开新建的文件,写入以下内容:
SUBSYSTEM="usb",ATTRS{idVendor}=="19D2",ATTRS{idProduct}="1382",MODE="0666"
具体的值根据你的实际情况填写,然后保存退出.
二、初识android
1.从最简单的程序入手:
helloworld!
从程序的执行顺序分析。
1.1在工程下面有一个AndroidManifest.xml文件,该文件是android工程的全局配置文
件,所有的组件只有声明在这个文件中才会生效。
打开该文件
xmlversion="1.0"encoding="utf-8"?
>
--1.theconfigurationfilewasthelistfileofanandroidprogram.Theglobalconfigurationfile
2.allunitsmustdeclareinthisfileandcanplayaroleintheprogram.
3.thefilealsodeclaresomepermissioninformationandotherconfigurationinformation.
-->
--xmlns:
android:
meanthenamespace-->
--package:
meanthenameofpackage-->
--android:
versionCode:
theversioninfo-->
--android:
versionName:
theversioninfo-->
android="
package="com.example.androidtest"
android:
versionCode="1"
android:
versionName="1.0">
--theminimumversionofusingsdk-->
android:
minSdkVersion="17"
android:
targetSdkVersion="17"/>
--1.'application'meansthewholeapplicationprogram
2.'allowBackup'means
3.'icon'means
4.'label'meansquotingstringconstant
5.'theme'means
-->
android:
allowBackup="true"
android:
icon="@drawable/ic_launcher"
android:
label="@string/app_name"
android:
theme="@style/AppTheme">
--'activity'inordertodeclaretheActivityunit-->
android:
name="com.example.androidtest.MainActivity"
android:
label="@string/app_name">
--'intent-filter'meansthevisitabilityofActivity-->
--1.'action'meansthecurrentActivityistheaccessoftheprogram
2.'category'meansrunningthecurrentActivitywhenlaunchingtheprogram
-->
name="android.intent.action.MAIN"/>
name="android.intent.category.LAUNCHER"/>
详细内容看注释,值得注意的是标签中的该过滤器声明了程序的入口。
name="android.intent.action.MAIN"/>这句话表明当前的Activity(android:
name="com.example.androidtest.MainActivity"
这里指明了Activity文件的位置)就是程序的入口。
name="android.intent.category.LAUNCHER"/>这句的意思是在加载程序时加载。
1.2Activity类:
android组件中的最基本也是最常用的一种组件,在一个Android应用程序中,一个Activity通常就是一个单独的屏幕。
每一个Activity都被实现为一个独立的类,并且继承与Activity这个基类。
由上可知com.example.androidtest.MainActivity为程序的入口,打开该文件。
packagecom.example.androidtest;
importandroid.os.Bundle;
importandroid.app.Activity;//oneActivityisusualasinglescreen
importandroid.view.Menu;
//extendstheActivityclass
publicclassMainActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){//overridetheparentclass'smethodofonCreate
super.onCreate(savedInstanceState);//calltheparent'smethodonCreate()
/**
*1.callthesetContentView()inordertoshowview.
*2.R.layout.activity_mainisapropertyintheR.javafile.
*
*/
setContentView(R.layout.activity_main);
getWindow().setBackgroundDrawableResource(R.color.red_bg);//quotethecolorsourceandsetbackgroundred
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
}
继承自Activity,重写了父类的方法onCreate(),setContentView(R.layout.activity_main);
这句代码的作用是设置界面的布局。
其中R.layout.activity_main为布局的具体样式,而该文件又是从何而来呢?
如上图中的R.java则是出处。
R.java文件是系统自动生成的,无需手动修改。
打开文件
代码中,有红色底线的部分就是MainActivity类中引用的R.layout.activity_main。
但是这里只能看出一个地址,无法得知布局的具体样式。
这个R.java又是根据什么生成的呢?
在工程下有个res文件夹,该文件夹下存放各种资源文件,包括布局文件,如上图红色底线的文件activity_main.xml就是R.java中自动生成layout中的activity_main的原因。
只要在/res/layout下新建布局文件,就会自动在R.java中生成对应的映射地址,通过R.layout.*调用即可(*代表文件名)。
其他资源文件调用类似。
1.3打开布局文件activity_main.xml。
android="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
android:
paddingBottom="@dimen/activity_vertical_margin"
android:
paddingLeft="@dimen/activity_horizontal_margin"
android:
paddingRight="@dimen/activity_horizontal_margin"
android:
paddingTop="@dimen/activity_vertical_margin"
tools:
context=".MainActivity">
--thelayoutdocumentofdeclaringpage'selements-->
--1.layout_width:
thewidthrelayoncontent'width.
2.layout_height:
theheightrelayoncontent'height.
3.text:
meanquotingstringconstant.
4.textColor:
thefontcolor.
-->
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="@string/myHelloWorld"
android:
textColor="@color/blue_text"/>
在标签中指定了布局的具体样式宽、高、字符串、字体的颜色(不只是这几种)。
其中android:
text="@string/myHelloWorld"表示要输出的内容,这又涉及到另一种资源“字符串资源”。
在/res/values/中有名为srings.xml的文件,该文件中声明了具体输出的字符串。
打开strings.xml文件:
xmlversion="1.0"encoding="utf-8"?
>
--configurationfileofstringconstant-->
androidTest
Settings
myHelloWorld!
文件中的此处内容自定义标签指明了输出的字符,也就是显示在屏幕上的myHelloWorld!
。
helloworld程序分析完毕。
2.android中的资源
2.1颜色(color)资源
文件内容:
xmlversion="1.0"encoding="utf-8"?
>
#f00
#0000ff
2.2字符串(string)资源
文件内容:
xmlversion="1.0"encoding="utf-8"?
>
--configurationfileofstringconstant-->
androidTest
Settings
Helloworld!
Iamcoming!
myHelloWorld!
quotedinxmlfile!
quotedinjavacode!
test_dimen
test_dimen2
2.3尺寸(dimen)资源
文件内容:
xmlversion="1.0"encoding="utf-8"?
>
--Defaultscreenmargins,pertheAndroidDesignguidelines.-->
--1.thedpwasdimensionunitlikepxbutithadnoconcernwithdensity-->
16dp
16dp
150px
100px
30mm
10mm
2.4原始xml文件资源,与其他资源路径不同
文件内容:
xmlversion="1.0"encoding="utf-8"?
>
--1.thefolderof'/res/xml'usedtosaveoriginalxmlfile.
2.thexmltagisself-defining.
-->
文件中的
这里的标签自定义
2.5drawables图片资源
drawable文件夹分几种,
四种密度分类:
ldpi(low),mdpi(medium),hdpi(high),andxhdpi(extrahigh)
位图资源的名称规范,不能以数字开头,[a-z0-9_]。
2.6layout布局资源
test_layout.xml:
xmlversion="1.0"encoding="utf-8"?
>
--1.LinearLayout:
theunitlayoutpattern
2.xmlns:
android:
namespace
3.orientation:
theunitlayoutdirection-->
android="
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
android:
orientation="vertical">
--1.TableLayout:
theunitlayoutpattern.BenestedwithinLinearLayout.
2.stretchColumns:
-->
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
android:
stretchColumns="1">
android:
text="layouttest:
"
android:
id="@+id/layoutTextView01"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
textColor="@color/blue_text"/>
android:
text=""
andr