1 the demand

Have you ever used the SDK of Umeng, wechat, Weibo and Alipay? Ever want to look into the other person’s code, only to find that it’s already mixed up? Did you ever think that one day you’d be in a great company and need to release your own SDK? Or is it just a matter of satisfying your own vanity and releasing a tool that you’re proud of?

This article aims to do just that

2 Development environment and tools

  • MAC (Windows doesn’t matter, just a different path)
  • Android Studio 2.3.1
  • JDK 1.8
  • Github
  • Maven

3 Implementation Steps

3.1 New Project

Create a new project TestModule, select Empty Activity, and let Android Studio generate a simple activity. What will we do with this project? As you know, the code you deliver needs to be of quality assurance, so it needs to be tested in detail. This project is our test project, which simply simulates the user’s development environment

3.2 the new module

The New version of Android Studio already supports Module development. We go to File–>New–>New Module, and a popup will appear. We select Android Library and call it MySDK





The new Android Module

After the new project is successfully created, you will find an extra module project in the left project navigation bar. This project has an independent set of directory structure, just like app. It has its own gradle configuration





Same directory structure

3.2 Establishing function classes

We created a new activity called mySDKTest and created the layout automatically. This article does not focus on functions, so we will create a view in this layout. Instead, we will create a button. For a simple function, we will print a sentence “You clicked module button”. So let’s say that this is the functionality that we’re ultimately going to encapsulate.





Paste_Image.png

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="Button"/>

</LinearLayout>Copy the code
public class mySDKTest extends AppCompatActivity {

    private static final String TAG = "mySDKTest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sdktest);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.i(TAG."You clicked module button!!"); }}); }}Copy the code

3.3 Test Function

At this point, you will find that you can not run the project, there is no option to run this function, only app, no mysdk.





Only app is optional. PNG

This is why we called the new 3.1 project a test project, because the Module will not work

3.4 Configuring Dependencies

So we’re going to use the app to test the SDK, so we’re going to configure it to rely on mysdk in the app. You can do this by adding dependency statements to your app’s build.gradle

compile project(':mysdk')Copy the code

At this point, go to the app’s MainActivity and you can import the mySDKTest we just created. Also, we implement a button and click to jump to the mysdk activity.

        Button button = (Button)findViewById(com.flame.mysdk.R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, mySDKTest.class); startActivity(intent); }});Copy the code

So you can run and test it and when you run and you click the jump button, you’ll see that you’re in the SDK page, and then you click the button, and you’ll see that log. Then you can continue to develop and test as you see fit.





Go to the SDK page and print the log

3.5 Managing Dependencies

3.5.1 Simple Mode

Let’s say you’ve finished developing and testing features, and now you need to publish or submit them to users. How do you deliver them? We opened mysdk directory, under build–>outputs–>aar there is a generated AAR file, you can copy this file to the user, in fact, it is also possible, everyone search import AAR file can be.





Generate the file

3.5.2 Maven + dead simple

Apache Maven is a tool developed by Apache that provides a file server for contributing to the Library. With Maven + Github, we can publish more easily and do version management more easily. Users can import more easily. And that’s what we’re going to focus on

3.5.2.1 Configuring gradle packaging

Create a file named maven-release-aal. gradle in mysdk and add the following fields to build.gradle:

apply from: 'maven-release-kline-aar.gradle'Copy the code




Paste_Image.png

3.5.2.2 configure maven – release – the aar. Gradle

Maven-release-aar. gradle is the script we used to set up the package, adding the following code to the file:

Pay attention to the comments!!

/ / 1. Maven plug-in
apply plugin: 'maven'

/ / 2. Maven - information
ext {// ext is a gradle closure allowing the declaration of global properties
    PUBLISH_GROUP_ID = 'com.flame'
    PUBLISH_ARTIFACT_ID = 'mySDK'
    PUBLISH_VERSION = android.defaultConfig.versionName
}

// 3. Maven - Output path
uploadArchives {
    repositories.mavenDeployer {
        Create a folder on your computer and paste the folder path here
        // note the "file://" + path, with three slashes
        repository(url: "file:///Users/flame/Documents/sourceTree/mysdk")

        pom.project {
            groupId project.PUBLISH_GROUP_ID
            artifactId project.PUBLISH_ARTIFACT_ID
            version project.PUBLISH_VERSION}}}// The following code will generate the jar package source file, if you do not open source, please do not enter this paragraph
The AAR package contains comments
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
}Copy the code

3.5.2.3 Generating an AAR File

  • The next step is to generate the final AAR file. On the right side of Android Studio, there is a Gradle sidebar. Click on it and you will see the following screen: select mysdk and click uploadArchives




uploadArchives

  • Finally build successfully




build success

  • If you go to the maven output path above, you will find that the build file already exists.
  • Note that version is 1.0, and there is a JAR package, which should be unpacked to get all the source files
  • If you did not open source in the previous step and commented out the code that generated the JAR package, there would be no JAR package here




Path is generated

3.5.2.4 Uploading to Github

  • Upload the entire folder to Github, note that you want to upload the full path
  • Create a new organization on Github, create new-new Organization.
  • Create New has two options: New Repository and New Organization. Make sure you select Organization. If you select repository, you will not be able to import it as a private repository into a new project.

3.5.2.5 Version Management

One of the reasons for using Maven is versioning, and here we’ll show you what versioning is

  • Open mysdk build.gradle and change the versionName under defaultConfig to “1.1”.




A modified version

  • #3.5.2.3 – uploadArchives, build successfully and then look at the directory. A new version 1.1 has been generated

  • Upload to Github

  • So we have clear version control





The new version

3.6 How Can I Reference A File?

In the previous section, we generated the corresponding code, so how do we reference it?

First, we need to remove the dependency method in our app build.gradle

//Comment that out. I don't need it//compile project(':mysdk')Copy the code

3.6.1 Local Mode

The code is local, so of course we can reference it nearby

We added the following code to our app build.gradle. We have omitted the irrelevant code here

 repositories {

    jcenter()

    / / a little

    // Specify an absolute path
    maven { url "file:///Users/flame/Documents/sourceTree/mysdk"}}dependencies {
    / / a little

    //mysdk, where you can specify the version, we have 1.0,1.1 two versions to choose from
    compile('com. Flame: mySDK: 1.1')}Copy the code

3.6.2 Network Mode

The methods mentioned above, of course, are a few, after all, most of us are in the network import dependency library, here is the need to use the code uploaded to Github before.

Just point the path to Github

 repositories {

    jcenter()

    / / a little

    // Specify the Github path
    maven { url "https://github.com/flameandroid/mysdk/raw/master"}}dependencies {
    / / a little
å
    //mysdk, where you can specify the version, we have 1.0,1.1 two versions to choose from
    compile('com. Flame: mySDK: 1.1')}Copy the code

3.7 confused

When you open External Libraries, you will find that mySDK has been imported into the project and is completely open source.

A lot of times we don’t need open source, so how do we do that? This is exactly the same as the normal Android packaging mix-up

Upload the muddled code to Github, and we’re done building and releasing the SDK

        release {
            // Do not display Log
            buildConfigField "boolean"."LOG_DEBUG"."false"
            / / confusion
            minifyEnabled true
            / / the Zipalign optimization
            zipAlignEnabled true

            // Remove useless resource files
            shrinkResources true
            // The first part represents the system's default Android obfuscation file, which already contains the basic obfuscation declaration, and the second part is its own definition obfuscation file
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}Copy the code

4 Precautions

  • Note that the path must be written correctly, especially in case, because you cannot find the file with the wrong case. This is a simple error, but it is quite common
  • Note that the Github path is not the usual github.com/flameandroi… But github.com/flameandroi…
  • Note that if you choose to obtrude, do not generate the JAR in the packaging step and upload it to Github, otherwise you will get confused