现在的位置: 首页 > 移动开发 > 正文

2014新手学Android(1)- Android开发环境安装和支持库安装

2018年12月16日 移动开发 ⁄ 共 5162字 ⁄ 字号 评论关闭

    纯粹是个人兴趣,想学习一下Android的开发。哥之前可是有5年Windows .Net的开发经验,Asp.net和Windows Form玩得很熟。没想到,一接触Android就傻眼了,完全不一样啊,哥不理解啊。Android的发展非常快,如果是11、12、13年的Android技术文章,千万别看,否则会走很多弯路,说多了都是泪啊。

一、安装

    遇到第一个问题,就是怎么安装Android的开发环境。复杂点说就是需要 Java SDK + Eclipse(IDE) + ADT(Android developer Tools),作为新手,我不建议一个个去安装,因为太麻烦。强烈推荐Google Android的官网:https://developer.android.com/index.html,直接去官网下载集成了所有开发工具的安装包(就是打包好了,下一个就行)。下载地址为:https://developer.android.com/sdk/index.html

     这个安装包已经集成了如下功能:

With a single download, the Eclipse ADT bundle includes everything you need to begin developing apps:
Eclipse + ADT plugin //Eclipse和ADT插件
Android SDK Tools //Android开发工具
Android Platform-tools //Android平台工具
A version of the Android platform //Android各个版本支持
A version of the Android system image for the emulator //Android系统图片和模拟器

    如果您是老手,也强烈建议 用这个安装包,为什么呢,因为总所周知的原因,不翻墙无法更新ADT中的东东。

   Java的话,老手都会了吧?但是我是业余选手,所以只是略懂。下载JDK(Java Developer Kit),别只下JRE(Java Runtime Enviroment,这只是执行Java程序,无法编译Java代码)。去www.oracle.com下载一个好了。可以官网:http://www.oracle.com/technetwork/java/javase/downloads/index.html,载Java
SE 1.7,这个页面:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

    另外一点,建议都下载x86的,别下64位的,即便是你是64为的机器,因为网上教程和大部分的工具对x86支持较好。

    最后总结一下:

    1、JDK 1.7 (文件名是这个:jdk-7u71-windows-i586.exe),安装好,看是否需要配置Java的环境,如果需要请百度之。

    2、adt-bundle-windows-x86-20140702.zip,解压即可使用。

二、Android低版本支持

    哥从网上下了一个Android的开源项目,打开之后发现有问题,问题描述如下:

error: Error retrieving parent for item: No resource found that matches the given name '@style/Theme.AppCompat'.

    于是哥满百度和Google去查找,最后的最后,还是哥自己解决了。先说说Android低版本支持的事吧。

    Android的版本发展速度非常快,最早的好像是1.6的版本,现在都4.4了。如果开发一款应用,想要支持低版本,那么需要加入低版本的支持。如下图,新建Android项目时,会有选择,Minimum Required SDK,就是指最小支持什么版本。

    google提供了Android Support Library package 系列的包来保证来高版本sdk开发的向下兼容性,即我们用4.x开发时,在1.6等版本上,可以使用高版本的有些特性,如fragement,ViewPager等,下面,简单说明下这几个版本间的区别:
    Android Support v4:  这个包是为了照顾1.6及更高版本而设计的,这个包是使用最广泛的,eclipse新建工程时,都默认带有了。
    Android Support v7:  这个包是为了考虑照顾2.1及以上版本而设计的,但不包含更低,故如果不考虑1.6,我们可以采用再加上这个包,另外注意,v7是要依赖v4这个包的,即,两个得同时被包含。
    Android Support v13  :这个包的设计是为了android 3.2及更高版本的,一般我们都不常用,平板开发中能用到。

    另外,设置好了,在AndroidManifest.xml中也能修改,android:minSdkVersion="8",指Android2.2以上了。

    简而言之,你只需要搞定V7一个支持包就足够了,因为V7依赖V4。当然,在开发过程中,如果真的需要支持2.1以下的版本,写法上也有一些区别。

    交代了来龙去脉,现在开始解决问题了。先贴一段官方的说明吧,其实没有那么复杂。

Create a library project based on the support library code:

  1. Make sure you have downloaded the Android Support Library using the SDK
    Manager
    .
  2. Create a library project and ensure the required JAR files are included in the project's build path:
    1. Select File > Import.
    2. Select Existing Android Code Into Workspace and click Next.
    3. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding theappcompat project, browse to <sdk>/extras/android/support/v7/appcompat/.
    4. Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
    5. In the new library project, expand the libs/ folder, right-click each .jar file
      and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar andandroid-support-v7-appcompat.jar files
      to the build path.
    6. Right-click the project and select Build Path > Configure Build Path.
    7. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this
      library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    8. Uncheck Android Dependencies.
    9. Click OK to complete the changes.

You now have a library project for your selected Support Library that you can use with one or more application projects.

Add the library to your application project:

  1. In the Project Explorer, right-click your project and select Properties.
  2. In the Library pane, click Add.
  3. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.
  4. In the properties window, click OK.

Once your project is set up with the support library, here's how to add the action bar:

  1. Create your activity by extending ActionBarActivity.
  2. Use (or extend) one of the Theme.AppCompat themes
    for your activity. For example:
    <span class="tag" style="color: rgb(0, 0, 136);"><activity</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(136, 34, 136);">android:theme</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"@style/Theme.AppCompat.Light"</span><span class="pln" style="color: rgb(0, 0, 0);"> ... </span><span class="tag" style="color: rgb(0, 0, 136);">></span>

Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.

On API level 11 or higher

The action bar is included in all activities that use the Theme.Holo theme
(or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute
is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.

大致上分为两步

1、添加v7的项目,File->New->Project->Android Project from Existing Code->选择adt-bundle-windows-x86-20140702.zip的目录(具体是这个目录:<sdk>/extras/android/support/v7/appcompat/),然后就把v7的项目建立好了,上面的英文说得很复杂,其实其他的什么都不用设置。

2、在你的自己的项目中,只需要设置一处:右键你的项目(如果你喜欢菜单,也可以左键选中你的项目,然后从菜单Project中打开),选择Propeties,然后选择Android这个项目,然后点击右下方的Add按钮,添加android-support-v7-appcompat即可,其他的一样不用设置。

    另外,在Eclipse->Windows->Android SDK Manager下面可以更新其他的包,根本连不上(网上大部分教程都说要更新这个,大坑),如果要用这个更新,建议翻墙。如果你是从官网下载的完全安装包,这个更新应该是没有必要做的。

抱歉!评论已关闭.