Android Nuwa implementation steps: Android nuwa implementation steps
Nuwa implementation steps for Android hot update
Recently, the hotfix feature of hot updates has become increasingly popular in Android applications, and my products have finally put forward corresponding demands.
After two days of research, I have completed this function. Here I would like to thank my blog. The analysis of the principles of these experts is in place, however, the biggest obstacle for me is the use of git and some errors that occur when I create a jar package. Therefore, write down the specific implementation steps here, I hope I can help the shrimps that are similar to me.
There are many hot update frameworks, such as AndFix, HotFix, Dexposed, and Nuwa. After considering compatibility and functional requirements, I chose Nuwa because the framework can add classes and fields, compatible with the Android system.
Currently, the basic principles of the hot update framework on the market are classloader, that is, the class loader, which limits the possibility that we cannot modify resource files or slice la S, other methods, such as dynamic layout, are required.
Let's talk about the preparation work. First, your development tool must be Android Studio, because the. gradle file must be used when creating a jar package. Next, you need a local git library on your computer to facilitate the management of new and old versions. It is more convenient to use git commands. You need to download the nuwa library and use the user library to your project, if you are not using this step, please use Baidu. Here we recommend a github project with the integrated nuwa library.
Https://github.com/jasonross/Nuwa
Suppose we have created a project and successfully integrated nuwa. Now we need a git. If you have not installed git, please install it in the following tutorial. This tutorial is easy to understand.
Http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
After git is successfully installed, create a local version library, select a proper file path, and enter the following git command:
Mkdir hotfix is used to create a local version library named hotfix, cd hotfix is used to locate the version library, and pwd is used to output the version library. If you use it on a windows system, to avoid unpredictable problems, make sure that the path does not contain any special symbols or Chinese characters.
After creating a database, run the following command to convert it into a version library that can be managed by git:
After the file is successfully created, you will find a. git file in the folder. If you cannot see it, it may be because the file is hidden.ls -ah
Command.
The version library has been created successfully. Now we need to import the project we just created to the version library, copy your project to the hotfix folder, and run the following command:
Git add is followed by your project name. During the add process, a series of waning warnings will appear. Ignore this, as long as no error occurs. After adding is successful, submit the code again. If there is no problem, your project will be imported successfully:
A series of warnings appear during the submission process. Note that-m is followed by the comments you submit each time and cannot be blank. To facilitate future maintenance, we hope you can clarify the reason for this submission.
After successfully importing the project, we will start to write the code below. If your project is pulled from the github just recommended, you should have the complete code in the project, no, please follow me to complete the code step by step.
The principle of hot update is to pull a patch file from the server, and then use the class loader to load the classes in the patch. If we want to load the patch file at the beginning of the project, the patch loading operation should be implemented in the Application class. The Code is as follows:
import android.app.Application;import android.content.Context;import android.os.Environment;import cn.jiajixin.nuwa.Nuwa;public class NuwaApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); Nuwa.init(this); Nuwa.loadPatch(this, Environment.getExternalStorageDirectory().getAbsolutePath().concat("/patch.jar")); }}
Very simple. The nuwa framework has encapsulated a lot of operations and we only need to call them. The parameters in the loadpatch method are the location of the patch file and should be dynamically retrieved from the server, it is stored in the dedicated directory of the specified project in sdcard. Here we are testing the demo. It is easy to fix the location of the jar file and the sdcard root directory. Here, I would like to remind you that you should not forget to configure Appliction in the Manifest file.
Others only need to display the text to be output in MainActivity. Here we put the text sheet in a class to change:
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;import cn.jiajixin.nuwasample.Hello.Hello;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textview); textView.setText(new Hello().say()); }}
public class Hello { public String say() { return "hello world"; }}
Okay, the Code has been written, isn't it easy? Note that the following are some core steps that cannot be found in other documents.
Create a bugfix branch on the local version library we just created. Open git in the hotfix folder and enter the following command:
Git branch bugfix is used to create a new branch named bugfix. git checkout bugfix is transferred to this branch. Now, the code of the following two branches is the same. Now, run the following command: git checkout master switch back to master branch.
Use android studio to run the project on your mobile phone. After the project is successfully run, a nuwa folder will be created under the project directory. If you are creating your own project, the path should be app/build/outputs/nuwa. If you are importing a project on github, the path should be sample/build/outputs/nuwa, and copy the nuwa folder to a location, for example, my Desktop is C: \ Users \ thinkpad \ Desktop \ nuwa. In this case, you must note that the path must be matched when each user copies a file in different locations.
Now use the git checkout bugfix command to switch to the bugfix branch and modify the output string below the Hello class, as shown below:
public class Hello { public String say() { return "hello world I'm hotfix"; }}
After modification, we need to call. gradle through the git command to create a jar package. Do not run the project on the bugfix branch.
If you create a new app, create a folder named extras in the project:
There are two configuration files in the folder, one is the empty java class named hack, and the other has written a command to encapsulate the jar package using dx:
After these two files are configured, enable git in the hotfix and enter it in git. /gradlew clean nuwaQihooDebugPatch-P NuwaDir = C:/Users/thinkpad/Desktop/nuwa, the generated nuwa folder. The specific path depends on the copied address. Another reason is that this command is used to call the. gradle file. Our version library is a two-tier folder. You need to run the cd command in the Nuwa project folder:
After running successfully, you will see a patch in your desired project directory. jar file. If you are creating a new project, the specific path should be app \ build \ outputs \ nuwa \ qihoo \ debug \ patch. jar. If you pull the project from github, the path should be sample \ build \ outputs \ nuwa \ qihoo \ debug \ patch. jar.
Import the jar file generated above to the root directory of the mobile phone sdcard file. You can choose to copy it on your computer or use the android command:
Adb push E: hotfix \ Nuwa \ app \ build \ outputs \ nuwa \ qihoo \ debug \ patch. jar/sdcard/
After writing for so long, we finally got to the harvest. Now, we quit the project on the master branch installed on our mobile phone, quit completely, and kill the process. And then try again. Are there any surprises that the previous "hello world" has changed to "hello world I'm hotfix" we modified "? This enables hot Patching and creates classes using the nuwa framework. Although resource files cannot be changed, most of the requirements can still be fulfilled with dynamic layout.
Well, I believe everyone knows about the nuwa hot update process. I wrote this blog with great care because I encountered many major problems during my research on hot updates, so I want to share it with people who are studying this function as well as me or want to study this function in the future. If you have any questions or steps, please leave a message and I will reply in time.