Android Things:撸起袖子来创建第一个Things工程

http://blog.csdn.net/p106786860/article/details/60161020

——————————————————————————————————————————————————————————————————————————————————————

在前面的章节,我们利用官方的sample-simplepio项目的blink模块,给大家演示了如何导入和运行现成的Android Things工程。那么这个章节,我们来手把手的给大家演示,如何从零开始创建、开发和运行一个Android Things项目。
1.前期准备
在你开始创建你的Android Things项目之前,你必须完成以下事情:
  更新你的SDK工具为24或者更高版本,更新的SDK工具可以让你能构建和测试Things应用。
  更新你的SDK为Android 7.0(API 24)或者更高,更新的平台版本为Things应用提供了新的API。
Android Studio->Tools->Android->SDK Manager,如下图:


2.创建项目
前面我们已经看到了Android Things项目的开发环境、开发工具、项目结构和Android项目都是一致的,其实它就是一个Android项目。那么我们新创建一个Andoird项目ThingsDemo。

注意:创建和更新你的应用项目,为了访问Things新的API,你必须创建一个项目或者修改一个已存在的项目,它的目标为Android7.0(API 24)或者更高;
4.添加库
Android Things设备通过support library公开API,它并不是Android SDK的一部分。在你的app中声明Things Support Library依赖。
在你的应用级别的build.gradle文件中添加依赖映射:
ThingsDemoappuild.gradle

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. apply plugin: 'com.android.application'  
  2. android {  
  3.     ... ...   
  4. }  
  5. dependencies {  
  6.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  7.     ... ...   
  8.     provided 'com.google.android.things:androidthings:0.1-devpreview'  
  9. }  

在你的清单文件中添加things共享库条目:
ThingsDemoappsrcmainAndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.chengxiang.thingsdemo">  
  4.     <application  
  5.         android:allowBackup="true"  
  6.         android:icon="@mipmap/ic_launcher"  
  7.         android:label="@string/app_name"  
  8.         android:supportsRtl="true"  
  9.         android:theme="@style/AppTheme">  
  10.         <uses-library android:name="com.google.android.things"/>  
  11.         ... ...  
  12.     </application>  
  13. </manifest>  

5.声明主Activity
一个想运行到嵌入式设备的应用,必须在清单文件中声明一个Activity,作为系统启动后的主入口。应用包含下面属性的Intent Filger;
  Action:ACTION_MAIN
  Category:CATEGORY_DEFAUULT
  Category:IOT_LAUNCHER
ThingsDemoappsrcmainAndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.chengxiang.thingsdemo">  
  4.     <uses-library android:name="com.google.android.things"/>  
  5.   
  6.     <application  
  7.         ... ...  
  8.         android:theme="@style/AppTheme">  
  9.         <activity android:name=".MainActivity">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN"/>  
  12.                 <category android:name="android.intent.category.LAUNCHER"/>  
  13.             </intent-filter>  
  14.   
  15.             <!-- Launch activity automatically on boot -->  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN"/>  
  18.                 <category android:name="android.intent.category.IOT_LAUNCHER"/>  
  19.                 <category android:name="android.intent.category.DEFAULT"/>  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23. </manifest>  

6.编译运行
这里我们尝试使用gradle构建任务的方式编译和运行项目,运行如下:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. C:Userschengxiang.peng.QUNARSERVERSAndroidThingsSourcesThingsDemo>gradle app:installDebug  
  2. Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reused, use --status for details  
  3. Incremental java compilation is an incubating feature.  
  4. The TaskInputs.source(Object) method has been deprecated and is scheduled to be removed in Gradle 4.0. Please use TaskInputs.file(Object).skipWhenEmpty() instead.  
  5. :app:preBuild UP-TO-DATE  
  6. ... ...   
  7. :app:installDebug  
  8. Unable to get density for device iot_rpi3 - 7.0  
  9. Installing APK 'app-debug.apk' on 'iot_rpi3 - 7.0' for app:debug  
  10. Installed on 1 device.  
  11. BUILD SUCCESSFUL  
  12. Total time: 1 mins 31.038 secs  
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. C:Userschengxiang.peng.QUNARSERVERSAndroidThingsSourcesThingsDemo>adb shell am start com.chengxiang.thingsdemo/.MainActivity  
  2. Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.chengxiang.thingsdemo/.MainActivity }  



提示1:运行adb shell am start com.chengxiang.thingsdemo/.MainActivity命令的时候,报错error:unknown host service
分析1:因为运行adb命名需要使用5037端口,应该是某个程序占用的该端口,故报错。
处理1:找到占用端口的程序,并杀死。

1.新技术,新未来!欢迎大家关注“1024工场”微信服务号,时刻关注我们的最新的技术讯息。2.抛弃各种找元器件的烦恼,来“1024工场”微店,一次性买到你所想要的。3.加入“Android Things开发”QQ讨论群,一起学习一起Hi。(甭客气!尽情的扫描或者长按!)

            
原文地址:https://www.cnblogs.com/cuizhf/p/6681377.html