系统环境:Android Studio 3.4,其中Gradle插件版本为3.4.0,而Gradle插件版本为5.1.1。 我创建了一个新项目->文件->新建->导入示例-> Ndk-> Hello JNI。我将build.gradle文件minSdkVersion更改为22,因为我的Nexus 7无法执行更多操作。 然后我从https://sourceforge.net/projects/jexcelandroid/下载了jexcel_android.jar并将jexcel_android.jar复制到... app / libs / 在HelloJni.java中,我添加了一些代码,以查看详细信息。 在AndroidManifest中,我刚刚添加了写入sdcard的权限。
–我没有更改或添加任何其他内容。我能够在Nexus 7和Nexus 5x上安装并运行该应用程序。 在Nexus 7上创建的文件会显示预期的英文字母。 Nexus 5x上创建的文件显示了非预期的中文字母。 为什么?通常,两种设备在所有其他应用上都显示英文字母。
这是我在HelloJni.java中添加的代码: 字符串str =“”;
class Component
{
public:
typedef uint16_t TypeID;
static const TypeID TYPE_ID_UNKNOWN = -1;
virtual ~Component() = default;
virtual void test()
{
std::cout << "I'm the base!" << std::endl;
}
};
class AComponent : public Component
{
private:
static int nextValue;
int someValue; // To test if different sizes of Component derivatives cause any problems!
public:
AComponent()
{
this->someValue = nextValue++;
}
void test()
{
std::cout << "I'm AComponent! Value: " << someValue << std::endl;
}
};
int AComponent::nextValue = 0;
class BComponent : public Component
{
public:
void test()
{
std::cout << "I'm BComponent!" << std::endl;
}
};
class CComponent : public Component
{
public:
void test()
{
std::cout << "I'm CComponent!" << std::endl;
}
};
适用于Nexus 7的Android Studio的Logcat看起来像预期的那样。 但是在Nexus 5x中,我得到了很多信息/错误输出。约5倍于此,每次相同,所以您只会看到不重复的内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Retrieve our TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
setContentView(R.layout.activity_hello_jni);
TextView tv = (TextView)findViewById(R.id.hello_textview);
WritableWorkbook wb = createWorkbook("my.xls");
WritableSheet sh = createSheet(wb,"mysheet", 0);
try {
//writeCell(0, 0, "hi", true, sh);
wb.write();
wb.close();
}
catch (
IOException ex) {
ex.printStackTrace();
}
catch (
WriteException ex) {
ex.printStackTrace();
}
str += "\n" +stringFromJNI();
tv.setText( str );
}
/**
*
* @param wb - WritableWorkbook to create new sheet in
* @param sheetName - name to be given to new sheet
* @param sheetIndex - position in sheet tabs at bottom of workbook
* @return - a new WritableSheet in given WritableWorkbook
*/
public WritableSheet createSheet(WritableWorkbook wb,
String sheetName, int sheetIndex){
//create a new WritableSheet and return it
return wb.createSheet(sheetName, sheetIndex);
}
/**
*
* @param fileName - the name to give the new workbook file
* @return - a new WritableWorkbook with the given fileName
*/
public WritableWorkbook createWorkbook(String fileName){
//exports must use a temp file while writing to avoid memory hogging
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
str += wbSettings.getLocale().getLanguage();
//Log.i("HelloJni", " wbSettings.getLocale().getLanguage()=" +wbSettings.getLocale().getLanguage());
//get the sdcard's directory
File sdCard = Environment.getExternalStorageDirectory();
//add on the your app's path
File dir = new File(sdCard.getAbsolutePath() + "/");
//make them in case they're not there
dir.mkdirs();
//create a standard java.io.File object for the Workbook to use
File wbfile = new File(dir,fileName);
WritableWorkbook wb = null;
try{
//create a new WritableWorkbook using the java.io.File and
//WorkbookSettings from above
wb = Workbook.createWorkbook(wbfile,wbSettings);
}catch(IOException ex){
Log.e(className,ex.getStackTrace().toString());
Log.e(className, ex.getMessage());
}
return wb;
}