Use android studio generating jar package, make your own SDK

       When collaborate on projects, based on modular thinking, the other party to use your program, and you do not want the source code to the other side, the program will usually packaged generate jar, and as obfuscation.
1. Create a project
       [File] - [New Module] - [] Android Library, named, and then edit the code.
       Create a new Class file, and then add a method (ie Method jar behind the bag to be called).
Here Insert Picture Description
Create a class test:

public class MyJarTest {
   public static String testJarString() {
       return "测试生成的Jar包";
   }
}

2. Configure build.gradle file
       code in AS confusion needs to be set in build.gradle files and proguard-rules.pro files (before and after comparison can confuse effect by jd-gui tool):
       Under your library (myjarlibrary) Library Opens build.gradle file, the following code is added at the end.

task makeJar(type: Copy) {
    //删除之前编译混淆jar包
    //delete 'build/outputs/test.jar'
    delete 'build/outputs/test.jar'
    //jar文件来源(AS3.0前)
    //from('build/intermediates/bundles/release/')
    //jar文件来源(AS3.0后)
    from('build/intermediates/packaged-classes/release/')
    //生成jar文件路径(可改)
    into('build/outputs/')
    //jar文件名
    include('classes.jar')
    //命名为test.jar
    rename('classes.jar', 'test.jar')
}
makeJar.dependsOn(build)

       Click Sync Now, and other Android Studio run is complete, click the button in the upper right corner of Gradle, then open our module (that is, before construction sdklibrary), what you get in this name is the name of what, opening only at the point to open other the method then expand inside to find our makeJar.
3. Run
       [Gradle] - [New Module (myjarlibrary)] - [other] - [make]
       Double-click makeJar button, wait until the end of the run, you can see. build / intermediates / bundles / release / folder under the file classes.jar generated and stored in the specified path jar (such as build / outputs / test.jar).
       Note: The new AS: intermediates / packaged-classes replace intermediates / bundles up.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
       The jar package into the project you want to use, and then call the appropriate method just fine.
4. Test

将test.Jar包导入依赖后编写代码测试。
public class MainActivity extends AppCompatActivity {
    private TextView tvTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvTxt = findViewById(R.id.tv_main_txt);
        tvTxt.setText(MyJarTest.testJarString());
    }
}

effect:
Here Insert Picture Description

Published 22 original articles · won praise 31 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_36158551/article/details/103967470