创建库并上传至私有Maven

欢迎Follow我的GitHub, 关注我的.

创建库并上传至私有Maven_第1张图片
Maven

本文的合集已经编著成书,高级Android开发强化实战,欢迎各位读友的建议和指导。在京东即可购买:https://item.jd.com/12385680.html

Android

今天我把项目中S健康, 修改为库, 方便添加和删除, 最后上传私有maven, 做法如下.

1. 使用库

创建库, 并把需要的内容加入.

配置AndroidManifest.xml, 添加权限和服务



    
    

    

        
        
            
        

    


初始化服务

/**
 * 初始化三星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健康的跟踪器, 集成使用
 * 

* 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, 但是又无法获取引用, 所以只能采取写入组件和包名的方式, 即设置ComponentPackage达到效果.

在主工程的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/

3. 使用方式

项目最外层build.gradle, 添加Maven库的地址, 如果是私有, 则需要传入账号和密码.

allprojects {
    repositories {
        mavenCentral()
        maven {
            url "http://maven.chunyu.mobi/content/groups/public/"
            credentials {
                username maven_user
                password maven_password
            }
        }
    }

    repositories {
        jcenter()
    }
}

在项目中添加依赖, 即[GROUP_ID]:[ARTIFACT_ID]:[VERSION].

compile 'me.chunyu.android:shealth:1.0.0'

OK, 就这些了.

你可能感兴趣的:(创建库并上传至私有Maven)