创建库并上传至私有Maven
2015-11-08 20:59
447 查看
今天我把项目中S健康, 修改为库, 方便添加和删除, 最后上传私有maven, 做法如下.
配置
初始化服务
卡片跟踪器
Tracker的Intent写法略有不同, 因为要启动主工程的Activity, 但是又无法获取引用, 所以只能采取写入组件和包名的方式, 即设置
在主工程的
重写库的
在库目录下, 导入
在库目录中, 执行
存放地址
http://maven.xxx.mobi/content/repositories/releases/me/chunyu/android/shealth/1.0.0/
OK, 就这些了.
1. 使用库
创建库, 并把需要的内容加入.配置
AndroidManifest.xml, 添加权限和服务
<manifest package="me.chunyu.shealth" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 三星SHealth Service SDK接入 --> <uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY"/> <application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true"> <!-- Registration PluginService with Tracker Information --> <service android:name="com.samsung.android.sdk.shealth.PluginService" android:exported="true"> <meta-data android:name="tracker.chunyu_doctor" android:value="@string/testtracker_manifest"/> </service> </application> </manifest>
初始化服务
/** * 初始化三星S健康服务 * * Created by wangchenlong on 15/11/4. */ public class SHealthInit { /** * 初始化SHealth Health Service */ public static void initSHealth(Context context) { Shealth shealth = new Shealth(); try { shealth.initialize(context); Log.d("DEBUG-WCL", "SHealth初始化"); } catch (SsdkUnsupportedException e) { int eType = e.getType(); Log.d("DEBUG-WCL", "Samsung Digital Health Initialization failed. Error type : " + eType); if (eType == SsdkUnsupportedException.VENDOR_NOT_SUPPORTED) { // It is thrown if the device does not support the S Health SDK. } else if (eType == SsdkUnsupportedException.LIBRARY_NOT_INSTALLED) { // It is thrown if the library of the SDK is not found. } } catch (Exception e1) { Log.d("DEBUG-WCL", "Samsung Digital Health Initialization failed."); } } }
卡片跟踪器
/** * S健康的跟踪器, 集成使用 * <p/> * Created by wangchenlong on 15/10/31. */ @SuppressWarnings("unused") public class ChunyuDoctorTracker implements TrackerEventListener { private static final String TAG = "DEBUG-WCL: " + ChunyuDoctorTracker.class.getSimpleName(); public static final String ACTION_VIEW_DOCTOR_HOME = "me.chunyu.ChunyuIntent.ACTION_VIEW_DOCTOR_HOME"; private static final String MY_TILE_ID = "chunyu_tracker"; private TrackerTileManager mTrackerTileManager; @Override public void onCreate(Context context, String s) { Log.e(TAG, "onCreate"); if (mTrackerTileManager == null) { try { mTrackerTileManager = new TrackerTileManager(context); } catch (IllegalArgumentException e) { Log.d(TAG, "MyTracker onCreate() - IllegalArgumentException"); } } } @Override public void onSubscribed(Context context, String trackerId) { Log.e(TAG, "onSubscribed"); updateTile(context, trackerId, MY_TILE_ID); } @Override public void onUnsubscribed(Context context, String trackerId) { Log.e(TAG, "onUnsubscribed"); } @Override public void onTileRequested(Context context, String trackerId, String tileId) { Log.e(TAG, "onTileRequested"); // The main screen requested the tracker tile. if (tileId == null) { tileId = MY_TILE_ID; } // Update your tracker tile. updateTile(context, trackerId, tileId); } @Override public void onTileRemoved(Context context, String s, String s1) { Log.e(TAG, "onTileRemoved"); } @Override public void onPaused(Context context, String s) { Log.e(TAG, "onPaused"); } public void updateTile(Context context, String trackerId, String tileId) { Log.d(TAG, "updateTile(" + trackerId + ", " + tileId + ")"); TrackerTile myTrackerTile = null; if (tileId == null) { tileId = MY_TILE_ID; } Log.e(TAG, "trackerId = " + trackerId + ", tileId = " + tileId); try { // Create Intent to do an action // when the tracker tile is clicked Intent launchIntent = new Intent(); launchIntent.setPackage("me.chunyu.ChunyuDoctor"); launchIntent.setComponent(new ComponentName("me.chunyu.ChunyuDoctor", "me.chunyu.ChunyuDoctor.Activities.WelcomeActivity")); // Set template int template = TrackerTile.TRACKER_TILE_TYPE_1; // Create TrackerTile and set each values and intents myTrackerTile = new TrackerTile(context, trackerId, tileId, template); // Set a tracker tile myTrackerTile .setTitle(R.string.tracker_display_name) .setIcon(R.drawable.icon) .setDate(new Date()) .setContentColor(Color.parseColor("#e75c49")) .setContentIntent(TrackerTile.INTENT_TYPE_ACTIVITY, launchIntent) .setButtonIntent(context.getString(R.string.tracker_display_start), TrackerTile.INTENT_TYPE_ACTIVITY, launchIntent); if (mTrackerTileManager != null) { mTrackerTileManager.post(myTrackerTile); } } catch (IllegalArgumentException e) { Log.d(TAG, "MyTracker updateTile(" + trackerId + ", " + tileId + ") IllegalArgumentException " + e.toString()); } catch (Resources.NotFoundException e) { Log.d(TAG, "MyTracker updateTile(" + trackerId + ", " + tileId + ") NotFoundException"); } } }
Tracker的Intent写法略有不同, 因为要启动主工程的Activity, 但是又无法获取引用, 所以只能采取写入组件和包名的方式, 即设置
Component和
Package达到效果.
在主工程的
build.gradle和项目的
settings.gradle添加, 即可使用.
2. 使用Maven
已经有写好的配置文档, 在gradle_base项目下, 按照说明配置即可.
重写库的
build.gradle, 导入maven的配置脚本, 设置PUBLISH参数.
PUBLISH_GROUP_ID组织ID,
PUBLISH_ARTIFACT_ID工程ID,
PUBLISH_VERSION版本号,
PUBLISH_IS_JAR是否Jar包
buildscript { apply from: 'https://git.chunyu.me/android/gradle_base/raw/master/gradle/chunyu_build_script.gradle' dependencies { classpath androidPlugin } } apply plugin: 'com.android.library' ext { PUBLISH_GROUP_ID = 'me.chunyu.android' PUBLISH_ARTIFACT_ID = 'shealth' PUBLISH_VERSION = '1.0.0' PUBLISH_IS_JAR = false } // 导入两个基础的gradle文件的配置 // 必需 apply from: 'https://git.chunyu.me/android/gradle_base/raw/master/gradle/chunyu_base.gradle' // 如果上传maven,则需要配置这行 apply from: 'https://git.chunyu.me/android/gradle_base/raw/master/gradle/mvn_release.gradle' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }
在库目录下, 导入
mvn_release.gradle脚本, 执行uploadArchives方法.
// gradle clean build generateRelease apply plugin: 'maven' ext.getGroupId = { -> if (project.hasProperty('PUBLISH_GROUP_ID')) { return project.PUBLISH_GROUP_ID; } else { return 'me.chunyu.android' } } ext.getPomVersion = { -> if (project.hasProperty('PUBLISH_VERSION')) { return project.PUBLISH_VERSION; } else { return null } } ext.getArtifactId = { -> if (project.hasProperty('PUBLISH_ARTIFACT_ID')) { return project.PUBLISH_ARTIFACT_ID; } else { return null } } def groupId = getGroupId() def artifactId = getArtifactId() def version = getPomVersion() def isJar = project.hasProperty('PUBLISH_IS_JAR') && project.PUBLISH_IS_JAR def repo = "http://maven.chunyu.mobi/content/repositories/releases/" task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } uploadArchives { repositories.mavenDeployer { pom.groupId = groupId pom.artifactId = artifactId pom.version = version // Add other pom properties here if you want (developer details / licenses) repository(url: repo) { authentication(userName: maven_user, password: maven_password) } } } if (isJar) { artifacts { archives file: file('build/intermediates/bundles/release/classes.jar'), type: 'jar' } } artifacts { archives androidSourcesJar }
在库目录中, 执行
uploadArchives命令, 即可.
gradle uploadArchives
存放地址
http://maven.xxx.mobi/content/repositories/releases/me/chunyu/android/shealth/1.0.0/
OK, 就这些了.
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories