set filename=test2.jar set srcDir=D:\__job\study20230717-pkgJar\pkgJarForLoad\mylibrary1\build\intermediates\aar_main_jar\debug\ set dx="D:\Android SDK\build-tools\34.0.0\d8.bat" del out\%filename% call %dx% --dex --output=out\%filename% %srcDir% pause
使用gradlew生成
模块内build.gradle添加:
task makeJar(type:Copy)
{
//删除旧的jar
delete 'build/libs/test.jar'
//设置源.jar目录
from('build/intermediates/arr_main_jar/release')
//设置产出目录
into('build/libs/')
//设置源目录内文件过滤
include('classes.jar')
//重命名
rename('classes.jar','test.jar)
}
项目根目录执行Ctrl+Enter:
gradlew makeJar
NDK编程
创建NDK项目及JNI函数
创建NDK项目:
1、下载ndk
2、创建so项目
NDK(Native Development Kit)是Android提供的一个工具集,允许开发者使用C或C++等本地语言编写部分Android应用的代码,并通过JNI(Java Native Interface)与Java代码进行交互。NDK主要用于提高应用程序的性能、复用现有的本地代码库以及访问设备硬件等功能
privatefinalstaticintMY_PERMISSIONS_REQUEST_WRITE_CODE=11; // Used to load the 'ndk01' library on application startup. static { System.loadLibrary("ndk01"); } publicinttestFun(String a,Double b,long c) { return1; }
// Example of a call to a native method TextViewtv= binding.sampleText; tv.setText(stringFromJNI()); tv.setOnClickListener(newView.OnClickListener() { @Override publicvoidonClick(View view) { intret= ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);// ContextCompat这里选Android if(ret == PackageManager.PERMISSION_GRANTED) { Log.i("qqqqqq","已经有写SDCar的权限"); Stringfp1= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); Stringfc= readSDcardFile(fp1+"b.txt"); Log.i("qqqqqq","文件内容" + fc); } else { Log.i("qqqqqq","还没有写SDCar的权限"); ActivityCompat.requestPermissions(MainActivity.this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_WRITE_CODE); } } }); }
/** * A native method that is implemented by the 'ndk01' native library, * which is packaged with this application. */ publicnative String stringFromJNI(); publicnativeintgetlength(String param); publicnative String readSDcardFile(String filepath); }