高效建立健壮的Android应用
第一次写(翻译)这么长的文章,请多包含. 本标题纯粹是别人的广告语,它说他可以
- 让你开发更加迅速
- 让你的应用更加强壮
- 提高应用的质量
- 构建你的企业解决方案
- 而且还是免费开源(我加的)
它包含以下组件:
-
日志
import de.akquinet.android.androlog.Log; import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initializes androlog // This will read /sdcard/my.application.properties Log.init(this); // Log a message Log.i(this, "This is a logged message"); setContentView(R.layout.main); } }
-
快速测试
public void testValidAuth() throws Exception { activity().view().withId(R.id.username).setText("testuser"); activity().view().withId(R.id.password).setText("testpw"); activity().view().withId(R.id.buttonSubmit).click(); NextActivity nextActivity = await().activity( NextActivity.class, 10, TimeUnit.SECONDS); assertThat(nextActivity, notNullValue()); assertThat(activity(nextActivity).view(). withId(R.id.nextActivityContent), hasText("success")); }
-
持续集成和测试你的应用
-
让你的应用可以直接上架(With ProGuard to AppStore)
-
获得应用的错误等日志反馈
-
构建企业应用程序
企业应用程序有不同的要求,与后端系统的有关部署,安全和通信。它为在Android上构建企业解决方案提供了几个组件:
交货门户网站:管理用户和交付/更新/管理您的应用程序在空中。
先进的后端连接:易于与您的后端使用REST,Web服务,JSON或protobuf的沟通。支持几种加密方法,以及使用云服务。 从现场设备的数据同步:保持与后端同步,支持离线模式,合并。
隐私权存储:提高你的设备上的数据通过加密和您的应用程序的私有数据的安全性。
使用STAND,你可以轻松地构建基于Android的企业级解决方案。
详细介绍
-
MAVEN PLUGIN
网址: http://code.google.com/p/maven-android-plugin
源代码: GitHub
-
MAVEN 骨架
使用方法:
mvn archetype:generate \ -DarchetypeArtifactId=android-quickstart \ -DarchetypeGroupId=de.akquinet.android.archetypes \ -DarchetypeVersion=1.0.11 \ -DgroupId=your.company \ -DartifactId=my-android-application
源代码:Github
-
ANDROLOG
Maven依赖
<dependency> <groupId>de.akquinet.android.androlog</groupId> <artifactId>androlog</artifactId> <version>1.0.1</version> <scope>compile</scope> </dependency>
使用方法1(跟Android log一样使用,无需配置):
import de.akquinet.android.androlog.Log; public class AndrologExample { public static final String TAG = "MY_TAG"; public void doSomething() { Log.i(TAG, "A message"); } }
使用方法2:
import de.akquinet.android.androlog.Log; import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initializes androlog // This will read the /sdcard/my.application.properties file Log.init(this); // Log a messgage Log.i(this, "This is a logged message"); setContentView(R.layout.main); } }
配置:
文件名是包名+.properties.是不是很简单?
格式如下:
androlog.active = true|false androlog.default.level=VERBOSE|DEBUG|INFO|WARN|ERROR 默认Info org.package.name = info //这个格式很眼熟吧? androlog.delegate.wtf = false //如果你的系统低于2.3则需要这样设置以免报错
发布前提条件,在~/.m2/settings.xml定义
<profiles> <profile> <id>android</id> <properties> <!-- CHANGE HERE --> <sdk.path>/Users/clement/dev/android</sdk.path> </properties> </profile> <!-- others profiles --> </profiles> <activeProfiles> <activeProfile>android</activeProfile> </activeProfiles>
源代码:Github
-
更好的单元测试工具 - MARVIN
Maven依赖:
<dependency> <groupId>de.akquinet.android.marvin</groupId> <artifactId>marvin</artifactId> <version>1.1.0</version> <<scope>compile</scope> </dependency>
原生的测试方法:
public class LoginActivityTest extends AndroidTestCase { public void testLogin() { Instrumentation instrumentation = getInstrumentation(); ActivityMonitor monitor = instrumentation .addMonitor(MainActivity.class.getName(), null, false); Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClassName(instrumentation.getTargetContext(), LoginActivity.class.getName()); final LoginActivity loginActivity= (LoginActivity) instrumentation.startActivitySync(intent); instrumentation.waitForIdleSync(); injectViewTestActivity.runOnUiThread(new Runnable() { public void run() { EditText username = loginActivity .findViewById(R.id.username); EditText password = loginActivity .findViewById(R.id.password); username.setText("Username"); // Step 1 password.setText("Password"); // Step 2 loginActivity.findViewById(R.id.loginbutton) .performClick(); // Step 3 } }); Activity mainActivity = instrumentation .waitForMonitorWithTimeout(monitor, 30000); // Step 4 assertNotNull(mainActivity); // Step 5 } }
更好的测试方法(译者:你认为呢?):
public class LoginActivityTest extends ActivityTestCase<> { public LoginActivityTest(Class activityType) { super(LoginActivity.class); } public void testLogin() { // Step 1 activity().view().withId(R.id.username).setText("Username"); // Step 2 activity().view().withId(R.id.password).setText("Password"); // Step 3 activity().view().withId(R.id.loginbutton).click(); // Step 4 Activity mainActivity = await().activity(MainActivity.class, 30, TimeUnit.SECONDS); assertNotNull(mainActivity); //Step 5 } }
总结:
使用Marvin,你的测试可以继承以下几个的类:AndroidTestCase, AndroidActivityTestCase, AndroidServiceTestCase.提供以下perform(), await() 和 activity() 或者 view()的交互方法.还支持HAMCREST Matcher, 还有几个全局监控类:
getMostRecentlyStartedActivity() - 返回最后的已经开启的 activitygetStartedActivities() - 返回所有当前正在运行的activity waitForActivity(Class, long, TimeUnit) - 延时启动特定activity
源代码:Github
-
简化开发难度(代码工人的福音)
简单介绍: 使用roboject必须继承RobojectActivity,包括几个可用的注解
-
@InjectLayout: Injects an XML layout to your activity.
-
@InjectView: Inject an XML view to a field of your activity.
-
@InjectExtra: Inject an Intent extra passed from another activity to a field of your activity.
-
@InjectService: Inject the binder object of a service to a field of your activity.
And two additional methods:
- onServicesConnected: Runs in brackground and is called, when all Services were bound. Use this method to process time consuming parts, such as clietn requests to a server.
- onReady: Is called subsequently to onServicesConnected on the UI-Thread. Manipulate the layout here.
源代码:Github
-
最后
All STAND frameworks are available under the Apache License 2.0
##真的是最后 本文来自英文网站