您的位置:首页 > 移动开发 > Android开发

创建库并上传至私有Maven

2015-11-08 20:59 447 查看
今天我把项目中S健康, 修改为库, 方便添加和删除, 最后上传私有maven, 做法如下.

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, 就这些了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mystra android maven