Things made out of nothing in Android development

Author: Ma Jinrong

Preface

I believe that many Android programmers who have worked for several years are familiar with the Android API, and the calls are unambiguous. It is easy to check the API documentation for the uncommon classes.

But have you ever encountered such a problem, a customer or boss made a request, and Android's own API does not support this function? I have encountered such a problem. In order to save electricity and save costs, commercial display equipment will shut down by itself when no one is in the middle of the night, and then restart at 6 in the morning. However, the Andorid system needs low-level adaptation from power failure to self-starting. Yes, or it is to synchronize the time of the RTC clock on the board (hardware clock, similar to a module on the computer motherboard that still records the time after shutdown)

This type of problem can be achieved by first modifying the Android source code to achieve low-level adaptation, and then importing the self-compiled framework.jar package. The following is an example of synchronizing the RTC clock:


Start

You can see that we have made some changes in the frameworks, by adding jni to call some of the methods in the underlying cpp, and then adding a new definition in the AlarmManager.java class to encapsulate our setRtcTime method.

After the changes are completed, compile and burn, so that a new method called setRtcTime is added to the AlarmManager class in the Android system of our machine. The name is whatever you want, which is equivalent to adding a brand new API. Can the apk we run on this machine's system be directly called? Let's try.

Create a new project, call FrameWorkApp, encapsulate a tool class NewAlarmHelper, you can call our setRtcTime method:

Bind a button click in MainActivity to set the hardware clock time.

public class MainActivity extends AppCompatActivity {

    private Button btn;

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

        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar calendar = Calendar.getInstance();

                calendar.set(2020, 11, 12, 12, 12, 11);

                NewAlarmHelper.setRtcTime(MainActivity.this,calendar.getTimeInMillis());

            }
        });

    }
}

Well, that's it, let's compile and generate apk and see how it turns out.

Oh no, you can't compile, just tell you that there is no such method. This is what today’s theme is going to say. Although you have already adapted it at the system level, AndroidStudio’s API is still compiled according to Google’s default API. How can Google know what methods you want to add?

So what do we do to generate the apk normally? First find your project source code, my project source code is amlogic platform, Android 9.0 system, so this blog is also suitable for solving problems such as high versions of Android.

We find the classes.jar file in the out\target\common\obj\JAVA_LIBRARIES\framework_intermediates directory. This file is the framework.jar package we want.

This jar package can be used by AS. It has its own certain size. My size is about 21M, which varies from platform to platform. If you are naive, you can also search framework.jar directly in the out directory.

But these jar packages are not working. I just started searching for some information on the Internet. Most of them did not have the problem of naming, which caused me to take a lot of detours. These jar packages are about a few kb, so you must look for them. For files (must have a size, not a few kb), don't take such a detour.

Now that we have found the corresponding jar package, we will rename it to framework.jar, which means this jar package is framework-related.

Because your project may be maintained by more than one person, or in case you quit, and there is no such detailed description of the problem during the handover, then other programmers will be confused when they see this named classes.jar.

Speaking of importing packages, some people will say, I know this, copy and paste it into the lib directory, and then Add As Library will do. You don't need to talk about the following.

Actually otherwise, this is very different from the previous jar package import. So next, I will teach you step by step how to import this framework.jar compiled by ourselves.

First, we copy and paste the framework.jar file with the changed name into the lib directory, _ (haha).

Then, the point is here, instead of right-clicking and selecting Add As Library, you can directly open the build.gradle file in the app directory.

Delete the reference to the first line of the project and add compileOnly files('libs\framework.jar'). Because we want this package to work only when compiling, we need to change the implementation to compileOnly to help compile. Package to apk.

Finally, in the allprojects of the build.gradle of the Project, set the framework.jar package compiled by ourselves to take precedence over the system package, and then Sync Porject.

// 设置framework.jar包优先系统包
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs.add('-Xbootclasspath/p:app/libs/framework.jar')
        }
    }

In the three steps here, we have successfully installed the elephant into the refrigerator, and imported the framework.jar package compiled by ourselves in AS. There is no proof, let's try to compile and generate the apk package.

Now everyone can clearly see that although the tool class has a red wavy line, we can still compile and generate an apk file. Then put it on the machine we have adapted to see the effect, we click the Button here to set the system time and RTC hardware clock to December 12, 2020, 12:12.

The system time has been modified, and then look at the hardware clock.

Hardware clock 2020-12-12 04:12, why not 12:12? ? ? Haha, because the time recorded by the hardware clock is Greenwich Mean Time, we generally use Beijing Time in the East Eighth District, so it takes 8 hours more than Greenwich Mean Time.

So our system time and hardware clock are all set successfully!

This is the end of today's sharing. I hope to achieve the role of inviting bricks and jade, so that when you encounter such problems in the future, you can learn by analogy and draw inferences.


At last

Finally, here I also share a piece of dry goods, the Android learning PDF + architecture video + source notes collected by the big guys , as well as advanced architecture technology advanced brain maps, Android development interview special materials, advanced advanced architecture materials to help you learn Improve the advanced level, and save everyone's time to search for information on the Internet to learn, and you can also share with friends around you to learn together.

If you need it, you can get it by [ Private Message ] or add a fan group: [ 1087084956 ] to get it.

Guess you like

Origin blog.csdn.net/ajsliu1233/article/details/111599529