简体   繁体   中英

Class Not Found at RunTime, but there at compile time, using command line

I know my source compiles out fine in eclipse, but for the sake of easier production using the same source over and over I've opted to set up a .bat to handle most of the work for me. Well, short and sweet, my external jars are available at compile not, however not at runTime, which I receive force closes (Android ClassNotFoundDefError). I'm sure the problem lies within how my commands are set on my .bat file, and after reading websites (I'll list a set of references I used at the end) I've came to this code....

echo on
@REM %1 NOTUSED
@REM %2 SHORT NAME (DIR HIERARCHY POINTER)
@REM %3 SRCPATH (RECEIVES PACKAGE PATH)
@SET PREV_PATH=%CD%
@cd /d %0\..

@rmdir %2\bin /S /Q
@rmdir %2\gen /S /Q
@mkdir %2\bin || goto EXIT
@mkdir %2\gen || goto EXIT
@SET APP_NAME=%2
@SET ANDROID_REV=android-7

@SET ANDROID_AAPT_ADD="%ANDROID-SDK%\platform-tools\aapt.exe" add

@SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platform-tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar" -j ".\usrsrc\libs\MobileSDK.jar" -j ".\usrsrc\libs\MobileSDK2.jar"

@SET ANDROID_DX="%ANDROID-SDK%\platform-tools\dx.bat" --dex

@SET JAVAC="%JAVABIN%\javac.exe" -classpath ".\%2;%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar;%2\libs\MobileSDK.jar;%2\libs\MobileSDK2.jar"

@SET JAVAC_BUILD=%JAVAC% -sourcepath "%2\src;%2\gen" -d "%2\bin" -jar "%2\libs\MobileSDK.jar;%2\libs\MobileSDK2.jar"

call %ANDROID_AAPT_PACK% -M "%2\AndroidManifest.xml" -A "%2\assets" -S "%2\res" -m -J "%2\gen" -F "%2\bin\resources.ap_" -j "%2\libs\MobileSDK.jar" -j "%2\libs\MobileSDK2.jar" || goto EXIT

call %JAVAC_BUILD% %2%3*.java || goto EXIT

call %ANDROID_DX% --output="%CD%\%2\bin\classes.dex" %CD%\%2\bin || goto EXIT

copy "%CD%\%2\bin\resources.ap_" "%CD%\%2\bin\%APP_NAME%.ap_" || goto EXIT

call %ANDROID_AAPT_ADD% "%CD%\%2\bin\%APP_NAME%.ap_" "%CD%\%2\bin\classes.dex" || goto EXIT

call "%JAVABIN%\jarsigner" -keystore "%CD%\keystore.keystore" -storepass "XXXXXXXXXX" - keypass "XXXXXXXX" -signedjar "%CD%\%2\%APP_NAME%.apk" "%CD%\%2\bin\%APP_NAME%.ap_" "XXXXXXXXX" || goto EXIT
del "%2\bin\%APP_NAME%.ap_"
:EXIT
cd "%PREV_PATH%"
ENDLOCAL
pause
exit /b %ERRORLEVEL%

Now I know I'm prolly gonna feel really really stupid whenever someone shows me how painfully obvious the answer is. But I've been on this one thing for about 24 hours now, and I just can't stand it any longer. I would greatly appreciate any help.

FYI: Most of the variables fed into this bat are fed from another program that sets up the file hierarchy and everything else before we even go to the .bat. That's all I can really describe about this issue.

Reference:
http://www.herongyang.com/Android/Project-aapt-Android-Asset-Packaging-Tool.html
http://www.apriorit.com/our-company/dev-blog/233-how-to-build-apk-file-from-command-line
and all the questions about external jar's on stack overflow; There are a few more; I fail to find at the moment will update as I do.

Because unlike some people; I did want to help, and have received answers lurking stack overflow before in the past, I came back after I researched the answer because I have seen this question a number of times across stack overflow with no one giving out a correct answer to this:

"You have to explicitly call for your external jars inside of dx.bat"

call %ANDROID_DX% --output="%CD%\%2\bin\classes.dex" "%CD%\%2\bin" "C:\<dir>\mobileSDK.jar" "C:\<dir>\MobileSDK2.jar" || goto EXIT

You have to give a static address WITHOUT SPACES IN IT in order to comply with "java standards" (see my references above in the question). This tells the Android building stuff to include those two jars as compiled code.


The first call for them

@SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platform-tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar" -j ".\usrsrc\libs\MobileSDK.jar" -j ".\usrsrc\libs\MobileSDK2.jar"

Is soley for them to be apparant during compileTime and have the classes available for the compiler to avoid throwing errors for lack of a better term.
Including them in dx.bat as well tells it to include those .jars along with all the .class files when compacting it into an APK. But I hope this helps someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM